From 32bec289c85175e81882e1ea352b43b98689190c Mon Sep 17 00:00:00 2001 From: Alkalus Date: Tue, 12 Sep 2017 14:31:45 +1000 Subject: + Work on Crafting handler. --- .../gregtech/common/helpers/CraftingHelper.java | 41 ++++++++ .../helpers/autocrafter/AC_Helper_Container.java | 117 +++++++++++++++++++++ .../helpers/autocrafter/AC_Helper_Utils.java | 90 ++++++++++++++++ 3 files changed, 248 insertions(+) create mode 100644 src/Java/gtPlusPlus/xmod/gregtech/common/helpers/CraftingHelper.java create mode 100644 src/Java/gtPlusPlus/xmod/gregtech/common/helpers/autocrafter/AC_Helper_Container.java create mode 100644 src/Java/gtPlusPlus/xmod/gregtech/common/helpers/autocrafter/AC_Helper_Utils.java (limited to 'src/Java/gtPlusPlus/xmod/gregtech/common/helpers') diff --git a/src/Java/gtPlusPlus/xmod/gregtech/common/helpers/CraftingHelper.java b/src/Java/gtPlusPlus/xmod/gregtech/common/helpers/CraftingHelper.java new file mode 100644 index 0000000000..e3bfc81291 --- /dev/null +++ b/src/Java/gtPlusPlus/xmod/gregtech/common/helpers/CraftingHelper.java @@ -0,0 +1,41 @@ +package gtPlusPlus.xmod.gregtech.common.helpers; + +import gtPlusPlus.core.lib.CORE; +import gtPlusPlus.core.util.Utils; +import gtPlusPlus.xmod.gregtech.common.helpers.autocrafter.AC_Helper_Container; +import gtPlusPlus.xmod.gregtech.common.helpers.autocrafter.AC_Helper_Utils; +import gtPlusPlus.xmod.gregtech.common.tileentities.machines.multi.GT4Entity_AutoCrafter; +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.inventory.Container; +import net.minecraft.world.World; +import net.minecraft.world.WorldServer; +import net.minecraftforge.common.util.FakePlayerFactory; + +public class CraftingHelper{ + + public final String mInventoryName; + public final int mPosX; + public final int mPosY; + public final int mPosZ; + public final GT4Entity_AutoCrafter crafter; + public final World world; + public final EntityPlayerMP player; + public final AC_Helper_Container inventory; + + public CraftingHelper(GT4Entity_AutoCrafter AC){ + Utils.LOG_INFO("[A-C] Created a crafting helper."); + crafter = AC; + AC_Helper_Utils.addCrafter(AC); + //Get some variables. + world = AC.getBaseMetaTileEntity().getWorld(); + mPosX = AC.getBaseMetaTileEntity().getXCoord(); + mPosY = AC.getBaseMetaTileEntity().getYCoord(); + mPosZ = AC.getBaseMetaTileEntity().getZCoord(); + //Create Fake player to handle crating. + player = FakePlayerFactory.get((WorldServer) world, CORE.gameProfile); + //Set storage container + inventory = new AC_Helper_Container(player.inventory, world, mPosX, mPosY, mPosZ); + mInventoryName = inventory.getMatrix().getInventoryName(); + + } +} \ No newline at end of file diff --git a/src/Java/gtPlusPlus/xmod/gregtech/common/helpers/autocrafter/AC_Helper_Container.java b/src/Java/gtPlusPlus/xmod/gregtech/common/helpers/autocrafter/AC_Helper_Container.java new file mode 100644 index 0000000000..f5b066fdc2 --- /dev/null +++ b/src/Java/gtPlusPlus/xmod/gregtech/common/helpers/autocrafter/AC_Helper_Container.java @@ -0,0 +1,117 @@ +package gtPlusPlus.xmod.gregtech.common.helpers.autocrafter; + +import gtPlusPlus.core.util.Utils; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.InventoryPlayer; +import net.minecraft.inventory.Container; +import net.minecraft.inventory.IInventory; +import net.minecraft.inventory.InventoryCraftResult; +import net.minecraft.inventory.InventoryCrafting; +import net.minecraft.inventory.Slot; +import net.minecraft.inventory.SlotCrafting; +import net.minecraft.item.ItemStack; +import net.minecraft.item.crafting.CraftingManager; +import net.minecraft.world.World; + +public class AC_Helper_Container extends Container +{ + /** The crafting matrix inventory (3x3). */ + public InventoryCrafting craftMatrix = new InventoryCrafting(this, 3, 3); + public IInventory craftResult = new InventoryCraftResult(); + private World worldObj; + + public InventoryCrafting getMatrix(){ + return this.craftMatrix; + } + + public boolean putItemsIntoGrid(ItemStack[] inputs){ + if (inputs.length < 9){ + return false; + } + for (int i=0;i<9;i++){ + this.putStackInSlot(i, inputs[i]); + } + this.onCraftMatrixChanged(this.craftMatrix); + return true; + } + + public AC_Helper_Container(InventoryPlayer playerInventory, World world, int x, int y, int z) + { + this.worldObj = world; + this.addSlotToContainer(new SlotCrafting(playerInventory.player, this.craftMatrix, this.craftResult, 0, 124, 35)); + int l; + int i1; + + for (l = 0; l < 3; ++l) + { + for (i1 = 0; i1 < 3; ++i1) + { + this.addSlotToContainer(new Slot(this.craftMatrix, i1 + l * 3, 30 + i1 * 18, 17 + l * 18)); + } + } + + for (l = 0; l < 3; ++l) + { + for (i1 = 0; i1 < 9; ++i1) + { + this.addSlotToContainer(new Slot(playerInventory, i1 + l * 9 + 9, 8 + i1 * 18, 84 + l * 18)); + } + } + + for (l = 0; l < 9; ++l) + { + this.addSlotToContainer(new Slot(playerInventory, l, 8 + l * 18, 142)); + } + + this.onCraftMatrixChanged(this.craftMatrix); + } + + /** + * Callback for when the crafting matrix is changed. + */ + public void onCraftMatrixChanged(IInventory p_75130_1_) + { + this.craftResult.setInventorySlotContents(0, CraftingManager.getInstance().findMatchingRecipe(this.craftMatrix, this.worldObj)); + Utils.LOG_INFO("Crafted "+this.craftResult.getStackInSlot(0)); + + } + + /** + * Called when the container is closed. + */ + public void onContainerClosed(EntityPlayer p_75134_1_) + { + super.onContainerClosed(p_75134_1_); + + if (!this.worldObj.isRemote) + { + for (int i = 0; i < 9; ++i) + { + ItemStack itemstack = this.craftMatrix.getStackInSlotOnClosing(i); + + if (itemstack != null) + { + p_75134_1_.dropPlayerItemWithRandomChoice(itemstack, false); + } + } + } + } + + public boolean canInteractWith(EntityPlayer p_75145_1_) + { + return true; + } + + /** + * Called when a player shift-clicks on a slot. You must override this or you will crash when someone does that. + */ + public ItemStack transferStackInSlot(EntityPlayer p_82846_1_, int p_82846_2_){ + ItemStack itemstack = null; + return itemstack; + } + + public boolean func_94530_a(ItemStack p_94530_1_, Slot p_94530_2_) + { + return p_94530_2_.inventory != this.craftResult && super.func_94530_a(p_94530_1_, p_94530_2_); + } +} \ No newline at end of file diff --git a/src/Java/gtPlusPlus/xmod/gregtech/common/helpers/autocrafter/AC_Helper_Utils.java b/src/Java/gtPlusPlus/xmod/gregtech/common/helpers/autocrafter/AC_Helper_Utils.java new file mode 100644 index 0000000000..052b611250 --- /dev/null +++ b/src/Java/gtPlusPlus/xmod/gregtech/common/helpers/autocrafter/AC_Helper_Utils.java @@ -0,0 +1,90 @@ +package gtPlusPlus.xmod.gregtech.common.helpers.autocrafter; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + +import gtPlusPlus.core.lib.CORE; +import gtPlusPlus.core.util.Utils; +import gtPlusPlus.xmod.gregtech.common.tileentities.machines.multi.GT4Entity_AutoCrafter; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.inventory.Container; +import net.minecraft.world.World; +import net.minecraft.world.WorldServer; +import net.minecraftforge.common.util.FakePlayerFactory; + +public class AC_Helper_Utils { + + //AC maps + public static final Map sAutocrafterMap = new HashMap(); + + //Add Crafter + public final static int addCrafter(GT4Entity_AutoCrafter AC) { + if (!sAutocrafterMap.containsValue(AC)){ + int increase = sAutocrafterMap.size()+1; + sAutocrafterMap.put(increase, AC); + Utils.LOG_INFO("[A-C] "+"Added Auto-Crafter to index on position "+increase+"."); + return increase; + } + else { + Utils.LOG_INFO("[A-C] Tried adding an Auto-Crafter to Index, but found one already there."); + } + return 0; + } + + //Remove Crafter + public final static boolean removeCrafter(int frequency) { + if (!sAutocrafterMap.isEmpty()){ + if (sAutocrafterMap.containsKey(frequency)){ + sAutocrafterMap.remove(frequency); + return true; + } + } + return false; + } + + public final static boolean removeCrafter(GT4Entity_AutoCrafter AC) { + if (!sAutocrafterMap.isEmpty()){ + if (sAutocrafterMap.containsValue(AC)){ + sAutocrafterMap.remove(AC); + return true; + } + } + return false; + } + + //Get Crafter + public final static GT4Entity_AutoCrafter getCrafterByID(int ID) { + if (!sAutocrafterMap.isEmpty()) { + Set> players = sAutocrafterMap.entrySet(); + Iterator> i = players.iterator(); + while (i.hasNext()) { + Entry current = i.next(); + if (current.getKey().equals(ID)) { + return current.getValue(); + } + } + } + Utils.LOG_WARNING("Failed. [getCrafterByID]"); + return null; + } + + public final static int getIDByCrafter(GT4Entity_AutoCrafter AC) { + if (!sAutocrafterMap.isEmpty()) { + Set> players = sAutocrafterMap.entrySet(); + Iterator> i = players.iterator(); + while (i.hasNext()) { + Entry current = i.next(); + if (current.getValue().equals(AC)) { + return current.getKey(); + } + } + } + Utils.LOG_WARNING("Failed. [getIDByCrafter]"); + return 0; + } + +} -- cgit