Posted April 12, 20169 yr Hi, I'm a novice modder (and coder), and I need your help...!!! I have a working furnace with multiple input slots (1 fuel and 1 output). All basic functionality is working properly. I'd like to implement some additional behavior, but for the life of me I can't figure out how. I did an initial search and was surprised to turn up nothing about this. All the info I found was to set behaviors for the entire entity, not individual slots. [*]I'd like to restrict maxstacksize for input slots to 1, and leave maxstacksize for fuel and output slots at default (64). [*]I'd also like to be able to restrict valid ItemStack for input slots based on oredict tags (ore* and dust*) PART 1 I've been able to get correct shift-click behavior with custom Slot class, custom mergeItemStack(), and transferStackInSlot() in the Container class. But this yields odd left-click behavior if implemented (left clicking an occupied input slot while holding a matching ItemStack withdraws the stack from the slot and increases the held stack, even past stacksize limits) so I've reverted it. I also can't get automation (hoppers) to change behavior (round robin) or respect the custom inputSlot's stacksize limits, without setting the TileEntity'sgetInventoryStackLimit() to return 1 instead of 64. This of course causes problems for the fuel and output slots. PART 2 Honestly have no clue where to begin. I looked through the oredict class, but couldn't find any way to make a given ItemStack return its oredict tag. CODE Tile Entity: public class TileCupolaFurnace extends TileEntity implements ITickable, IInventory { // index 0-3 = input, 4 = fuel, 5 = output private ItemStack[] furnaceItemStacks = new ItemStack[6]; private int burnTimeRemaining; // number of ticks remaining on current piece of fuel private int fuelBurnTime; // initial fuel value of the currently burning fuel private int[] itemCookTime = new int[4]; // current cook time for input slots private static final int COOK_TIME_FOR_COMPLETION = 400; // the number of ticks to smelt @Override public int getSizeInventory() { return furnaceItemStacks.length; } @Override public ItemStack getStackInSlot(int index) { return furnaceItemStacks[index]; } @Override public ItemStack decrStackSize(int index, int count) { ItemStack stackInSlot = furnaceItemStacks[index]; ItemStack stackRemoved; if (stackInSlot == null) return null; if (stackInSlot.stackSize <= count) { stackRemoved = stackInSlot; setInventorySlotContents(index, null); } else { stackRemoved = stackInSlot.splitStack(count); if (stackInSlot.stackSize == 0) setInventorySlotContents(index, null); } markDirty(); return stackRemoved; } @Override public void setInventorySlotContents(int index, ItemStack stack) { furnaceItemStacks[index] = stack; if (stack != null && stack.stackSize > getInventoryStackLimit()) stack.stackSize = getInventoryStackLimit(); markDirty(); } @Override public int getInventoryStackLimit() { return 64; } @Override public boolean isUseableByPlayer(EntityPlayer player) { if (this.worldObj.getTileEntity(this.pos) != this) return false; final double X_CENTRE_OFFSET = 0.5; final double Y_CENTRE_OFFSET = 0.5; final double Z_CENTRE_OFFSET = 0.5; final double MAXIMUM_DISTANCE_SQ = 8.0 * 8.0; return player.getDistanceSq(pos.getX() + X_CENTRE_OFFSET, pos.getY() + Y_CENTRE_OFFSET, pos.getZ() + Z_CENTRE_OFFSET) < MAXIMUM_DISTANCE_SQ; } @Override public boolean isItemValidForSlot(int index, ItemStack stack) { return index != 5 && (index != 4 || isItemFuel(stack)); } @Override public void update() { for (int i = 0; i < 4; i++) { ItemStack input = furnaceItemStacks[i]; if (input != null && canSmelt(i)) { startBurnFuel(); cookItem(i); } else { if (input == null) itemCookTime[i] = 0; if (itemCookTime[i] > 0 && !isBurning()) itemCookTime[i]--; } } if (isBurning()) burnTimeRemaining--; } private boolean isBurning() { return this.burnTimeRemaining > 0; } private void startBurnFuel() { if (!this.isBurning()) { boolean inventoryChanged = false; if (this.furnaceItemStacks[4] != null && isItemFuel(this.furnaceItemStacks[4]) && this.furnaceItemStacks[4].stackSize > 0) { this.burnTimeRemaining = this.fuelBurnTime = getFuelBurnTime(this.furnaceItemStacks[4]); this.furnaceItemStacks[4].stackSize--; inventoryChanged = true; if (this.furnaceItemStacks[4].stackSize == 0) this.furnaceItemStacks[4] = null; } if (inventoryChanged) markDirty(); } } private boolean canSmelt(int index) { ItemStack input = this.furnaceItemStacks[index]; ItemStack output = FurnaceRecipes.instance().getSmeltingResult(input); if (output == null) return false; if (this.furnaceItemStacks[5] == null) return true; if (!this.furnaceItemStacks[5].isItemEqual(output)) return false; int result = this.furnaceItemStacks[5].stackSize + output.stackSize; return result <= getInventoryStackLimit() && result <= this.furnaceItemStacks[5].getMaxStackSize(); } private void cookItem(int index) { if (isBurning()) itemCookTime[index]++; if (itemCookTime[index] == COOK_TIME_FOR_COMPLETION) { smeltItem(index); itemCookTime[index] = 0; } } private void smeltItem(int index) { ItemStack smeltingResult = FurnaceRecipes.instance().getSmeltingResult(this.furnaceItemStacks[index]); if (this.furnaceItemStacks[5] == null) this.furnaceItemStacks[5] = smeltingResult.copy(); else if (this.furnaceItemStacks[5].getItem() == smeltingResult.getItem()) this.furnaceItemStacks[5].stackSize += smeltingResult.stackSize; this.furnaceItemStacks[index].stackSize--; if (furnaceItemStacks[index].stackSize <= 0) furnaceItemStacks[index] = null; markDirty(); } public boolean isItemFuel(ItemStack stack) { return stack != null && getFuelBurnTime(stack) > 0; } private int getFuelBurnTime(ItemStack stack) { if (stack == null) { return 0; } else { Item item = stack.getItem(); if (item instanceof ItemBlock && Block.getBlockFromItem(item) != Blocks.air) { Block block = Block.getBlockFromItem(item); if (block == Blocks.coal_block) return 16000; else return 0; } if (item == Items.coal) return 1600; } return 0; } public int getBurnRemainingSeconds() { if (burnTimeRemaining <= 0) return 0; return burnTimeRemaining / 20; } public double getBurnRemainingRatio() { if (burnTimeRemaining <= 0) return 0; double ratio = (double) burnTimeRemaining / (double) fuelBurnTime; return MathHelper.clamp_double(ratio, 0.0, 1.0); } public double getCookProgressRatio(int index) { double ratio = (double) itemCookTime[index] / (double) COOK_TIME_FOR_COMPLETION; return MathHelper.clamp_double(ratio, 0.0, 1.0); } @Override public void writeToNBT(NBTTagCompound compound) { super.writeToNBT(compound); NBTTagList tagList = new NBTTagList(); for (int i = 0; i < this.furnaceItemStacks.length; i++) { if (this.furnaceItemStacks[i] != null) { NBTTagCompound stackTag = new NBTTagCompound(); stackTag.setByte("Slot", (byte) i); this.furnaceItemStacks[i].writeToNBT(stackTag); tagList.appendTag(stackTag); } } compound.setTag("Items", tagList); compound.setInteger("burnTimeRemaining", this.burnTimeRemaining); compound.setInteger("fuelBurnTime", this.fuelBurnTime); compound.setTag("itemCookTime", new NBTTagIntArray(this.itemCookTime)); } @Override public void readFromNBT(NBTTagCompound compound) { super.readFromNBT(compound); final byte NBT_TYPE = 10; NBTTagList tagList = compound.getTagList("Items", NBT_TYPE); for (int i = 0; i < tagList.tagCount(); i++) { NBTTagCompound stackTag = tagList.getCompoundTagAt(i); int slot = stackTag.getByte("Slot") & 255; this.furnaceItemStacks[slot] = ItemStack.loadItemStackFromNBT(stackTag); } this.burnTimeRemaining = compound.getInteger("burnTimeRemaining"); this.fuelBurnTime = compound.getInteger("fuelBurnTime"); this.itemCookTime = Arrays.copyOf(compound.getIntArray("itemCookTime"), 4); } @Override public void clear() { Arrays.fill(furnaceItemStacks, null); } @Override public String getName() { return "container.cupola_furnace.name"; } @Override public boolean hasCustomName() { return false; } @Override public IChatComponent getDisplayName() { return this.hasCustomName() ? new ChatComponentText(this.getName()) : new ChatComponentTranslation(this.getName()); } @Override public Packet getDescriptionPacket() { NBTTagCompound nbtTagCompound = new NBTTagCompound(); writeToNBT(nbtTagCompound); int metadata = getBlockMetadata(); return new S35PacketUpdateTileEntity(this.pos, metadata, nbtTagCompound); } @Override public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt) { readFromNBT(pkt.getNbtCompound()); } @Override public ItemStack removeStackFromSlot(int index) { ItemStack stackremoved = furnaceItemStacks[index]; if (stackremoved != null) setInventorySlotContents(index, null); return stackremoved; } @Override public void openInventory(EntityPlayer player) { } @Override public void closeInventory(EntityPlayer player) { } @Override public int getField(int id) { if (id == 0) return this.burnTimeRemaining; if (id == 1) return this.fuelBurnTime; if (id > 1 && id <= 5) return itemCookTime[id - 2]; System.err.println("Invalid field ID in TileCupolaFurnace.getField:" + id); return 0; } @Override public void setField(int id, int value) { if (id == 0) this.burnTimeRemaining = value; else if (id == 1) this.fuelBurnTime = value; else if (id > 1 && id <= 5) this.itemCookTime[id - 2] = value; else System.err.println("Invalid field ID in TileCupolaFurnace.setField:" + id); } @Override public int getFieldCount() { return 6; } } Container: public class ContainerCupolaFurnace extends Container { /** * SLOTS INDEX * <p> * 0-8 = hotbar slots 0-8 * 9-35 = player inventory slots 9-35 * <p> * 36-41 = furnace slots * 36-39 = input slots 0-3 * 40 = fuel slot 4 * 41 = output slot 5 */ private TileCupolaFurnace tileCupolaFurnace; public ContainerCupolaFurnace(InventoryPlayer invPlayer, TileCupolaFurnace tileCupolaFurnace) { this.tileCupolaFurnace = tileCupolaFurnace; // hotbar slots for (int x = 0; x < 9; x++) { addSlotToContainer(new Slot(invPlayer, x, 8 + 18 * x, 134)); } // player inv slots for (int y = 0; y < 3; ++y) { for (int x = 0; x < 9; ++x) { addSlotToContainer(new Slot(invPlayer, x + y * 9 + 9, 8 + x * 18, 76 + y * 18)); } } // furnace input slots for (int y = 0; y < 2; y++) { for (int x = 0; x < 2; x++) { addSlotToContainer(new inputSlot(tileCupolaFurnace, x + y * 2, 24 + x * 21, 24 + y * 18)); } } // furnace fuel slot addSlotToContainer(new Slot(tileCupolaFurnace, 4, 80, 51)); //furnace output slot addSlotToContainer(new Slot(tileCupolaFurnace, 5, 125, 27)); } @Override public boolean canInteractWith(EntityPlayer playerIn) { return this.tileCupolaFurnace.isUseableByPlayer(playerIn); } @Override public ItemStack transferStackInSlot(EntityPlayer playerIn, int index) { Slot sourceSlot = this.inventorySlots.get(index); if (sourceSlot == null || !sourceSlot.getHasStack()) return null; ItemStack sourceStack = sourceSlot.getStack(); ItemStack copyOfSourceStack = sourceStack.copy(); if (index >= 0 && index < 36) { if (tileCupolaFurnace.isItemFuel(sourceStack)) { if (!mergeItemStack(sourceStack, 40, 41, false)) { return null; } } else if (!mergeItemStack(sourceStack, 36, 40, false)) { return null; } } else if (index >= 36 && index < 42) { if (!mergeItemStack(sourceStack, 0, 36, false)) { return null; } } else { System.err.print("Invalid slotIndex:" + index); return null; } if (sourceStack.stackSize == 0) { sourceSlot.putStack(null); } else { sourceSlot.onSlotChanged(); } sourceSlot.onPickupFromSlot(playerIn, sourceStack); return copyOfSourceStack; } // private boolean mergeInputStack(ItemStack stack, int startIndex, int endIndex, boolean useEndIndex) { // boolean success = false; // int index = startIndex; // // if (useEndIndex) // index = endIndex - 1; // // Slot slot; // ItemStack stackInSlot; // // if (stack.isStackable()) { // while (stack.stackSize > 0 && (!useEndIndex && index < endIndex || useEndIndex && index >= startIndex)) { // slot = this.inventorySlots.get(index); // stackInSlot = slot.getStack(); // // if (stackInSlot != null && stackInSlot.getItem() == stack.getItem() && (!stack.getHasSubtypes() || stack.getMetadata() == stackInSlot.getMetadata()) && ItemStack.areItemStackTagsEqual(stack, stackInSlot)) { // int l = stackInSlot.stackSize + stack.stackSize; // int maxsize = Math.min(stack.getMaxStackSize(), slot.getItemStackLimit(stack)); // // if (l <= maxsize) { // stack.stackSize = 0; // stackInSlot.stackSize = l; // slot.onSlotChanged(); // success = true; // } else if (stackInSlot.stackSize < maxsize) { // stack.stackSize -= stack.getMaxStackSize() - stackInSlot.stackSize; // stackInSlot.stackSize = stack.getMaxStackSize(); // slot.onSlotChanged(); // success = true; // } // } // // if (useEndIndex) { // --index; // } else { // ++index; // } // } // } // // if (stack.stackSize > 0) { // if (useEndIndex) { // index = endIndex - 1; // } else { // index = startIndex; // } // // while (!useEndIndex && index < endIndex || useEndIndex && index >= startIndex && stack.stackSize > 0) { // slot = this.inventorySlots.get(index); // stackInSlot = slot.getStack(); // // // Forge: Make sure to respect isItemValid in the slot. // if (stackInSlot == null && slot.isItemValid(stack)) { // if (stack.stackSize < slot.getItemStackLimit(stack)) { // slot.putStack(stack.copy()); // stack.stackSize = 0; // success = true; // break; // } else { // ItemStack newstack = stack.copy(); // newstack.stackSize = slot.getItemStackLimit(stack); // slot.putStack(newstack); // stack.stackSize -= slot.getItemStackLimit(stack); // success = true; // } // } // // if (useEndIndex) { // --index; // } else { // ++index; // } // } // } // // return success; // } @Override public void onContainerClosed(EntityPlayer playerIn) { super.onContainerClosed(playerIn); this.tileCupolaFurnace.closeInventory(playerIn); } private int[] cachedFields; @Override public void detectAndSendChanges() { super.detectAndSendChanges(); boolean allFieldsHaveChanged = false; boolean fieldHasChanged[] = new boolean[tileCupolaFurnace.getFieldCount()]; if (cachedFields == null) { cachedFields = new int[tileCupolaFurnace.getFieldCount()]; allFieldsHaveChanged = true; } for (int i = 0; i < cachedFields.length; ++i) { if (allFieldsHaveChanged || cachedFields[i] != tileCupolaFurnace.getField(i)) { cachedFields[i] = tileCupolaFurnace.getField(i); fieldHasChanged[i] = true; } } for (ICrafting icrafting : this.crafters) { for (int fieldID = 0; fieldID < tileCupolaFurnace.getFieldCount(); ++fieldID) { if (fieldHasChanged[fieldID]) { icrafting.sendProgressBarUpdate(this, fieldID, cachedFields[fieldID]); } } } } private class inputSlot extends Slot { inputSlot(IInventory inventoryIn, int index, int xPosition, int yPosition) { super(inventoryIn, index, xPosition, yPosition); } @Override public int getSlotStackLimit() { return 64; } } @SideOnly(Side.CLIENT) @Override public void updateProgressBar(int id, int data) { tileCupolaFurnace.setField(id, data); } } Thanks for any help!
April 12, 20169 yr Use your custom slot for the slot that should have a max stack size of 1 and return 1 for the Slot#getSlotStackLimit method. However, the vanilla #mergeStackInSlot implementations DO NOT check the slot's stack limit (at least as of 1.8 - maybe it has changed), and also has trouble handling slot limits of 1. Here is an implementation I came up with for #mergeStackInSlot - there are others out there as well that do the same thing. As for the shift-clicking moving items into the wrong slots, that has to do with how you choose to implement #transferStackInSlot - you can find some information about that in this tutorial, though I recommend you skip to the final spoiler section first, as that is the most recent. http://i.imgur.com/NdrFdld.png[/img]
April 12, 20169 yr Author !!! Hey, it's you! Your tutorials (and others') are the reason I've gotten this far. Thanks for putting those together! I've re-implemented custom Slot classes and MergeStackInSlot() to handle the Container interactions. This time I've also implemented more comprehensive logic in TileEntity.isItemValidForSlot() to handle automated insertion, to catch itemstack.stackSize > 1 for those slots. Hopefully that'll cover all of it. The errant clicking behavior actually happens on regular click and not shift click, as described in the OP: (If for whatever reason the itemstack in the slot has stacksize > 1) left clicking an occupied input slot while holding a matching ItemStack withdraws the stack from the slot and increases the held stack, even past stacksize limits, so I've reverted it. Hopefully now it's a moot point, unless there's a way to get an itemstack > 1 into a slot that I'm missing. I still want to fix that behavior, just in case. Thanks for the help, and again for the tutorials.
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.