A lot of refactoring in an attempt to get a proper API package. Do not consider this done or safe at all - pushing so that saner men/women than I can critique it

This commit is contained in:
Pahimar 2014-07-07 11:22:21 -04:00
parent dc90ee2ada
commit a1dc2d2130
23 changed files with 137 additions and 195 deletions

View file

@ -1,10 +1,5 @@
package com.pahimar.ee3.exchange;
package com.pahimar.ee3.api.core;
import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSyntaxException;
import com.pahimar.ee3.reference.Compare;
import com.pahimar.ee3.util.LogHelper;
import net.minecraft.item.ItemStack;
import net.minecraftforge.oredict.OreDictionary;
@ -14,9 +9,6 @@ import java.util.List;
public class OreStack implements Comparable<OreStack>
{
// Gson serializer for serializing to/deserializing from json
private static final Gson gsonSerializer = new Gson();
public String oreName;
public int stackSize;
public static Comparator<OreStack> comparator = new Comparator<OreStack>()
@ -41,18 +33,18 @@ public class OreStack implements Comparable<OreStack>
}
else
{
return Compare.LESSER_THAN;
return -1;
}
}
else
{
if (oreStack2 != null)
{
return Compare.GREATER_THAN;
return 1;
}
else
{
return Compare.EQUALS;
return 0;
}
}
}
@ -91,31 +83,6 @@ public class OreStack implements Comparable<OreStack>
return false;
}
/**
* Deserializes a OreStack object from the given serialized json String
*
* @param jsonOreStack Json encoded String representing a OreStack object
* @return The OreStack that was encoded as json, or null if a valid OreStack could not be decoded from given String
*/
@SuppressWarnings("unused")
public static OreStack createFromJson(String jsonOreStack)
{
try
{
return gsonSerializer.fromJson(jsonOreStack, OreStack.class);
}
catch (JsonSyntaxException exception)
{
LogHelper.warn(exception.getMessage());
}
catch (JsonParseException exception)
{
LogHelper.warn(exception.getMessage());
}
return null;
}
public static OreStack getOreStackFromList(Object... objects)
{
return getOreStackFromList(Arrays.asList(objects));
@ -147,7 +114,7 @@ public class OreStack implements Comparable<OreStack>
@Override
public boolean equals(Object object)
{
return object instanceof OreStack && (comparator.compare(this, (OreStack) object) == Compare.EQUALS);
return object instanceof OreStack && (comparator.compare(this, (OreStack) object) == 0);
}
@Override
@ -161,15 +128,4 @@ public class OreStack implements Comparable<OreStack>
{
return comparator.compare(this, oreStack);
}
/**
* Returns this OreStack as a json serialized String
*
* @return Json serialized String of this OreStack
*/
@SuppressWarnings("unused")
public String toJson()
{
return gsonSerializer.toJson(this);
}
}

View file

@ -1,6 +1,5 @@
package com.pahimar.ee3.exchange;
package com.pahimar.ee3.api.core;
import com.pahimar.ee3.reference.Compare;
import com.pahimar.ee3.util.FluidHelper;
import com.pahimar.ee3.util.ItemHelper;
import net.minecraft.block.Block;
@ -31,14 +30,14 @@ public class WrappedStack implements Comparable<WrappedStack>
}
else
{
return Compare.GREATER_THAN;
return 1;
}
}
else if (wrappedStack1.wrappedStack instanceof OreStack)
{
if (wrappedStack2.wrappedStack instanceof ItemStack)
{
return Compare.LESSER_THAN;
return -1;
}
else if (wrappedStack2.wrappedStack instanceof OreStack)
{
@ -46,14 +45,14 @@ public class WrappedStack implements Comparable<WrappedStack>
}
else
{
return Compare.GREATER_THAN;
return 1;
}
}
else if (wrappedStack1.wrappedStack instanceof FluidStack)
{
if (wrappedStack2.wrappedStack instanceof ItemStack || wrappedStack2.wrappedStack instanceof OreStack)
{
return Compare.LESSER_THAN;
return -1;
}
else if (wrappedStack2.wrappedStack instanceof FluidStack)
{
@ -61,22 +60,22 @@ public class WrappedStack implements Comparable<WrappedStack>
}
else
{
return Compare.GREATER_THAN;
return 1;
}
}
else if (wrappedStack1.wrappedStack == null)
{
if (wrappedStack2.wrappedStack != null)
{
return Compare.LESSER_THAN;
return -1;
}
else
{
return Compare.EQUALS;
return 0;
}
}
return Compare.EQUALS;
return 0;
}
};
private int stackSize;
@ -169,6 +168,12 @@ public class WrappedStack implements Comparable<WrappedStack>
}
}
public Object getWrappedStack()
{
return wrappedStack;
}
public WrappedStack(Object object, int stackSize)
{
if (object instanceof Item)
@ -289,23 +294,6 @@ public class WrappedStack implements Comparable<WrappedStack>
this.stackSize = stackSize;
}
public Object getWrappedStack()
{
return wrappedStack;
}
/*
* Sort order (class-wise) goes ItemStack, OreStack, EnergyStack,
* FluidStack, null
* @see java.lang.Comparable#compareTo(java.lang.Object)
*/
@Override
public int compareTo(WrappedStack wrappedStack)
{
return comparator.compare(this, wrappedStack);
}
/**
*
*/
@ -348,7 +336,18 @@ public class WrappedStack implements Comparable<WrappedStack>
@Override
public boolean equals(Object object)
{
return object instanceof WrappedStack && (this.compareTo((WrappedStack) object) == Compare.EQUALS);
return object instanceof WrappedStack && (this.compareTo((WrappedStack) object) == 0);
}
/*
* Sort order (class-wise) goes ItemStack, OreStack, EnergyStack,
* FluidStack, null
* @see java.lang.Comparable#compareTo(java.lang.Object)
*/
@Override
public int compareTo(WrappedStack wrappedStack)
{
return comparator.compare(this, wrappedStack);
}
/**

View file

@ -1,4 +1,4 @@
package com.pahimar.ee3.exchange;
package com.pahimar.ee3.api.exchange;
public class EnergyComponent implements Comparable<EnergyComponent>
{
@ -6,6 +6,12 @@ public class EnergyComponent implements Comparable<EnergyComponent>
public final EnergyType type;
public final int weight;
public EnergyComponent(EnergyType type)
{
this(type, 1);
}
public EnergyComponent(EnergyType type, int weight)
{
@ -21,12 +27,6 @@ public class EnergyComponent implements Comparable<EnergyComponent>
}
}
public EnergyComponent(EnergyType type)
{
this(type, 1);
}
@Override
public boolean equals(Object object)
{

View file

@ -1,4 +1,4 @@
package com.pahimar.ee3.exchange;
package com.pahimar.ee3.api.exchange;
public enum EnergyType
{

View file

@ -1,4 +1,4 @@
package com.pahimar.ee3.exchange;
package com.pahimar.ee3.api.exchange;
import com.google.gson.*;
import com.pahimar.ee3.util.LogHelper;

View file

@ -1,8 +1,10 @@
package com.pahimar.ee3.exchange;
package com.pahimar.ee3.api.exchange;
import com.google.common.collect.ImmutableSortedMap;
import com.pahimar.ee3.api.core.OreStack;
import com.pahimar.ee3.api.core.WrappedStack;
import com.pahimar.ee3.api.recipe.RecipeRegistry;
import com.pahimar.ee3.init.EnergyValues;
import com.pahimar.ee3.recipe.RecipeRegistry;
import com.pahimar.ee3.util.EnergyValueHelper;
import com.pahimar.ee3.util.LogHelper;
import net.minecraft.item.Item;
@ -343,6 +345,11 @@ public class EnergyValueRegistry
return hasEnergyValue(object, false);
}
public EnergyValue getEnergyValue(Object object)
{
return getEnergyValue(object, false);
}
public EnergyValue getEnergyValue(Object object, boolean strict)
{
if (WrappedStack.canBeWrapped(object))
@ -448,22 +455,12 @@ public class EnergyValueRegistry
return null;
}
public EnergyValue getEnergyValue(Object object)
{
return getEnergyValue(object, false);
}
@SuppressWarnings("unused")
public List<WrappedStack> getStacksInRange(int start, int finish)
{
return getStacksInRange(new EnergyValue(start), new EnergyValue(finish));
}
public List<WrappedStack> getStacksInRange(float start, float finish)
{
return getStacksInRange(new EnergyValue(start), new EnergyValue(finish));
}
public List<WrappedStack> getStacksInRange(EnergyValue start, EnergyValue finish)
{
List<WrappedStack> stacksInRange = new ArrayList<WrappedStack>();
@ -500,9 +497,9 @@ public class EnergyValueRegistry
return stacksInRange;
}
public ImmutableSortedMap<WrappedStack, EnergyValue> getStackToEnergyValueMap()
public List<WrappedStack> getStacksInRange(float start, float finish)
{
return stackMappings;
return getStacksInRange(new EnergyValue(start), new EnergyValue(finish));
}
public ImmutableSortedMap<EnergyValue, List<WrappedStack>> getEnergyValueToStackMap()
@ -518,6 +515,11 @@ public class EnergyValueRegistry
}
}
public ImmutableSortedMap<WrappedStack, EnergyValue> getStackToEnergyValueMap()
{
return stackMappings;
}
public void dumpValueMappings()
{

View file

@ -0,0 +1,8 @@
package com.pahimar.ee3.api.exchange;
import net.minecraft.item.ItemStack;
public interface IEnergyValueProvider
{
public abstract EnergyValue getEnergyValue(ItemStack itemStack);
}

View file

@ -1,7 +1,7 @@
package com.pahimar.ee3.item.crafting;
package com.pahimar.ee3.api.recipe;
import com.pahimar.ee3.exchange.OreStack;
import com.pahimar.ee3.exchange.WrappedStack;
import com.pahimar.ee3.api.core.OreStack;
import com.pahimar.ee3.api.core.WrappedStack;
import net.minecraft.item.ItemStack;
import net.minecraftforge.oredict.OreDictionary;
@ -28,6 +28,24 @@ public class RecipeAludel
this.dustStack = dustStack.copy();
}
public boolean matches(ItemStack inputStack, ItemStack dustStack)
{
if (OreDictionary.getOreIDs(inputStack).length > 0)
{
if (matches(new WrappedStack(new OreStack(inputStack)), dustStack))
{
return matches(new WrappedStack(new OreStack(inputStack)), dustStack);
}
}
return matches(new WrappedStack(inputStack), dustStack);
}
public boolean matches(WrappedStack inputStack, ItemStack dustStack)
{
return compareStacks(this.inputStack, inputStack) && compareItemStacks(this.dustStack, dustStack);
}
private static boolean compareStacks(WrappedStack wrappedStack1, WrappedStack wrappedStack2)
{
if (wrappedStack1 != null && wrappedStack1.getWrappedStack() != null && wrappedStack2 != null && wrappedStack2.getWrappedStack() != null)
@ -79,24 +97,6 @@ public class RecipeAludel
return false;
}
public boolean matches(ItemStack inputStack, ItemStack dustStack)
{
if (OreDictionary.getOreIDs(inputStack).length > 0)
{
if (matches(new WrappedStack(new OreStack(inputStack)), dustStack))
{
return matches(new WrappedStack(new OreStack(inputStack)), dustStack);
}
}
return matches(new WrappedStack(inputStack), dustStack);
}
public boolean matches(WrappedStack inputStack, ItemStack dustStack)
{
return compareStacks(this.inputStack, inputStack) && compareItemStacks(this.dustStack, dustStack);
}
public ItemStack getRecipeOutput()
{
return this.recipeOutput;

View file

@ -1,9 +1,11 @@
package com.pahimar.ee3.recipe;
package com.pahimar.ee3.api.recipe;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.pahimar.ee3.exchange.WrappedStack;
import com.pahimar.ee3.item.crafting.RecipeAludel;
import com.pahimar.ee3.api.core.WrappedStack;
import com.pahimar.ee3.recipe.RecipesFluidContainers;
import com.pahimar.ee3.recipe.RecipesPotions;
import com.pahimar.ee3.recipe.RecipesVanilla;
import com.pahimar.ee3.util.LogHelper;
import java.util.Collection;
@ -13,7 +15,6 @@ import java.util.TreeSet;
public class RecipeRegistry
{
private static RecipeRegistry recipeRegistry = null;
private Multimap<WrappedStack, List<WrappedStack>> recipeMap;
@ -25,16 +26,6 @@ public class RecipeRegistry
init();
}
public static RecipeRegistry getInstance()
{
if (recipeRegistry == null)
{
recipeRegistry = new RecipeRegistry();
}
return recipeRegistry;
}
private void init()
{
// Add recipes in the vanilla crafting manager
@ -86,6 +77,16 @@ public class RecipeRegistry
}
}
public static RecipeRegistry getInstance()
{
if (recipeRegistry == null)
{
recipeRegistry = new RecipeRegistry();
}
return recipeRegistry;
}
public Multimap<WrappedStack, List<WrappedStack>> getRecipeMappings()
{
return recipeRegistry.recipeMap;

View file

@ -1,8 +1,6 @@
package com.pahimar.ee3.recipe;
package com.pahimar.ee3.api.recipe;
import com.pahimar.ee3.exchange.OreStack;
import com.pahimar.ee3.item.crafting.RecipeAludel;
import com.pahimar.ee3.util.LogHelper;
import com.pahimar.ee3.api.core.OreStack;
import net.minecraft.item.ItemStack;
import java.util.ArrayList;
@ -40,10 +38,6 @@ public class RecipesAludel
{
aludelRecipes.add(recipeAludel);
}
else
{
LogHelper.debug(String.format("Attempted to add RecipeAludel '%s' but already exists in the recipe list", recipeAludel));
}
}
public void addRecipe(ItemStack recipeOutput, OreStack recipeInputStack, ItemStack recipeInputDust)
@ -81,12 +75,4 @@ public class RecipesAludel
{
return aludelRecipes;
}
public void debugDumpMap()
{
for (RecipeAludel recipeAludel : aludelRecipes)
{
LogHelper.debug(String.format("Output: %s, Input Stack: %s, Dust Stack: %s", recipeAludel.getRecipeOutput(), recipeAludel.getRecipeInputs()[0], recipeAludel.getRecipeInputs()[1]));
}
}
}

View file

@ -1,8 +1,8 @@
package com.pahimar.ee3.client.handler;
import com.pahimar.ee3.exchange.EnergyValue;
import com.pahimar.ee3.exchange.EnergyValueRegistry;
import com.pahimar.ee3.exchange.WrappedStack;
import com.pahimar.ee3.api.core.WrappedStack;
import com.pahimar.ee3.api.exchange.EnergyValue;
import com.pahimar.ee3.api.exchange.EnergyValueRegistry;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

View file

@ -1,8 +1,8 @@
package com.pahimar.ee3.init;
import com.pahimar.ee3.exchange.EnergyValue;
import com.pahimar.ee3.exchange.OreStack;
import com.pahimar.ee3.exchange.WrappedStack;
import com.pahimar.ee3.api.core.OreStack;
import com.pahimar.ee3.api.core.WrappedStack;
import com.pahimar.ee3.api.exchange.EnergyValue;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
@ -22,6 +22,12 @@ public class EnergyValues
valueMap = new HashMap<WrappedStack, EnergyValue>();
}
public static Map<WrappedStack, EnergyValue> getValueMap()
{
lazyInit();
return energyValues.valueMap;
}
private static void lazyInit()
{
if (energyValues == null)
@ -31,12 +37,6 @@ public class EnergyValues
}
}
public static Map<WrappedStack, EnergyValue> getValueMap()
{
lazyInit();
return energyValues.valueMap;
}
private void init()
{
// OreDictionary assignment

View file

@ -1,6 +1,6 @@
package com.pahimar.ee3.init;
import com.pahimar.ee3.recipe.RecipesAludel;
import com.pahimar.ee3.api.recipe.RecipesAludel;
import com.pahimar.ee3.util.CraftingHelper;
import cpw.mods.fml.common.registry.GameRegistry;
import net.minecraft.init.Blocks;

View file

@ -2,7 +2,7 @@ package com.pahimar.ee3.recipe;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.pahimar.ee3.exchange.WrappedStack;
import com.pahimar.ee3.api.core.WrappedStack;
import net.minecraftforge.fluids.FluidContainerRegistry;
import net.minecraftforge.fluids.FluidContainerRegistry.FluidContainerData;

View file

@ -2,7 +2,7 @@ package com.pahimar.ee3.recipe;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.pahimar.ee3.exchange.WrappedStack;
import com.pahimar.ee3.api.core.WrappedStack;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;

View file

@ -2,7 +2,7 @@ package com.pahimar.ee3.recipe;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.pahimar.ee3.exchange.WrappedStack;
import com.pahimar.ee3.api.core.WrappedStack;
import com.pahimar.ee3.util.RecipeHelper;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.CraftingManager;

View file

@ -1,9 +0,0 @@
package com.pahimar.ee3.reference;
public class Compare
{
// Comparator stuff
public static final int LESSER_THAN = -1;
public static final int EQUALS = 0;
public static final int GREATER_THAN = 1;
}

View file

@ -1,6 +1,6 @@
package com.pahimar.ee3.thread;
import com.pahimar.ee3.exchange.EnergyValueRegistry;
import com.pahimar.ee3.api.exchange.EnergyValueRegistry;
import com.pahimar.ee3.util.LogHelper;
public class DynamicEnergyValueInitThread implements Runnable

View file

@ -1,10 +1,10 @@
package com.pahimar.ee3.tileentity;
import com.pahimar.ee3.api.recipe.RecipeAludel;
import com.pahimar.ee3.api.recipe.RecipesAludel;
import com.pahimar.ee3.item.ItemAlchemicalDust;
import com.pahimar.ee3.item.crafting.RecipeAludel;
import com.pahimar.ee3.network.PacketHandler;
import com.pahimar.ee3.network.message.MessageTileEntityAludel;
import com.pahimar.ee3.recipe.RecipesAludel;
import com.pahimar.ee3.reference.Names;
import cpw.mods.fml.common.network.NetworkRegistry;
import cpw.mods.fml.relauncher.Side;

View file

@ -1,7 +1,7 @@
package com.pahimar.ee3.util;
import com.pahimar.ee3.exchange.EnergyValue;
import com.pahimar.ee3.exchange.EnergyValueRegistry;
import com.pahimar.ee3.api.exchange.EnergyValue;
import com.pahimar.ee3.api.exchange.EnergyValueRegistry;
import com.pahimar.ee3.init.ModItems;
import com.pahimar.ee3.item.ItemAlchemicalDust;
import net.minecraft.item.ItemStack;

View file

@ -1,9 +1,9 @@
package com.pahimar.ee3.util;
import com.pahimar.ee3.exchange.EnergyType;
import com.pahimar.ee3.exchange.EnergyValue;
import com.pahimar.ee3.exchange.EnergyValueRegistry;
import com.pahimar.ee3.exchange.WrappedStack;
import com.pahimar.ee3.api.core.WrappedStack;
import com.pahimar.ee3.api.exchange.EnergyType;
import com.pahimar.ee3.api.exchange.EnergyValue;
import com.pahimar.ee3.api.exchange.EnergyValueRegistry;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidContainerRegistry;
@ -13,6 +13,12 @@ import java.util.List;
public class EnergyValueHelper
{
@SuppressWarnings("unused")
public static List<WrappedStack> filterStacksByEnergyValue(float start, float end, EnergyValue filterValue)
{
return filterStacksByEnergyValue(EnergyValueRegistry.getInstance().getStacksInRange(start, end), filterValue);
}
public static List<WrappedStack> filterStacksByEnergyValue(List<WrappedStack> unfilteredStacks, EnergyValue filterValue)
{
List<WrappedStack> filteredStacks = new ArrayList<WrappedStack>();
@ -44,12 +50,6 @@ public class EnergyValueHelper
return filteredStacks;
}
@SuppressWarnings("unused")
public static List<WrappedStack> filterStacksByEnergyValue(float start, float end, EnergyValue filterValue)
{
return filterStacksByEnergyValue(EnergyValueRegistry.getInstance().getStacksInRange(start, end), filterValue);
}
public static EnergyValue computeEnergyValueFromList(List<WrappedStack> wrappedStacks)
{
float[] computedSubValues = new float[EnergyType.TYPES.length];

View file

@ -1,6 +1,5 @@
package com.pahimar.ee3.util;
import com.pahimar.ee3.reference.Compare;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
@ -15,7 +14,7 @@ public class ItemHelper
if (itemStack1 != null && itemStack2 != null)
{
// Sort on itemID
if (Item.getIdFromItem(itemStack1.getItem()) - Item.getIdFromItem(itemStack2.getItem()) == Compare.EQUALS)
if (Item.getIdFromItem(itemStack1.getItem()) - Item.getIdFromItem(itemStack2.getItem()) == 0)
{
// Then sort on meta
if (itemStack1.getItemDamage() == itemStack2.getItemDamage())

View file

@ -1,7 +1,7 @@
package com.pahimar.ee3.util;
import com.pahimar.ee3.exchange.OreStack;
import com.pahimar.ee3.exchange.WrappedStack;
import com.pahimar.ee3.api.core.OreStack;
import com.pahimar.ee3.api.core.WrappedStack;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.item.crafting.ShapedRecipes;