Ca marche … presque … J’ai des comportements étrange quand je transfert des items :
- Si je fais passer de l’inventaire joueur à l’inventaire custom ça double les quantités.
- Si je prend les items de l’inventaire custom il disparaissent. Si j’utilise maj+clic on voit l’item apparaitre un court instant dans l’inventaire avant de disparaitre.
J’ai l’impression que cela vient de la séparation client / serveur qui est foireuse et que du coup et le client et le serveur transfert l’item et donc double ses quantité.
:::
Handler :
| |
| public class GuiHandler implements IGuiHandler |
| { |
| |
| |
| |
| |
| |
| @Override |
| public Object getServerGuiElement(int id, EntityPlayer player, World world, int x, int y, int z) |
| { |
| switch(id) |
| { |
| case Robotica.GUI.RobotInventory: |
| |
| return getRobotInventoryTileEntity(player, x); |
| |
| default: |
| return null; |
| } |
| } |
| |
| private Object getRobotInventoryTileEntity(EntityPlayer player, int id) |
| { |
| MechaEntity e = MechaEntity.getMecha(id); |
| |
| if(!(e instanceof RobotEntity)) |
| return null; |
| |
| if( new Coords(player).distanceTo((RobotEntity) e) > RobotEntity.GUIRANGE) |
| return null; |
| |
| return new RobotInventoryContainer(player.inventory, ((RobotEntity) e).inventory); |
| } |
| |
| |
| |
| |
| |
| @Override |
| public Object getClientGuiElement(int id, EntityPlayer player, World world, int x, int y, int z) |
| { |
| switch(id) |
| { |
| case Robotica.GUI.ControlPanel: |
| return new ControlPanelGUI(); |
| |
| case Robotica.GUI.RobotInfo: |
| return new InfoFrameGUI(); |
| |
| case Robotica.GUI.RobotInventory: |
| return getRobotInventoryGui(player, x); |
| |
| default: |
| return null; |
| } |
| } |
| |
| private Object getRobotInventoryGui(EntityPlayer player, int id) |
| { |
| MechaEntity e = MechaEntity.getMecha(id); |
| |
| if(!(e instanceof RobotEntity)) |
| return null; |
| |
| if( new Coords(player).distanceTo((RobotEntity) e) > RobotEntity.GUIRANGE) |
| return null; |
| |
| return new RobotInventoryGui(player, (RobotEntity) e); |
| } |
| } |
| |
GUI :
| |
| public class RobotInventoryGui extends GuiContainer |
| { |
| public static final ResourceLocation texture = new ResourceLocation(Robotica.SID, "textures/gui/robotinv.png"); |
| |
| public RobotInventoryGui(EntityPlayer player, RobotEntity robot) |
| { |
| super(new RobotInventoryContainer(player.inventory, robot.inventory)); |
| xSize = 175; |
| ySize = 165; |
| } |
| |
| @Override |
| protected void drawGuiContainerBackgroundLayer(float f, int i, int j) |
| { |
| GL11.glColor4f(1F, 1F, 1F, 1F); |
| FMLClientHandler.instance().getClient().renderEngine.func_110577_a(texture); |
| |
| int x = (this.width - this.xSize) / 2; |
| int y = (this.height - this.ySize) / 2; |
| this.drawTexturedModalRect(x, y, 0, 0, this.xSize, this.ySize); |
| } |
| } |
| |
TileEntity :
| |
| public class RobotInventory extends TileEntity implements IInventory |
| { |
| public static final int GRAB_RANGE = 3; |
| |
| protected RobotEntity entity; |
| protected ItemStack[] inv; |
| |
| public RobotInventory(RobotEntity robotEntity) |
| { |
| entity = robotEntity; |
| inv = new ItemStack[24]; |
| } |
| |
| @Override |
| public int getSizeInventory() |
| { |
| return inv.length; |
| } |
| |
| @Override |
| public ItemStack getStackInSlot(int i) |
| { |
| return inv*; |
| } |
| |
| @Override |
| public ItemStack decrStackSize(int slotId, int quantity) |
| { |
| if (this.inv[slotId] != null) |
| { |
| ItemStack itemstack; |
| |
| if (this.inv[slotId].stackSize <= quantity) |
| { |
| itemstack = this.inv[slotId]; |
| this.inv[slotId] = null; |
| this.onInventoryChanged(); |
| return itemstack; |
| } |
| else |
| { |
| itemstack = this.inv[slotId].splitStack(quantity); |
| |
| if (this.inv[slotId].stackSize == 0) |
| { |
| this.inv[slotId] = null; |
| } |
| |
| this.onInventoryChanged(); |
| return itemstack; |
| } |
| } |
| else |
| { |
| return null; |
| } |
| } |
| |
| @Override |
| public ItemStack getStackInSlotOnClosing(int slotId) |
| { |
| if (this.inv[slotId] != null) |
| { |
| ItemStack itemstack = this.inv[slotId]; |
| this.inv[slotId] = null; |
| return itemstack; |
| } |
| else |
| { |
| return null; |
| } |
| } |
| |
| @Override |
| public void setInventorySlotContents(int slotId, ItemStack stack) |
| { |
| System.out.println("Set slot content of " + slotId); |
| |
| this.inv[slotId] = stack; |
| |
| if (stack != null && stack.stackSize > this.getInventoryStackLimit()) |
| { |
| stack.stackSize = this.getInventoryStackLimit(); |
| } |
| |
| this.onInventoryChanged(); |
| } |
| |
| @Override |
| public String getInvName() |
| { |
| return "Robot inventory"; |
| } |
| |
| @Override |
| public boolean isInvNameLocalized() |
| { |
| return true; |
| } |
| |
| @Override |
| public int getInventoryStackLimit() |
| { |
| return 64; |
| } |
| |
| @Override |
| public boolean isUseableByPlayer(EntityPlayer player) |
| { |
| return player.getDistanceSq(entity.posX, entity.posY, entity.posZ) < 25; |
| } |
| |
| @Override |
| public void openChest() |
| { |
| } |
| |
| @Override |
| public void closeChest() |
| { |
| } |
| |
| @Override |
| public boolean isItemValidForSlot(int i, ItemStack itemstack) |
| { |
| return true; |
| } |
| } |
| |
Container :
| |
| public class RobotInventoryContainer extends Container |
| { |
| private RobotInventory inventory; |
| |
| public RobotInventoryContainer(InventoryPlayer playerinv, RobotInventory robotinv) |
| { |
| this.inventory = robotinv; |
| |
| for(int i=0;i<4;i++) |
| for(int j=0;j<5;j++) |
| addSlotToContainer(new Slot(robotinv, i * 5 + j, 82 + j * 18, 6 + i * 18)); |
| |
| for(int i=0;i<4;i++) |
| addSlotToContainer(new Slot(robotinv, 20 + i, 6, 6 + i * 18)); |
| |
| for(int i=0;i<3;i++) |
| for(int j=0;j<9;j++) |
| addSlotToContainer(new Slot(playerinv, 9 + i * 9 + j, 8 + j * 18, 84 + i * 18)); |
| |
| for(int j=0;j<9;j++) |
| addSlotToContainer(new Slot(playerinv, j, 8 + j * 18, 142)); |
| } |
| |
| public ItemStack transferStackInSlot(EntityPlayer player, int n) |
| { |
| System.out.println("Transfert in slot " + n); |
| |
| ItemStack stack = null; |
| Slot slotObject = (Slot)this.inventorySlots.get(n); |
| |
| if (slotObject != null && slotObject.getHasStack()) |
| { |
| ItemStack stack1 = slotObject.getStack(); |
| stack = stack1.copy(); |
| |
| if(n < 24) |
| { |
| if(!this.mergeItemStack(stack1,24,59,false)) |
| { |
| return null; |
| } |
| } |
| else if (!this.mergeItemStack(stack1, 0, 20, false)) |
| { |
| return null; |
| } |
| |
| if(stack1.stackSize == 0) |
| { |
| slotObject.putStack(null); |
| } |
| else |
| { |
| slotObject.onSlotChanged(); |
| } |
| } |
| |
| return stack; |
| } |
| |
| @Override |
| public boolean canInteractWith(EntityPlayer entityplayer) |
| { |
| return inventory.isUseableByPlayer(entityplayer); |
| } |
| } |
| |
:::
NB : pour info les slots custom sont formé d’un groupe de 20 et d’un groupe de 4.