2014-07-07 21:31:10 +02:00
package com.pahimar.ee3.exchange ;
2014-06-14 21:40:45 +02:00
2014-07-14 18:04:20 +02:00
import com.google.common.collect.ImmutableSortedMap ;
2015-02-16 03:13:35 +01:00
import com.google.gson.* ;
2014-07-14 04:05:27 +02:00
import com.pahimar.ee3.api.EnergyValue ;
2014-07-14 20:30:50 +02:00
import com.pahimar.ee3.api.IEnergyValueProvider ;
2015-02-20 04:09:10 +01:00
import com.pahimar.ee3.knowledge.AbilityRegistry ;
2014-07-07 21:31:10 +02:00
import com.pahimar.ee3.recipe.RecipeRegistry ;
2014-09-15 22:06:20 +02:00
import com.pahimar.ee3.reference.Files ;
2015-01-27 05:17:32 +01:00
import com.pahimar.ee3.reference.Reference ;
2015-02-12 06:15:45 +01:00
import com.pahimar.ee3.reference.Settings ;
2015-02-18 16:11:06 +01:00
import com.pahimar.ee3.util.* ;
2015-01-27 05:17:32 +01:00
import cpw.mods.fml.common.FMLCommonHandler ;
2014-06-14 21:40:45 +02:00
import net.minecraft.item.Item ;
import net.minecraft.item.ItemStack ;
2014-08-29 22:25:31 +02:00
import net.minecraft.nbt.NBTTagCompound ;
import net.minecraft.nbt.NBTTagList ;
2014-07-15 00:37:50 +02:00
import net.minecraftforge.fluids.FluidStack ;
2014-06-14 21:40:45 +02:00
import net.minecraftforge.oredict.OreDictionary ;
2015-01-27 05:17:32 +01:00
import java.io.File ;
2015-02-16 03:13:35 +01:00
import java.lang.reflect.Type ;
2014-06-14 21:40:45 +02:00
import java.util.* ;
2015-02-16 03:13:35 +01:00
public class EnergyValueRegistry implements INBTTaggable , JsonSerializer < EnergyValueRegistry > , JsonDeserializer < EnergyValueRegistry >
2014-06-14 21:40:45 +02:00
{
2015-02-16 03:13:35 +01:00
private static final Gson jsonSerializer = ( new GsonBuilder ( ) ) . registerTypeAdapter ( EnergyValueRegistry . class , new EnergyValueRegistry ( ) ) . registerTypeAdapter ( EnergyValueStackMapping . class , new EnergyValueStackMapping ( ) ) . create ( ) ;
private static final Gson prettyJsonSerializer = ( new GsonBuilder ( ) ) . setPrettyPrinting ( ) . registerTypeAdapter ( EnergyValueRegistry . class , new EnergyValueRegistry ( ) ) . registerTypeAdapter ( EnergyValueStackMapping . class , new EnergyValueStackMapping ( ) ) . create ( ) ;
2014-09-16 18:08:16 +02:00
private boolean shouldRegenNextRestart = false ;
2014-06-20 21:57:27 +02:00
private static EnergyValueRegistry energyValueRegistry = null ;
2014-07-14 04:05:27 +02:00
private static Map < WrappedStack , EnergyValue > preAssignedMappings ;
private static Map < WrappedStack , EnergyValue > postAssignedMappings ;
2014-07-14 18:04:20 +02:00
private ImmutableSortedMap < WrappedStack , EnergyValue > stackMappings ;
private ImmutableSortedMap < EnergyValue , List < WrappedStack > > valueMappings ;
2014-06-14 21:40:45 +02:00
2015-02-18 16:11:06 +01:00
private Set < ItemStack > allItemStacksWithValues = new TreeSet < ItemStack > ( ItemHelper . baseComparator ) ;
2015-02-18 02:05:18 +01:00
2014-06-20 21:57:27 +02:00
private EnergyValueRegistry ( )
2014-06-14 21:40:45 +02:00
{
}
2014-06-20 21:57:27 +02:00
public static EnergyValueRegistry getInstance ( )
2014-06-14 21:40:45 +02:00
{
2014-06-20 21:57:27 +02:00
if ( energyValueRegistry = = null )
2014-06-14 21:40:45 +02:00
{
2014-06-20 21:57:27 +02:00
energyValueRegistry = new EnergyValueRegistry ( ) ;
2014-06-14 21:40:45 +02:00
}
2014-06-20 21:57:27 +02:00
return energyValueRegistry ;
2014-06-14 21:40:45 +02:00
}
2014-07-22 21:56:39 +02:00
public void addPreAssignedEnergyValue ( Object object , float energyValue )
2014-06-14 21:40:45 +02:00
{
2014-07-14 04:05:27 +02:00
addPreAssignedEnergyValue ( object , new EnergyValue ( energyValue ) ) ;
}
2014-06-14 21:40:45 +02:00
2014-07-22 21:56:39 +02:00
public void addPreAssignedEnergyValue ( Object object , EnergyValue energyValue )
2014-07-14 04:05:27 +02:00
{
if ( preAssignedMappings = = null )
{
preAssignedMappings = new HashMap < WrappedStack , EnergyValue > ( ) ;
2014-06-14 21:40:45 +02:00
}
2014-07-14 04:05:27 +02:00
if ( WrappedStack . canBeWrapped ( object ) & & energyValue ! = null & & Float . compare ( energyValue . getEnergyValue ( ) , 0f ) > 0 )
2014-06-14 21:40:45 +02:00
{
2014-07-14 04:05:27 +02:00
WrappedStack wrappedStack = new WrappedStack ( object ) ;
2014-06-14 21:40:45 +02:00
2014-07-14 04:05:27 +02:00
if ( wrappedStack . getStackSize ( ) > 0 )
2014-06-14 21:40:45 +02:00
{
2014-07-14 04:05:27 +02:00
WrappedStack factoredWrappedStack = new WrappedStack ( wrappedStack , 1 ) ;
EnergyValue factoredEnergyValue = EnergyValueHelper . factorEnergyValue ( energyValue , wrappedStack . getStackSize ( ) ) ;
2014-06-14 21:40:45 +02:00
2014-07-14 04:05:27 +02:00
if ( preAssignedMappings . containsKey ( factoredWrappedStack ) )
2014-06-14 21:40:45 +02:00
{
2014-07-14 04:05:27 +02:00
if ( factoredEnergyValue . compareTo ( preAssignedMappings . get ( factoredWrappedStack ) ) < 0 )
2014-06-14 21:40:45 +02:00
{
2014-07-14 04:05:27 +02:00
preAssignedMappings . put ( factoredWrappedStack , factoredEnergyValue ) ;
2014-06-14 21:40:45 +02:00
}
}
2014-07-14 04:05:27 +02:00
else
2014-06-14 21:40:45 +02:00
{
2014-07-14 04:05:27 +02:00
preAssignedMappings . put ( factoredWrappedStack , factoredEnergyValue ) ;
2014-06-14 21:40:45 +02:00
}
}
}
2014-07-14 04:05:27 +02:00
}
2014-06-14 21:40:45 +02:00
2014-07-22 21:56:39 +02:00
public void addPostAssignedEnergyValue ( Object object , float energyValue )
2014-07-14 04:05:27 +02:00
{
if ( postAssignedMappings = = null )
{
postAssignedMappings = new HashMap < WrappedStack , EnergyValue > ( ) ;
}
2014-06-14 21:40:45 +02:00
2014-07-14 04:05:27 +02:00
if ( WrappedStack . canBeWrapped ( object ) & & Float . compare ( energyValue , 0f ) > 0 )
2014-06-14 21:40:45 +02:00
{
2014-07-14 04:05:27 +02:00
WrappedStack wrappedStack = new WrappedStack ( object ) ;
if ( wrappedStack . getStackSize ( ) > 0 )
2014-06-14 21:40:45 +02:00
{
2014-07-14 04:05:27 +02:00
WrappedStack factoredWrappedStack = new WrappedStack ( wrappedStack , 1 ) ;
EnergyValue factoredEnergyValue = new EnergyValue ( energyValue * 1f / wrappedStack . getStackSize ( ) , EnergyValue . EnergyType . CORPOREAL ) ;
2014-06-14 21:40:45 +02:00
2014-07-14 04:05:27 +02:00
postAssignedMappings . put ( factoredWrappedStack , factoredEnergyValue ) ;
2014-06-14 21:40:45 +02:00
}
}
}
2014-07-22 21:56:39 +02:00
public void addPostAssignedEnergyValue ( Object object , EnergyValue energyValue )
2014-06-14 21:40:45 +02:00
{
2014-07-14 04:05:27 +02:00
if ( postAssignedMappings = = null )
2014-06-14 21:40:45 +02:00
{
2014-07-14 04:05:27 +02:00
postAssignedMappings = new HashMap < WrappedStack , EnergyValue > ( ) ;
}
2014-06-14 21:40:45 +02:00
2014-07-14 04:05:27 +02:00
if ( WrappedStack . canBeWrapped ( object ) & & energyValue ! = null & & Float . compare ( energyValue . getEnergyValue ( ) , 0f ) > 0 )
{
WrappedStack wrappedStack = new WrappedStack ( object ) ;
2014-06-14 21:40:45 +02:00
2014-07-14 04:05:27 +02:00
if ( wrappedStack . getStackSize ( ) > 0 )
{
WrappedStack factoredWrappedStack = new WrappedStack ( wrappedStack , 1 ) ;
EnergyValue factoredEnergyValue = EnergyValueHelper . factorEnergyValue ( energyValue , wrappedStack . getStackSize ( ) ) ;
2014-06-14 21:40:45 +02:00
2014-07-14 04:05:27 +02:00
postAssignedMappings . put ( factoredWrappedStack , factoredEnergyValue ) ;
2014-06-14 21:40:45 +02:00
}
}
}
public boolean hasEnergyValue ( Object object , boolean strict )
{
if ( WrappedStack . canBeWrapped ( object ) )
{
WrappedStack stack = new WrappedStack ( object ) ;
2015-02-21 14:07:17 +01:00
if ( stack . getWrappedStack ( ) instanceof ItemStack & & ( ( ItemStack ) stack . getWrappedStack ( ) ) . getItem ( ) instanceof IEnergyValueProvider & & ! strict )
2014-06-14 21:40:45 +02:00
{
2014-07-14 20:30:50 +02:00
EnergyValue energyValue = ( ( IEnergyValueProvider ) ( ( ItemStack ) stack . getWrappedStack ( ) ) . getItem ( ) ) . getEnergyValue ( ( ItemStack ) stack . getWrappedStack ( ) ) ;
if ( energyValue ! = null & & energyValue . getEnergyValue ( ) > 0f )
{
return true ;
}
2014-06-14 21:40:45 +02:00
}
else
{
2015-02-21 14:07:17 +01:00
if ( energyValueRegistry . stackMappings ! = null )
2014-06-14 21:40:45 +02:00
{
2015-02-21 14:07:17 +01:00
if ( energyValueRegistry . stackMappings . containsKey ( new WrappedStack ( stack . getWrappedStack ( ) ) ) )
2014-06-14 21:40:45 +02:00
{
2015-02-21 14:07:17 +01:00
return true ;
}
else
{
if ( ! strict )
2014-06-14 21:40:45 +02:00
{
2015-02-21 14:07:17 +01:00
if ( stack . getWrappedStack ( ) instanceof ItemStack )
2014-06-14 21:40:45 +02:00
{
2015-02-21 14:07:17 +01:00
ItemStack wrappedItemStack = ( ItemStack ) stack . getWrappedStack ( ) ;
2014-07-14 20:30:50 +02:00
2015-02-21 14:07:17 +01:00
// If its an OreDictionary item, scan its siblings for values
if ( OreDictionary . getOreIDs ( wrappedItemStack ) . length > 0 )
2014-07-14 20:30:50 +02:00
{
2015-02-21 14:07:17 +01:00
OreStack oreStack = new OreStack ( wrappedItemStack ) ;
if ( energyValueRegistry . stackMappings . containsKey ( new WrappedStack ( oreStack ) ) )
2014-06-14 21:40:45 +02:00
{
2015-02-21 14:07:17 +01:00
return true ;
}
else
{
for ( int oreId : OreDictionary . getOreIDs ( wrappedItemStack ) )
2014-07-14 18:04:20 +02:00
{
2015-02-21 14:07:17 +01:00
for ( ItemStack itemStack : OreDictionary . getOres ( OreDictionary . getOreName ( oreId ) ) )
2014-07-14 20:30:50 +02:00
{
2015-02-21 14:07:17 +01:00
if ( energyValueRegistry . stackMappings . containsKey ( new WrappedStack ( itemStack ) ) )
{
return true ;
}
2014-07-14 20:30:50 +02:00
}
2014-07-14 18:04:20 +02:00
}
2014-06-14 21:40:45 +02:00
}
}
2015-02-21 14:07:17 +01:00
// Else, scan for if there is a wildcard value for it
else
2014-06-14 21:40:45 +02:00
{
2015-02-21 14:07:17 +01:00
for ( WrappedStack valuedStack : energyValueRegistry . stackMappings . keySet ( ) )
2014-06-14 21:40:45 +02:00
{
2015-02-21 14:07:17 +01:00
if ( valuedStack . getWrappedStack ( ) instanceof ItemStack )
2014-06-14 21:40:45 +02:00
{
2015-02-21 14:07:17 +01:00
ItemStack valuedItemStack = ( ItemStack ) valuedStack . getWrappedStack ( ) ;
if ( Item . getIdFromItem ( valuedItemStack . getItem ( ) ) = = Item . getIdFromItem ( wrappedItemStack . getItem ( ) ) )
2014-07-14 20:30:50 +02:00
{
2015-02-21 14:07:17 +01:00
if ( valuedItemStack . getItemDamage ( ) = = OreDictionary . WILDCARD_VALUE | | wrappedItemStack . getItemDamage ( ) = = OreDictionary . WILDCARD_VALUE )
{
return true ;
}
else if ( wrappedItemStack . getItem ( ) . isDamageable ( ) & & wrappedItemStack . isItemDamaged ( ) )
{
return true ;
}
2014-07-14 20:30:50 +02:00
}
2014-06-14 21:40:45 +02:00
}
}
}
}
2015-02-21 14:07:17 +01:00
else if ( stack . getWrappedStack ( ) instanceof OreStack )
2014-06-14 21:40:45 +02:00
{
2015-02-21 14:07:17 +01:00
OreStack oreStack = ( OreStack ) stack . getWrappedStack ( ) ;
for ( ItemStack oreItemStack : OreDictionary . getOres ( oreStack . oreName ) )
2014-07-14 20:30:50 +02:00
{
2015-02-21 14:07:17 +01:00
if ( energyValueRegistry . stackMappings . containsKey ( new WrappedStack ( oreItemStack ) ) )
{
return true ;
}
2014-07-14 20:30:50 +02:00
}
2014-06-14 21:40:45 +02:00
}
}
}
}
}
}
return false ;
}
public boolean hasEnergyValue ( Object object )
{
return hasEnergyValue ( object , false ) ;
}
2014-07-07 17:22:21 +02:00
public EnergyValue getEnergyValue ( Object object )
{
return getEnergyValue ( object , false ) ;
}
2014-06-14 21:40:45 +02:00
public EnergyValue getEnergyValue ( Object object , boolean strict )
{
if ( WrappedStack . canBeWrapped ( object ) )
{
WrappedStack stack = new WrappedStack ( object ) ;
2015-02-20 04:09:10 +01:00
if ( stack . getWrappedStack ( ) instanceof ItemStack & & ( ( ItemStack ) stack . getWrappedStack ( ) ) . getItem ( ) instanceof IEnergyValueProvider & & ! strict )
2014-06-14 21:40:45 +02:00
{
2015-02-20 04:09:10 +01:00
ItemStack itemStack = ( ItemStack ) stack . getWrappedStack ( ) ;
IEnergyValueProvider iEnergyValueProvider = ( IEnergyValueProvider ) itemStack . getItem ( ) ;
EnergyValue energyValue = iEnergyValueProvider . getEnergyValue ( itemStack ) ;
2014-07-14 20:30:50 +02:00
if ( energyValue ! = null & & energyValue . getEnergyValue ( ) > 0f )
{
return energyValue ;
}
2014-06-14 21:40:45 +02:00
}
else
{
2015-02-21 14:07:17 +01:00
if ( energyValueRegistry . stackMappings ! = null )
2014-07-14 20:30:50 +02:00
{
2015-02-21 14:07:17 +01:00
if ( energyValueRegistry . stackMappings . containsKey ( new WrappedStack ( stack . getWrappedStack ( ) ) ) )
{
return energyValueRegistry . stackMappings . get ( new WrappedStack ( stack . getWrappedStack ( ) ) ) ;
}
else
2014-06-14 21:40:45 +02:00
{
2015-02-21 14:07:17 +01:00
if ( ! strict )
2014-06-14 21:40:45 +02:00
{
2015-02-21 14:07:17 +01:00
if ( stack . getWrappedStack ( ) instanceof ItemStack )
2014-06-14 21:40:45 +02:00
{
2015-02-21 14:07:17 +01:00
EnergyValue lowestValue = null ;
ItemStack wrappedItemStack = ( ItemStack ) stack . getWrappedStack ( ) ;
2014-07-14 20:30:50 +02:00
2015-02-21 14:07:17 +01:00
if ( OreDictionary . getOreIDs ( wrappedItemStack ) . length > 0 )
2014-06-14 21:40:45 +02:00
{
2015-02-21 14:07:17 +01:00
OreStack oreStack = new OreStack ( wrappedItemStack ) ;
if ( energyValueRegistry . stackMappings . containsKey ( new WrappedStack ( oreStack ) ) )
2014-06-14 21:40:45 +02:00
{
2015-02-21 14:07:17 +01:00
return energyValueRegistry . stackMappings . get ( new WrappedStack ( oreStack ) ) ;
}
else
{
for ( int oreId : OreDictionary . getOreIDs ( wrappedItemStack ) )
2014-06-14 21:40:45 +02:00
{
2015-02-21 14:07:17 +01:00
for ( ItemStack itemStack : OreDictionary . getOres ( OreDictionary . getOreName ( oreId ) ) )
2014-06-14 21:40:45 +02:00
{
2015-02-21 14:07:17 +01:00
if ( energyValueRegistry . stackMappings . containsKey ( new WrappedStack ( itemStack ) ) )
2014-07-14 18:04:20 +02:00
{
2015-02-21 14:07:17 +01:00
if ( lowestValue = = null )
2014-07-14 20:30:50 +02:00
{
2015-02-21 14:07:17 +01:00
lowestValue = energyValueRegistry . stackMappings . get ( new WrappedStack ( itemStack ) ) ;
}
else
{
EnergyValue itemValue = energyValueRegistry . stackMappings . get ( new WrappedStack ( itemStack ) ) ;
if ( itemValue . compareTo ( lowestValue ) < 0 )
{
lowestValue = itemValue ;
}
2014-07-14 20:30:50 +02:00
}
2014-07-14 18:04:20 +02:00
}
2014-06-14 21:40:45 +02:00
}
}
2015-02-21 14:07:17 +01:00
return lowestValue ;
}
2014-07-14 20:30:50 +02:00
}
2015-02-21 14:07:17 +01:00
else
2014-06-14 21:40:45 +02:00
{
2015-02-21 14:07:17 +01:00
for ( WrappedStack valuedStack : energyValueRegistry . stackMappings . keySet ( ) )
2014-06-14 21:40:45 +02:00
{
2015-02-21 14:07:17 +01:00
if ( valuedStack . getWrappedStack ( ) instanceof ItemStack )
2014-06-14 21:40:45 +02:00
{
2015-02-21 14:07:17 +01:00
ItemStack valuedItemStack = ( ItemStack ) valuedStack . getWrappedStack ( ) ;
2014-06-14 21:40:45 +02:00
2015-02-21 14:07:17 +01:00
if ( Item . getIdFromItem ( valuedItemStack . getItem ( ) ) = = Item . getIdFromItem ( wrappedItemStack . getItem ( ) ) )
2014-06-14 21:40:45 +02:00
{
2015-02-21 14:07:17 +01:00
if ( valuedItemStack . getItemDamage ( ) = = OreDictionary . WILDCARD_VALUE | | wrappedItemStack . getItemDamage ( ) = = OreDictionary . WILDCARD_VALUE )
{
EnergyValue stackValue = energyValueRegistry . stackMappings . get ( valuedStack ) ;
2014-07-14 20:30:50 +02:00
2015-02-21 14:07:17 +01:00
if ( stackValue . compareTo ( lowestValue ) < 0 )
{
lowestValue = stackValue ;
}
}
else if ( wrappedItemStack . getItem ( ) . isDamageable ( ) & & wrappedItemStack . isItemDamaged ( ) )
2014-07-14 20:30:50 +02:00
{
2015-02-21 14:07:17 +01:00
EnergyValue stackValue = new EnergyValue ( energyValueRegistry . stackMappings . get ( valuedStack ) . getEnergyValue ( ) * ( 1 - ( wrappedItemStack . getItemDamage ( ) * 1 . 0F / wrappedItemStack . getMaxDamage ( ) ) ) ) ;
if ( stackValue . compareTo ( lowestValue ) < 0 )
{
lowestValue = stackValue ;
}
2014-07-14 20:30:50 +02:00
}
2014-06-14 21:40:45 +02:00
}
}
}
2015-02-21 14:07:17 +01:00
return lowestValue ;
}
2014-07-14 20:30:50 +02:00
}
2015-02-21 14:07:17 +01:00
else if ( stack . getWrappedStack ( ) instanceof OreStack )
2014-06-14 21:40:45 +02:00
{
2015-02-21 14:07:17 +01:00
OreStack oreStack = ( OreStack ) stack . getWrappedStack ( ) ;
for ( ItemStack oreItemStack : OreDictionary . getOres ( oreStack . oreName ) )
2014-07-14 20:30:50 +02:00
{
2015-02-21 14:07:17 +01:00
if ( energyValueRegistry . stackMappings . containsKey ( new WrappedStack ( oreItemStack ) ) )
{
return energyValueRegistry . stackMappings . get ( new WrappedStack ( oreItemStack ) ) ;
}
2014-07-14 20:30:50 +02:00
}
2014-06-14 21:40:45 +02:00
}
}
}
}
}
}
return null ;
}
2014-07-14 18:04:20 +02:00
protected final void init ( )
2014-08-29 22:25:31 +02:00
{
2015-02-12 06:15:45 +01:00
if ( ! loadEnergyValueRegistryFromFile ( ) )
2014-08-29 22:25:31 +02:00
{
runDynamicEnergyValueResolution ( ) ;
}
2015-02-20 04:09:10 +01:00
AbilityRegistry . getInstance ( ) . discoverAllLearnableItemStacks ( ) ;
2014-09-16 18:08:16 +02:00
this . shouldRegenNextRestart = false ;
2014-08-29 22:25:31 +02:00
}
private void runDynamicEnergyValueResolution ( )
2014-07-14 04:05:27 +02:00
{
2014-07-14 18:04:20 +02:00
HashMap < WrappedStack , EnergyValue > stackValueMap = new HashMap < WrappedStack , EnergyValue > ( ) ;
/ *
* Pre - assigned values
* /
stackValueMap . putAll ( preAssignedMappings ) ;
2014-07-14 04:05:27 +02:00
2014-09-12 22:11:18 +02:00
// Grab custom pre-assigned values from file
2014-09-15 22:06:20 +02:00
Map < WrappedStack , EnergyValue > preAssignedValueMap = SerializationHelper . readEnergyValueStackMapFromJsonFile ( Files . PRE_ASSIGNED_ENERGY_VALUES ) ;
2014-09-12 22:11:18 +02:00
stackValueMap . putAll ( preAssignedValueMap ) ;
2014-07-14 04:05:27 +02:00
/ *
* Auto - assignment
* /
// Initialize the maps for the first pass to happen
2014-07-14 18:04:20 +02:00
ImmutableSortedMap . Builder < WrappedStack , EnergyValue > stackMappingsBuilder = ImmutableSortedMap . naturalOrder ( ) ;
stackMappingsBuilder . putAll ( stackValueMap ) ;
stackMappings = stackMappingsBuilder . build ( ) ;
2014-07-14 04:05:27 +02:00
Map < WrappedStack , EnergyValue > computedStackValues = computeStackMappings ( ) ;
// Initialize the pass counter
int passNumber = 0 ;
2015-02-12 06:15:45 +01:00
long computationStartTime = System . currentTimeMillis ( ) ;
long passStartTime ;
2015-02-14 04:42:28 +01:00
int computedValueCount ;
int totalComputedValueCount = 0 ;
LogHelper . info ( " Beginning dynamic value computation " ) ;
2014-07-14 04:05:27 +02:00
while ( ( computedStackValues . size ( ) > 0 ) & & ( passNumber < 16 ) )
{
2015-02-14 04:42:28 +01:00
computedValueCount = 0 ;
2015-02-12 06:15:45 +01:00
passStartTime = System . currentTimeMillis ( ) ;
2014-07-14 04:05:27 +02:00
// Increment the pass counter
passNumber + + ;
2014-07-22 03:43:04 +02:00
// Set the values for getEnergyValue calls in the auto-assignment computation
stackMappingsBuilder = ImmutableSortedMap . naturalOrder ( ) ;
stackMappingsBuilder . putAll ( stackValueMap ) ;
stackMappings = stackMappingsBuilder . build ( ) ;
2014-07-14 04:05:27 +02:00
// Compute stack mappings from existing stack mappings
computedStackValues = computeStackMappings ( ) ;
2014-07-22 03:43:04 +02:00
2014-07-14 04:05:27 +02:00
for ( WrappedStack keyStack : computedStackValues . keySet ( ) )
{
EnergyValue factoredExchangeEnergyValue = null ;
WrappedStack factoredKeyStack = null ;
if ( keyStack ! = null & & keyStack . getWrappedStack ( ) ! = null & & keyStack . getStackSize ( ) > 0 )
{
if ( computedStackValues . get ( keyStack ) ! = null & & Float . compare ( computedStackValues . get ( keyStack ) . getEnergyValue ( ) , 0f ) > 0 )
{
factoredExchangeEnergyValue = EnergyValueHelper . factorEnergyValue ( computedStackValues . get ( keyStack ) , keyStack . getStackSize ( ) ) ;
factoredKeyStack = new WrappedStack ( keyStack , 1 ) ;
}
}
if ( factoredExchangeEnergyValue ! = null )
{
2014-07-14 18:04:20 +02:00
if ( stackValueMap . containsKey ( factoredKeyStack ) )
2014-07-14 04:05:27 +02:00
{
2014-07-14 18:04:20 +02:00
if ( factoredExchangeEnergyValue . compareTo ( stackValueMap . get ( factoredKeyStack ) ) = = - 1 )
2014-07-14 04:05:27 +02:00
{
2014-07-14 18:04:20 +02:00
stackValueMap . put ( factoredKeyStack , factoredExchangeEnergyValue ) ;
2014-07-14 04:05:27 +02:00
}
}
else
{
2014-07-14 18:04:20 +02:00
stackValueMap . put ( factoredKeyStack , factoredExchangeEnergyValue ) ;
2015-02-14 04:42:28 +01:00
computedValueCount + + ;
totalComputedValueCount + + ;
2014-07-14 04:05:27 +02:00
}
}
}
2015-02-19 06:06:18 +01:00
LogHelper . info ( String . format ( " Pass %s: Computed %s values for objects in %s ms " , passNumber , computedValueCount , System . currentTimeMillis ( ) - passStartTime ) ) ;
2014-07-14 04:05:27 +02:00
}
2015-02-14 04:42:28 +01:00
LogHelper . info ( String . format ( " Finished dynamic value computation (computed %s values for objects in %s ms) " , totalComputedValueCount , System . currentTimeMillis ( ) - computationStartTime ) ) ;
2014-07-14 04:05:27 +02:00
/ *
* Post - assigned values
* /
if ( postAssignedMappings ! = null )
{
for ( WrappedStack wrappedStack : postAssignedMappings . keySet ( ) )
{
2014-07-14 18:04:20 +02:00
stackValueMap . put ( wrappedStack , postAssignedMappings . get ( wrappedStack ) ) ;
2014-07-14 04:05:27 +02:00
}
}
2014-07-25 04:03:36 +02:00
else
{
postAssignedMappings = new HashMap < WrappedStack , EnergyValue > ( ) ;
}
2014-07-14 04:05:27 +02:00
2014-09-12 22:11:18 +02:00
// Grab custom post-assigned values from file
2014-09-15 22:06:20 +02:00
Map < WrappedStack , EnergyValue > postAssignedValueMap = SerializationHelper . readEnergyValueStackMapFromJsonFile ( Files . POST_ASSIGNED_ENERGY_VALUES ) ;
2014-09-12 22:11:18 +02:00
stackValueMap . putAll ( postAssignedValueMap ) ;
2014-07-14 18:04:20 +02:00
/ * *
* Finalize the stack to value map
* /
stackMappingsBuilder = ImmutableSortedMap . naturalOrder ( ) ;
stackMappingsBuilder . putAll ( stackValueMap ) ;
stackMappings = stackMappingsBuilder . build ( ) ;
/ * *
2014-07-14 04:05:27 +02:00
* Value map resolution
* /
2015-02-16 03:13:35 +01:00
generateValueStackMappings ( ) ;
// Serialize values to disk
LogHelper . info ( " Saving energy values to disk " ) ;
saveEnergyValueRegistryToFile ( ) ;
}
private void generateValueStackMappings ( )
{
2014-07-14 18:04:20 +02:00
SortedMap < EnergyValue , List < WrappedStack > > tempValueMappings = new TreeMap < EnergyValue , List < WrappedStack > > ( ) ;
2014-07-14 04:05:27 +02:00
for ( WrappedStack stack : stackMappings . keySet ( ) )
{
if ( stack ! = null )
{
EnergyValue value = stackMappings . get ( stack ) ;
if ( value ! = null )
{
2014-07-14 18:04:20 +02:00
if ( tempValueMappings . containsKey ( value ) )
2014-07-14 04:05:27 +02:00
{
2014-07-14 18:04:20 +02:00
if ( ! ( tempValueMappings . get ( value ) . contains ( stack ) ) )
2014-07-14 04:05:27 +02:00
{
2014-07-14 18:04:20 +02:00
tempValueMappings . get ( value ) . add ( stack ) ;
2014-07-14 04:05:27 +02:00
}
}
else
{
2014-07-14 18:04:20 +02:00
tempValueMappings . put ( value , new ArrayList < WrappedStack > ( Arrays . asList ( stack ) ) ) ;
2014-07-14 04:05:27 +02:00
}
}
}
}
2014-07-14 18:04:20 +02:00
valueMappings = ImmutableSortedMap . copyOf ( tempValueMappings ) ;
2014-07-14 04:05:27 +02:00
}
private Map < WrappedStack , EnergyValue > computeStackMappings ( )
{
Map < WrappedStack , EnergyValue > computedStackMap = new HashMap < WrappedStack , EnergyValue > ( ) ;
for ( WrappedStack recipeOutput : RecipeRegistry . getInstance ( ) . getRecipeMappings ( ) . keySet ( ) )
{
if ( ! hasEnergyValue ( recipeOutput . getWrappedStack ( ) , false ) & & ! computedStackMap . containsKey ( recipeOutput ) )
{
EnergyValue lowestValue = null ;
for ( List < WrappedStack > recipeInputs : RecipeRegistry . getInstance ( ) . getRecipeMappings ( ) . get ( recipeOutput ) )
{
EnergyValue computedValue = EnergyValueHelper . computeEnergyValueFromList ( recipeInputs ) ;
computedValue = EnergyValueHelper . factorEnergyValue ( computedValue , recipeOutput . getStackSize ( ) ) ;
if ( computedValue ! = null )
{
if ( computedValue . compareTo ( lowestValue ) < 0 )
{
lowestValue = computedValue ;
}
}
}
if ( ( lowestValue ! = null ) & & ( lowestValue . getEnergyValue ( ) > 0f ) )
{
computedStackMap . put ( new WrappedStack ( recipeOutput . getWrappedStack ( ) ) , lowestValue ) ;
}
}
}
return computedStackMap ;
}
2014-07-15 00:37:50 +02:00
public List getStacksInRange ( int start , int finish )
2014-06-14 21:40:45 +02:00
{
return getStacksInRange ( new EnergyValue ( start ) , new EnergyValue ( finish ) ) ;
}
2014-07-15 00:37:50 +02:00
public List getStacksInRange ( float start , float finish )
2014-07-14 04:05:27 +02:00
{
return getStacksInRange ( new EnergyValue ( start ) , new EnergyValue ( finish ) ) ;
}
2014-07-15 00:37:50 +02:00
public List getStacksInRange ( EnergyValue start , EnergyValue finish )
2014-06-14 21:40:45 +02:00
{
2014-07-15 00:37:50 +02:00
List stacksInRange = new ArrayList < WrappedStack > ( ) ;
2014-06-14 21:40:45 +02:00
2014-06-20 21:57:27 +02:00
SortedMap < EnergyValue , List < WrappedStack > > tailMap = energyValueRegistry . valueMappings . tailMap ( start ) ;
SortedMap < EnergyValue , List < WrappedStack > > headMap = energyValueRegistry . valueMappings . headMap ( finish ) ;
2014-06-14 21:40:45 +02:00
SortedMap < EnergyValue , List < WrappedStack > > smallerMap ;
SortedMap < EnergyValue , List < WrappedStack > > biggerMap ;
if ( ! tailMap . isEmpty ( ) & & ! headMap . isEmpty ( ) )
{
if ( tailMap . size ( ) < = headMap . size ( ) )
{
smallerMap = tailMap ;
biggerMap = headMap ;
}
else
{
smallerMap = headMap ;
biggerMap = tailMap ;
}
for ( EnergyValue value : smallerMap . keySet ( ) )
{
if ( biggerMap . containsKey ( value ) )
{
2014-07-15 00:37:50 +02:00
for ( WrappedStack wrappedStack : energyValueRegistry . valueMappings . get ( value ) )
{
if ( wrappedStack . getWrappedStack ( ) instanceof ItemStack | | wrappedStack . getWrappedStack ( ) instanceof FluidStack )
{
stacksInRange . add ( wrappedStack . getWrappedStack ( ) ) ;
}
else if ( wrappedStack . getWrappedStack ( ) instanceof OreStack )
{
for ( ItemStack itemStack : OreDictionary . getOres ( ( ( OreStack ) wrappedStack . getWrappedStack ( ) ) . oreName ) )
{
stacksInRange . add ( itemStack ) ;
}
}
}
2014-06-14 21:40:45 +02:00
}
}
}
return stacksInRange ;
}
2014-07-25 04:03:36 +02:00
2014-08-29 22:25:31 +02:00
@Override
public void readFromNBT ( NBTTagCompound nbtTagCompound )
{
if ( nbtTagCompound ! = null & & nbtTagCompound . hasKey ( " stackMappingList " ) )
{
HashMap < WrappedStack , EnergyValue > stackValueMap = new HashMap < WrappedStack , EnergyValue > ( ) ;
/ * *
* Read stack value mappings from NBTTagCompound
* /
NBTTagList stackMappingTagList = nbtTagCompound . getTagList ( " stackMappingList " , 10 ) ;
for ( int i = 0 ; i < stackMappingTagList . tagCount ( ) ; i + + )
{
NBTTagCompound tagCompound = stackMappingTagList . getCompoundTagAt ( i ) ;
WrappedStack wrappedStack = WrappedStack . fromNBTTagCompound ( tagCompound . getCompoundTag ( " wrappedStack " ) ) ;
EnergyValue energyValue = EnergyValue . loadEnergyValueFromNBT ( tagCompound . getCompoundTag ( " energyValue " ) ) ;
stackValueMap . put ( wrappedStack , energyValue ) ;
}
ImmutableSortedMap . Builder < WrappedStack , EnergyValue > stackMappingsBuilder = ImmutableSortedMap . naturalOrder ( ) ;
stackMappingsBuilder . putAll ( stackValueMap ) ;
stackMappings = stackMappingsBuilder . build ( ) ;
/ * *
* Resolve value stack mappings from the newly loaded stack mappings
* /
2015-02-16 03:13:35 +01:00
generateValueStackMappings ( ) ;
2014-08-29 22:25:31 +02:00
}
}
@Override
public void writeToNBT ( NBTTagCompound nbtTagCompound )
{
NBTTagList stackMappingTagList = new NBTTagList ( ) ;
for ( WrappedStack wrappedStack : stackMappings . keySet ( ) )
{
2014-09-10 04:52:46 +02:00
if ( wrappedStack ! = null & & stackMappings . get ( wrappedStack ) ! = null )
{
2014-09-05 03:12:22 +02:00
NBTTagCompound stackMappingCompound = new NBTTagCompound ( ) ;
stackMappingCompound . setTag ( " wrappedStack " , WrappedStack . toNBTTagCompound ( wrappedStack ) ) ;
stackMappingCompound . setTag ( " energyValue " , EnergyValue . writeEnergyValueToNBT ( stackMappings . get ( wrappedStack ) ) ) ;
stackMappingTagList . appendTag ( stackMappingCompound ) ;
}
2014-08-29 22:25:31 +02:00
}
nbtTagCompound . setTag ( " stackMappingList " , stackMappingTagList ) ;
2015-02-12 06:15:45 +01:00
nbtTagCompound . setString ( " modListMD5 " , SerializationHelper . getModListMD5 ( ) ) ;
2014-08-29 22:25:31 +02:00
}
2014-09-10 04:52:46 +02:00
2015-01-27 05:17:32 +01:00
@Override
public String getTagLabel ( )
{
return " EnergyValueRegistry " ;
}
2014-09-10 04:52:46 +02:00
public void setEnergyValue ( WrappedStack wrappedStack , EnergyValue energyValue )
{
2014-09-11 22:13:39 +02:00
if ( wrappedStack ! = null & & energyValue ! = null & & Float . compare ( energyValue . getEnergyValue ( ) , 0f ) > 0 )
{
HashMap < WrappedStack , EnergyValue > stackValueMap = new HashMap < WrappedStack , EnergyValue > ( ) ;
2014-09-10 04:52:46 +02:00
2014-09-11 22:13:39 +02:00
/ * *
* Read stack value mappings from NBTTagCompound
* /
stackValueMap . putAll ( stackMappings ) ;
stackValueMap . put ( wrappedStack , energyValue ) ;
2014-09-10 04:52:46 +02:00
2014-09-11 22:13:39 +02:00
ImmutableSortedMap . Builder < WrappedStack , EnergyValue > stackMappingsBuilder = ImmutableSortedMap . naturalOrder ( ) ;
stackMappingsBuilder . putAll ( stackValueMap ) ;
stackMappings = stackMappingsBuilder . build ( ) ;
2014-09-10 04:52:46 +02:00
2014-09-11 22:13:39 +02:00
/ * *
* Resolve value stack mappings from the newly loaded stack mappings
* /
SortedMap < EnergyValue , List < WrappedStack > > tempValueMappings = new TreeMap < EnergyValue , List < WrappedStack > > ( ) ;
2014-09-10 04:52:46 +02:00
2014-09-11 22:13:39 +02:00
for ( WrappedStack stack : stackMappings . keySet ( ) )
2014-09-10 04:52:46 +02:00
{
2014-09-11 22:13:39 +02:00
if ( stack ! = null )
2014-09-10 04:52:46 +02:00
{
2014-09-11 22:13:39 +02:00
EnergyValue value = stackMappings . get ( stack ) ;
if ( value ! = null )
2014-09-10 04:52:46 +02:00
{
2014-09-11 22:13:39 +02:00
if ( tempValueMappings . containsKey ( value ) )
2014-09-10 04:52:46 +02:00
{
2014-09-11 22:13:39 +02:00
if ( ! ( tempValueMappings . get ( value ) . contains ( stack ) ) )
{
tempValueMappings . get ( value ) . add ( stack ) ;
}
}
else
{
tempValueMappings . put ( value , new ArrayList < WrappedStack > ( Arrays . asList ( stack ) ) ) ;
2014-09-10 04:52:46 +02:00
}
}
}
}
2014-09-11 22:13:39 +02:00
valueMappings = ImmutableSortedMap . copyOf ( tempValueMappings ) ;
}
2014-09-10 04:52:46 +02:00
}
2014-09-16 18:08:16 +02:00
public boolean getShouldRegenNextRestart ( )
{
return shouldRegenNextRestart ;
}
public void setShouldRegenNextRestart ( boolean shouldRegenNextRestart )
{
this . shouldRegenNextRestart = shouldRegenNextRestart ;
}
2015-02-11 05:38:05 +01:00
2015-02-20 04:09:10 +01:00
public ImmutableSortedMap < WrappedStack , EnergyValue > getStackValueMap ( )
{
return stackMappings ;
}
public ImmutableSortedMap < EnergyValue , List < WrappedStack > > getValueStackMap ( )
{
return valueMappings ;
}
2015-02-11 05:38:05 +01:00
public void saveEnergyValueRegistryToFile ( )
{
2015-02-12 06:15:45 +01:00
File energyValuesDataDirectory = new File ( FMLCommonHandler . instance ( ) . getMinecraftServerInstance ( ) . getEntityWorld ( ) . getSaveHandler ( ) . getWorldDirectory ( ) , " data " + File . separator + Reference . LOWERCASE_MOD_ID + File . separator + " energyvalues " ) ;
energyValuesDataDirectory . mkdirs ( ) ;
if ( shouldRegenNextRestart )
{
File staticEnergyValuesFile = new File ( energyValuesDataDirectory , Files . STATIC_ENERGY_VALUES ) ;
File md5EnergyValuesFile = new File ( energyValuesDataDirectory , SerializationHelper . getModListMD5 ( ) + " .dat " ) ;
if ( staticEnergyValuesFile . exists ( ) )
{
staticEnergyValuesFile . delete ( ) ;
}
if ( md5EnergyValuesFile . exists ( ) )
{
md5EnergyValuesFile . delete ( ) ;
}
2015-02-13 06:23:35 +01:00
shouldRegenNextRestart = false ;
2015-02-12 06:15:45 +01:00
}
else
{
SerializationHelper . writeNBTToFile ( energyValuesDataDirectory , Files . STATIC_ENERGY_VALUES , this ) ;
SerializationHelper . writeNBTToFile ( energyValuesDataDirectory , SerializationHelper . getModListMD5 ( ) + " .dat " , this ) ;
}
2015-02-11 05:38:05 +01:00
}
2015-02-12 06:15:45 +01:00
public boolean loadEnergyValueRegistryFromFile ( )
2015-02-11 05:38:05 +01:00
{
2015-02-12 06:15:45 +01:00
File energyValuesDataDirectory = new File ( FMLCommonHandler . instance ( ) . getMinecraftServerInstance ( ) . getEntityWorld ( ) . getSaveHandler ( ) . getWorldDirectory ( ) , " data " + File . separator + Reference . LOWERCASE_MOD_ID + File . separator + " energyvalues " ) ;
energyValuesDataDirectory . mkdirs ( ) ;
File staticEnergyValuesFile = new File ( energyValuesDataDirectory , Files . STATIC_ENERGY_VALUES ) ;
File md5EnergyValuesFile = new File ( energyValuesDataDirectory , SerializationHelper . getModListMD5 ( ) + " .dat " ) ;
NBTTagCompound nbtTagCompound = null ;
if ( Settings . DynamicEnergyValueGeneration . regenerateEnergyValuesWhen . equalsIgnoreCase ( " Never " ) )
{
if ( staticEnergyValuesFile . exists ( ) )
{
LogHelper . info ( " Attempting to load energy values from file: " + staticEnergyValuesFile . getAbsolutePath ( ) ) ;
nbtTagCompound = SerializationHelper . readNBTFromFile ( staticEnergyValuesFile ) ;
}
else if ( md5EnergyValuesFile . exists ( ) )
{
LogHelper . info ( " Attempting to load energy values from file: " + md5EnergyValuesFile . getAbsolutePath ( ) ) ;
nbtTagCompound = SerializationHelper . readNBTFromFile ( md5EnergyValuesFile ) ;
}
}
else if ( md5EnergyValuesFile . exists ( ) )
{
LogHelper . info ( " Attempting to load energy values from file: " + md5EnergyValuesFile . getAbsolutePath ( ) ) ;
nbtTagCompound = SerializationHelper . readNBTFromFile ( md5EnergyValuesFile ) ;
}
if ( nbtTagCompound ! = null )
{
energyValueRegistry . readFromNBT ( nbtTagCompound ) ;
LogHelper . info ( " Successfully loaded energy values from file " ) ;
return true ;
}
else
{
LogHelper . info ( " No energy value file to load values from, generating new values " ) ;
return false ;
}
2015-02-11 05:38:05 +01:00
}
2015-02-16 03:13:35 +01:00
public String toJson ( )
{
return jsonSerializer . toJson ( this ) ;
}
@Override
public EnergyValueRegistry deserialize ( JsonElement json , Type typeOfT , JsonDeserializationContext context ) throws JsonParseException
{
if ( json . isJsonArray ( ) )
{
JsonArray jsonArray = ( JsonArray ) json ;
Map < WrappedStack , EnergyValue > stackValueMap = new HashMap < WrappedStack , EnergyValue > ( ) ;
Iterator < JsonElement > iterator = jsonArray . iterator ( ) ;
while ( iterator . hasNext ( ) )
{
JsonElement jsonElement = iterator . next ( ) ;
EnergyValueStackMapping energyValueStackMapping = new EnergyValueStackMapping ( ) . deserialize ( jsonElement , typeOfT , context ) ;
if ( energyValueStackMapping ! = null )
{
stackValueMap . put ( energyValueStackMapping . wrappedStack , energyValueStackMapping . energyValue ) ;
}
}
ImmutableSortedMap . Builder < WrappedStack , EnergyValue > stackMappingsBuilder = ImmutableSortedMap . naturalOrder ( ) ;
stackMappingsBuilder . putAll ( stackValueMap ) ;
stackMappings = stackMappingsBuilder . build ( ) ;
generateValueStackMappings ( ) ;
}
return null ;
}
@Override
public JsonElement serialize ( EnergyValueRegistry energyValueRegistry , Type typeOfSrc , JsonSerializationContext context )
{
JsonArray jsonEnergyValueRegistry = new JsonArray ( ) ;
for ( WrappedStack wrappedStack : energyValueRegistry . stackMappings . keySet ( ) )
{
jsonEnergyValueRegistry . add ( EnergyValueStackMapping . jsonSerializer . toJsonTree ( new EnergyValueStackMapping ( wrappedStack , energyValueRegistry . stackMappings . get ( wrappedStack ) ) ) ) ;
}
return jsonEnergyValueRegistry ;
}
2014-06-14 21:40:45 +02:00
}