Skip to content

Commit

Permalink
Added a custom class for smeltery inputs for better external readability
Browse files Browse the repository at this point in the history
  • Loading branch information
Tonius committed Aug 10, 2014
1 parent 88b4d19 commit 7c510f2
Showing 1 changed file with 59 additions and 26 deletions.
85 changes: 59 additions & 26 deletions src/main/java/tconstruct/library/crafting/Smeltery.java
@@ -1,11 +1,12 @@
package tconstruct.library.crafting;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidStack;
Expand All @@ -16,10 +17,10 @@ public class Smeltery
{
public static Smeltery instance = new Smeltery();

private final HashMap<List<Integer>, FluidStack> smeltingList = new HashMap<List<Integer>, FluidStack>();
private final HashMap<List<Integer>, Integer> temperatureList = new HashMap<List<Integer>, Integer>();
private final HashMap<List<Integer>, ItemStack> renderIndex = new HashMap<List<Integer>, ItemStack>();
private final ArrayList<AlloyMix> alloys = new ArrayList<AlloyMix>();
private final Map<SmelteryInput, FluidStack> smeltingList = new HashMap<SmelteryInput, FluidStack>();
private final Map<SmelteryInput, Integer> temperatureList = new HashMap<SmelteryInput, Integer>();
private final Map<SmelteryInput, ItemStack> renderIndex = new HashMap<SmelteryInput, ItemStack>();
private final List<AlloyMix> alloys = new ArrayList<AlloyMix>();

/**
* Adds mappings between an itemstack and an output liquid Example:
Expand Down Expand Up @@ -60,16 +61,17 @@ public static void addMelting (Block block, int metadata, int temperature, Fluid
* TConstruct.ingotLiquidValue * 2, 0));
*
* @param input The item to liquify
* @param blockID The ID of the block to render
* @param block The ID of the block to render
* @param metadata The metadata of the block to render
* @param temperature How hot the block should be before liquifying
* @param liquid The result of the process
*/
public static void addMelting (ItemStack input, Block blockID, int metadata, int temperature, FluidStack liquid)
public static void addMelting (ItemStack input, Block block, int metadata, int temperature, FluidStack liquid)
{
instance.smeltingList.put(Arrays.asList(input.getItem().hashCode(), input.getItemDamage()), liquid);
instance.temperatureList.put(Arrays.asList(input.getItem().hashCode(), input.getItemDamage()), temperature);
instance.renderIndex.put(Arrays.asList(input.getItem().hashCode(), input.getItemDamage()), new ItemStack(blockID, input.stackSize, metadata));
SmelteryInput in = new SmelteryInput(input);
instance.smeltingList.put(in, liquid);
instance.temperatureList.put(in, temperature);
instance.renderIndex.put(in, new ItemStack(block, input.stackSize, metadata));
}

/**
Expand Down Expand Up @@ -101,7 +103,7 @@ public static Integer getLiquifyTemperature (ItemStack item)
if (item == null)
return 20;

Integer temp = instance.temperatureList.get(Arrays.asList(item.getItem().hashCode(), item.getItemDamage()));
Integer temp = instance.temperatureList.get(new SmelteryInput(item));
if (temp == null)
return 20;
else
Expand All @@ -111,12 +113,12 @@ public static Integer getLiquifyTemperature (ItemStack item)
/**
* Used to get the resulting temperature from a source Block
*
* @param item The Source ItemStack
* @param block The Source Block
* @return The result ItemStack
*/
public static Integer getLiquifyTemperature (int blockID, int metadata)
public static Integer getLiquifyTemperature (Block block, int metadata)
{
return instance.temperatureList.get(Arrays.asList(blockID, metadata));
return instance.getLiquifyTemperature(new ItemStack(block, 1, metadata));
}

/**
Expand All @@ -130,7 +132,7 @@ public static FluidStack getSmelteryResult (ItemStack item)
if (item == null)
return null;

FluidStack stack = instance.smeltingList.get(Arrays.asList(item.getItem().hashCode(), item.getItemDamage()));
FluidStack stack = instance.smeltingList.get(new SmelteryInput(item));
if (stack == null)
return null;
return stack.copy();
Expand All @@ -139,20 +141,17 @@ public static FluidStack getSmelteryResult (ItemStack item)
/**
* Used to get the resulting ItemStack from a source Block
*
* @param item The Source ItemStack
* @param block The Source Block
* @return The result ItemStack
*/
public static FluidStack getSmelteryResult (int blockID, int metadata)
public static FluidStack getSmelteryResult (Block block, int metadata)
{
FluidStack stack = instance.smeltingList.get(Arrays.asList(blockID, metadata));
if (stack == null)
return null;
return stack.copy();
return instance.getSmelteryResult(new ItemStack(block, 1, metadata));
}

public static ItemStack getRenderIndex (ItemStack input)
{
return instance.renderIndex.get(Arrays.asList(input.getItem().hashCode(), input.getItemDamage()));
return instance.renderIndex.get(new SmelteryInput(input));
}

public static ArrayList mixMetals (ArrayList<FluidStack> moltenMetal)
Expand All @@ -167,22 +166,22 @@ public static ArrayList mixMetals (ArrayList<FluidStack> moltenMetal)
return liquids;
}

public static HashMap<List<Integer>, FluidStack> getSmeltingList ()
public static Map<SmelteryInput, FluidStack> getSmeltingList ()
{
return instance.smeltingList;
}

public static HashMap<List<Integer>, Integer> getTemperatureList ()
public static Map<SmelteryInput, Integer> getTemperatureList ()
{
return instance.temperatureList;
}

public static HashMap<List<Integer>, ItemStack> getRenderIndex ()
public static Map<SmelteryInput, ItemStack> getRenderIndex ()
{
return instance.renderIndex;
}

public static ArrayList<AlloyMix> getAlloyList ()
public static List<AlloyMix> getAlloyList ()
{
return instance.alloys;
}
Expand Down Expand Up @@ -224,4 +223,38 @@ public static void addDictionaryMelting (String oreName, FluidType type, int tem
for (ItemStack is : OreDictionary.getOres(oreName))
addMelting(type, is, temperatureDifference, fluidAmount);
}

public static class SmelteryInput
{
public final Item input;
public final int meta;

public SmelteryInput(ItemStack inputStack)
{
this(inputStack.getItem(), inputStack.getItemDamage());
}

public SmelteryInput(Item input, int meta)
{
this.input = input;
this.meta = meta;
}

@Override
public int hashCode ()
{
return Item.getIdFromItem(this.input) << 16 | this.meta;

This comment has been minimized.

Copy link
@Texasjake95

Texasjake95 Aug 11, 2014

Using the Item's id is a bad idea due to FML changing it on world/server change causing an incorrect look up on all items whose ids have change

}

@Override
public boolean equals (Object o)
{
if (o == this)
return true;
else if (o instanceof SmelteryInput)
return this.input == ((SmelteryInput) o).input && this.meta == ((SmelteryInput) o).meta;

return false;
}
}
}

0 comments on commit 7c510f2

Please sign in to comment.