2015-02-03 05:12:08 +01:00
package com.pahimar.ee3.knowledge ;
2015-02-08 05:12:25 +01:00
import com.google.gson.* ;
import com.google.gson.stream.JsonReader ;
import com.google.gson.stream.JsonWriter ;
2015-05-22 00:41:44 +02:00
import com.pahimar.ee3.api.event.AbilityEvent ;
2015-05-07 19:45:06 +02:00
import com.pahimar.ee3.api.knowledge.AbilityRegistryProxy ;
2015-02-16 19:09:03 +01:00
import com.pahimar.ee3.exchange.EnergyValueRegistry ;
2015-02-03 05:12:08 +01:00
import com.pahimar.ee3.exchange.WrappedStack ;
2015-02-10 01:08:18 +01:00
import com.pahimar.ee3.reference.Files ;
2015-05-07 21:11:23 +02:00
import com.pahimar.ee3.util.LoaderHelper ;
2015-04-09 18:40:19 +02:00
import com.pahimar.ee3.util.LogHelper ;
2015-02-10 01:08:18 +01:00
import com.pahimar.ee3.util.SerializationHelper ;
2015-05-07 21:11:23 +02:00
import cpw.mods.fml.common.Loader ;
2015-02-20 04:09:10 +01:00
import net.minecraft.item.ItemStack ;
2015-05-22 00:41:44 +02:00
import net.minecraftforge.common.MinecraftForge ;
2015-11-19 21:24:40 +01:00
import org.apache.logging.log4j.Marker ;
import org.apache.logging.log4j.MarkerManager ;
2015-02-03 05:12:08 +01:00
2015-02-08 05:12:25 +01:00
import java.io.* ;
import java.lang.reflect.Type ;
2015-02-10 01:08:18 +01:00
import java.util.Iterator ;
2015-02-03 05:12:08 +01:00
import java.util.Set ;
import java.util.TreeSet ;
2015-02-08 05:12:25 +01:00
public class AbilityRegistry implements JsonSerializer < AbilityRegistry > , JsonDeserializer < AbilityRegistry >
2015-02-05 05:48:07 +01:00
{
2015-11-19 21:24:40 +01:00
public static final Marker ABILITY_MARKER = MarkerManager . getMarker ( " EE3_ABILITY " , LogHelper . MOD_MARKER ) ;
private static final Marker NOT_LEARABLE_MARKER = MarkerManager . getMarker ( " EE3_ABILITY_NOT_LEARNABLE " , ABILITY_MARKER ) ;
private static final Marker LEARABLE_MARKER = MarkerManager . getMarker ( " EE3_ABILITY_LEARNABLE " , ABILITY_MARKER ) ;
private static final Marker NOT_RECOVERABLE_MARKER = MarkerManager . getMarker ( " EE3_ABILITY_NOT_RECOVERABLE " , ABILITY_MARKER ) ;
private static final Marker RECOVERABLE_MARKER = MarkerManager . getMarker ( " EE3_ABILITY_RECOVERABLE " , ABILITY_MARKER ) ;
2015-02-08 05:12:25 +01:00
private static final Gson jsonSerializer = ( new GsonBuilder ( ) ) . setPrettyPrinting ( ) . registerTypeAdapter ( AbilityRegistry . class , new AbilityRegistry ( ) ) . create ( ) ;
private static AbilityRegistry abilityRegistry = null ;
2015-02-10 06:22:39 +01:00
private static File abilityDirectory ;
private boolean hasBeenModified ;
2015-02-03 05:12:08 +01:00
private Set < WrappedStack > notLearnableSet ;
private Set < WrappedStack > notRecoverableSet ;
2015-02-05 05:48:07 +01:00
private AbilityRegistry ( )
{
2015-02-10 06:22:39 +01:00
hasBeenModified = false ;
2015-02-08 05:12:25 +01:00
notLearnableSet = new TreeSet < WrappedStack > ( ) ;
notRecoverableSet = new TreeSet < WrappedStack > ( ) ;
2015-02-03 05:12:08 +01:00
}
2015-02-05 05:48:07 +01:00
public static AbilityRegistry getInstance ( )
{
2015-02-08 05:12:25 +01:00
if ( abilityRegistry = = null )
2015-02-05 05:48:07 +01:00
{
2015-02-08 05:12:25 +01:00
abilityRegistry = new AbilityRegistry ( ) ;
abilityRegistry . init ( ) ;
2015-02-03 05:12:08 +01:00
}
2015-02-08 05:12:25 +01:00
return abilityRegistry ;
2015-02-03 05:12:08 +01:00
}
2015-02-05 05:48:07 +01:00
private void init ( )
{
2015-02-03 05:12:08 +01:00
notLearnableSet = new TreeSet < WrappedStack > ( ) ;
notRecoverableSet = new TreeSet < WrappedStack > ( ) ;
}
2015-02-08 05:12:25 +01:00
public Set < WrappedStack > getNotLearnableStacks ( )
{
return this . notLearnableSet ;
}
2015-02-05 05:48:07 +01:00
public boolean isLearnable ( Object object )
{
if ( WrappedStack . canBeWrapped ( object ) )
{
2015-04-19 21:01:35 +02:00
WrappedStack wrappedObject = WrappedStack . wrap ( object ) ;
2015-05-07 22:18:12 +02:00
if ( object instanceof ItemStack & & ( ( ItemStack ) object ) . isItemDamaged ( ) )
{
return false ;
}
else
{
return ! notLearnableSet . contains ( wrappedObject ) & & EnergyValueRegistry . getInstance ( ) . hasEnergyValue ( wrappedObject ) ;
}
2015-02-03 05:12:08 +01:00
}
return false ;
}
2015-02-05 05:48:07 +01:00
public void setAsLearnable ( Object object )
{
if ( WrappedStack . canBeWrapped ( object ) )
{
2015-05-07 21:11:23 +02:00
WrappedStack wrappedStack = WrappedStack . wrap ( object ) ;
2015-05-22 00:41:44 +02:00
if ( wrappedStack ! = null & & ! MinecraftForge . EVENT_BUS . post ( new AbilityEvent . SetLearnableEvent ( object ) ) )
2015-05-07 21:11:23 +02:00
{
2015-05-22 00:41:44 +02:00
if ( notLearnableSet . remove ( wrappedStack ) )
{
hasBeenModified = true ;
2015-11-19 21:24:40 +01:00
LogHelper . trace ( LEARABLE_MARKER , " [{}] Mod with ID '{}' set object {} as LEARNABLE " , LoaderHelper . getLoaderState ( ) , Loader . instance ( ) . activeModContainer ( ) . getModId ( ) , wrappedStack ) ;
2015-05-22 00:41:44 +02:00
}
2015-05-07 21:11:23 +02:00
}
2015-02-03 05:12:08 +01:00
}
}
2015-02-05 05:48:07 +01:00
public void setAsNotLearnable ( Object object )
{
if ( WrappedStack . canBeWrapped ( object ) )
{
2015-05-07 21:11:23 +02:00
WrappedStack wrappedStack = WrappedStack . wrap ( object ) ;
2015-05-22 00:41:44 +02:00
if ( wrappedStack ! = null & & ! MinecraftForge . EVENT_BUS . post ( new AbilityEvent . SetNotLearnableEvent ( object ) ) )
2015-05-07 21:11:23 +02:00
{
2015-05-22 00:41:44 +02:00
if ( notLearnableSet . add ( wrappedStack ) )
{
hasBeenModified = true ;
2015-11-19 21:24:40 +01:00
LogHelper . trace ( NOT_LEARABLE_MARKER , " [{}] Mod with ID '{}' set object {} as NOT LEARNABLE " , LoaderHelper . getLoaderState ( ) , Loader . instance ( ) . activeModContainer ( ) . getModId ( ) , wrappedStack ) ;
2015-05-22 00:41:44 +02:00
}
2015-05-07 21:11:23 +02:00
}
2015-02-03 05:12:08 +01:00
}
}
2015-02-08 05:12:25 +01:00
public Set < WrappedStack > getNotRecoverableSet ( )
{
return this . notRecoverableSet ;
}
2015-02-05 05:48:07 +01:00
public boolean isRecoverable ( Object object )
{
if ( WrappedStack . canBeWrapped ( object ) )
{
2015-04-19 21:01:35 +02:00
WrappedStack wrappedObject = WrappedStack . wrap ( object ) ;
2015-02-16 19:09:03 +01:00
return ! notRecoverableSet . contains ( wrappedObject ) & & EnergyValueRegistry . getInstance ( ) . hasEnergyValue ( wrappedObject ) ;
2015-02-03 05:12:08 +01:00
}
return false ;
}
2015-02-05 05:48:07 +01:00
public void setAsRecoverable ( Object object )
{
if ( WrappedStack . canBeWrapped ( object ) )
{
2015-05-07 21:11:23 +02:00
WrappedStack wrappedStack = WrappedStack . wrap ( object ) ;
2015-05-22 00:41:44 +02:00
if ( wrappedStack ! = null & & ! MinecraftForge . EVENT_BUS . post ( new AbilityEvent . SetRecoverableEvent ( object ) ) )
2015-05-07 21:11:23 +02:00
{
2015-05-22 00:41:44 +02:00
if ( notRecoverableSet . remove ( wrappedStack ) )
{
hasBeenModified = true ;
2015-11-19 21:24:40 +01:00
LogHelper . trace ( RECOVERABLE_MARKER , " [{}] Mod with ID '{}' set object {} as RECOVERABLE " , LoaderHelper . getLoaderState ( ) , Loader . instance ( ) . activeModContainer ( ) . getModId ( ) , wrappedStack ) ;
2015-05-22 00:41:44 +02:00
}
2015-05-07 21:11:23 +02:00
}
2015-02-03 05:12:08 +01:00
}
}
2015-02-05 05:48:07 +01:00
public void setAsNotRecoverable ( Object object )
{
if ( WrappedStack . canBeWrapped ( object ) )
{
2015-05-07 21:11:23 +02:00
WrappedStack wrappedStack = WrappedStack . wrap ( object ) ;
2015-05-22 00:41:44 +02:00
if ( wrappedStack ! = null & & ! MinecraftForge . EVENT_BUS . post ( new AbilityEvent . SetNotRecoverableEvent ( object ) ) )
2015-05-07 21:11:23 +02:00
{
2015-05-22 00:41:44 +02:00
if ( notRecoverableSet . add ( wrappedStack ) )
{
hasBeenModified = true ;
2015-11-19 21:24:40 +01:00
LogHelper . trace ( NOT_RECOVERABLE_MARKER , " [{}] Mod with ID '{}' set object {} as NOT RECOVERABLE " , LoaderHelper . getLoaderState ( ) , Loader . instance ( ) . activeModContainer ( ) . getModId ( ) , wrappedStack ) ;
2015-05-22 00:41:44 +02:00
}
2015-05-07 21:11:23 +02:00
}
2015-02-03 05:12:08 +01:00
}
}
2015-02-08 05:12:25 +01:00
@Override
public String toString ( )
{
StringBuilder stringBuilder = new StringBuilder ( ) ;
2015-02-10 01:08:18 +01:00
stringBuilder . append ( " Not Learnables: " ) ;
2015-02-08 05:12:25 +01:00
for ( WrappedStack wrappedStack : notLearnableSet )
{
2015-02-10 01:08:18 +01:00
stringBuilder . append ( wrappedStack + " " ) ;
2015-02-08 05:12:25 +01:00
}
2015-02-10 01:08:18 +01:00
stringBuilder . append ( " , Not Recoverables: " ) ;
2015-02-08 05:12:25 +01:00
for ( WrappedStack wrappedStack : notRecoverableSet )
{
2015-02-10 01:08:18 +01:00
stringBuilder . append ( wrappedStack + " " ) ;
2015-02-08 05:12:25 +01:00
}
return stringBuilder . toString ( ) ;
}
@Override
public AbilityRegistry deserialize ( JsonElement json , Type typeOfT , JsonDeserializationContext context ) throws JsonParseException
{
if ( json . isJsonObject ( ) )
{
JsonObject jsonObject = ( JsonObject ) json ;
Set < WrappedStack > notLearnableStacks = new TreeSet < WrappedStack > ( ) ;
Set < WrappedStack > notRecoverableStacks = new TreeSet < WrappedStack > ( ) ;
if ( jsonObject . has ( " notLearnable " ) & & jsonObject . get ( " notLearnable " ) . isJsonArray ( ) )
{
JsonArray jsonArray = ( JsonArray ) jsonObject . get ( " notLearnable " ) ;
2015-02-10 01:08:18 +01:00
Iterator < JsonElement > iterator = jsonArray . iterator ( ) ;
while ( iterator . hasNext ( ) )
{
JsonElement jsonElement = iterator . next ( ) ;
WrappedStack wrappedStack = WrappedStack . jsonSerializer . fromJson ( jsonElement , WrappedStack . class ) ;
if ( wrappedStack ! = null )
{
notLearnableStacks . add ( wrappedStack ) ;
}
}
2015-02-08 05:12:25 +01:00
}
if ( jsonObject . has ( " notRecoverable " ) & & jsonObject . get ( " notRecoverable " ) . isJsonArray ( ) )
{
JsonArray jsonArray = ( JsonArray ) jsonObject . get ( " notRecoverable " ) ;
2015-02-10 01:08:18 +01:00
Iterator < JsonElement > iterator = jsonArray . iterator ( ) ;
while ( iterator . hasNext ( ) )
{
JsonElement jsonElement = iterator . next ( ) ;
WrappedStack wrappedStack = WrappedStack . jsonSerializer . fromJson ( jsonElement , WrappedStack . class ) ;
if ( wrappedStack ! = null )
{
notRecoverableStacks . add ( wrappedStack ) ;
}
}
2015-02-08 05:12:25 +01:00
}
2015-02-10 01:08:18 +01:00
AbilityRegistry abilityRegistry1 = new AbilityRegistry ( ) ;
abilityRegistry1 . notLearnableSet = notLearnableStacks ;
abilityRegistry1 . notRecoverableSet = notRecoverableStacks ;
return abilityRegistry1 ;
2015-02-08 05:12:25 +01:00
}
return null ;
}
@Override
public JsonElement serialize ( AbilityRegistry abilityRegistry , Type typeOfSrc , JsonSerializationContext context )
{
JsonObject jsonAbilityRegistry = new JsonObject ( ) ;
JsonArray notLearnables = new JsonArray ( ) ;
for ( WrappedStack wrappedStack : abilityRegistry . getNotLearnableStacks ( ) )
{
notLearnables . add ( WrappedStack . jsonSerializer . toJsonTree ( wrappedStack ) ) ;
}
jsonAbilityRegistry . add ( " notLearnable " , notLearnables ) ;
JsonArray notRecoverables = new JsonArray ( ) ;
for ( WrappedStack wrappedStack : abilityRegistry . getNotRecoverableSet ( ) )
{
notRecoverables . add ( WrappedStack . jsonSerializer . toJsonTree ( wrappedStack ) ) ;
}
jsonAbilityRegistry . add ( " notRecoverable " , notRecoverables ) ;
return jsonAbilityRegistry ;
}
2015-04-21 03:16:48 +02:00
public void save ( )
2015-02-10 01:08:18 +01:00
{
2015-02-10 06:22:39 +01:00
if ( abilityDirectory ! = null )
{
abilityDirectory . mkdirs ( ) ;
writeToFile ( new File ( abilityDirectory , Files . ABILITIES_JSON_FILE ) ) ;
}
2015-02-10 01:08:18 +01:00
}
private void writeToFile ( File file )
2015-02-08 05:12:25 +01:00
{
JsonWriter jsonWriter ;
2015-02-10 06:22:39 +01:00
if ( hasBeenModified )
2015-02-08 05:12:25 +01:00
{
2015-02-10 06:22:39 +01:00
try
{
jsonWriter = new JsonWriter ( new FileWriter ( file ) ) ;
jsonWriter . setIndent ( " " ) ;
jsonSerializer . toJson ( this , AbilityRegistry . class , jsonWriter ) ;
jsonWriter . close ( ) ;
hasBeenModified = false ;
}
catch ( IOException e )
{
e . printStackTrace ( ) ;
}
2015-02-08 05:12:25 +01:00
}
}
2015-02-10 06:22:39 +01:00
public void loadAbilityRegistryFromFile ( boolean loadFileOnly )
2015-02-10 01:08:18 +01:00
{
2015-02-10 06:22:39 +01:00
if ( abilityDirectory ! = null )
2015-02-10 01:08:18 +01:00
{
2015-02-10 06:22:39 +01:00
File abilitiesFile = new File ( abilityDirectory , Files . ABILITIES_JSON_FILE ) ;
if ( abilitiesFile . exists ( ) )
{
readFromFile ( abilitiesFile , loadFileOnly ) ;
}
2015-02-10 01:08:18 +01:00
}
2015-02-14 04:42:28 +01:00
else
{
2015-06-10 05:25:40 +02:00
abilityDirectory = new File ( SerializationHelper . getInstanceDataDirectory ( ) , " abilities " ) ;
2015-02-14 04:42:28 +01:00
abilityDirectory . mkdirs ( ) ;
}
2015-02-10 01:08:18 +01:00
}
2015-02-10 06:22:39 +01:00
private void readFromFile ( File file , boolean loadFileOnly )
2015-02-08 05:12:25 +01:00
{
JsonReader jsonReader ;
try
{
jsonReader = new JsonReader ( new FileReader ( file ) ) ;
AbilityRegistry abilityRegistry1 = jsonSerializer . fromJson ( jsonReader , AbilityRegistry . class ) ;
jsonReader . close ( ) ;
2015-02-10 06:22:39 +01:00
if ( ! loadFileOnly )
2015-02-08 05:12:25 +01:00
{
2015-02-10 06:22:39 +01:00
for ( WrappedStack wrappedStack : abilityRegistry1 . getNotLearnableStacks ( ) )
2015-02-08 05:12:25 +01:00
{
2015-02-10 06:22:39 +01:00
if ( ! this . notLearnableSet . contains ( wrappedStack ) )
{
this . notLearnableSet . add ( wrappedStack ) ;
}
2015-02-08 05:12:25 +01:00
}
2015-02-10 06:22:39 +01:00
for ( WrappedStack wrappedStack : abilityRegistry1 . getNotRecoverableSet ( ) )
2015-02-08 05:12:25 +01:00
{
2015-02-10 06:22:39 +01:00
if ( ! this . notRecoverableSet . contains ( wrappedStack ) )
{
this . notRecoverableSet . add ( wrappedStack ) ;
}
2015-02-08 05:12:25 +01:00
}
}
2015-02-10 06:22:39 +01:00
else
{
this . notLearnableSet = abilityRegistry1 . notLearnableSet ;
this . notRecoverableSet = abilityRegistry1 . notRecoverableSet ;
}
hasBeenModified = true ;
2015-02-08 05:12:25 +01:00
}
catch ( FileNotFoundException ignored )
{
// NOOP
}
catch ( IOException e )
{
e . printStackTrace ( ) ;
}
}
2015-04-09 18:40:19 +02:00
public void dumpAbilityRegistryToLog ( )
{
dumpAbilityRegistryToLog ( AbilityRegistryProxy . Abilities . ALL ) ;
}
public void dumpAbilityRegistryToLog ( AbilityRegistryProxy . Abilities abilityType )
{
2015-11-19 21:24:40 +01:00
if ( abilityType = = AbilityRegistryProxy . Abilities . NOT_LEARNABLE ) {
if ( this . notLearnableSet ! = null ) {
for ( WrappedStack wrappedStack : this . notLearnableSet ) {
LogHelper . info ( NOT_LEARABLE_MARKER , " Not Learnable: {} " , wrappedStack ) ;
2015-04-09 18:40:19 +02:00
}
}
2015-11-19 21:24:40 +01:00
} else if ( abilityType = = AbilityRegistryProxy . Abilities . NOT_RECOVERABLE ) {
if ( this . notRecoverableSet ! = null ) {
for ( WrappedStack wrappedStack : this . notRecoverableSet ) {
LogHelper . info ( NOT_RECOVERABLE_MARKER , " Not Recoverable: {} " , wrappedStack ) ;
2015-04-09 18:40:19 +02:00
}
}
2015-11-19 21:24:40 +01:00
} else if ( abilityType = = AbilityRegistryProxy . Abilities . ALL ) {
if ( this . notLearnableSet ! = null ) {
for ( WrappedStack wrappedStack : this . notLearnableSet ) {
LogHelper . info ( NOT_LEARABLE_MARKER , " Not Learnable: {} " , wrappedStack ) ;
2015-04-09 18:40:19 +02:00
}
}
2015-11-19 21:24:40 +01:00
if ( this . notRecoverableSet ! = null ) {
for ( WrappedStack wrappedStack : this . notRecoverableSet ) {
LogHelper . info ( NOT_RECOVERABLE_MARKER , " Not Recoverable: {} " , wrappedStack ) ;
2015-04-09 18:40:19 +02:00
}
}
}
}
2015-02-03 05:12:08 +01:00
}