Item container pas dans la main
-
Salut,
J’ai suivi ce tutoriel : https://www.minecraftforgefrance.fr/showthread.php?tid=2197
Seulement j’aimerais faire qu’il n’y est pas à faire clique droit avec l’item dans la main pour ouvrir le Sac mais que celui-ci ce trouve au slot 9 du mainInventory[] en haut à gauche pour être exact et en appuyant sur une touche cela ouvre le Sac.
Voici le code actuel que j’ai :
GuiSac :
package fr.altiscraft.benjaminloison.gui; import org.lwjgl.opengl.GL11; import fr.altiscraft.benjaminloison.conteneurs.InventaireSac; import fr.altiscraft.benjaminloison.conteneurs.SacConteneur; import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.client.resources.I18n; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.util.ResourceLocation; public class GuiSac extends GuiContainer { public static final ResourceLocation texture = new ResourceLocation("textures/gui/container/generic_54.png"); protected InventaireSac inv; protected InventoryPlayer playerInv; public int rows; public GuiSac(InventoryPlayer playerInv, InventaireSac inv) { super(new SacConteneur(playerInv, inv)); this.playerInv = playerInv; this.inv = inv; allowUserInput = false; rows = inv.getSizeInventory() / 9; ySize = 114 + rows * 18; } @Override protected void drawGuiContainerForegroundLayer(int x, int y) { fontRendererObj.drawString(I18n.format(inv.getInventoryName(), new Object[0]), 8, 6, 4210752); fontRendererObj.drawString(playerInv.hasCustomInventoryName() ? playerInv.getInventoryName() : I18n.format(playerInv.getInventoryName(), new Object[0]), 8, ySize - 96 + 2, 4210752); } @Override protected void drawGuiContainerBackgroundLayer(float prt, int x, int y) { GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); mc.getTextureManager().bindTexture(texture); int k = (width - xSize) / 2, l = (height - ySize) / 2; drawTexturedModalRect(k, l, 0, 0, xSize, rows * 18 + 17); drawTexturedModalRect(k, l + rows * 18 + 17, 0, 126, xSize, 96); } }
L’Inventaire du Sac :
package fr.altiscraft.benjaminloison.conteneurs; import fr.altiscraft.benjaminloison.common.ModAltisCraft; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraftforge.common.util.Constants; public class InventaireSac implements IInventory { public ItemStack[] content; public int size; public InventaireSac(ItemStack container, int size) { this.size = size; content = new ItemStack; if(!container.hasTagCompound()) container.setTagCompound(new NBTTagCompound()); readFromNBT(container.getTagCompound()); } public void readFromNBT(NBTTagCompound comp) { NBTTagList nbtlist = comp.getTagList("Inventory", Constants.NBT.TAG_COMPOUND); for(int i = 0; i < nbtlist.tagCount(); i++) { NBTTagCompound comp1 = nbtlist.getCompoundTagAt(i); int slot = comp1.getInteger("Slot"); content[slot] = ItemStack.loadItemStackFromNBT(comp1); } } public void writeToNBT(NBTTagCompound comp) { NBTTagList nbtlist = new NBTTagList(); for(int i = 0; i < size; i++) { if(content* != null) { NBTTagCompound comp1 = new NBTTagCompound(); comp1.setInteger("Slot", i); content*.writeToNBT(comp1); nbtlist.appendTag(comp1); } } comp.setTag("Inventory", nbtlist); } @Override public int getSizeInventory() { return size; } @Override public ItemStack getStackInSlot(int index) { return content[index]; } @Override public ItemStack decrStackSize(int index, int amount) { ItemStack stack = getStackInSlot(index); if(stack != null) { if(stack.stackSize > amount) { stack = stack.splitStack(amount); if(stack.stackSize == 0) content[index] = null; } else { content[index] = null; } } return stack; } @Override public ItemStack getStackInSlotOnClosing(int index) { ItemStack stack = getStackInSlot(index); if(stack != null) content[index] = null; return stack; } @Override public void setInventorySlotContents(int index, ItemStack stack) { content[index] = stack; } @Override public String getInventoryName() { return ModAltisCraft.MODID + ".container.backpack"; } @Override public boolean hasCustomInventoryName() { return false; } @Override public int getInventoryStackLimit() { return 64; } @Override public void markDirty() {} @Override public boolean isUseableByPlayer(EntityPlayer player) { return true; } @Override public void openInventory() {} @Override public void closeInventory() {} @Override public boolean isItemValidForSlot(int index, ItemStack stack) { return !(stack.getItem() == ModAltisCraft.Sac); } }
Conteneur du Sac :
package fr.altiscraft.benjaminloison.conteneurs; import fr.altiscraft.benjaminloison.common.ModAltisCraft; 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; import net.minecraft.nbt.NBTTagCompound; public class SacConteneur extends Container { public InventaireSac invBackpack; public int rows; public SacConteneur(InventoryPlayer playerInv, InventaireSac inv) { invBackpack = inv; rows = inv.getSizeInventory() / 9; int i = (rows - 4) * 18, j, k; for(j = 0; j < rows; ++j) { for(k = 0; k < 9; ++k) { addSlotToContainer(new SacSlot(inv, k + j * 9, 8 + k * 18, 18 + j * 18)); } } for(j = 0; j < 3; ++j) { for(k = 0; k < 9; ++k) { addSlotToContainer(new Slot(playerInv, k + j * 9 + 9, 8 + k * 18, 103 + j * 18 + i)); } } for(j = 0; j < 9; ++j) { addSlotToContainer(new Slot(playerInv, j, 8 + j * 18, 161 + i)); } } @Override public boolean canInteractWith(EntityPlayer player) { return true; } public void writeToNBT(ItemStack stack) { if(stack != null) { if(!stack.hasTagCompound()) stack.setTagCompound(new NBTTagCompound()); invBackpack.writeToNBT(stack.getTagCompound()); } } @Override public ItemStack transferStackInSlot(EntityPlayer player, int index) { ItemStack itemstack = null; Slot slot = (Slot)inventorySlots.get(index); if(slot != null && slot.getHasStack()) { ItemStack itemstack1 = slot.getStack(); itemstack = itemstack1.copy(); if(itemstack.getItem() == ModAltisCraft.Sac) return null; if(index < invBackpack.getSizeInventory()) { if(!mergeItemStack(itemstack1, invBackpack.getSizeInventory(), inventorySlots.size(), true)) return null; } else if(!mergeItemStack(itemstack1, 0, invBackpack.getSizeInventory(), false)) { return null; } if(itemstack1.stackSize == 0) slot.putStack((ItemStack)null); else slot.onSlotChanged(); } return itemstack; } @Override public ItemStack slotClick(int slotIndex, int buttonPressed, int flag, EntityPlayer player) { return super.slotClick(slotIndex, buttonPressed, flag, player); /* * if(flag == 2 && buttonPressed == 9) * return null; * if(slotIndex - invBackpack.getSizeInventory() - 27 == 9) * return null; * return super.slotClick(slotIndex, buttonPressed, flag, player); */ } @Override public void onContainerClosed(EntityPlayer player) { writeToNBT(player.inventory.mainInventory[9]); super.onContainerClosed(player); } }
Slot utilisé pour le Gui du Sac :
package fr.altiscraft.benjaminloison.conteneurs; import fr.altiscraft.benjaminloison.common.ModAltisCraft; import net.minecraft.inventory.IInventory; import net.minecraft.inventory.Slot; import net.minecraft.item.ItemStack; public class SacSlot extends Slot { public SacSlot(IInventory inv, int index, int x, int y) { super(inv, index, x, y); } @Override public boolean isItemValid(ItemStack stack) { return !(stack.getItem() == ModAltisCraft.Sac); } }
Classe Container mère :
package net.minecraft.inventory; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.util.MathHelper; public abstract class Container { /** the list of all items(stacks) for the corresponding slot */ public List inventoryItemStacks = new ArrayList(); /** the list of all slots in the inventory */ public List inventorySlots = new ArrayList(); public int windowId; @SideOnly(Side.CLIENT) private short transactionID; private int field_94535_f = -1; private int field_94536_g; private final Set field_94537_h = new HashSet(); /** list of all people that need to be notified when this craftinventory changes */ protected List crafters = new ArrayList(); private Set playerList = new HashSet(); private static final String __OBFID = "CL_00001730"; /** * the slot is assumed empty */ protected Slot addSlotToContainer(Slot p_75146_1_) { p_75146_1_.slotNumber = this.inventorySlots.size(); this.inventorySlots.add(p_75146_1_); this.inventoryItemStacks.add((Object)null); return p_75146_1_; } public void addCraftingToCrafters(ICrafting p_75132_1_) { if (this.crafters.contains(p_75132_1_)) { throw new IllegalArgumentException("Listener already listening"); } else { this.crafters.add(p_75132_1_); p_75132_1_.sendContainerAndContentsToPlayer(this, this.getInventory()); this.detectAndSendChanges(); } } /** * returns a list if itemStacks, for each slot. */ public List getInventory() { ArrayList arraylist = new ArrayList(); for (int i = 0; i < this.inventorySlots.size(); ++i) { arraylist.add(((Slot)this.inventorySlots.get(i)).getStack()); } return arraylist; } /** * Remove this crafting listener from the listener list. */ @SideOnly(Side.CLIENT) public void removeCraftingFromCrafters(ICrafting p_82847_1_) { this.crafters.remove(p_82847_1_); } /** * Looks for changes made in the container, sends them to every listener. */ public void detectAndSendChanges() { for (int i = 0; i < this.inventorySlots.size(); ++i) { ItemStack itemstack = ((Slot)this.inventorySlots.get(i)).getStack(); ItemStack itemstack1 = (ItemStack)this.inventoryItemStacks.get(i); if (!ItemStack.areItemStacksEqual(itemstack1, itemstack)) { itemstack1 = itemstack == null ? null : itemstack.copy(); this.inventoryItemStacks.set(i, itemstack1); for (int j = 0; j < this.crafters.size(); ++j) { ((ICrafting)this.crafters.get(j)).sendSlotContents(this, i, itemstack1); } } } } /** * enchants the item on the table using the specified slot; also deducts XP from player */ public boolean enchantItem(EntityPlayer p_75140_1_, int p_75140_2_) { return false; } public Slot getSlotFromInventory(IInventory p_75147_1_, int p_75147_2_) { for (int j = 0; j < this.inventorySlots.size(); ++j) { Slot slot = (Slot)this.inventorySlots.get(j); if (slot.isSlotInInventory(p_75147_1_, p_75147_2_)) { return slot; } } return null; } public Slot getSlot(int p_75139_1_) { return (Slot)this.inventorySlots.get(p_75139_1_); } /** * Called when a player shift-clicks on a slot. You must override this or you will crash when someone does that. */ public ItemStack transferStackInSlot(EntityPlayer p_82846_1_, int p_82846_2_) { Slot slot = (Slot)this.inventorySlots.get(p_82846_2_); return slot != null ? slot.getStack() : null; } public ItemStack slotClick(int p_75144_1_, int p_75144_2_, int p_75144_3_, EntityPlayer p_75144_4_) { ItemStack itemstack = null; InventoryPlayer inventoryplayer = p_75144_4_.inventory; int i1; ItemStack itemstack3; if (p_75144_3_ == 5) { int l = this.field_94536_g; this.field_94536_g = func_94532_c(p_75144_2_); if ((l != 1 || this.field_94536_g != 2) && l != this.field_94536_g) { this.func_94533_d(); } else if (inventoryplayer.getItemStack() == null) { this.func_94533_d(); } else if (this.field_94536_g == 0) { this.field_94535_f = func_94529_b(p_75144_2_); if (func_94528_d(this.field_94535_f)) { this.field_94536_g = 1; this.field_94537_h.clear(); } else { this.func_94533_d(); } } else if (this.field_94536_g == 1) { Slot slot = (Slot)this.inventorySlots.get(p_75144_1_); if (slot != null && func_94527_a(slot, inventoryplayer.getItemStack(), true) && slot.isItemValid(inventoryplayer.getItemStack()) && inventoryplayer.getItemStack().stackSize > this.field_94537_h.size() && this.canDragIntoSlot(slot)) { this.field_94537_h.add(slot); } } else if (this.field_94536_g == 2) { if (!this.field_94537_h.isEmpty()) { itemstack3 = inventoryplayer.getItemStack().copy(); i1 = inventoryplayer.getItemStack().stackSize; Iterator iterator = this.field_94537_h.iterator(); while (iterator.hasNext()) { Slot slot1 = (Slot)iterator.next(); if (slot1 != null && func_94527_a(slot1, inventoryplayer.getItemStack(), true) && slot1.isItemValid(inventoryplayer.getItemStack()) && inventoryplayer.getItemStack().stackSize >= this.field_94537_h.size() && this.canDragIntoSlot(slot1)) { ItemStack itemstack1 = itemstack3.copy(); int j1 = slot1.getHasStack() ? slot1.getStack().stackSize : 0; func_94525_a(this.field_94537_h, this.field_94535_f, itemstack1, j1); if (itemstack1.stackSize > itemstack1.getMaxStackSize()) { itemstack1.stackSize = itemstack1.getMaxStackSize(); } if (itemstack1.stackSize > slot1.getSlotStackLimit()) { itemstack1.stackSize = slot1.getSlotStackLimit(); } i1 -= itemstack1.stackSize - j1; slot1.putStack(itemstack1); } } itemstack3.stackSize = i1; if (itemstack3.stackSize <= 0) { itemstack3 = null; } inventoryplayer.setItemStack(itemstack3); } this.func_94533_d(); } else { this.func_94533_d(); } } else if (this.field_94536_g != 0) { this.func_94533_d(); } else { Slot slot2; int l1; ItemStack itemstack5; if ((p_75144_3_ == 0 || p_75144_3_ == 1) && (p_75144_2_ == 0 || p_75144_2_ == 1)) { if (p_75144_1_ == -999) { if (inventoryplayer.getItemStack() != null && p_75144_1_ == -999) { if (p_75144_2_ == 0) { p_75144_4_.dropPlayerItemWithRandomChoice(inventoryplayer.getItemStack(), true); inventoryplayer.setItemStack((ItemStack)null); } if (p_75144_2_ == 1) { p_75144_4_.dropPlayerItemWithRandomChoice(inventoryplayer.getItemStack().splitStack(1), true); if (inventoryplayer.getItemStack().stackSize == 0) { inventoryplayer.setItemStack((ItemStack)null); } } } } else if (p_75144_3_ == 1) { if (p_75144_1_ < 0) { return null; } slot2 = (Slot)this.inventorySlots.get(p_75144_1_); if (slot2 != null && slot2.canTakeStack(p_75144_4_)) { itemstack3 = this.transferStackInSlot(p_75144_4_, p_75144_1_); if (itemstack3 != null) { Item item = itemstack3.getItem(); itemstack = itemstack3.copy(); if (slot2.getStack() != null && slot2.getStack().getItem() == item) { this.retrySlotClick(p_75144_1_, p_75144_2_, true, p_75144_4_); } } } } else { if (p_75144_1_ < 0) { return null; } slot2 = (Slot)this.inventorySlots.get(p_75144_1_); if (slot2 != null) { itemstack3 = slot2.getStack(); ItemStack itemstack4 = inventoryplayer.getItemStack(); if (itemstack3 != null) { itemstack = itemstack3.copy(); } if (itemstack3 == null) { if (itemstack4 != null && slot2.isItemValid(itemstack4)) { l1 = p_75144_2_ == 0 ? itemstack4.stackSize : 1; if (l1 > slot2.getSlotStackLimit()) { l1 = slot2.getSlotStackLimit(); } if (itemstack4.stackSize >= l1) { slot2.putStack(itemstack4.splitStack(l1)); } if (itemstack4.stackSize == 0) { inventoryplayer.setItemStack((ItemStack)null); } } } else if (slot2.canTakeStack(p_75144_4_)) { if (itemstack4 == null) { l1 = p_75144_2_ == 0 ? itemstack3.stackSize : (itemstack3.stackSize + 1) / 2; itemstack5 = slot2.decrStackSize(l1); inventoryplayer.setItemStack(itemstack5); if (itemstack3.stackSize == 0) { slot2.putStack((ItemStack)null); } slot2.onPickupFromSlot(p_75144_4_, inventoryplayer.getItemStack()); } else if (slot2.isItemValid(itemstack4)) { if (itemstack3.getItem() == itemstack4.getItem() && itemstack3.getItemDamage() == itemstack4.getItemDamage() && ItemStack.areItemStackTagsEqual(itemstack3, itemstack4)) { l1 = p_75144_2_ == 0 ? itemstack4.stackSize : 1; if (l1 > slot2.getSlotStackLimit() - itemstack3.stackSize) { l1 = slot2.getSlotStackLimit() - itemstack3.stackSize; } if (l1 > itemstack4.getMaxStackSize() - itemstack3.stackSize) { l1 = itemstack4.getMaxStackSize() - itemstack3.stackSize; } itemstack4.splitStack(l1); if (itemstack4.stackSize == 0) { inventoryplayer.setItemStack((ItemStack)null); } itemstack3.stackSize += l1; } else if (itemstack4.stackSize <= slot2.getSlotStackLimit()) { slot2.putStack(itemstack4); inventoryplayer.setItemStack(itemstack3); } } else if (itemstack3.getItem() == itemstack4.getItem() && itemstack4.getMaxStackSize() > 1 && (!itemstack3.getHasSubtypes() || itemstack3.getItemDamage() == itemstack4.getItemDamage()) && ItemStack.areItemStackTagsEqual(itemstack3, itemstack4)) { l1 = itemstack3.stackSize; if (l1 > 0 && l1 + itemstack4.stackSize <= itemstack4.getMaxStackSize()) { itemstack4.stackSize += l1; itemstack3 = slot2.decrStackSize(l1); if (itemstack3.stackSize == 0) { slot2.putStack((ItemStack)null); } slot2.onPickupFromSlot(p_75144_4_, inventoryplayer.getItemStack()); } } } slot2.onSlotChanged(); } } } else if (p_75144_3_ == 2 && p_75144_2_ >= 0 && p_75144_2_ < 9) { slot2 = (Slot)this.inventorySlots.get(p_75144_1_); if (slot2.canTakeStack(p_75144_4_)) { itemstack3 = inventoryplayer.getStackInSlot(p_75144_2_); boolean flag = itemstack3 == null || slot2.inventory == inventoryplayer && slot2.isItemValid(itemstack3); l1 = -1; if (!flag) { l1 = inventoryplayer.getFirstEmptyStack(); flag |= l1 > -1; } if (slot2.getHasStack() && flag) { itemstack5 = slot2.getStack(); inventoryplayer.setInventorySlotContents(p_75144_2_, itemstack5.copy()); if ((slot2.inventory != inventoryplayer || !slot2.isItemValid(itemstack3)) && itemstack3 != null) { if (l1 > -1) { inventoryplayer.addItemStackToInventory(itemstack3); slot2.decrStackSize(itemstack5.stackSize); slot2.putStack((ItemStack)null); slot2.onPickupFromSlot(p_75144_4_, itemstack5); } } else { slot2.decrStackSize(itemstack5.stackSize); slot2.putStack(itemstack3); slot2.onPickupFromSlot(p_75144_4_, itemstack5); } } else if (!slot2.getHasStack() && itemstack3 != null && slot2.isItemValid(itemstack3)) { inventoryplayer.setInventorySlotContents(p_75144_2_, (ItemStack)null); slot2.putStack(itemstack3); } } } else if (p_75144_3_ == 3 && p_75144_4_.capabilities.isCreativeMode && inventoryplayer.getItemStack() == null && p_75144_1_ >= 0) { slot2 = (Slot)this.inventorySlots.get(p_75144_1_); if (slot2 != null && slot2.getHasStack()) { itemstack3 = slot2.getStack().copy(); itemstack3.stackSize = itemstack3.getMaxStackSize(); inventoryplayer.setItemStack(itemstack3); } } else if (p_75144_3_ == 4 && inventoryplayer.getItemStack() == null && p_75144_1_ >= 0) { slot2 = (Slot)this.inventorySlots.get(p_75144_1_); if (slot2 != null && slot2.getHasStack() && slot2.canTakeStack(p_75144_4_)) { itemstack3 = slot2.decrStackSize(p_75144_2_ == 0 ? 1 : slot2.getStack().stackSize); slot2.onPickupFromSlot(p_75144_4_, itemstack3); p_75144_4_.dropPlayerItemWithRandomChoice(itemstack3, true); } } else if (p_75144_3_ == 6 && p_75144_1_ >= 0) { slot2 = (Slot)this.inventorySlots.get(p_75144_1_); itemstack3 = inventoryplayer.getItemStack(); if (itemstack3 != null && (slot2 == null || !slot2.getHasStack() || !slot2.canTakeStack(p_75144_4_))) { i1 = p_75144_2_ == 0 ? 0 : this.inventorySlots.size() - 1; l1 = p_75144_2_ == 0 ? 1 : -1; for (int i2 = 0; i2 < 2; ++i2) { for (int j2 = i1; j2 >= 0 && j2 < this.inventorySlots.size() && itemstack3.stackSize < itemstack3.getMaxStackSize(); j2 += l1) { Slot slot3 = (Slot)this.inventorySlots.get(j2); if (slot3.getHasStack() && func_94527_a(slot3, itemstack3, true) && slot3.canTakeStack(p_75144_4_) && this.func_94530_a(itemstack3, slot3) && (i2 != 0 || slot3.getStack().stackSize != slot3.getStack().getMaxStackSize())) { int k1 = Math.min(itemstack3.getMaxStackSize() - itemstack3.stackSize, slot3.getStack().stackSize); ItemStack itemstack2 = slot3.decrStackSize(k1); itemstack3.stackSize += k1; if (itemstack2.stackSize <= 0) { slot3.putStack((ItemStack)null); } slot3.onPickupFromSlot(p_75144_4_, itemstack2); } } } } this.detectAndSendChanges(); } } return itemstack; } public boolean func_94530_a(ItemStack p_94530_1_, Slot p_94530_2_) { return true; } protected void retrySlotClick(int p_75133_1_, int p_75133_2_, boolean p_75133_3_, EntityPlayer p_75133_4_) { this.slotClick(p_75133_1_, p_75133_2_, 1, p_75133_4_); } /** * Called when the container is closed. */ public void onContainerClosed(EntityPlayer p_75134_1_) { InventoryPlayer inventoryplayer = p_75134_1_.inventory; if (inventoryplayer.getItemStack() != null) { p_75134_1_.dropPlayerItemWithRandomChoice(inventoryplayer.getItemStack(), false); inventoryplayer.setItemStack((ItemStack)null); } } /** * Callback for when the crafting matrix is changed. */ public void onCraftMatrixChanged(IInventory p_75130_1_) { this.detectAndSendChanges(); } /** * args: slotID, itemStack to put in slot */ public void putStackInSlot(int p_75141_1_, ItemStack p_75141_2_) { this.getSlot(p_75141_1_).putStack(p_75141_2_); } /** * places itemstacks in first x slots, x being aitemstack.lenght */ @SideOnly(Side.CLIENT) public void putStacksInSlots(ItemStack[] p_75131_1_) { for (int i = 0; i < p_75131_1_.length; ++i) { this.getSlot(i).putStack(p_75131_1_*); } } @SideOnly(Side.CLIENT) public void updateProgressBar(int p_75137_1_, int p_75137_2_) {} /** * Gets a unique transaction ID. Parameter is unused. */ @SideOnly(Side.CLIENT) public short getNextTransactionID(InventoryPlayer p_75136_1_) { ++this.transactionID; return this.transactionID; } /** * NotUsing because adding a player twice is an error */ public boolean isPlayerNotUsingContainer(EntityPlayer p_75129_1_) { return !this.playerList.contains(p_75129_1_); } /** * adds or removes the player from the container based on par2 */ public void setPlayerIsPresent(EntityPlayer p_75128_1_, boolean p_75128_2_) { if (p_75128_2_) { this.playerList.remove(p_75128_1_); } else { this.playerList.add(p_75128_1_); } } public abstract boolean canInteractWith(EntityPlayer p_75145_1_); /** * merges provided ItemStack with the first avaliable one in the container/player inventory */ protected boolean mergeItemStack(ItemStack p_75135_1_, int p_75135_2_, int p_75135_3_, boolean p_75135_4_) { boolean flag1 = false; int k = p_75135_2_; if (p_75135_4_) { k = p_75135_3_ - 1; } Slot slot; ItemStack itemstack1; if (p_75135_1_.isStackable()) { while (p_75135_1_.stackSize > 0 && (!p_75135_4_ && k < p_75135_3_ || p_75135_4_ && k >= p_75135_2_)) { slot = (Slot)this.inventorySlots.get(k); itemstack1 = slot.getStack(); if (itemstack1 != null && itemstack1.getItem() == p_75135_1_.getItem() && (!p_75135_1_.getHasSubtypes() || p_75135_1_.getItemDamage() == itemstack1.getItemDamage()) && ItemStack.areItemStackTagsEqual(p_75135_1_, itemstack1)) { int l = itemstack1.stackSize + p_75135_1_.stackSize; if (l <= p_75135_1_.getMaxStackSize()) { p_75135_1_.stackSize = 0; itemstack1.stackSize = l; slot.onSlotChanged(); flag1 = true; } else if (itemstack1.stackSize < p_75135_1_.getMaxStackSize()) { p_75135_1_.stackSize -= p_75135_1_.getMaxStackSize() - itemstack1.stackSize; itemstack1.stackSize = p_75135_1_.getMaxStackSize(); slot.onSlotChanged(); flag1 = true; } } if (p_75135_4_) { –k; } else { ++k; } } } if (p_75135_1_.stackSize > 0) { if (p_75135_4_) { k = p_75135_3_ - 1; } else { k = p_75135_2_; } while (!p_75135_4_ && k < p_75135_3_ || p_75135_4_ && k >= p_75135_2_) { slot = (Slot)this.inventorySlots.get(k); itemstack1 = slot.getStack(); if (itemstack1 == null) { slot.putStack(p_75135_1_.copy()); slot.onSlotChanged(); p_75135_1_.stackSize = 0; flag1 = true; break; } if (p_75135_4_) { --k; } else { ++k; } } } return flag1; } public static int func_94529_b(int p_94529_0_) { return p_94529_0_ >> 2 & 3; } public static int func_94532_c(int p_94532_0_) { return p_94532_0_ & 3; } @SideOnly(Side.CLIENT) public static int func_94534_d(int p_94534_0_, int p_94534_1_) { return p_94534_0_ & 3 | (p_94534_1_ & 3) << 2; } public static boolean func_94528_d(int p_94528_0_) { return p_94528_0_ == 0 || p_94528_0_ == 1; } protected void func_94533_d() { this.field_94536_g = 0; this.field_94537_h.clear(); } public static boolean func_94527_a(Slot p_94527_0_, ItemStack p_94527_1_, boolean p_94527_2_) { boolean flag1 = p_94527_0_ == null || !p_94527_0_.getHasStack(); if (p_94527_0_ != null && p_94527_0_.getHasStack() && p_94527_1_ != null && p_94527_1_.isItemEqual(p_94527_0_.getStack()) && ItemStack.areItemStackTagsEqual(p_94527_0_.getStack(), p_94527_1_)) { int i = p_94527_2_ ? 0 : p_94527_1_.stackSize; flag1 |= p_94527_0_.getStack().stackSize + i <= p_94527_1_.getMaxStackSize(); } return flag1; } public static void func_94525_a(Set p_94525_0_, int p_94525_1_, ItemStack p_94525_2_, int p_94525_3_) { switch (p_94525_1_) { case 0: p_94525_2_.stackSize = MathHelper.floor_float((float)p_94525_2_.stackSize / (float)p_94525_0_.size()); break; case 1: p_94525_2_.stackSize = 1; } p_94525_2_.stackSize += p_94525_3_; } /** * Returns true if the player can "drag-spilt" items into this slot,. returns true by default. Called to check if * the slot can be added to a list of Slots to split the held ItemStack across. */ public boolean canDragIntoSlot(Slot p_94531_1_) { return true; } public static int calcRedstoneFromInventory(IInventory p_94526_0_) { if (p_94526_0_ == null) { return 0; } else { int i = 0; float f = 0.0F; for (int j = 0; j < p_94526_0_.getSizeInventory(); ++j) { ItemStack itemstack = p_94526_0_.getStackInSlot(j); if (itemstack != null) { f += (float)itemstack.stackSize / (float)Math.min(p_94526_0_.getInventoryStackLimit(), itemstack.getMaxStackSize()); ++i; } } f /= (float)p_94526_0_.getSizeInventory(); return MathHelper.floor_float(f * 14.0F) + (i > 0 ? 1 : 0); } } }
Tout marche, cependant lorsque je clique sur une item de mon inventaire dans le container (que la fonction slotClick est donc appellée), le jeu crash avec ce crash :
Description: Ticking memory connection java.lang.IndexOutOfBoundsException: Index: 81, Size: 45 at java.util.ArrayList.rangeCheck(Unknown Source) at java.util.ArrayList.get(Unknown Source) at net.minecraft.inventory.Container.slotClick(Container.java:300) at net.minecraft.network.NetHandlerPlayServer.processClickWindow(NetHandlerPlayServer.java:955) at net.minecraft.network.play.client.C0EPacketClickWindow.processPacket(C0EPacketClickWindow.java:41) at net.minecraft.network.play.client.C0EPacketClickWindow.processPacket(C0EPacketClickWindow.java:113) at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:241) at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:182) at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:726) at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:614) at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:118) at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:485) at net.minecraft.server.MinecraftServer$2.run(MinecraftServer.java:752) A detailed walkthrough of the error, its code path and all known details is as follows: --------------------------------------------------------------------------------------- -- Head -- Stacktrace: at java.util.ArrayList.rangeCheck(Unknown Source) at java.util.ArrayList.get(Unknown Source) at net.minecraft.inventory.Container.slotClick(Container.java:300) at net.minecraft.network.NetHandlerPlayServer.processClickWindow(NetHandlerPlayServer.java:955) at net.minecraft.network.play.client.C0EPacketClickWindow.processPacket(C0EPacketClickWindow.java:41) at net.minecraft.network.play.client.C0EPacketClickWindow.processPacket(C0EPacketClickWindow.java:113) at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:241) -- Ticking connection -- Details: Connection: net.minecraft.network.NetworkManager@32c0d217 Stacktrace: at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:182) at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:726) at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:614) at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:118) at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:485) at net.minecraft.server.MinecraftServer$2.run(MinecraftServer.java:752) -- System Details -- Details: Minecraft Version: 1.7.10 Operating System: Windows 10 (amd64) version 10.0 Java Version: 1.8.0_66, Oracle Corporation Java VM Version: Java HotSpot(TM) 64-Bit Server VM (mixed mode), Oracle Corporation Memory: 614311208 bytes (585 MB) / 1037959168 bytes (989 MB) up to 1037959168 bytes (989 MB) JVM Flags: 3 total; -Xincgc -Xmx1024M -Xms1024M AABB Pool Size: 0 (0 bytes; 0 MB) allocated, 0 (0 bytes; 0 MB) used IntCache: cache: 0, tcache: 0, allocated: 0, tallocated: 0 FML: MCP v9.05 FML v7.10.99.99 Minecraft Forge 10.13.4.1492 7 mods loaded, 7 mods active States: 'U' = Unloaded 'L' = Loaded 'C' = Constructed 'H' = Pre-initialized 'I' = Initialized 'J' = Post-initialized 'A' = Available 'D' = Disabled 'E' = Errored UCHIJAAAA mcp{9.05} [Minecraft Coder Pack] (minecraft.jar) UCHIJAAAA FML{7.10.99.99} [Forge Mod Loader] (forgeSrc-1.7.10-10.13.4.1492-1.7.10.jar) UCHIJAAAA Forge{10.13.4.1492} [Minecraft Forge] (forgeSrc-1.7.10-10.13.4.1492-1.7.10.jar) UCHIJAAAA CodeChickenCore{1.0.4.29} [CodeChicken Core] (minecraft.jar) UCHIJAAAA altiscraft.MODID{1.0.0} [AltisCraft.fr] (bin) UCHIJAAAA CarpentersBlocks{3.3.7} [Carpenter's Blocks] (Carpenter's Blocks.jar) UCHIJAAAA gvc{0.6.1} [�aGliby's�f Voice Chat Mod] (GlibysVC-1.7.10-0.6.2a.jar) GL info: ~~ERROR~~ RuntimeException: No OpenGL context found in the current thread. Profiler Position: N/A (disabled) Vec3 Pool Size: 0 (0 bytes; 0 MB) allocated, 0 (0 bytes; 0 MB) used Player Count: 1 / 8; [EntityPlayerMP['Player43'/150, l='Nouveau monde', x=-841,39, y=4,00, z=734,04]] Type: Integrated Server (map_client.txt) Is Modded: Definitely; Client brand changed to 'fml,forge'
Le code que j’ai dans ma fonction slotClick semble correct et il est @Override alors je suis allé dans la classe mère seulement celle-ci est pas très bien lisible à cause de la décompilation, je ne comprends pas mon erreur.
Merci d’avance
-
Le problème c’est que tu rajoutes des slots, or quand le joueur clique, l’index su slot se trouve au-dessus de la limite du container de minecraft je crois.
Pour moi le seul moyen c’est d’intercepter le clique avant et de vérifier si c’est ton slot ou non
-
En fait il tente de get le slot 81 or il n’y en a que 45.
Il faudrait comprendre pourquoi il essaie de get le slot 81. -
J’ai abandonné cette idée ^^