From d43694eb98a0968190c3dbaa4ab5121c39feaf1f Mon Sep 17 00:00:00 2001 From: Draknyte1 Date: Sun, 25 Sep 2016 21:11:05 +1000 Subject: + Added the Work Bench, from Gregtech 4. % Renamed a method that converted arrays to fixed sized lists. % Added a Util function to determine server or client easier. --- src/Java/gtPlusPlus/core/handler/GuiHandler.java | 24 ++- .../workbench/Workbench_CraftingHandler.java | 213 +++++++++++++++++++++ .../handler/workbench/Workbench_RecipeSorter.java | 43 +++++ 3 files changed, 279 insertions(+), 1 deletion(-) create mode 100644 src/Java/gtPlusPlus/core/handler/workbench/Workbench_CraftingHandler.java create mode 100644 src/Java/gtPlusPlus/core/handler/workbench/Workbench_RecipeSorter.java (limited to 'src/Java/gtPlusPlus/core/handler') diff --git a/src/Java/gtPlusPlus/core/handler/GuiHandler.java b/src/Java/gtPlusPlus/core/handler/GuiHandler.java index 7c9c7a7625..b9b3229f13 100644 --- a/src/Java/gtPlusPlus/core/handler/GuiHandler.java +++ b/src/Java/gtPlusPlus/core/handler/GuiHandler.java @@ -2,12 +2,15 @@ package gtPlusPlus.core.handler; import gtPlusPlus.GTplusplus; import gtPlusPlus.core.container.Container_BackpackBase; +import gtPlusPlus.core.container.Container_Workbench; import gtPlusPlus.core.gui.beta.Gui_ID_Registry; import gtPlusPlus.core.gui.beta.MU_GuiId; import gtPlusPlus.core.gui.item.GuiBaseBackpack; +import gtPlusPlus.core.gui.machine.GUI_Workbench; import gtPlusPlus.core.interfaces.IGuiManager; import gtPlusPlus.core.inventories.BaseInventoryBackpack; import gtPlusPlus.core.lib.CORE; +import gtPlusPlus.core.tileentities.machines.TileEntityWorkbench; import gtPlusPlus.core.util.Utils; import gtPlusPlus.xmod.forestry.bees.alveary.TileAlvearyFrameHousing; import gtPlusPlus.xmod.forestry.bees.alveary.gui.CONTAINER_FrameHousing; @@ -24,7 +27,7 @@ public class GuiHandler implements IGuiHandler { public static final int GUI1 = 0; //Frame Alveary public static final int GUI2 = 1; //RTG public static final int GUI3 = 2; //BackpackHandler - public static final int GUI4 = 3; // + public static final int GUI4 = 3; //Workbench public static final int GUI5 = 4; // public static final int GUI6 = 5; // public static final int GUI7 = 6; // @@ -63,6 +66,19 @@ public class GuiHandler implements IGuiHandler { // Use the player's held item to create the inventory return new Container_BackpackBase(player, player.inventory, new BaseInventoryBackpack(player.getHeldItem())); } + + if (te != null){ + if (ID == GUI4){ + return new Container_Workbench(player.inventory, (TileEntityWorkbench)te); + } + } + + + + + + + return null; } @@ -90,6 +106,12 @@ public class GuiHandler implements IGuiHandler { return new GuiBaseBackpack((Container_BackpackBase) new Container_BackpackBase(player, player.inventory, new BaseInventoryBackpack(player.getHeldItem()))); } + if (te != null){ + if (ID == GUI4){ + return new GUI_Workbench(player.inventory, (TileEntityWorkbench)te); + } + } + return null; } diff --git a/src/Java/gtPlusPlus/core/handler/workbench/Workbench_CraftingHandler.java b/src/Java/gtPlusPlus/core/handler/workbench/Workbench_CraftingHandler.java new file mode 100644 index 0000000000..b5c0c035ec --- /dev/null +++ b/src/Java/gtPlusPlus/core/handler/workbench/Workbench_CraftingHandler.java @@ -0,0 +1,213 @@ +package gtPlusPlus.core.handler.workbench; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import net.minecraft.block.Block; +import net.minecraft.init.Blocks; +import net.minecraft.init.Items; +import net.minecraft.inventory.InventoryCrafting; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.item.crafting.IRecipe; +import net.minecraft.item.crafting.ShapedRecipes; +import net.minecraft.item.crafting.ShapelessRecipes; +import net.minecraft.world.World; + +public class Workbench_CraftingHandler { + + private static final Workbench_CraftingHandler instance = new Workbench_CraftingHandler(); + private List recipes = new ArrayList(); + + public static final Workbench_CraftingHandler getInstance() { + return instance; + } + public Workbench_CraftingHandler() { + + //just a example recipe so you know how to add them + addRecipe(new ItemStack(Blocks.iron_block), new Object[] + { + "###", + "###", + "###", + Character.valueOf('#'), Items.iron_ingot + }); + + //another example Recipe, but shapeless + addShapelessRecipe(new ItemStack(Items.cake),new Object[]{Items.stick}); + + + } + + void addRecipe(ItemStack par1ItemStack, Object par2ArrayOfObj[]) + { + String s = ""; + int i = 0; + int j = 0; + int k = 0; + + if (par2ArrayOfObj[i] instanceof String[]) + { + String as[] = (String[])par2ArrayOfObj[i++]; + + for (int l = 0; l < as.length; l++) + { + String s2 = as[l]; + k++; + j = s2.length(); + s = (new StringBuilder()).append(s).append(s2).toString(); + } + } + else + { + while (par2ArrayOfObj[i] instanceof String) + { + String s1 = (String)par2ArrayOfObj[i++]; + k++; + j = s1.length(); + s = (new StringBuilder()).append(s).append(s1).toString(); + } + } + + HashMap hashmap = new HashMap(); + + for (; i < par2ArrayOfObj.length; i += 2) + { + Character character = (Character)par2ArrayOfObj[i]; + ItemStack itemstack = null; + + if (par2ArrayOfObj[i + 1] instanceof Item) + { + itemstack = new ItemStack((Item)par2ArrayOfObj[i + 1]); + } + else if (par2ArrayOfObj[i + 1] instanceof Block) + { + itemstack = new ItemStack((Block)par2ArrayOfObj[i + 1], 1, -1); + } + else if (par2ArrayOfObj[i + 1] instanceof ItemStack) + { + itemstack = (ItemStack)par2ArrayOfObj[i + 1]; + } + + hashmap.put(character, itemstack); + } + + ItemStack aitemstack[] = new ItemStack[j * k]; + + for (int i1 = 0; i1 < j * k; i1++) + { + char c = s.charAt(i1); + + if (hashmap.containsKey(Character.valueOf(c))) + { + aitemstack[i1] = ((ItemStack)hashmap.get(Character.valueOf(c))).copy(); + } + else + { + aitemstack[i1] = null; + } + } + + recipes.add(new ShapedRecipes(j, k, aitemstack, par1ItemStack)); + } + + public void addShapelessRecipe(ItemStack par1ItemStack, Object par2ArrayOfObj[]) + { + ArrayList arraylist = new ArrayList(); + Object aobj[] = par2ArrayOfObj; + int i = aobj.length; + + for (int j = 0; j < i; j++) + { + Object obj = aobj[j]; + + if (obj instanceof ItemStack) + { + arraylist.add(((ItemStack)obj).copy()); + continue; + } + + if (obj instanceof Item) + { + arraylist.add(new ItemStack((Item)obj)); + continue; + } + + if (obj instanceof Block) + { + arraylist.add(new ItemStack((Block)obj)); + } + else + { + throw new RuntimeException("Invalid shapeless recipe!"); + } + } + + recipes.add(new ShapelessRecipes(par1ItemStack, arraylist)); + } + + public ItemStack findMatchingRecipe(InventoryCrafting par1InventoryCrafting, World par2World) + { + int i = 0; + ItemStack itemstack = null; + ItemStack itemstack1 = null; + + for (int j = 0; j < par1InventoryCrafting.getSizeInventory(); j++) + { + ItemStack itemstack2 = par1InventoryCrafting.getStackInSlot(j); + + if (itemstack2 == null) + { + continue; + } + + if (i == 0) + { + itemstack = itemstack2; + } + + if (i == 1) + { + itemstack1 = itemstack2; + } + + i++; + } + + //TODO - Update from itemIDs + /*if (i == 2 && itemstack.itemID == itemstack1.itemID && itemstack.stackSize == 1 && itemstack1.stackSize == 1 && Item.itemsList[itemstack.itemID].isDamageable()) + { + Item item = Item.itemsList[itemstack.itemID]; + int l = item.getMaxDamage() - itemstack.getItemDamageForDisplay(); + int i1 = item.getMaxDamage() - itemstack1.getItemDamageForDisplay(); + int j1 = l + i1 + (item.getMaxDamage() * 10) / 100; + int k1 = item.getMaxDamage() - j1; + + if (k1 < 0) + { + k1 = 0; + } + + return new ItemStack(itemstack.itemID, 1, k1); + }*/ + + for (int k = 0; k < recipes.size(); k++) + { + IRecipe irecipe = (IRecipe)recipes.get(k); + + if (irecipe.matches(par1InventoryCrafting, par2World)) + { + return irecipe.getCraftingResult(par1InventoryCrafting); + } + } + + return null; + } + + + public List getRecipeList() + { + return recipes; + } +} \ No newline at end of file diff --git a/src/Java/gtPlusPlus/core/handler/workbench/Workbench_RecipeSorter.java b/src/Java/gtPlusPlus/core/handler/workbench/Workbench_RecipeSorter.java new file mode 100644 index 0000000000..79d80aa677 --- /dev/null +++ b/src/Java/gtPlusPlus/core/handler/workbench/Workbench_RecipeSorter.java @@ -0,0 +1,43 @@ +package gtPlusPlus.core.handler.workbench; + +import java.util.Comparator; + +import net.minecraft.item.crafting.IRecipe; +import net.minecraft.item.crafting.ShapedRecipes; +import net.minecraft.item.crafting.ShapelessRecipes; + +public class Workbench_RecipeSorter implements Comparator +{ + final Workbench_CraftingHandler CraftingManagerCrafter; + + Workbench_RecipeSorter(Workbench_CraftingHandler par1CraftingManager) + { + CraftingManagerCrafter = par1CraftingManager; + } + + public int compareRecipes(IRecipe par1IRecipe, IRecipe par2IRecipe) + { + if ((par1IRecipe instanceof ShapelessRecipes) && (par2IRecipe instanceof ShapedRecipes)) + { + return 1; + } + + if ((par2IRecipe instanceof ShapelessRecipes) && (par1IRecipe instanceof ShapedRecipes)) + { + return -1; + } + + if (par2IRecipe.getRecipeSize() < par1IRecipe.getRecipeSize()) + { + return -1; + } + + return par2IRecipe.getRecipeSize() <= par1IRecipe.getRecipeSize() ? 0 : 1; + } + + public int compare(Object par1Obj, Object par2Obj) + { + return compareRecipes((IRecipe)par1Obj, (IRecipe)par2Obj); + } + +} \ No newline at end of file -- cgit