diff --git a/src/main/java/appeng/container/implementations/ContainerExpandedProcessingPatternTerm.java b/src/main/java/appeng/container/implementations/ContainerExpandedProcessingPatternTerm.java index b3f6312b7..dbdf4e17a 100644 --- a/src/main/java/appeng/container/implementations/ContainerExpandedProcessingPatternTerm.java +++ b/src/main/java/appeng/container/implementations/ContainerExpandedProcessingPatternTerm.java @@ -1,55 +1,33 @@ package appeng.container.implementations; -import appeng.api.AEApi; -import appeng.api.definitions.IDefinitions; import appeng.api.storage.ITerminalHost; -import appeng.container.slot.*; -import appeng.helpers.IContainerCraftingPacket; -import appeng.parts.reporting.PartExpandedProcessingPatternTerminal; -import appeng.util.Platform; -import appeng.util.inv.IAEAppEngInventory; -import appeng.util.inv.InvOperation; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.entity.player.EntityPlayerMP; +import appeng.container.slot.OptionalSlotFake; +import appeng.container.slot.SlotFakeCraftingMatrix; +import appeng.container.slot.SlotPatternOutputs; +import appeng.container.slot.SlotRestrictedInput; import net.minecraft.entity.player.InventoryPlayer; -import net.minecraft.inventory.IContainerListener; -import net.minecraft.inventory.Slot; -import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NBTBase; -import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.nbt.NBTTagList; import net.minecraftforge.items.IItemHandler; -import net.minecraftforge.items.wrapper.PlayerInvWrapper; - -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; import static appeng.helpers.PatternHelper.PROCESSING_INPUT_LIMIT; import static appeng.helpers.PatternHelper.PROCESSING_OUTPUT_LIMIT; -public class ContainerExpandedProcessingPatternTerm extends ContainerMEMonitorable implements IAEAppEngInventory, IOptionalSlotHost, IContainerCraftingPacket { - private final IItemHandler crafting; - private final SlotFakeCraftingMatrix[] gridSlots = new SlotFakeCraftingMatrix[PROCESSING_INPUT_LIMIT]; - private final OptionalSlotFake[] outputSlots = new OptionalSlotFake[PROCESSING_OUTPUT_LIMIT]; - private final SlotRestrictedInput patternSlotIN; - private final SlotRestrictedInput patternSlotOUT; - private final PartExpandedProcessingPatternTerminal expandedProcessingPatternTerminal; +public class ContainerExpandedProcessingPatternTerm extends ContainerPatternEncoder { public ContainerExpandedProcessingPatternTerm(InventoryPlayer ip, ITerminalHost monitorable) { super(ip, monitorable, false); - this.expandedProcessingPatternTerminal = (PartExpandedProcessingPatternTerminal) monitorable; + this.craftingSlots = new SlotFakeCraftingMatrix[PROCESSING_INPUT_LIMIT]; + this.outputSlots = new OptionalSlotFake[PROCESSING_OUTPUT_LIMIT]; - final IItemHandler patternInv = this.getExpandedPatternTerminal().getInventoryByName("pattern"); - final IItemHandler output = this.getExpandedPatternTerminal().getInventoryByName("output"); + final IItemHandler patternInv = this.getPart().getInventoryByName("pattern"); + final IItemHandler output = this.getPart().getInventoryByName("output"); - this.crafting = this.getExpandedPatternTerminal().getInventoryByName("crafting"); + this.crafting = this.getPart().getInventoryByName("crafting"); for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { - this.addSlotToContainer(this.gridSlots[x + y * 4] = new SlotFakeCraftingMatrix(this.crafting, x + y * 4, 4 + x * 18, -85 + y * 18)); + this.addSlotToContainer(this.craftingSlots[x + y * 4] = new SlotFakeCraftingMatrix(this.crafting, x + y * 4, 4 + x * 18, -85 + y * 18)); } } @@ -67,124 +45,7 @@ public class ContainerExpandedProcessingPatternTerm extends ContainerMEMonitorab this.patternSlotOUT.setStackLimit(1); this.bindPlayerInventory(ip, 0, 0); - } - @Override - public ItemStack transferStackInSlot(final EntityPlayer p, final int idx) { - if (Platform.isClient()) { - return ItemStack.EMPTY; - } - if (this.inventorySlots.get(idx) instanceof SlotPlayerInv || this.inventorySlots.get(idx) instanceof SlotPlayerHotBar) { - final AppEngSlot clickSlot = (AppEngSlot) this.inventorySlots.get(idx); // require AE SLots! - ItemStack itemStack = clickSlot.getStack(); - if (AEApi.instance().definitions().materials().blankPattern().isSameAs(itemStack)) { - IItemHandler patternInv = this.getExpandedPatternTerminal().getInventoryByName("pattern"); - ItemStack remainder = patternInv.insertItem(0, itemStack, false); - clickSlot.putStack(remainder); - } - } - return super.transferStackInSlot(p, idx); - } - - @Override - public void saveChanges() { - - } - - @Override - public void onChangeInventory(final IItemHandler inv, final int slot, final InvOperation mc, final ItemStack removedStack, final ItemStack newStack) { - - } - - public void encodeAndMoveToInventory() { - encode(); - ItemStack output = this.patternSlotOUT.getStack(); - if (!output.isEmpty()) { - if (!getPlayerInv().addItemStackToInventory(output)) { - getPlayerInv().player.dropItem(output, false); - } - this.patternSlotOUT.putStack(ItemStack.EMPTY); - } - } - - public void encode() { - ItemStack output = this.patternSlotOUT.getStack(); - - final ItemStack[] in = this.getInputs(); - final ItemStack[] out = this.getOutputs(); - - // if there is no input, this would be silly. - if (in == null || out == null) { - return; - } - - // first check the output slots, should either be null, or a pattern - if (!output.isEmpty() && !this.isPattern(output)) { - return; - } // if nothing is there we should snag a new pattern. - else if (output.isEmpty()) { - output = this.patternSlotIN.getStack(); - if (output.isEmpty() || !this.isPattern(output)) { - return; // no blanks. - } - - // remove one, and clear the input slot. - output.setCount(output.getCount() - 1); - if (output.getCount() == 0) { - this.patternSlotIN.putStack(ItemStack.EMPTY); - } - - // add a new encoded pattern. - Optional maybePattern = AEApi.instance().definitions().items().encodedPattern().maybeStack(1); - if (maybePattern.isPresent()) { - output = maybePattern.get(); - this.patternSlotOUT.putStack(output); - } - } - - // encode the slot. - final NBTTagCompound encodedValue = new NBTTagCompound(); - - final NBTTagList tagIn = new NBTTagList(); - final NBTTagList tagOut = new NBTTagList(); - - for (final ItemStack i : in) { - tagIn.appendTag(this.createItemTag(i)); - } - - for (final ItemStack i : out) { - tagOut.appendTag(this.createItemTag(i)); - } - - encodedValue.setTag("in", tagIn); - encodedValue.setTag("out", tagOut); - encodedValue.setBoolean("crafting", false); - encodedValue.setBoolean("substitute", false); - - output.setTagCompound(encodedValue); - } - - boolean isPattern(final ItemStack output) { - if (output.isEmpty()) { - return false; - } - - final IDefinitions definitions = AEApi.instance().definitions(); - - boolean isPattern = definitions.items().encodedPattern().isSameAs(output); - isPattern |= definitions.materials().blankPattern().isSameAs(output); - - return isPattern; - } - - NBTBase createItemTag(final ItemStack i) { - final NBTTagCompound c = new NBTTagCompound(); - - if (!i.isEmpty()) { - i.writeToNBT(c); - } - - return c; } @Override @@ -192,261 +53,8 @@ public class ContainerExpandedProcessingPatternTerm extends ContainerMEMonitorab return true; } - protected ItemStack[] getInputs() { - final ItemStack[] input = new ItemStack[16]; - boolean hasValue = false; - - for (int x = 0; x < this.gridSlots.length; x++) { - input[x] = this.gridSlots[x].getStack(); - if (!input[x].isEmpty()) { - hasValue = true; - } - } - - if (hasValue) { - return input; - } - - return null; - } - - protected ItemStack[] getOutputs() { - final List list = new ArrayList<>(3); - boolean hasValue = false; - - for (final OptionalSlotFake outputSlot : this.outputSlots) { - final ItemStack out = outputSlot.getStack(); - - if (!out.isEmpty() && out.getCount() > 0) { - list.add(out); - hasValue = true; - } - } - - if (hasValue) { - return list.toArray(new ItemStack[0]); - } - return null; - } - - public void clear() { - for (final Slot s : this.gridSlots) { - s.putStack(ItemStack.EMPTY); - } - - for (final Slot s : this.outputSlots) { - s.putStack(ItemStack.EMPTY); - } - - this.detectAndSendChanges(); - } - @Override - public IItemHandler getInventoryByName(final String name) { - if (name.equals("player")) { - return new PlayerInvWrapper(this.getInventoryPlayer()); - } - return this.getExpandedPatternTerminal().getInventoryByName(name); - } - - @Override - public boolean useRealItems() { + public boolean isCraftingMode() { return false; } - - public PartExpandedProcessingPatternTerminal getExpandedPatternTerminal() { - return this.expandedProcessingPatternTerminal; - } - - public void multiply(int multiple) { - ItemStack[] input = new ItemStack[gridSlots.length]; - boolean canMultiplyInputs = true; - boolean canMultiplyOutputs = true; - - for (int x = 0; x < this.gridSlots.length; x++) { - input[x] = this.gridSlots[x].getStack(); - if (!input[x].isEmpty() && input[x].getCount() * multiple > input[x].getMaxStackSize()) { - canMultiplyInputs = false; - } - } - for (final OptionalSlotFake outputSlot : this.outputSlots) { - final ItemStack out = outputSlot.getStack(); - if (!out.isEmpty() && out.getCount() * multiple > out.getMaxStackSize()) { - canMultiplyOutputs = false; - } - } - if (canMultiplyInputs && canMultiplyOutputs) { - for (SlotFakeCraftingMatrix craftingSlot : this.gridSlots) { - ItemStack stack = craftingSlot.getStack(); - if (!stack.isEmpty()) { - craftingSlot.getStack().setCount(stack.getCount() * multiple); - } - } - for (OptionalSlotFake outputSlot : this.outputSlots) { - ItemStack stack = outputSlot.getStack(); - if (!stack.isEmpty()) { - outputSlot.getStack().setCount(stack.getCount() * multiple); - } - } - } - } - - public void divide(int divide) { - ItemStack[] input = new ItemStack[gridSlots.length]; - boolean canDivideInputs = true; - boolean canDivideOutputs = true; - - for (int x = 0; x < this.gridSlots.length; x++) { - input[x] = this.gridSlots[x].getStack(); - if (!input[x].isEmpty() && input[x].getCount() % divide != 0) { - canDivideInputs = false; - } - } - for (final OptionalSlotFake outputSlot : this.outputSlots) { - final ItemStack out = outputSlot.getStack(); - if (!out.isEmpty() && out.getCount() % divide != 0) { - canDivideOutputs = false; - } - } - if (canDivideInputs && canDivideOutputs) { - for (SlotFakeCraftingMatrix craftingSlot : this.gridSlots) { - ItemStack stack = craftingSlot.getStack(); - if (!stack.isEmpty()) { - craftingSlot.getStack().setCount(stack.getCount() / divide); - } - } - for (OptionalSlotFake outputSlot : this.outputSlots) { - ItemStack stack = outputSlot.getStack(); - if (!stack.isEmpty()) { - outputSlot.getStack().setCount(stack.getCount() / divide); - } - } - } - } - - public void increase(int increase) { - ItemStack[] input = new ItemStack[gridSlots.length]; - boolean canIncreaseInputs = true; - boolean canIncreaseOutputs = true; - - for (int x = 0; x < this.gridSlots.length; x++) { - input[x] = this.gridSlots[x].getStack(); - if (!input[x].isEmpty() && input[x].getCount() + increase > input[x].getMaxStackSize()) { - canIncreaseInputs = false; - } - } - for (final OptionalSlotFake outputSlot : this.outputSlots) { - final ItemStack out = outputSlot.getStack(); - if (!out.isEmpty() && out.getCount() + increase > out.getMaxStackSize()) { - canIncreaseOutputs = false; - } - } - if (canIncreaseInputs && canIncreaseOutputs) { - for (SlotFakeCraftingMatrix craftingSlot : this.gridSlots) { - ItemStack stack = craftingSlot.getStack(); - if (!stack.isEmpty()) { - craftingSlot.getStack().setCount(stack.getCount() + increase); - } - } - for (OptionalSlotFake outputSlot : this.outputSlots) { - ItemStack stack = outputSlot.getStack(); - if (!stack.isEmpty()) { - outputSlot.getStack().setCount(stack.getCount() + increase); - } - } - } - } - - public void decrease(int decrease) { - ItemStack[] input = new ItemStack[gridSlots.length]; - boolean canDecreaseInputs = true; - boolean canDecreaseOutputs = true; - - for (int x = 0; x < this.gridSlots.length; x++) { - input[x] = this.gridSlots[x].getStack(); - if (!input[x].isEmpty() && input[x].getCount() - decrease < 1) { - canDecreaseInputs = false; - } - } - for (final OptionalSlotFake outputSlot : this.outputSlots) { - final ItemStack out = outputSlot.getStack(); - if (!out.isEmpty() && out.getCount() - decrease < 1) { - canDecreaseOutputs = false; - } - } - if (canDecreaseInputs && canDecreaseOutputs) { - for (SlotFakeCraftingMatrix craftingSlot : this.gridSlots) { - ItemStack stack = craftingSlot.getStack(); - if (!stack.isEmpty()) { - craftingSlot.getStack().setCount(stack.getCount() - decrease); - } - } - for (OptionalSlotFake outputSlot : this.outputSlots) { - ItemStack stack = outputSlot.getStack(); - if (!stack.isEmpty()) { - outputSlot.getStack().setCount(stack.getCount() - decrease); - } - } - } - } - - public void maximizeCount() { - ItemStack[] input = new ItemStack[gridSlots.length]; - boolean canGrowInputs = true; - boolean canGrowOutputs = true; - int maxInputStackGrowth = 0; - int maxOutputStackGrowth = 0; - - for (int x = 0; x < this.gridSlots.length; x++) { - input[x] = this.gridSlots[x].getStack(); - if (!input[x].isEmpty() && input[x].getMaxStackSize() - input[x].getCount() > maxInputStackGrowth) { - maxInputStackGrowth = input[x].getMaxStackSize() - input[x].getCount(); - } - if (!input[x].isEmpty() && input[x].getCount() + maxInputStackGrowth > input[x].getMaxStackSize()) { - canGrowInputs = false; - } - } - for (final OptionalSlotFake outputSlot : this.outputSlots) { - final ItemStack out = outputSlot.getStack(); - { - maxOutputStackGrowth = out.getMaxStackSize() - out.getCount(); - } - if (!out.isEmpty() && out.getCount() + maxOutputStackGrowth > out.getMaxStackSize()) { - canGrowOutputs = false; - } - } - if (canGrowInputs && canGrowOutputs) { - int maxStackGrowth = Math.min(maxInputStackGrowth, maxOutputStackGrowth); - for (SlotFakeCraftingMatrix craftingSlot : this.gridSlots) { - ItemStack stack = craftingSlot.getStack(); - if (!stack.isEmpty()) { - craftingSlot.getStack().setCount(stack.getCount() + maxStackGrowth); - } - } - for (OptionalSlotFake outputSlot : this.outputSlots) { - ItemStack stack = outputSlot.getStack(); - if (!stack.isEmpty()) { - outputSlot.getStack().setCount(stack.getCount() + maxStackGrowth); - } - } - } - } - - @Override - public void onSlotChange(final Slot s) { - if (s == this.patternSlotOUT && Platform.isServer()) { - for (final IContainerListener listener : this.listeners) { - for (final Slot slot : this.inventorySlots) { - if (slot instanceof OptionalSlotFake || slot instanceof SlotFakeCraftingMatrix) { - listener.sendSlotContents(this, slot.slotNumber, slot.getStack()); - } - } - if (listener instanceof EntityPlayerMP) { - ((EntityPlayerMP) listener).isChangingQuantityOnly = false; - } - } - this.detectAndSendChanges(); - } - } - } diff --git a/src/main/java/appeng/container/implementations/ContainerPatternEncoder.java b/src/main/java/appeng/container/implementations/ContainerPatternEncoder.java new file mode 100644 index 000000000..b5c490861 --- /dev/null +++ b/src/main/java/appeng/container/implementations/ContainerPatternEncoder.java @@ -0,0 +1,550 @@ +package appeng.container.implementations; + +import appeng.api.AEApi; +import appeng.api.definitions.IDefinitions; +import appeng.api.storage.ITerminalHost; +import appeng.container.guisync.GuiSync; +import appeng.container.slot.AppEngSlot; +import appeng.container.slot.IOptionalSlotHost; +import appeng.container.slot.OptionalSlotFake; +import appeng.container.slot.SlotFakeCraftingMatrix; +import appeng.container.slot.SlotPatternTerm; +import appeng.container.slot.SlotPlayerHotBar; +import appeng.container.slot.SlotPlayerInv; +import appeng.container.slot.SlotRestrictedInput; +import appeng.helpers.IContainerCraftingPacket; +import appeng.parts.reporting.AbstractPartEncoder; +import appeng.tile.inventory.AppEngInternalInventory; +import appeng.util.Platform; +import appeng.util.inv.IAEAppEngInventory; +import appeng.util.inv.InvOperation; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.entity.player.InventoryPlayer; +import net.minecraft.inventory.IContainerListener; +import net.minecraft.inventory.InventoryCrafting; +import net.minecraft.inventory.Slot; +import net.minecraft.item.ItemStack; +import net.minecraft.item.crafting.CraftingManager; +import net.minecraft.item.crafting.IRecipe; +import net.minecraft.nbt.NBTBase; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; +import net.minecraft.world.World; +import net.minecraftforge.items.IItemHandler; +import net.minecraftforge.items.wrapper.PlayerInvWrapper; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +public abstract class ContainerPatternEncoder extends ContainerMEMonitorable implements IAEAppEngInventory, IOptionalSlotHost, IContainerCraftingPacket { + + private final AbstractPartEncoder patternTerminal; + + final AppEngInternalInventory cOut = new AppEngInternalInventory(null, 1); + + protected IItemHandler crafting; + protected SlotPatternTerm craftSlot; + protected SlotRestrictedInput patternSlotIN; + protected SlotRestrictedInput patternSlotOUT; + private IRecipe currentRecipe; + + protected SlotFakeCraftingMatrix[] craftingSlots; + protected OptionalSlotFake[] outputSlots; + + @GuiSync(97) + public boolean craftingMode = true; + @GuiSync(96) + public boolean substitute = false; + + public ContainerPatternEncoder(InventoryPlayer ip, ITerminalHost monitorable) { + super(ip, monitorable); + patternTerminal = (AbstractPartEncoder) monitorable; + } + + protected ContainerPatternEncoder(InventoryPlayer ip, ITerminalHost monitorable, boolean bindInventory) { + super(ip, monitorable, bindInventory); + patternTerminal = (AbstractPartEncoder) monitorable; + } + + @Override + public ItemStack transferStackInSlot(final EntityPlayer p, final int idx) { + if (Platform.isClient()) { + return ItemStack.EMPTY; + } + if (this.inventorySlots.get(idx) instanceof SlotPlayerInv || this.inventorySlots.get(idx) instanceof SlotPlayerHotBar) { + final AppEngSlot clickSlot = (AppEngSlot) this.inventorySlots.get(idx); // require AE SLots! + ItemStack itemStack = clickSlot.getStack(); + if (AEApi.instance().definitions().materials().blankPattern().isSameAs(itemStack)) { + IItemHandler patternInv = this.getPart().getInventoryByName("pattern"); + ItemStack remainder = patternInv.insertItem(0, itemStack, false); + clickSlot.putStack(remainder); + } + } + return super.transferStackInSlot(p, idx); + } + + public AbstractPartEncoder getPart() { + return this.patternTerminal; + } + + @Override + public abstract boolean isSlotEnabled(int idx); + + @Override + public IItemHandler getInventoryByName(String name) { + if (name.equals("player")) { + return new PlayerInvWrapper(this.getInventoryPlayer()); + } + return this.getPart().getInventoryByName(name); + } + + @Override + public boolean useRealItems() { + return false; + } + + @Override + public void saveChanges() { + + } + + @Override + public void onChangeInventory(IItemHandler inv, int slot, InvOperation mc, ItemStack removedStack, ItemStack newStack) { + + } + + @Override + public void putStackInSlot(int slotID, ItemStack stack) { + super.putStackInSlot(slotID, stack); + this.getAndUpdateOutput(); + } + + protected void updateOrderOfOutputSlots() { + if (!this.isCraftingMode()) { + if (craftSlot != null) { + this.craftSlot.xPos = -9000; + } + + for (int y = 0; y < 3; y++) { + this.outputSlots[y].xPos = this.outputSlots[y].getX(); + } + } else { + if (craftSlot != null) { + this.craftSlot.xPos = this.craftSlot.getX(); + } + for (int y = 0; y < 3; y++) { + this.outputSlots[y].xPos = -9000; + } + } + } + + @Override + public void onSlotChange(final Slot s) { + if (s == this.patternSlotOUT && Platform.isServer()) { + for (final IContainerListener listener : this.listeners) { + for (final Slot slot : this.inventorySlots) { + if (slot instanceof OptionalSlotFake || slot instanceof SlotFakeCraftingMatrix) { + listener.sendSlotContents(this, slot.slotNumber, slot.getStack()); + } + } + if (listener instanceof EntityPlayerMP) { + ((EntityPlayerMP) listener).isChangingQuantityOnly = false; + } + } + this.detectAndSendChanges(); + } + } + + public void encodeAndMoveToInventory() { + encode(); + ItemStack output = this.patternSlotOUT.getStack(); + if (!output.isEmpty()) { + if (!getPlayerInv().addItemStackToInventory(output)) { + getPlayerInv().player.dropItem(output, false); + } + this.patternSlotOUT.putStack(ItemStack.EMPTY); + } + } + + public void encode() { + ItemStack output = this.patternSlotOUT.getStack(); + + final ItemStack[] in = this.getInputs(); + final ItemStack[] out = this.getOutputs(); + + // if there is no input, this would be silly. + if (in == null || out == null) { + return; + } + + // first check the output slots, should either be null, or a pattern + if (!output.isEmpty() && !this.isPattern(output)) { + return; + } // if nothing is there we should snag a new pattern. + else if (output.isEmpty()) { + output = this.patternSlotIN.getStack(); + if (output.isEmpty() || !this.isPattern(output)) { + return; // no blanks. + } + + // remove one, and clear the input slot. + output.setCount(output.getCount() - 1); + if (output.getCount() == 0) { + this.patternSlotIN.putStack(ItemStack.EMPTY); + } + + // add a new encoded pattern. + Optional maybePattern = AEApi.instance().definitions().items().encodedPattern().maybeStack(1); + if (maybePattern.isPresent()) { + output = maybePattern.get(); + this.patternSlotOUT.putStack(output); + } + } + + // encode the slot. + final NBTTagCompound encodedValue = new NBTTagCompound(); + + final NBTTagList tagIn = new NBTTagList(); + final NBTTagList tagOut = new NBTTagList(); + + for (final ItemStack i : in) { + tagIn.appendTag(this.createItemTag(i)); + } + + for (final ItemStack i : out) { + tagOut.appendTag(this.createItemTag(i)); + } + + encodedValue.setTag("in", tagIn); + encodedValue.setTag("out", tagOut); + encodedValue.setBoolean("crafting", this.isCraftingMode()); + encodedValue.setBoolean("substitute", this.isSubstitute()); + + output.setTagCompound(encodedValue); + } + + public void multiply(int multiple) { + ItemStack[] input = new ItemStack[craftingSlots.length]; + boolean canMultiplyInputs = true; + boolean canMultiplyOutputs = true; + + for (int x = 0; x < this.craftingSlots.length; x++) { + input[x] = this.craftingSlots[x].getStack(); + if (!input[x].isEmpty() && input[x].getCount() * multiple < 1) { + canMultiplyInputs = false; + } + } + for (final OptionalSlotFake outputSlot : this.outputSlots) { + final ItemStack out = outputSlot.getStack(); + if (!out.isEmpty() && out.getCount() * multiple < 1) { + canMultiplyOutputs = false; + } + } + if (canMultiplyInputs && canMultiplyOutputs) { + for (SlotFakeCraftingMatrix craftingSlot : this.craftingSlots) { + ItemStack stack = craftingSlot.getStack(); + if (!stack.isEmpty()) { + craftingSlot.getStack().setCount(stack.getCount() * multiple); + } + } + for (OptionalSlotFake outputSlot : this.outputSlots) { + ItemStack stack = outputSlot.getStack(); + if (!stack.isEmpty()) { + outputSlot.getStack().setCount(stack.getCount() * multiple); + } + } + } + } + + public void divide(int divide) { + ItemStack[] input = new ItemStack[craftingSlots.length]; + boolean canDivideInputs = true; + boolean canDivideOutputs = true; + + for (int x = 0; x < this.craftingSlots.length; x++) { + input[x] = this.craftingSlots[x].getStack(); + if (!input[x].isEmpty() && input[x].getCount() % divide != 0) { + canDivideInputs = false; + } + } + for (final OptionalSlotFake outputSlot : this.outputSlots) { + final ItemStack out = outputSlot.getStack(); + if (!out.isEmpty() && out.getCount() % divide != 0) { + canDivideOutputs = false; + } + } + if (canDivideInputs && canDivideOutputs) { + for (SlotFakeCraftingMatrix craftingSlot : this.craftingSlots) { + ItemStack stack = craftingSlot.getStack(); + if (!stack.isEmpty()) { + craftingSlot.getStack().setCount(stack.getCount() / divide); + } + } + for (OptionalSlotFake outputSlot : this.outputSlots) { + ItemStack stack = outputSlot.getStack(); + if (!stack.isEmpty()) { + outputSlot.getStack().setCount(stack.getCount() / divide); + } + } + } + } + + public void increase(int increase) { + ItemStack[] input = new ItemStack[craftingSlots.length]; + boolean canIncreaseInputs = true; + boolean canIncreaseOutputs = true; + + for (int x = 0; x < this.craftingSlots.length; x++) { + input[x] = this.craftingSlots[x].getStack(); + if (!input[x].isEmpty() && input[x].getCount() + increase < 1) { + canIncreaseInputs = false; + } + } + for (final OptionalSlotFake outputSlot : this.outputSlots) { + final ItemStack out = outputSlot.getStack(); + if (!out.isEmpty() && out.getCount() + increase < 1) { + canIncreaseOutputs = false; + } + } + if (canIncreaseInputs && canIncreaseOutputs) { + for (SlotFakeCraftingMatrix craftingSlot : this.craftingSlots) { + ItemStack stack = craftingSlot.getStack(); + if (!stack.isEmpty()) { + craftingSlot.getStack().setCount(stack.getCount() + increase); + } + } + for (OptionalSlotFake outputSlot : this.outputSlots) { + ItemStack stack = outputSlot.getStack(); + if (!stack.isEmpty()) { + outputSlot.getStack().setCount(stack.getCount() + increase); + } + } + } + } + + public void decrease(int decrease) { + ItemStack[] input = new ItemStack[craftingSlots.length]; + boolean canDecreaseInputs = true; + boolean canDecreaseOutputs = true; + + for (int x = 0; x < this.craftingSlots.length; x++) { + input[x] = this.craftingSlots[x].getStack(); + if (!input[x].isEmpty() && input[x].getCount() - decrease < 1) { + canDecreaseInputs = false; + } + } + for (final OptionalSlotFake outputSlot : this.outputSlots) { + final ItemStack out = outputSlot.getStack(); + if (!out.isEmpty() && out.getCount() - decrease < 1) { + canDecreaseOutputs = false; + } + } + if (canDecreaseInputs && canDecreaseOutputs) { + for (SlotFakeCraftingMatrix craftingSlot : this.craftingSlots) { + ItemStack stack = craftingSlot.getStack(); + if (!stack.isEmpty()) { + craftingSlot.getStack().setCount(stack.getCount() - decrease); + } + } + for (OptionalSlotFake outputSlot : this.outputSlots) { + ItemStack stack = outputSlot.getStack(); + if (!stack.isEmpty()) { + outputSlot.getStack().setCount(stack.getCount() - decrease); + } + } + } + } + + public void maximizeCount() { + ItemStack[] input = new ItemStack[craftingSlots.length]; + boolean canGrowInputs = true; + boolean canGrowOutputs = true; + int maxInputStackGrowth = 0; + int maxOutputStackGrowth = 0; + + for (int x = 0; x < this.craftingSlots.length; x++) { + input[x] = this.craftingSlots[x].getStack(); + if (!input[x].isEmpty() && input[x].getMaxStackSize() - input[x].getCount() > maxInputStackGrowth) { + maxInputStackGrowth = input[x].getMaxStackSize() - input[x].getCount(); + } + if (!input[x].isEmpty() && input[x].getCount() + maxInputStackGrowth > input[x].getMaxStackSize()) { + canGrowInputs = false; + } + } + for (final OptionalSlotFake outputSlot : this.outputSlots) { + final ItemStack out = outputSlot.getStack(); + { + maxOutputStackGrowth = out.getMaxStackSize() - out.getCount(); + } + if (!out.isEmpty() && out.getCount() + maxOutputStackGrowth > out.getMaxStackSize()) { + canGrowOutputs = false; + } + } + if (canGrowInputs && canGrowOutputs) { + int maxStackGrowth = Math.min(maxInputStackGrowth, maxOutputStackGrowth); + for (SlotFakeCraftingMatrix craftingSlot : this.craftingSlots) { + ItemStack stack = craftingSlot.getStack(); + if (!stack.isEmpty()) { + craftingSlot.getStack().setCount(stack.getCount() + maxStackGrowth); + } + } + for (OptionalSlotFake outputSlot : this.outputSlots) { + ItemStack stack = outputSlot.getStack(); + if (!stack.isEmpty()) { + outputSlot.getStack().setCount(stack.getCount() + maxStackGrowth); + } + } + } + } + + protected ItemStack[] getInputs() { + final ItemStack[] input = new ItemStack[craftingSlots.length]; + boolean hasValue = false; + + for (int x = 0; x < this.craftingSlots.length; x++) { + input[x] = this.craftingSlots[x].getStack(); + if (!input[x].isEmpty()) { + hasValue = true; + } + } + + if (hasValue) { + return input; + } + + return null; + } + + protected ItemStack[] getOutputs() { + if (this.isCraftingMode()) { + final ItemStack out = this.getAndUpdateOutput(); + + if (!out.isEmpty() && out.getCount() > 0) { + return new ItemStack[]{out}; + } + } else { + final List list = new ArrayList<>(); + boolean hasValue = false; + + for (final OptionalSlotFake outputSlot : this.outputSlots) { + final ItemStack out = outputSlot.getStack(); + + if (!out.isEmpty() && out.getCount() > 0) { + list.add(out); + hasValue = true; + } + } + + if (hasValue) { + return list.toArray(new ItemStack[0]); + } + } + + return null; + } + + protected ItemStack getAndUpdateOutput() { + final World world = this.getPlayerInv().player.world; + final InventoryCrafting ic = new InventoryCrafting(this, 3, 3); + + for (int x = 0; x < ic.getSizeInventory(); x++) { + ic.setInventorySlotContents(x, this.crafting.getStackInSlot(x)); + } + + if (this.currentRecipe == null || !this.currentRecipe.matches(ic, world)) { + this.currentRecipe = CraftingManager.findMatchingRecipe(ic, world); + } + + final ItemStack is; + + if (this.currentRecipe == null) { + is = ItemStack.EMPTY; + } else { + is = this.currentRecipe.getCraftingResult(ic); + } + + this.cOut.setStackInSlot(0, is); + return is; + } + + public boolean isCraftingMode() { + return this.craftingMode; + } + + void setCraftingMode(final boolean craftingMode) { + this.craftingMode = craftingMode; + } + + + boolean isSubstitute() { + return this.substitute; + } + + public void setSubstitute(final boolean substitute) { + this.substitute = substitute; + } + + @Override + public void detectAndSendChanges() { + super.detectAndSendChanges(); + if (Platform.isServer()) { + if (this.isCraftingMode() != this.getPart().isCraftingRecipe()) { + this.setCraftingMode(this.getPart().isCraftingRecipe()); + this.updateOrderOfOutputSlots(); + } + + this.substitute = this.getPart().isSubstitution(); + } + } + + @Override + public void onUpdate(final String field, final Object oldValue, final Object newValue) { + super.onUpdate(field, oldValue, newValue); + + if (field.equals("craftingMode")) { + this.getAndUpdateOutput(); + this.updateOrderOfOutputSlots(); + } + } + + boolean isPattern(final ItemStack output) { + if (output.isEmpty()) { + return false; + } + + final IDefinitions definitions = AEApi.instance().definitions(); + + boolean isPattern = definitions.items().encodedPattern().isSameAs(output); + isPattern |= definitions.materials().blankPattern().isSameAs(output); + + return isPattern; + } + + NBTBase createItemTag(final ItemStack i) { + final NBTTagCompound c = new NBTTagCompound(); + + if (!i.isEmpty()) { + i.writeToNBT(c); + if (i.getCount() > i.getMaxStackSize()) { + c.setInteger("stackSize", i.getCount()); + } + } + + return c; + } + + public void clear() { + for (final Slot s : this.craftingSlots) { + s.putStack(ItemStack.EMPTY); + } + + for (final Slot s : this.outputSlots) { + s.putStack(ItemStack.EMPTY); + } + + this.detectAndSendChanges(); + this.getAndUpdateOutput(); + } +} diff --git a/src/main/java/appeng/container/implementations/ContainerPatternTerm.java b/src/main/java/appeng/container/implementations/ContainerPatternTerm.java index 5b1e146b6..c39893b01 100644 --- a/src/main/java/appeng/container/implementations/ContainerPatternTerm.java +++ b/src/main/java/appeng/container/implementations/ContainerPatternTerm.java @@ -29,9 +29,15 @@ import appeng.api.storage.data.IAEItemStack; import appeng.api.storage.data.IItemList; import appeng.container.ContainerNull; import appeng.container.guisync.GuiSync; -import appeng.container.slot.*; +import appeng.container.slot.AppEngSlot; +import appeng.container.slot.OptionalSlotFake; +import appeng.container.slot.SlotFakeCraftingMatrix; +import appeng.container.slot.SlotPatternOutputs; +import appeng.container.slot.SlotPatternTerm; +import appeng.container.slot.SlotPlayerHotBar; +import appeng.container.slot.SlotPlayerInv; +import appeng.container.slot.SlotRestrictedInput; import appeng.core.sync.packets.PacketPatternSlot; -import appeng.helpers.IContainerCraftingPacket; import appeng.items.storage.ItemViewCell; import appeng.me.helpers.MachineSource; import appeng.parts.reporting.PartPatternTerminal; @@ -39,7 +45,6 @@ import appeng.tile.inventory.AppEngInternalInventory; import appeng.util.InventoryAdaptor; import appeng.util.Platform; import appeng.util.inv.AdaptorItemHandler; -import appeng.util.inv.IAEAppEngInventory; import appeng.util.inv.InvOperation; import appeng.util.inv.WrapperCursorItemHandler; import appeng.util.item.AEItemStack; @@ -62,31 +67,19 @@ import java.util.List; import java.util.Optional; -public class ContainerPatternTerm extends ContainerMEMonitorable implements IAEAppEngInventory, IOptionalSlotHost, IContainerCraftingPacket { +public class ContainerPatternTerm extends ContainerPatternEncoder { - private final PartPatternTerminal patternTerminal; - private final AppEngInternalInventory cOut = new AppEngInternalInventory(null, 1); - private final IItemHandler crafting; - private final SlotFakeCraftingMatrix[] craftingSlots = new SlotFakeCraftingMatrix[9]; - private final OptionalSlotFake[] outputSlots = new OptionalSlotFake[3]; - private final SlotPatternTerm craftSlot; - private final SlotRestrictedInput patternSlotIN; - private final SlotRestrictedInput patternSlotOUT; - - private IRecipe currentRecipe; - @GuiSync(97) - public boolean craftingMode = true; - @GuiSync(96) - public boolean substitute = false; public ContainerPatternTerm(final InventoryPlayer ip, final ITerminalHost monitorable) { super(ip, monitorable, false); - this.patternTerminal = (PartPatternTerminal) monitorable; - final IItemHandler patternInv = this.getPatternTerminal().getInventoryByName("pattern"); - final IItemHandler output = this.getPatternTerminal().getInventoryByName("output"); + this.craftingSlots = new SlotFakeCraftingMatrix[9]; + this.outputSlots = new OptionalSlotFake[3]; - this.crafting = this.getPatternTerminal().getInventoryByName("crafting"); + final IItemHandler patternInv = this.getPart().getInventoryByName("pattern"); + final IItemHandler output = this.getPart().getInventoryByName("output"); + + this.crafting = this.getPart().getInventoryByName("crafting"); for (int y = 0; y < 3; y++) { for (int x = 0; x < 3; x++) { @@ -117,391 +110,13 @@ public class ContainerPatternTerm extends ContainerMEMonitorable implements IAEA this.updateOrderOfOutputSlots(); } - @Override - public ItemStack transferStackInSlot(final EntityPlayer p, final int idx) { - if (Platform.isClient()) { - return ItemStack.EMPTY; - } - if (this.inventorySlots.get(idx) instanceof SlotPlayerInv || this.inventorySlots.get(idx) instanceof SlotPlayerHotBar) { - final AppEngSlot clickSlot = (AppEngSlot) this.inventorySlots.get(idx); // require AE SLots! - ItemStack itemStack = clickSlot.getStack(); - if (AEApi.instance().definitions().materials().blankPattern().isSameAs(itemStack)) { - IItemHandler patternInv = this.getPatternTerminal().getInventoryByName("pattern"); - ItemStack remainder = patternInv.insertItem(0, itemStack, false); - clickSlot.putStack(remainder); - } - } - return super.transferStackInSlot(p, idx); - } - - - private void updateOrderOfOutputSlots() { - if (!this.isCraftingMode()) { - this.craftSlot.xPos = -9000; - - for (int y = 0; y < 3; y++) { - this.outputSlots[y].xPos = this.outputSlots[y].getX(); - } - } else { - this.craftSlot.xPos = this.craftSlot.getX(); - - for (int y = 0; y < 3; y++) { - this.outputSlots[y].xPos = -9000; - } - } - } - - @Override - public void putStackInSlot(int slotID, ItemStack stack) { - super.putStackInSlot(slotID, stack); - this.getAndUpdateOutput(); - } - - protected ItemStack getAndUpdateOutput() { - final World world = this.getPlayerInv().player.world; - final InventoryCrafting ic = new InventoryCrafting(this, 3, 3); - - for (int x = 0; x < ic.getSizeInventory(); x++) { - ic.setInventorySlotContents(x, this.crafting.getStackInSlot(x)); - } - - if (this.currentRecipe == null || !this.currentRecipe.matches(ic, world)) { - this.currentRecipe = CraftingManager.findMatchingRecipe(ic, world); - } - - final ItemStack is; - - if (this.currentRecipe == null) { - is = ItemStack.EMPTY; - } else { - is = this.currentRecipe.getCraftingResult(ic); - } - - this.cOut.setStackInSlot(0, is); - return is; - } - - @Override - public void saveChanges() { - - } - - @Override - public void onChangeInventory(final IItemHandler inv, final int slot, final InvOperation mc, final ItemStack removedStack, final ItemStack newStack) { - - } - - public void encodeAndMoveToInventory() { - encode(); - ItemStack output = this.patternSlotOUT.getStack(); - if (!output.isEmpty()) { - if (!getPlayerInv().addItemStackToInventory(output)) { - getPlayerInv().player.dropItem(output, false); - } - this.patternSlotOUT.putStack(ItemStack.EMPTY); - } - } - - public void encode() { - ItemStack output = this.patternSlotOUT.getStack(); - - final ItemStack[] in = this.getInputs(); - final ItemStack[] out = this.getOutputs(); - - // if there is no input, this would be silly. - if (in == null || out == null) { - return; - } - - // first check the output slots, should either be null, or a pattern - if (!output.isEmpty() && !this.isPattern(output)) { - return; - } // if nothing is there we should snag a new pattern. - else if (output.isEmpty()) { - output = this.patternSlotIN.getStack(); - if (output.isEmpty() || !this.isPattern(output)) { - return; // no blanks. - } - - // remove one, and clear the input slot. - output.setCount(output.getCount() - 1); - if (output.getCount() == 0) { - this.patternSlotIN.putStack(ItemStack.EMPTY); - } - - // add a new encoded pattern. - Optional maybePattern = AEApi.instance().definitions().items().encodedPattern().maybeStack(1); - if (maybePattern.isPresent()) { - output = maybePattern.get(); - this.patternSlotOUT.putStack(output); - } - } - - // encode the slot. - final NBTTagCompound encodedValue = new NBTTagCompound(); - - final NBTTagList tagIn = new NBTTagList(); - final NBTTagList tagOut = new NBTTagList(); - - for (final ItemStack i : in) { - tagIn.appendTag(this.createItemTag(i)); - } - - for (final ItemStack i : out) { - tagOut.appendTag(this.createItemTag(i)); - } - - encodedValue.setTag("in", tagIn); - encodedValue.setTag("out", tagOut); - encodedValue.setBoolean("crafting", this.isCraftingMode()); - encodedValue.setBoolean("substitute", this.isSubstitute()); - - output.setTagCompound(encodedValue); - } - - public void multiply(int multiple) { - ItemStack[] input = new ItemStack[9]; - boolean canMultiplyInputs = true; - boolean canMultiplyOutputs = true; - - for (int x = 0; x < this.craftingSlots.length; x++) { - input[x] = this.craftingSlots[x].getStack(); - if (!input[x].isEmpty() && input[x].getCount() * multiple < 0) { - canMultiplyInputs = false; - } - } - for (final OptionalSlotFake outputSlot : this.outputSlots) { - final ItemStack out = outputSlot.getStack(); - if (!out.isEmpty() && out.getCount() * multiple < 0) { - canMultiplyOutputs = false; - } - } - if (canMultiplyInputs && canMultiplyOutputs) { - for (SlotFakeCraftingMatrix craftingSlot : this.craftingSlots) { - ItemStack stack = craftingSlot.getStack(); - if (!stack.isEmpty()) - craftingSlot.getStack().setCount(stack.getCount() * multiple); - } - for (OptionalSlotFake outputSlot : this.outputSlots) { - ItemStack stack = outputSlot.getStack(); - if (!stack.isEmpty()) - outputSlot.getStack().setCount(stack.getCount() * multiple); - } - } - } - - public void divide(int divide) { - ItemStack[] input = new ItemStack[9]; - boolean canDivideInputs = true; - boolean canDivideOutputs = true; - - for (int x = 0; x < this.craftingSlots.length; x++) { - input[x] = this.craftingSlots[x].getStack(); - if (!input[x].isEmpty() && input[x].getCount() % divide != 0) { - canDivideInputs = false; - } - } - for (final OptionalSlotFake outputSlot : this.outputSlots) { - final ItemStack out = outputSlot.getStack(); - if (!out.isEmpty() && out.getCount() % divide != 0) { - canDivideOutputs = false; - } - } - if (canDivideInputs && canDivideOutputs) { - for (SlotFakeCraftingMatrix craftingSlot : this.craftingSlots) { - ItemStack stack = craftingSlot.getStack(); - if (!stack.isEmpty()) - craftingSlot.getStack().setCount(stack.getCount() / divide); - } - for (OptionalSlotFake outputSlot : this.outputSlots) { - ItemStack stack = outputSlot.getStack(); - if (!stack.isEmpty()) - outputSlot.getStack().setCount(stack.getCount() / divide); - } - } - } - - public void increase(int increase) { - ItemStack[] input = new ItemStack[9]; - boolean canIncreaseInputs = true; - boolean canIncreaseOutputs = true; - - for (int x = 0; x < this.craftingSlots.length; x++) { - input[x] = this.craftingSlots[x].getStack(); - if (!input[x].isEmpty() && input[x].getCount() + increase < 0) { - canIncreaseInputs = false; - } - } - for (final OptionalSlotFake outputSlot : this.outputSlots) { - final ItemStack out = outputSlot.getStack(); - if (!out.isEmpty() && out.getCount() + increase < 0) { - canIncreaseOutputs = false; - } - } - if (canIncreaseInputs && canIncreaseOutputs) { - for (SlotFakeCraftingMatrix craftingSlot : this.craftingSlots) { - ItemStack stack = craftingSlot.getStack(); - if (!stack.isEmpty()) - craftingSlot.getStack().setCount(stack.getCount() + increase); - } - for (OptionalSlotFake outputSlot : this.outputSlots) { - ItemStack stack = outputSlot.getStack(); - if (!stack.isEmpty()) - outputSlot.getStack().setCount(stack.getCount() + increase); - } - } - } - - public void decrease(int decrease) { - ItemStack[] input = new ItemStack[9]; - boolean canDecreaseInputs = true; - boolean canDecreaseOutputs = true; - - for (int x = 0; x < this.craftingSlots.length; x++) { - input[x] = this.craftingSlots[x].getStack(); - if (!input[x].isEmpty() && input[x].getCount() - decrease < 1) { - canDecreaseInputs = false; - } - } - for (final OptionalSlotFake outputSlot : this.outputSlots) { - final ItemStack out = outputSlot.getStack(); - if (!out.isEmpty() && out.getCount() - decrease < 1) { - canDecreaseOutputs = false; - } - } - if (canDecreaseInputs && canDecreaseOutputs) { - for (SlotFakeCraftingMatrix craftingSlot : this.craftingSlots) { - ItemStack stack = craftingSlot.getStack(); - if (!stack.isEmpty()) - craftingSlot.getStack().setCount(stack.getCount() - decrease); - } - for (OptionalSlotFake outputSlot : this.outputSlots) { - ItemStack stack = outputSlot.getStack(); - if (!stack.isEmpty()) - outputSlot.getStack().setCount(stack.getCount() - decrease); - } - } - } - - public void maximizeCount() { - ItemStack[] input = new ItemStack[9]; - boolean canGrowInputs = true; - boolean canGrowOutputs = true; - int maxInputStackGrowth = 0; - int maxOutputStackGrowth = 0; - - - for (int x = 0; x < this.craftingSlots.length; x++) { - input[x] = this.craftingSlots[x].getStack(); - if (!input[x].isEmpty() && input[x].getMaxStackSize() - input[x].getCount() > maxInputStackGrowth) { - maxInputStackGrowth = input[x].getMaxStackSize() - input[x].getCount(); - } - if (!input[x].isEmpty() && input[x].getCount() + maxInputStackGrowth > input[x].getMaxStackSize()) { - canGrowInputs = false; - } - } - for (final OptionalSlotFake outputSlot : this.outputSlots) { - final ItemStack out = outputSlot.getStack(); - { - maxOutputStackGrowth = out.getMaxStackSize() - out.getCount(); - } - if (!out.isEmpty() && out.getCount() + maxOutputStackGrowth > out.getMaxStackSize()) { - canGrowOutputs = false; - } - } - if (canGrowInputs && canGrowOutputs) { - int maxStackGrowth = Math.min(maxInputStackGrowth, maxOutputStackGrowth); - for (SlotFakeCraftingMatrix craftingSlot : this.craftingSlots) { - ItemStack stack = craftingSlot.getStack(); - if (!stack.isEmpty()) - craftingSlot.getStack().setCount(stack.getCount() + maxStackGrowth); - } - for (OptionalSlotFake outputSlot : this.outputSlots) { - ItemStack stack = outputSlot.getStack(); - if (!stack.isEmpty()) - outputSlot.getStack().setCount(stack.getCount() + maxStackGrowth); - } - } - } - - protected ItemStack[] getInputs() { - final ItemStack[] input = new ItemStack[9]; - boolean hasValue = false; - - for (int x = 0; x < this.craftingSlots.length; x++) { - input[x] = this.craftingSlots[x].getStack(); - if (!input[x].isEmpty()) { - hasValue = true; - } - } - - if (hasValue) { - return input; - } - - return null; - } - - protected ItemStack[] getOutputs() { - if (this.isCraftingMode()) { - final ItemStack out = this.getAndUpdateOutput(); - - if (!out.isEmpty() && out.getCount() > 0) { - return new ItemStack[]{out}; - } - } else { - final List list = new ArrayList<>(3); - boolean hasValue = false; - - for (final OptionalSlotFake outputSlot : this.outputSlots) { - final ItemStack out = outputSlot.getStack(); - - if (!out.isEmpty() && out.getCount() > 0) { - list.add(out); - hasValue = true; - } - } - - if (hasValue) { - return list.toArray(new ItemStack[list.size()]); - } - } - - return null; - } - - boolean isPattern(final ItemStack output) { - if (output.isEmpty()) { - return false; - } - - final IDefinitions definitions = AEApi.instance().definitions(); - - boolean isPattern = definitions.items().encodedPattern().isSameAs(output); - isPattern |= definitions.materials().blankPattern().isSameAs(output); - - return isPattern; - } - - NBTBase createItemTag(final ItemStack i) { - final NBTTagCompound c = new NBTTagCompound(); - - if (!i.isEmpty()) { - i.writeToNBT(c); - if (i.getCount() > i.getMaxStackSize()) { - c.setInteger("stackSize", i.getCount()); - } - } - - return c; - } @Override public boolean isSlotEnabled(final int idx) { if (idx == 1) { - return Platform.isServer() ? !this.getPatternTerminal().isCraftingRecipe() : !this.isCraftingMode(); + return Platform.isServer() ? !this.getPart().isCraftingRecipe() : !this.isCraftingMode(); } else if (idx == 2) { - return Platform.isServer() ? this.getPatternTerminal().isCraftingRecipe() : this.isCraftingMode(); + return Platform.isServer() ? this.getPart().isCraftingRecipe() : this.isCraftingMode(); } else { return false; } @@ -546,7 +161,7 @@ public class ContainerPatternTerm extends ContainerMEMonitorable implements IAEA return; } - final IMEMonitor storage = this.getPatternTerminal() + final IMEMonitor storage = this.getPart() .getInventory(AEApi.instance().storage().getStorageChannel(IItemStorageChannel.class)); final IItemList all = storage.getStorageList(); @@ -588,107 +203,10 @@ public class ContainerPatternTerm extends ContainerMEMonitorable implements IAEA if (!failed.isEmpty()) { this.getCellInventory() .injectItems(AEItemStack.fromItemStack(failed), Actionable.MODULATE, - new MachineSource(this.getPatternTerminal())); + new MachineSource(this.getPart())); } } } } } - - @Override - public void detectAndSendChanges() { - super.detectAndSendChanges(); - if (Platform.isServer()) { - if (this.isCraftingMode() != this.getPatternTerminal().isCraftingRecipe()) { - this.setCraftingMode(this.getPatternTerminal().isCraftingRecipe()); - this.updateOrderOfOutputSlots(); - } - - this.substitute = this.patternTerminal.isSubstitution(); - } - } - - @Override - public void onUpdate(final String field, final Object oldValue, final Object newValue) { - super.onUpdate(field, oldValue, newValue); - - if (field.equals("craftingMode")) { - this.getAndUpdateOutput(); - this.updateOrderOfOutputSlots(); - } - } - - @Override - public void onSlotChange(final Slot s) { - if (s == this.patternSlotOUT && Platform.isServer()) { - for (final IContainerListener listener : this.listeners) { - for (final Slot slot : this.inventorySlots) { - if (slot instanceof OptionalSlotFake || slot instanceof SlotFakeCraftingMatrix) { - listener.sendSlotContents(this, slot.slotNumber, slot.getStack()); - } - } - if (listener instanceof EntityPlayerMP) { - ((EntityPlayerMP) listener).isChangingQuantityOnly = false; - } - } - this.detectAndSendChanges(); - } - - if (s == this.craftSlot && Platform.isClient()) { - this.getAndUpdateOutput(); - } - } - - public void clear() { - for (final Slot s : this.craftingSlots) { - s.putStack(ItemStack.EMPTY); - } - - for (final Slot s : this.outputSlots) { - s.putStack(ItemStack.EMPTY); - } - - this.detectAndSendChanges(); - this.getAndUpdateOutput(); - } - - @Override - public IItemHandler getInventoryByName(final String name) { - if (name.equals("player")) { - return new PlayerInvWrapper(this.getInventoryPlayer()); - } - return this.getPatternTerminal().getInventoryByName(name); - } - - @Override - public boolean useRealItems() { - return false; - } - - public void toggleSubstitute() { - this.substitute = !this.substitute; - - this.detectAndSendChanges(); - this.getAndUpdateOutput(); - } - - public boolean isCraftingMode() { - return this.craftingMode; - } - - private void setCraftingMode(final boolean craftingMode) { - this.craftingMode = craftingMode; - } - - public PartPatternTerminal getPatternTerminal() { - return this.patternTerminal; - } - - boolean isSubstitute() { - return this.substitute; - } - - public void setSubstitute(final boolean substitute) { - this.substitute = substitute; - } } diff --git a/src/main/java/appeng/core/sync/packets/PacketValueConfig.java b/src/main/java/appeng/core/sync/packets/PacketValueConfig.java index c75d5e4df..2f94a69c1 100644 --- a/src/main/java/appeng/core/sync/packets/PacketValueConfig.java +++ b/src/main/java/appeng/core/sync/packets/PacketValueConfig.java @@ -133,7 +133,7 @@ public class PacketValueConfig extends AppEngPacket { if (c instanceof ContainerPatternTerm) { final ContainerPatternTerm cpt = (ContainerPatternTerm) c; if (this.Name.equals("PatternTerminal.CraftMode")) { - cpt.getPatternTerminal().setCraftingRecipe(this.Value.equals("1")); + cpt.getPart().setCraftingRecipe(this.Value.equals("1")); } else if (this.Name.equals("PatternTerminal.Encode")) { if (this.Value.equals("2")) { cpt.encodeAndMoveToInventory(); @@ -157,7 +157,7 @@ public class PacketValueConfig extends AppEngPacket { } else if (this.Name.equals("PatternTerminal.MaximizeCount")) { cpt.maximizeCount(); } else if (this.Name.equals("PatternTerminal.Substitute")) { - cpt.getPatternTerminal().setSubstitution(this.Value.equals("1")); + cpt.getPart().setSubstitution(this.Value.equals("1")); } } else if (c instanceof ContainerExpandedProcessingPatternTerm) { final ContainerExpandedProcessingPatternTerm cept = (ContainerExpandedProcessingPatternTerm) c; diff --git a/src/main/java/appeng/helpers/PatternHelper.java b/src/main/java/appeng/helpers/PatternHelper.java index c45e18650..5cc258136 100644 --- a/src/main/java/appeng/helpers/PatternHelper.java +++ b/src/main/java/appeng/helpers/PatternHelper.java @@ -43,12 +43,12 @@ import java.util.*; public class PatternHelper implements ICraftingPatternDetails, Comparable { - private static final int CRAFTING_GRID_DIMENSION = 3; + public static final int CRAFTING_GRID_DIMENSION = 3; private static final int CRAFTING_INPUT_LIMIT = CRAFTING_GRID_DIMENSION * CRAFTING_GRID_DIMENSION; public static final int PROCESSING_INPUT_HEIGHT = 4; public static final int PROCESSING_INPUT_WIDTH = 4; public static final int PROCESSING_INPUT_LIMIT = PROCESSING_INPUT_HEIGHT * PROCESSING_INPUT_WIDTH; - private static final int CRAFTING_OUTPUT_LIMIT = 1; + public static final int CRAFTING_OUTPUT_LIMIT = 1; public static final int PROCESSING_OUTPUT_LIMIT = 6; private final ItemStack patternItem; diff --git a/src/main/java/appeng/parts/reporting/AbstractPartEncoder.java b/src/main/java/appeng/parts/reporting/AbstractPartEncoder.java new file mode 100644 index 000000000..b92698a2a --- /dev/null +++ b/src/main/java/appeng/parts/reporting/AbstractPartEncoder.java @@ -0,0 +1,152 @@ +package appeng.parts.reporting; + +import appeng.api.implementations.ICraftingPatternItem; +import appeng.api.networking.crafting.ICraftingPatternDetails; +import appeng.api.parts.IPartModel; +import appeng.api.storage.data.IAEItemStack; +import appeng.core.sync.GuiBridge; +import appeng.tile.inventory.AppEngInternalInventory; +import appeng.util.inv.InvOperation; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraftforge.items.IItemHandler; + +import javax.annotation.Nonnull; +import java.util.List; + +public abstract class AbstractPartEncoder extends AbstractPartTerminal { + + protected AppEngInternalInventory crafting; + protected AppEngInternalInventory output; + protected AppEngInternalInventory pattern; + + protected boolean craftingMode = true; + protected boolean substitute = false; + + public AbstractPartEncoder(ItemStack is) { + super(is); + } + + @Override + public void getDrops(final List drops, final boolean wrenched) { + for (final ItemStack is : this.pattern) { + if (!is.isEmpty()) { + drops.add(is); + } + } + } + + @Override + public void readFromNBT(final NBTTagCompound data) { + super.readFromNBT(data); + this.pattern.readFromNBT(data, "pattern"); + this.output.readFromNBT(data, "outputList"); + this.crafting.readFromNBT(data, "crafting"); + } + + @Override + public void writeToNBT(final NBTTagCompound data) { + super.writeToNBT(data); + this.pattern.writeToNBT(data, "pattern"); + this.output.writeToNBT(data, "outputList"); + this.crafting.writeToNBT(data, "crafting"); + } + + @Override + public void onChangeInventory(final IItemHandler inv, final int slot, final InvOperation mc, final ItemStack removedStack, final ItemStack newStack) { + if (inv == this.pattern && slot == 1) { + final ItemStack is = this.pattern.getStackInSlot(1); + if (!is.isEmpty() && is.getItem() instanceof ICraftingPatternItem) { + final ICraftingPatternItem pattern = (ICraftingPatternItem) is.getItem(); + final ICraftingPatternDetails details = pattern.getPatternForItem(is, this.getHost().getTile().getWorld()); + if (details != null) { + this.setCraftingRecipe(details.isCraftable()); + this.setSubstitution(details.canSubstitute()); + + for (int x = 0; x < this.crafting.getSlots() && x < details.getInputs().length; x++) { + final IAEItemStack item = details.getInputs()[x]; + this.crafting.setStackInSlot(x, item == null ? ItemStack.EMPTY : item.createItemStack()); + } + + for (int x = 0; x < this.output.getSlots() && x < details.getOutputs().length; x++) { + final IAEItemStack item = details.getOutputs()[x]; + this.output.setStackInSlot(x, item == null ? ItemStack.EMPTY : item.createItemStack()); + } + } + } + } else if (inv == this.crafting) { + this.fixCraftingRecipes(); + } + + this.getHost().markForSave(); + } + + private void fixCraftingRecipes() { + if (this.isCraftingRecipe()) { + for (int x = 0; x < this.crafting.getSlots(); x++) { + final ItemStack is = this.crafting.getStackInSlot(x); + if (!is.isEmpty()) { + is.setCount(1); + } + } + } + } + + public boolean isCraftingRecipe() { + return this.craftingMode; + } + + public void setCraftingRecipe(final boolean craftingMode) { + this.craftingMode = craftingMode; + this.fixCraftingRecipes(); + } + + public boolean isSubstitution() { + return this.substitute; + } + + public void setSubstitution(final boolean canSubstitute) { + this.substitute = canSubstitute; + } + + @Override + public IItemHandler getInventoryByName(final String name) { + if (name.equals("crafting")) { + return this.crafting; + } + + if (name.equals("output")) { + return this.output; + } + + if (name.equals("pattern")) { + return this.pattern; + } + + return super.getInventoryByName(name); + } + + @Override + public GuiBridge getGui(final EntityPlayer p) { + int x = (int) p.posX; + int y = (int) p.posY; + int z = (int) p.posZ; + if (this.getHost().getTile() != null) { + x = this.getTile().getPos().getX(); + y = this.getTile().getPos().getY(); + z = this.getTile().getPos().getZ(); + } + + if (getGuiBridge().hasPermissions(this.getHost().getTile(), x, y, z, this.getSide(), p)) { + return getGuiBridge(); + } + return GuiBridge.GUI_ME; + } + + abstract public GuiBridge getGuiBridge(); + + @Nonnull + @Override + abstract public IPartModel getStaticModels(); +} diff --git a/src/main/java/appeng/parts/reporting/PartExpandedProcessingPatternTerminal.java b/src/main/java/appeng/parts/reporting/PartExpandedProcessingPatternTerminal.java index f0a31b531..087586ed6 100644 --- a/src/main/java/appeng/parts/reporting/PartExpandedProcessingPatternTerminal.java +++ b/src/main/java/appeng/parts/reporting/PartExpandedProcessingPatternTerminal.java @@ -1,29 +1,22 @@ package appeng.parts.reporting; -import appeng.api.implementations.ICraftingPatternItem; -import appeng.api.networking.crafting.ICraftingPatternDetails; import appeng.api.parts.IPartModel; -import appeng.api.storage.data.IAEItemStack; import appeng.core.AppEng; import appeng.core.sync.GuiBridge; import appeng.helpers.Reflected; import appeng.items.parts.PartModels; import appeng.parts.PartModel; import appeng.tile.inventory.AppEngInternalInventory; -import appeng.util.inv.InvOperation; -import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.ResourceLocation; -import net.minecraftforge.items.IItemHandler; -import java.util.List; +import javax.annotation.Nonnull; import static appeng.helpers.PatternHelper.PROCESSING_INPUT_LIMIT; import static appeng.helpers.PatternHelper.PROCESSING_OUTPUT_LIMIT; -public class PartExpandedProcessingPatternTerminal extends AbstractPartTerminal { +public class PartExpandedProcessingPatternTerminal extends AbstractPartEncoder { @PartModels public static final ResourceLocation MODEL_OFF = new ResourceLocation(AppEng.MOD_ID, "part/expanded_processing_pattern_terminal_off"); @PartModels @@ -33,96 +26,20 @@ public class PartExpandedProcessingPatternTerminal extends AbstractPartTerminal public static final IPartModel MODELS_ON = new PartModel(MODEL_BASE, MODEL_ON, MODEL_STATUS_ON); public static final IPartModel MODELS_HAS_CHANNEL = new PartModel(MODEL_BASE, MODEL_ON, MODEL_STATUS_HAS_CHANNEL); - private final AppEngInternalInventory crafting = new AppEngInternalInventory(this, PROCESSING_INPUT_LIMIT); - - private final AppEngInternalInventory output = new AppEngInternalInventory(this, PROCESSING_OUTPUT_LIMIT); - private final AppEngInternalInventory pattern = new AppEngInternalInventory(this, 2); - @Reflected public PartExpandedProcessingPatternTerminal(final ItemStack is) { super(is); + this.crafting = new AppEngInternalInventory(this, PROCESSING_INPUT_LIMIT); + this.output = new AppEngInternalInventory(this, PROCESSING_OUTPUT_LIMIT); + this.pattern = new AppEngInternalInventory(this, 2); } @Override - public void getDrops(final List drops, final boolean wrenched) { - for (final ItemStack is : this.pattern) { - if (!is.isEmpty()) { - drops.add(is); - } - } - } - - @Override - public void readFromNBT(NBTTagCompound data) { - super.readFromNBT(data); - this.crafting.readFromNBT(data, "processingGrid"); - this.pattern.readFromNBT(data, "pattern"); - this.output.readFromNBT(data, "outputList"); - } - - @Override - public void writeToNBT(NBTTagCompound data) { - super.writeToNBT(data); - this.crafting.writeToNBT(data, "processingGrid"); - this.pattern.writeToNBT(data, "pattern"); - this.output.writeToNBT(data, "outputList"); - } - - @Override - public GuiBridge getGui(final EntityPlayer p) { - int x = (int) p.posX; - int y = (int) p.posY; - int z = (int) p.posZ; - if (this.getHost().getTile() != null) { - x = this.getTile().getPos().getX(); - y = this.getTile().getPos().getY(); - z = this.getTile().getPos().getZ(); - } - - if (GuiBridge.GUI_EXPANDED_PROCESSING_PATTERN_TERMINAL.hasPermissions(this.getHost().getTile(), x, y, z, this.getSide(), p)) { - return GuiBridge.GUI_EXPANDED_PROCESSING_PATTERN_TERMINAL; - } - return GuiBridge.GUI_ME; - } - - @Override - public void onChangeInventory(final IItemHandler inv, final int slot, final InvOperation mc, final ItemStack removedStack, final ItemStack newStack) { - if (inv == this.pattern && slot == 1) { - final ItemStack is = this.pattern.getStackInSlot(1); - if (!is.isEmpty() && is.getItem() instanceof ICraftingPatternItem) { - final ICraftingPatternItem pattern = (ICraftingPatternItem) is.getItem(); - final ICraftingPatternDetails details = pattern.getPatternForItem(is, this.getHost().getTile().getWorld()); - if (details != null) { - for (int x = 0; x < this.crafting.getSlots() && x < details.getInputs().length; x++) { - final IAEItemStack item = details.getInputs()[x]; - this.crafting.setStackInSlot(x, item == null ? ItemStack.EMPTY : item.createItemStack()); - } - - for (int x = 0; x < this.output.getSlots() && x < details.getOutputs().length; x++) { - final IAEItemStack item = details.getOutputs()[x]; - this.output.setStackInSlot(x, item == null ? ItemStack.EMPTY : item.createItemStack()); - } - } - } - } - - this.getHost().markForSave(); - } - - @Override - public IItemHandler getInventoryByName(final String name) { - if (name.equals("crafting")) { - return this.crafting; - } - if (name.equals("output")) { - return this.output; - } - if (name.equals("pattern")) { - return this.pattern; - } - return super.getInventoryByName(name); + public GuiBridge getGuiBridge() { + return GuiBridge.GUI_EXPANDED_PROCESSING_PATTERN_TERMINAL; } + @Nonnull @Override public IPartModel getStaticModels() { return this.selectModel(MODELS_OFF, MODELS_ON, MODELS_HAS_CHANNEL); diff --git a/src/main/java/appeng/parts/reporting/PartPatternTerminal.java b/src/main/java/appeng/parts/reporting/PartPatternTerminal.java index a7f27dc0f..3e90a17ef 100644 --- a/src/main/java/appeng/parts/reporting/PartPatternTerminal.java +++ b/src/main/java/appeng/parts/reporting/PartPatternTerminal.java @@ -19,27 +19,24 @@ package appeng.parts.reporting; -import appeng.api.implementations.ICraftingPatternItem; -import appeng.api.networking.crafting.ICraftingPatternDetails; import appeng.api.parts.IPartModel; -import appeng.api.storage.data.IAEItemStack; import appeng.core.AppEng; import appeng.core.sync.GuiBridge; import appeng.helpers.Reflected; import appeng.items.parts.PartModels; import appeng.parts.PartModel; import appeng.tile.inventory.AppEngInternalInventory; -import appeng.util.inv.InvOperation; -import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.ResourceLocation; -import net.minecraftforge.items.IItemHandler; -import java.util.List; +import javax.annotation.Nonnull; + +import static appeng.helpers.PatternHelper.CRAFTING_GRID_DIMENSION; +import static appeng.helpers.PatternHelper.CRAFTING_OUTPUT_LIMIT; -public class PartPatternTerminal extends AbstractPartTerminal { +public class PartPatternTerminal extends AbstractPartEncoder { @PartModels public static final ResourceLocation MODEL_OFF = new ResourceLocation(AppEng.MOD_ID, "part/pattern_terminal_off"); @@ -50,25 +47,12 @@ public class PartPatternTerminal extends AbstractPartTerminal { public static final IPartModel MODELS_ON = new PartModel(MODEL_BASE, MODEL_ON, MODEL_STATUS_ON); public static final IPartModel MODELS_HAS_CHANNEL = new PartModel(MODEL_BASE, MODEL_ON, MODEL_STATUS_HAS_CHANNEL); - private final AppEngInternalInventory crafting = new AppEngInternalInventory(this, 9); - private final AppEngInternalInventory output = new AppEngInternalInventory(this, 3); - private final AppEngInternalInventory pattern = new AppEngInternalInventory(this, 2); - - private boolean craftingMode = true; - private boolean substitute = false; - @Reflected public PartPatternTerminal(final ItemStack is) { super(is); - } - - @Override - public void getDrops(final List drops, final boolean wrenched) { - for (final ItemStack is : this.pattern) { - if (!is.isEmpty()) { - drops.add(is); - } - } + this.crafting = new AppEngInternalInventory(this, CRAFTING_GRID_DIMENSION * CRAFTING_GRID_DIMENSION); + this.output = new AppEngInternalInventory(this, CRAFTING_OUTPUT_LIMIT); + this.pattern = new AppEngInternalInventory(this, 2); } @Override @@ -76,9 +60,6 @@ public class PartPatternTerminal extends AbstractPartTerminal { super.readFromNBT(data); this.setCraftingRecipe(data.getBoolean("craftingMode")); this.setSubstitution(data.getBoolean("substitute")); - this.pattern.readFromNBT(data, "pattern"); - this.output.readFromNBT(data, "outputList"); - this.crafting.readFromNBT(data, "craftingGrid"); } @Override @@ -86,102 +67,14 @@ public class PartPatternTerminal extends AbstractPartTerminal { super.writeToNBT(data); data.setBoolean("craftingMode", this.craftingMode); data.setBoolean("substitute", this.substitute); - this.pattern.writeToNBT(data, "pattern"); - this.output.writeToNBT(data, "outputList"); - this.crafting.writeToNBT(data, "craftingGrid"); } @Override - public GuiBridge getGui(final EntityPlayer p) { - int x = (int) p.posX; - int y = (int) p.posY; - int z = (int) p.posZ; - if (this.getHost().getTile() != null) { - x = this.getTile().getPos().getX(); - y = this.getTile().getPos().getY(); - z = this.getTile().getPos().getZ(); - } - - if (GuiBridge.GUI_PATTERN_TERMINAL.hasPermissions(this.getHost().getTile(), x, y, z, this.getSide(), p)) { - return GuiBridge.GUI_PATTERN_TERMINAL; - } - return GuiBridge.GUI_ME; - } - - @Override - public void onChangeInventory(final IItemHandler inv, final int slot, final InvOperation mc, final ItemStack removedStack, final ItemStack newStack) { - if (inv == this.pattern && slot == 1) { - final ItemStack is = this.pattern.getStackInSlot(1); - if (!is.isEmpty() && is.getItem() instanceof ICraftingPatternItem) { - final ICraftingPatternItem pattern = (ICraftingPatternItem) is.getItem(); - final ICraftingPatternDetails details = pattern.getPatternForItem(is, this.getHost().getTile().getWorld()); - if (details != null) { - this.setCraftingRecipe(details.isCraftable()); - this.setSubstitution(details.canSubstitute()); - - for (int x = 0; x < this.crafting.getSlots() && x < details.getInputs().length; x++) { - final IAEItemStack item = details.getInputs()[x]; - this.crafting.setStackInSlot(x, item == null ? ItemStack.EMPTY : item.createItemStack()); - } - - for (int x = 0; x < this.output.getSlots() && x < details.getOutputs().length; x++) { - final IAEItemStack item = details.getOutputs()[x]; - this.output.setStackInSlot(x, item == null ? ItemStack.EMPTY : item.createItemStack()); - } - } - } - } else if (inv == this.crafting) { - this.fixCraftingRecipes(); - } - - this.getHost().markForSave(); - } - - private void fixCraftingRecipes() { - if (this.craftingMode) { - for (int x = 0; x < this.crafting.getSlots(); x++) { - final ItemStack is = this.crafting.getStackInSlot(x); - if (!is.isEmpty()) { - is.setCount(1); - } - } - } - } - - public boolean isCraftingRecipe() { - return this.craftingMode; - } - - public void setCraftingRecipe(final boolean craftingMode) { - this.craftingMode = craftingMode; - this.fixCraftingRecipes(); - } - - public boolean isSubstitution() { - return this.substitute; - } - - public void setSubstitution(final boolean canSubstitute) { - this.substitute = canSubstitute; - } - - @Override - public IItemHandler getInventoryByName(final String name) { - if (name.equals("crafting")) { - return this.crafting; - } - - if (name.equals("output")) { - return this.output; - } - - if (name.equals("pattern")) { - return this.pattern; - } - - return super.getInventoryByName(name); + public GuiBridge getGuiBridge() { + return GuiBridge.GUI_PATTERN_TERMINAL; } + @Nonnull @Override public IPartModel getStaticModels() { return this.selectModel(MODELS_OFF, MODELS_ON, MODELS_HAS_CHANNEL);