gmod622 Posted November 15, 2016 Posted November 15, 2016 Hey all! So while making a crate, that displays a 3x3 grid, I get this error. I already tried messing with it and still haven't found a fix, here is the class: package com.lambda.PlentifulMisc.block.container; import com.lambda.PlentifulMisc.block.tileentity.TileEntityCrate; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.Container; import net.minecraft.inventory.Slot; import net.minecraft.item.ItemStack; public class ContainerCrate extends Container { // Stores a reference to the tile entity instance for later use private TileEntityCrate tileEntityInventoryBasic; private final int HOTBAR_SLOT_COUNT = 9; private final int PLAYER_INVENTORY_ROW_COUNT = 3; private final int PLAYER_INVENTORY_COLUMN_COUNT = 9; private final int PLAYER_INVENTORY_SLOT_COUNT = PLAYER_INVENTORY_COLUMN_COUNT * PLAYER_INVENTORY_ROW_COUNT; private final int VANILLA_SLOT_COUNT = HOTBAR_SLOT_COUNT + PLAYER_INVENTORY_SLOT_COUNT; private final int VANILLA_FIRST_SLOT_INDEX = 0; private final int TE_INVENTORY_FIRST_SLOT_INDEX = VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT; private final int TE_INVENTORY_SLOT_COUNT = 9; private final int TE_INVENTORY_SLOT_COUNT_ROW = 3; private final int TE_INVENTORY_SLOT_COUNT_COLUMN = 3; public ContainerCrate(InventoryPlayer invPlayer, TileEntityCrate tileEntityInventoryBasic) { this.tileEntityInventoryBasic = tileEntityInventoryBasic; final int SLOT_X_SPACING = 18; final int SLOT_Y_SPACING = 18; final int HOTBAR_XPOS = 48; final int HOTBAR_YPOS = 197; for (int x = 0; x < HOTBAR_SLOT_COUNT; x++) { int slotNumber = x; addSlotToContainer(new Slot(invPlayer, slotNumber, HOTBAR_XPOS + SLOT_X_SPACING * x, HOTBAR_YPOS)); } final int PLAYER_INVENTORY_XPOS = 48; final int PLAYER_INVENTORY_YPOS = 139; for (int y = 0; y < PLAYER_INVENTORY_ROW_COUNT; y++) { for (int x = 0; x < PLAYER_INVENTORY_COLUMN_COUNT; x++) { int slotNumber = HOTBAR_SLOT_COUNT + y * PLAYER_INVENTORY_COLUMN_COUNT + x; int xpos = PLAYER_INVENTORY_XPOS + x * SLOT_X_SPACING; int ypos = PLAYER_INVENTORY_YPOS + y * SLOT_Y_SPACING; addSlotToContainer(new Slot(invPlayer, slotNumber, xpos, ypos)); } } if (TE_INVENTORY_SLOT_COUNT != tileEntityInventoryBasic.getSizeInventory()) { System.err.println("Mismatched slot count in ContainerBasic(" + TE_INVENTORY_SLOT_COUNT + ") and TileInventory (" + tileEntityInventoryBasic.getSizeInventory()+")"); } final int TILE_INVENTORY_XPOS = 102; final int TILE_INVENTORY_YPOS = 108; for (int y = 0; y < TE_INVENTORY_SLOT_COUNT_ROW; y++) { for (int x = 0; x < TE_INVENTORY_SLOT_COUNT_COLUMN; x++) { int slotNumber = TE_INVENTORY_SLOT_COUNT + y * TE_INVENTORY_SLOT_COUNT_COLUMN + x; int xpos = TILE_INVENTORY_XPOS + x * SLOT_X_SPACING; int ypos = TILE_INVENTORY_YPOS + y * SLOT_Y_SPACING; addSlotToContainer(new Slot(tileEntityInventoryBasic, slotNumber, TILE_INVENTORY_XPOS + SLOT_X_SPACING * x, TILE_INVENTORY_YPOS)); } } } // Vanilla calls this method every tick to make sure the player is still able to access the inventory, and if not closes the gui @Override public boolean canInteractWith(EntityPlayer player) { return tileEntityInventoryBasic.isUseableByPlayer(player); } // This is where you specify what happens when a player shift clicks a slot in the gui // (when you shift click a slot in the TileEntity Inventory, it moves it to the first available position in the hotbar and/or // player inventory. When you you shift-click a hotbar or player inventory item, it moves it to the first available // position in the TileEntity inventory) // At the very least you must override this and return null or the game will crash when the player shift clicks a slot // returns null if the source slot is empty, or if none of the the source slot items could be moved // otherwise, returns a copy of the source stack @Override public ItemStack transferStackInSlot(EntityPlayer player, int sourceSlotIndex) { Slot sourceSlot = (Slot)inventorySlots.get(sourceSlotIndex); if (sourceSlot == null || !sourceSlot.getHasStack()) return null; ItemStack sourceStack = sourceSlot.getStack(); ItemStack copyOfSourceStack = sourceStack.copy(); // Check if the slot clicked is one of the vanilla container slots if (sourceSlotIndex >= VANILLA_FIRST_SLOT_INDEX && sourceSlotIndex < VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT) { // This is a vanilla container slot so merge the stack into the tile inventory if (!mergeItemStack(sourceStack, TE_INVENTORY_FIRST_SLOT_INDEX, TE_INVENTORY_FIRST_SLOT_INDEX + TE_INVENTORY_SLOT_COUNT, false)){ return null; } } else if (sourceSlotIndex >= TE_INVENTORY_FIRST_SLOT_INDEX && sourceSlotIndex < TE_INVENTORY_FIRST_SLOT_INDEX + TE_INVENTORY_SLOT_COUNT) { // This is a TE slot so merge the stack into the players inventory if (!mergeItemStack(sourceStack, VANILLA_FIRST_SLOT_INDEX, VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT, false)) { return null; } } else { System.err.print("Invalid slotIndex:" + sourceSlotIndex); return null; } // If stack size == 0 (the entire stack was moved) set slot contents to null if (sourceStack.stackSize == 0) { sourceSlot.putStack(null); } else { sourceSlot.onSlotChanged(); } sourceSlot.onPickupFromSlot(player, sourceStack); return copyOfSourceStack; } // pass the close container message to the tileEntityInventory (not strictly needed for this example) // see ContainerChest and TileEntityChest @Override public void onContainerClosed(EntityPlayer playerIn) { super.onContainerClosed(playerIn); this.tileEntityInventoryBasic.closeInventory(playerIn); } } Here is where its crashing: for (int y = 0; y < TE_INVENTORY_SLOT_COUNT_ROW; y++) { for (int x = 0; x < TE_INVENTORY_SLOT_COUNT_COLUMN; x++) { int slotNumber = TE_INVENTORY_SLOT_COUNT + y * TE_INVENTORY_SLOT_COUNT_COLUMN + x; int xpos = TILE_INVENTORY_XPOS + x * SLOT_X_SPACING; int ypos = TILE_INVENTORY_YPOS + y * SLOT_Y_SPACING; addSlotToContainer(new Slot(tileEntityInventoryBasic, slotNumber, TILE_INVENTORY_XPOS + SLOT_X_SPACING * x, TILE_INVENTORY_YPOS)); } } } Thanks! Quote Not new to java >> New to modding.
Draco18s Posted November 15, 2016 Posted November 15, 2016 int slotNumber = TE_INVENTORY_SLOT_COUNT + y * TE_INVENTORY_SLOT_COUNT_COLUMN + x; Run that math through your head again. Quote Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable. If you think this is the case, JUST REPORT ME. Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice. Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked. DO NOT PM ME WITH PROBLEMS. No help will be given.
Jay Avery Posted November 15, 2016 Posted November 15, 2016 It's everyone's favourite zero index problem. Slot index = slot count - 1. Quote
Draco18s Posted November 15, 2016 Posted November 15, 2016 It's everyone's favourite zero index problem. Slot index = slot count - 1. Actually, its not. See my post. Quote Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable. If you think this is the case, JUST REPORT ME. Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice. Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked. DO NOT PM ME WITH PROBLEMS. No help will be given.
gmod622 Posted November 15, 2016 Author Posted November 15, 2016 int slotNumber = TE_INVENTORY_SLOT_COUNT + y * TE_INVENTORY_SLOT_COUNT_COLUMN + x; Run that math through your head again. yeah, it gets me the 9 slots that I want. I dont see any other way i could do that. Quote Not new to java >> New to modding.
Matryoshika Posted November 15, 2016 Posted November 15, 2016 int slotNumber = TE_INVENTORY_SLOT_COUNT + y * TE_INVENTORY_SLOT_COUNT_COLUMN + x; (3+0)*(3+0) = 9 (3+0)*(3+1) = 12 (3+0)*(3+2) = 15 .... (3+3)*(3+3) = 36 I don't think your implementation & your expectation of what should happen are the same. Quote Also previously known as eAndPi. "Pi, is there a station coming up where we can board your train of thought?" -Kronnn Published Mods: Underworld Handy links: Vic_'s Forge events Own WIP Tutorials.
gmod622 Posted November 15, 2016 Author Posted November 15, 2016 Okay, look back at the crafting table and found this: this.addSlotToContainer(new Slot(tileEntityInventoryBasic, x + y * 3, TILE_INVENTORY_XPOS + x * 18,TILE_INVENTORY_YPOS + y * 18)); This seems to work, can somebody explain? Quote Not new to java >> New to modding.
Draco18s Posted November 15, 2016 Posted November 15, 2016 int slotNumber = TE_INVENTORY_SLOT_COUNT + y * TE_INVENTORY_SLOT_COUNT_COLUMN + x; (3+0)*(3+0) = 9 (3+0)*(3+1) = 12 (3+0)*(3+2) = 15 .... (3+3)*(3+3) = 36 Ehm, those brackets are wrong. It should be 3+(0*3)+0 = 3 3+(0*3)+1 = 4 3+(0*3)+2 = 5 .... 3+(3*3)+3 = 15 The first TE_INVENTORY_SLOT_COUNT in that calculation just doesn't make sense there... Correction: private final int TE_INVENTORY_SLOT_COUNT = 9; 9+(0*3)+0 = 9 9+(0*3)+1 = 10 9+(0*3)+2 = 11 .... 9+(3*3)+3 = 21 Quote Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable. If you think this is the case, JUST REPORT ME. Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice. Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked. DO NOT PM ME WITH PROBLEMS. No help will be given.
Jay Avery Posted November 15, 2016 Posted November 15, 2016 Okay, look back at the crafting table and found this: this.addSlotToContainer(new Slot(tileEntityInventoryBasic, x + y * 3, TILE_INVENTORY_XPOS + x * 18,TILE_INVENTORY_YPOS + y * 18)); This seems to work, can somebody explain? It works because it doesn't have the TE_INVENTORY_SLOT_COUNT added at the beginning like your code in the OP does - so it results in 0, 1, 2, ... 8. Your code results in 9, 10, 11, ... 17. When you add a Slot, you need to give it the index for the inventory it is connected to, not the index of the slot in the container. Quote
TheGreyGhost Posted November 15, 2016 Posted November 15, 2016 Hi I think the confusion is because the "TE_INVENTORY_SLOT_COUNT" offset is necessary for the player hotbar and the inventory - which is how the vanilla chest is set up (the GUI shows the player hotbar, the player inventory, and the chest contents). For your own TileEntity it will usually start from zero. // 0 - 8 = hotbar slots (which will map to the InventoryPlayer slot numbers 0 - // 9 - 35 = player inventory slots (which map to the InventoryPlayer slot numbers 9 - 35) // 36 - 44 = TileInventory slots, which map to our TileEntity slot numbers 0 - // Add the players hotbar to the gui - the [xpos, ypos] location of each item for (int x = 0; x < HOTBAR_SLOT_COUNT; x++) { int slotNumber = x; addSlotToContainer(new Slot(invPlayer, slotNumber, HOTBAR_XPOS + SLOT_X_SPACING * x, HOTBAR_YPOS)); } // Add the rest of the players inventory to the gui for (int y = 0; y < PLAYER_INVENTORY_ROW_COUNT; y++) { for (int x = 0; x < PLAYER_INVENTORY_COLUMN_COUNT; x++) { int slotNumber = HOTBAR_SLOT_COUNT + y * PLAYER_INVENTORY_COLUMN_COUNT + x; int xpos = PLAYER_INVENTORY_XPOS + x * SLOT_X_SPACING; int ypos = PLAYER_INVENTORY_YPOS + y * SLOT_Y_SPACING; addSlotToContainer(new Slot(invPlayer, slotNumber, xpos, ypos)); } } // Add the tile inventory container to the gui for (int x = 0; x < TE_INVENTORY_SLOT_COUNT; x++) { int slotNumber = x; addSlotToContainer(new Slot(tileEntityInventoryBasic, slotNumber, TILE_INVENTORY_XPOS + SLOT_X_SPACING * x, TILE_INVENTORY_YPOS)); } -TGG Quote
Recommended Posts
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.