Posted March 13, 20178 yr Hello everyone! I'm having yet another problem Here's how it goes : I have a block, with a TileEntity and a GUI that is a vendor. It shows 9 items at once, which you can click to select and then buy. The GUI is also open-able with an item, to benefit from remote access to the vendor. Here's the problem : When I open my GUI via the block : When I open the GUI via the item : However, in both cases, I can click on the slots to select the items, which means that the items ARE put in the slot in both GUIs, but they're not drawn on the item version. I have tried disabling the render of the background image in case the items were, for some reason, behind the background, and they are not. Here's the GUI code : package com.gugu42.rcmod.client.gui; import java.io.IOException; import org.lwjgl.opengl.GL11; import com.gugu42.rcmod.ContainerVendor; import com.gugu42.rcmod.RcMod; import com.gugu42.rcmod.capabilities.bolt.BoltProvider; import com.gugu42.rcmod.capabilities.bolt.IBolt; import com.gugu42.rcmod.items.EnumRcWeapons; import com.gugu42.rcmod.items.InventoryGadgetronPDA; import com.gugu42.rcmod.items.ItemRcWeap; import com.gugu42.rcmod.network.packets.PacketRefill; import com.gugu42.rcmod.network.packets.PacketVend; import com.gugu42.rcmod.tileentity.TileEntityVendor; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiButton; import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.client.renderer.RenderHelper; import net.minecraft.client.resources.I18n; import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.Slot; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.util.ResourceLocation; import net.minecraft.util.SoundEvent; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; @SideOnly(Side.CLIENT) public class GuiVendor extends GuiContainer { private static final ResourceLocation texturepath = new ResourceLocation( "rcmod", "textures/gui/vendor_new.png"); private static final ResourceLocation boltTexturePath = new ResourceLocation( "rcmod", "textures/gui/bolt.png"); private GuiButton buyBtn; private GuiButton exitBtn; private EnumRcWeapons weapons; private EntityPlayer player; private TileEntityVendor tileEntity; private ContainerVendor container; private Minecraft mc; private int selectedWeapon = -1; private int mouseX, mouseY; private ItemStack selectedItem; private ItemRcWeap selectedItemWeap; private EntityItem selectedItemEntity; public float rotation; public int weaponIndex = 1; public int lastID = 0; public int centerID = 1; private long timeOfLastAction; public GuiVendor(InventoryPlayer inventoryPlayer, TileEntityVendor tileEntity, EntityPlayer player, ContainerVendor container, InventoryGadgetronPDA inv) { super(new ContainerVendor(inventoryPlayer, tileEntity, inv)); this.player = player; this.tileEntity = tileEntity; this.container = container; this.mc = Minecraft.getMinecraft(); this.xSize = 256; this.ySize = 190; } @Override public void initGui() { super.initGui(); int posX = (this.width - xSize) / 2; int posY = (this.height - ySize) / 2; this.buttonList.clear(); this.buttonList.add(this.buyBtn = new GuiButton(0, posX + 41, posY + 137, 35, 20, I18n.format("gui.vendor.buy"))); this.buttonList.add(this.exitBtn = new GuiButton(1, posX + 181, posY + 137, 35, 20, I18n.format("gui.vendor.exit"))); timeOfLastAction = System.currentTimeMillis(); } @Override public void updateScreen() { putItemsInSlot(); //If the player idles on the gui, the vendor will say "Come on buddy I ain't got all day" or "Sooo... you gonna buy some or what ?" randomly if(System.currentTimeMillis() - timeOfLastAction >= 15000) { mc.player.playSound(new SoundEvent(new ResourceLocation("rcmod:vendor.speech.wait")), 1.0f, 1.0f); timeOfLastAction = System.currentTimeMillis(); } } public void handleSelectedWeapon() { if (selectedItemEntity != null) { selectedItemEntity.rotationYaw = 0; selectedItemEntity.hoverStart = 0; GL11.glPushMatrix(); GL11.glMatrixMode(GL11.GL_PROJECTION); GL11.glMatrixMode(GL11.GL_MODELVIEW); GL11.glTranslatef(xSize - 200, ySize - 92, 100); GL11.glScalef(-60f, 60f, 60f); GL11.glRotatef(180, 0, 0, 1); GL11.glRotatef(rotation, 0, 1, 0); RenderHelper.enableStandardItemLighting(); if (selectedWeapon >= 0 && selectedItem != null) Minecraft.getMinecraft().getRenderManager().doRenderEntity(selectedItemEntity, 0, -0.23, 0, 0, 0, false); RenderHelper.disableStandardItemLighting(); GL11.glPopMatrix(); rotation -= 1f; if (getItemInInventory(player.inventory, selectedItem.getItem()) == ItemStack.EMPTY) { this.mc.fontRendererObj.drawString( "" + EnumRcWeapons.getPriceFromItem(selectedItem .getItem()), 30, 105, 0xFFFFFF); this.buyBtn.displayString = I18n.format("gui.vendor.buy"); } else { this.mc.fontRendererObj.drawString( "" + getItemInInventory(player.inventory, selectedItem.getItem()).getItemDamage() * selectedItemWeap.getPrice(), 30, 105, 0xFFFFFF); this.buyBtn.displayString = I18n.format("gui.vendor.refill"); } this.buyBtn.enabled = true; GL11.glPushMatrix(); GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); GL11.glDisable(GL11.GL_LIGHTING); this.mc.getTextureManager().bindTexture(boltTexturePath); GL11.glEnable(GL11.GL_BLEND); GL11.glDisable(GL11.GL_DEPTH_TEST); GL11.glDepthMask(false); GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); GL11.glDisable(GL11.GL_ALPHA_TEST); //drawTexturedQuadFit(xSize - 185, ySize - 86, 8, 8, 0); drawModalRectWithCustomSizedTexture(xSize - 185, ySize - 86, 0, 0, 8, 8, 64, 64); GL11.glEnable(GL11.GL_DEPTH_TEST); GL11.glDepthMask(true); GL11.glPopMatrix(); } else { this.buyBtn.enabled = false; } } public void putItemsInSlot() { for (int i = 0; i < 9; i++) { if (EnumRcWeapons.getItemFromID(centerID + i) != null) { this.container.putStackInSlot(i, new ItemStack(EnumRcWeapons .getItemFromID(centerID + i).getWeapon())); lastID = i; } else { if (EnumRcWeapons.getItemFromID(i - lastID) != null) { this.container.putStackInSlot( i, new ItemStack(EnumRcWeapons.getItemFromID( i - lastID).getWeapon())); } } } } @Override public void mouseClicked(int par1, int par2, int par3) throws IOException { super.mouseClicked(par1, par2, par3); for (int i = 0; i < 9; i++) { if (isMouseOverSlot(container.getSlot(i), mouseX, mouseY)) { selectedWeapon = i; selectedItem = container.getSlot(i).getStack(); if (selectedItem != null) { selectedItemEntity = new EntityItem(this.mc.world, 0, 0, 0, selectedItem); selectedItemWeap = (ItemRcWeap) selectedItem.getItem(); } mc.player.playSound(new SoundEvent(new ResourceLocation("rcmod:MenuSelect")), 1.0f, 1.0f); weaponIndex = weaponIndex + i; centerID = EnumRcWeapons.getIDFromItem(selectedItemWeap); timeOfLastAction = System.currentTimeMillis(); //When clicking a weapon, will occassionally say "Oh that's a nice one" or "That's a real beauty" if(mc.world.rand.nextInt(10) == 5) mc.player.playSound(new SoundEvent(new ResourceLocation("rcmod:vendor.speech.weapclicked")), 1.0f, 1.0f); } else { //ok you can happen if you want, but not too much pls } } } @Override public void actionPerformed(GuiButton button) { switch (button.id) { case 0: if (getItemInInventory(player.inventory, selectedItem.getItem()) == ItemStack.EMPTY) { IBolt props = player.getCapability(BoltProvider.BOLT_CAP, null); if (props.getCurrentBolt() > 0) { try { PacketVend packetVend = new PacketVend(centerID); RcMod.rcModPacketHandler.sendToServer(packetVend); } catch (Exception exception) { exception.printStackTrace(); } } } else { ItemRcWeap weap = (ItemRcWeap) selectedItem.getItem(); if (weap.useAmmo) { PacketRefill packet = new PacketRefill(centerID); RcMod.rcModPacketHandler.sendToServer(packet); } } break; case 1: mc.player.playSound(new SoundEvent(new ResourceLocation("rcmod:vendor.exit")), 1.0f, 1.0f); player.closeScreen(); break; default: break; } } public boolean doesGuiPauseGame() { return false; } protected void drawGuiContainerForegroundLayer(int par1, int par2) { mouseX = par1; mouseY = par2; handleSelectedWeapon(); if (selectedItemEntity != null) { //Using this check to be sure that there is an item selecred, cuz that works. this.mc.fontRendererObj.drawString( EnumRcWeapons.getItemFromID(centerID).getName(), 108, 16, 0x00FF00); } } @Override protected void drawGuiContainerBackgroundLayer(float par1, int par2, int par3) { GL11.glEnable(GL11.GL_BLEND); GL11.glColor4f(1.0f, 1.0f, 1.0f, 1.0f); this.mc.renderEngine.bindTexture(texturepath); int x = (width - xSize) / 2; int y = (height - ySize) / 2; this.drawTexturedModalRect(x, y, 0, 0, 256, 256); GL11.glDisable(GL11.GL_BLEND); } private boolean isMouseOverSlot(Slot par1Slot, int par2, int par3) { return this.isPointInRegion(par1Slot.xPos, par1Slot.yPos, 16, 16, par2, par3); } private int getSlotContainingItem(InventoryPlayer inventory, Item item) { for (int i = 0; i < inventory.mainInventory.size(); ++i) { if (inventory.mainInventory.get(i) != ItemStack.EMPTY && inventory.mainInventory.get(i).getItem() == item) { return i; } } return -1; } public ItemStack getItemInInventory(InventoryPlayer inventory, Item p_146026_1_) { int i = this.getSlotContainingItem(inventory, p_146026_1_); if (i < 0) { return ItemStack.EMPTY; } else { return inventory.mainInventory.get(i); } } } If any other code is needed, be sure to ask. Please note that this problem is not specific to 1.11.2 since I also had it back in 1.7.10
March 13, 20178 yr Author Alright. Container : Spoiler package com.gugu42.rcmod; import com.gugu42.rcmod.client.gui.SlotVendor; import com.gugu42.rcmod.items.InventoryGadgetronPDA; import com.gugu42.rcmod.tileentity.TileEntityVendor; 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 ContainerVendor extends Container { protected TileEntityVendor tileEntity; protected InventoryGadgetronPDA inv; public ContainerVendor(InventoryPlayer inventoryPlayer, TileEntityVendor te, InventoryGadgetronPDA inv) { tileEntity = te; this.inv = inv; if(inv != null){ System.out.println("Item gui"); addSlotToContainer(new SlotVendor(inv, 1, 48, 173)); addSlotToContainer(new SlotVendor(inv, 2, 66, 173)); addSlotToContainer(new SlotVendor(inv, 3, 84, 173)); addSlotToContainer(new SlotVendor(inv, 4, 102, 173)); addSlotToContainer(new SlotVendor(inv, 5, 120, 173)); addSlotToContainer(new SlotVendor(inv, 6, 138, 173)); addSlotToContainer(new SlotVendor(inv, 7, 156, 173)); addSlotToContainer(new SlotVendor(inv, 8, 174, 173)); addSlotToContainer(new SlotVendor(inv, 9, 192, 173)); } else { addSlotToContainer(new SlotVendor(tileEntity, 1, 48, 173)); addSlotToContainer(new SlotVendor(tileEntity, 2, 66, 173)); addSlotToContainer(new SlotVendor(tileEntity, 3, 84, 173)); addSlotToContainer(new SlotVendor(tileEntity, 4, 102, 173)); addSlotToContainer(new SlotVendor(tileEntity, 5, 120, 173)); addSlotToContainer(new SlotVendor(tileEntity, 6, 138, 173)); addSlotToContainer(new SlotVendor(tileEntity, 7, 156, 173)); addSlotToContainer(new SlotVendor(tileEntity, 8, 174, 173)); addSlotToContainer(new SlotVendor(tileEntity, 9, 192, 173)); } } @Override public boolean canInteractWith(EntityPlayer player) { if(tileEntity != null){ return tileEntity.isUsableByPlayer(player); } else { return true; } } @Override public ItemStack transferStackInSlot(EntityPlayer player, int slot) { ItemStack stack = ItemStack.EMPTY; Slot slotObject = (Slot) inventorySlots.get(slot); if (slotObject != null && slotObject.getHasStack()) { ItemStack stackInSlot = slotObject.getStack(); stack = stackInSlot.copy(); if (slot < 1) { if (!this.mergeItemStack(stackInSlot, 5, 42, true)) { return ItemStack.EMPTY; } } else if (!this.mergeItemStack(stackInSlot, 0, 1, false)) { return ItemStack.EMPTY; } if (stackInSlot.getCount() == 0) { slotObject.putStack(ItemStack.EMPTY); } else { slotObject.onSlotChanged(); } if (stackInSlot.getCount() == stack.getCount()) { return ItemStack.EMPTY; } slotObject.onTake(player, stackInSlot); } return stack; } public TileEntityVendor getVendor() { return this.tileEntity; } } GuiHandler : Spoiler package com.gugu42.rcmod.network; import com.gugu42.rcmod.ContainerVendor; import com.gugu42.rcmod.client.gui.GuiGadgetronHelper; import com.gugu42.rcmod.client.gui.GuiShip; import com.gugu42.rcmod.client.gui.GuiShipPlatform; import com.gugu42.rcmod.client.gui.GuiVendor; import com.gugu42.rcmod.items.InventoryGadgetronPDA; import com.gugu42.rcmod.tileentity.TileEntityShip; import com.gugu42.rcmod.tileentity.TileEntityShipFiller; import com.gugu42.rcmod.tileentity.TileEntityShipPlatform; import com.gugu42.rcmod.tileentity.TileEntityVendor; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import net.minecraftforge.fml.common.network.IGuiHandler; public class GuiHandler implements IGuiHandler { // returns an instance of the Container you made earlier @Override public Object getServerGuiElement(int id, EntityPlayer player, World world, int x, int y, int z) { TileEntity tileEntity = world.getTileEntity(new BlockPos(x, y, z)); if (tileEntity instanceof TileEntityVendor) { return new ContainerVendor(player.inventory, (TileEntityVendor) tileEntity, null); } if (tileEntity instanceof TileEntityShip) { return null; } if (id == 3) { return null; } if(id == 5){ return new ContainerVendor(player.inventory, null, new InventoryGadgetronPDA()); } return null; } // returns an instance of the Gui you made earlier @Override public Object getClientGuiElement(int id, EntityPlayer player, World world, int x, int y, int z) { TileEntity tileEntity = world.getTileEntity(new BlockPos(x, y, z)); if (tileEntity instanceof TileEntityVendor) { return new GuiVendor(player.inventory, (TileEntityVendor) tileEntity, player, new ContainerVendor(player.inventory, (TileEntityVendor) tileEntity, null), null); } if (tileEntity instanceof TileEntityShip || tileEntity instanceof TileEntityShipFiller) { if (tileEntity instanceof TileEntityShip) { TileEntityShip te = (TileEntityShip) tileEntity; return new GuiShip(te, player); } else { TileEntityShipFiller te2 = (TileEntityShipFiller) tileEntity; TileEntityShip te = (TileEntityShip) te2.getOriginalTileEntity(); System.out.println("Received data for gui : " + x + " " + y + " " + z); if (te != null) { return new GuiShip(te, player); } else { System.out.println("ERROR ! NULL TILEENTITY"); return new GuiShip(null, player); } } } if (id == 3) { return new GuiGadgetronHelper(player); } if (id == 4 && tileEntity instanceof TileEntityShipPlatform) { TileEntityShipPlatform te = (TileEntityShipPlatform)tileEntity; return new GuiShipPlatform(player, te); } if(id == 5) { return new GuiVendor(player.inventory, (TileEntityVendor) tileEntity, player, new ContainerVendor(player.inventory, null, new InventoryGadgetronPDA()), new InventoryGadgetronPDA()); } return null; } } Item : Spoiler package com.gugu42.rcmod.items; import com.gugu42.rcmod.RcMod; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.util.ActionResult; import net.minecraft.util.EnumActionResult; import net.minecraft.util.EnumHand; import net.minecraft.world.World; public class ItemGadgetronHelper extends Item { public ItemGadgetronHelper() { super(); this.setCreativeTab(RcMod.rcGadgTab); this.setMaxStackSize(1); this.setFull3D(); } @Override public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, EnumHand hand) { super.onItemRightClick(world, player, hand); ItemStack stack = player.getHeldItem(hand); if(!world.isRemote) player.openGui(RcMod.instance, 5, player.world, 0, 0, 0); return new ActionResult(EnumActionResult.SUCCESS, stack); } } Block : Spoiler package com.gugu42.rcmod.blocks; import com.gugu42.rcmod.RcMod; import com.gugu42.rcmod.client.ClientProxy; import com.gugu42.rcmod.tileentity.TileEntityVendor; import net.minecraft.block.Block; import net.minecraft.block.ITileEntityProvider; import net.minecraft.block.material.Material; import net.minecraft.block.state.IBlockState; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumHand; import net.minecraft.util.ResourceLocation; import net.minecraft.util.SoundCategory; import net.minecraft.util.SoundEvent; import net.minecraft.util.math.BlockPos; import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; public class BlockVendor extends Block implements ITileEntityProvider { public BlockVendor(Material par2Material) { super(par2Material); this.setCreativeTab(RcMod.rcTab); } public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) { TileEntity tileEntity = world.getTileEntity(pos); if (tileEntity == null || player.isSneaking() || hand != EnumHand.MAIN_HAND) { return false; } player.openGui(RcMod.instance, 0, world, pos.getX(), pos.getY(), pos.getZ()); world.playSound(null, pos, new SoundEvent(new ResourceLocation("rcmod:MenuOpen")), SoundCategory.BLOCKS, 1.0f, 1.0f); //par1World.playSoundAtEntity(par5EntityPlayer, "rcmod:MenuOpen", 1.0F, 1.0F); return true; } /*@SideOnly(Side.CLIENT) public void initModel() { ModelLoader.setCustomModelResourceLocation(Item.getItemFromBlock(this), 0, new ModelResourceLocation(getRegistryName(), "inventory")); }*/ @Override @SideOnly(Side.CLIENT) public boolean shouldSideBeRendered(IBlockState blockState, IBlockAccess worldIn, BlockPos pos, EnumFacing side) { return false; } @Override public boolean isBlockNormalCube(IBlockState blockState) { return false; } @Override public boolean isOpaqueCube(IBlockState blockState) { return false; } @Override public TileEntity createNewTileEntity(World worldIn, int meta) { return new TileEntityVendor(); } } Some of the code is dirty, sorry
March 13, 20178 yr Author I don't need to save the content of the item's inventory (it is set when you open it), do I still need capabilities ? As far as I understand, those are meant to store data right ?
March 13, 20178 yr Author After converting to IItemHandler (only changing the Inventory class), I don't seem to get any changes from it. I mostly do not understand why it works with the block, and not with the item.
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.