Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Container slots problems. [SOLVED... I THINK]


Recommended Posts

(APPEARS TO BE) SOLVED: the tutorial I followed never mentioned that I needed to have an @NetworkMod annotation, if you have the same problem then add @NetworkMod after the @Mod annotation and it's parameters.

 

I've followed a tutorial for how to make a simple inventory block but I cannot get the container class to work properly (I think it's an issue with the container class at the least) it appears that the slots for the tile/block are for some reason 'tangling' with those of the player's inventory with the same slot id (clicking on the slot for the tile entity will *appear* to take an item from the corresponding slot from the player's inv).

 

Here is the code for the Container Class:

public class ContainerCrystaliser extends Container
{
    TileCrystaliser tile;
    InventoryPlayer player;
    
    public ContainerCrystaliser(TileCrystaliser _tile, InventoryPlayer _player)
    {
        tile = _tile;
        this.player = _player;
        
        //add player slots
        for (int i = 0; i < 9; i++) {
            addSlotToContainer(new Slot(player, i, 8 + i * 18, 142));
        }
        
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 9; j++) {
                addSlotToContainer(new Slot(player, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
            }
        }
        
        //add tile slots
        addSlotToContainer(new Slot(tile,0,17,17));
        addSlotToContainer(new Slot(tile,1,17,49));
        addSlotToContainer(new Slot(tile,2,143,49));
    }
    
    @Override
    public ItemStack transferStackInSlot(EntityPlayer player, int slot) 
    {
        ItemStack stack = null;
        Slot slotObject = (Slot) inventorySlots.get(slot);
        if (slotObject != null && slotObject.getHasStack()) {
                ItemStack stackInSlot = slotObject.getStack();
                stack = stackInSlot.copy();

                //merges the item into player inventory since its in the tileEntity
                if (slot >= 36) {
                        if (!this.mergeItemStack(stackInSlot, 0, 35, false)) {
                                return null;
                        }
                }
                //places it into the tileEntity is possible since its in the player inventory
                else if (!this.mergeItemStack(stackInSlot, 36, 39, false)) {
                        return null;
                }

                if (stackInSlot.stackSize == 0) {slotObject.putStack(null);} 
                else 
                    slotObject.onSlotChanged();

                if (stackInSlot.stackSize == stack.stackSize) 
                {
                        return null;
                }
                slotObject.onPickupFromSlot(player, stackInSlot);
        }
        return stack;
    }
    
    @Override
    public boolean canInteractWith(EntityPlayer player) 
    {
        return tile.isUseableByPlayer(player);
    } 
}

 

EDIT:

 

Gui Handler

public class GuiHandler implements IGuiHandler
{
    @Override
    public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z)
    {
        TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
        if(tileEntity instanceof TileCrystaliser){
            return new ContainerCrystaliser((TileCrystaliser) tileEntity, player.inventory);
        }
        return null;
    }
    
    @Override
    public Object getClientGuiElement(int id, EntityPlayer player, World world, int x, int y, int z) 
    {
        TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
        if(tileEntity instanceof TileCrystaliser){
            return new GuiCrystaliser(player.inventory, (TileCrystaliser) tileEntity);
        }
        return null;

    }
}

Proxies

public class CommonProxy
{
    public void registerRendering() {
    }

    public int addArmour(String path) {
        return 0;
    }
}

public class CommonProxy
{
    public void registerRendering() {
    }

    public int addArmour(String path) {
        return 0;
    }
}

 

and as a bit of a side note; if I swap the order of the code for the tile and player slots, clicking on a player inv slot will pick up an item from 6 slots to the left (though it wraps from hotbar to the end of the inventory, such that clicking from the leftmost hotbar slot will pick up an item 6 from the right of the bottom line of the main inv.)

 

though it does also sometimes do other odd things, such as the hotbar being completely non-interactable, even if I comment out the adding of the tile's slots.

 

Link to post
Share on other sites

With them swapped, clicking the leftmost slot of the player's main inventory it picks up an item 3 slots to the right of it, clicking the first slot of the tile appears to do nothing.

 

The hotbar definately exists though (and all the positions are definately correct, that is fine), when I click on it it picks up the item for a split second but it gets pulled back.

 

I have also updated the OP with the proxies and Gui Handler.

Link to post
Share on other sites

Having noticed that the slots were always offset by the ammount of tile slots minus nine, I tried making it so I had nine tile slots (though some were duplicates) like so:

        addSlotToContainer(new Slot(tile,0,17,17)); //Water In
        addSlotToContainer(new Slot(tile,1,17,49)); //Water Out
        addSlotToContainer(new Slot(tile,2,143,49)); //Output Crystals
        for(int i = 0; i< 6;i++)
        {
            addSlotToContainer(new Slot(tile,2,-18,18*i)); //THESE ARE THE DUMMY SLOTS
        }
        
        for (j = 0; j < 3; ++j)
        {
            for (k = 0; k < 9; ++k)
            {
                this.addSlotToContainer(new Slot(player, k + j * 9 + 9, 8 + k * 18, 84 + j * 18));
            }
        }

        for (j = 0; j < 9; ++j)
        {
            this.addSlotToContainer(new Slot(player, j, 8 + j * 18, 142));
        }

at which point interacting with the player's inventory was exactly as it should be (or is as far as I know, I've yet to experience a problem with it).

 

though the interaction with my tile's inventory is still quite wonkey, though I expecty that is an issue with the IInventory functions' implementations

 

EDIT: I've just found something very strange, I appear to be able to craft using my tile's inventory, in that slot 0 of the tile is the output and slots 1 and 2 are horisontally adjacent slots in the craft matrix, despite the fact I have no crafting logic stuff in it... (though the 'output' slot doesn't show the crafted item, but clicking on it does cause it to craft)

 

EDIT2: I thought I'd post my IGuiHandler's code since it's relevant here:

 

    @Override
    public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z)
    {
        TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
        if(tileEntity instanceof TileCrystaliser){
            return new ContainerCrystaliser((TileCrystaliser) tileEntity, player.inventory);
        }
        return null;
    }
    
    @Override
    public Object getClientGuiElement(int id, EntityPlayer player, World world, int x, int y, int z) 
    {
        TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
        if(tileEntity instanceof TileCrystaliser){
            return new GuiCrystaliser(player.inventory, (TileCrystaliser) tileEntity);
        }
        return null;

    }

 

EDIT3:

after noticing the ability to use the slots to craft I decided to do a test and expant the tile's inventory size to nine items and add a slot to the container for each of them, and I found that clicking on four of those slots drops the current worn armour, which makes me think that the server thinks that the player has their normal inventory open while the client has the tile's inventory open.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • I host a modded Minecraft server for my friends on 1.16.5 (forge version 36.1.16) and I was wondering, is there any way to configure what color INFO, WARN, and ERROR messages display as in the terminal? The green for all INFO messages is irritating to read, especially when I am checking the console remotely. I have seen other users discuss how to make the console display the ANSI color codes, when it was disabled by default in Windows. I am running my server on Ubuntu Server 24.0.2 LTS and use SSH to view the console. I have looked online for any configuration option and have checked the server files, but cannot seem to find an answer.
    • I fixed the problem! It was just a simple mod that I added that was 1.12 and not 1.16 lol, I removed it and now its working like a charm. Thanks for the help btw!
    • I need to trigger a player's attack action. I copied the net.minecraft.client.Minecraft.clickMouse() method from the sources and changed it a bit, removing everything about the leftClickCounter private property:   public static void callAttackAction() { Minecraft instance = Minecraft.getMinecraft(); if (!instance.player.isRowingBoat()) { switch (instance.objectMouseOver.typeOfHit) { case ENTITY: if (instance.objectMouseOver.entityHit != null && instance.playerController != null) { try { instance.playerController.attackEntity(instance.player, instance.objectMouseOver.entityHit); } catch (Exception e) { e.printStackTrace(); } } break; case BLOCK: BlockPos blockpos = instance.objectMouseOver.getBlockPos(); if (!instance.world.isAirBlock(blockpos)) { instance.playerController.clickBlock(blockpos, instance.objectMouseOver.sideHit); break; } case MISS: instance.player.resetCooldown(); net.minecraftforge.common.ForgeHooks.onEmptyLeftClick(instance.player); } instance.player.swingArm(EnumHand.MAIN_HAND); } } It works, but sometimes times this error occurs on line 168 in the attackEntity method: [Netty Client IO #1/ERROR] [FML]: NetworkDispatcher exception io.netty.handler.codec.EncoderException: java.lang.NullPointerException at io.netty.handler.codec.MessageToByteEncoder.write(MessageToByteEncoder.java:125) ~[MessageToByteEncoder.class:4.1.9.Final] at io.netty.channel.AbstractChannelHandlerContext.invokeWrite0(AbstractChannelHandlerContext.java:738) ~[AbstractChannelHandlerContext.class:4.1.9.Final] at io.netty.channel.AbstractChannelHandlerContext.invokeWrite(AbstractChannelHandlerContext.java:730) ~[AbstractChannelHandlerContext.class:4.1.9.Final] at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:816) ~[AbstractChannelHandlerContext.class:4.1.9.Final] at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:723) ~[AbstractChannelHandlerContext.class:4.1.9.Final] at net.minecraftforge.fml.common.network.handshake.NetworkDispatcher.write(NetworkDispatcher.java:548) ~[NetworkDispatcher.class:?] at io.netty.channel.AbstractChannelHandlerContext.invokeWrite0(AbstractChannelHandlerContext.java:738) ~[AbstractChannelHandlerContext.class:4.1.9.Final] at io.netty.channel.AbstractChannelHandlerContext.invokeWriteAndFlush(AbstractChannelHandlerContext.java:801) ~[AbstractChannelHandlerContext.class:4.1.9.Final] at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:814) ~[AbstractChannelHandlerContext.class:4.1.9.Final] at io.netty.channel.AbstractChannelHandlerContext.writeAndFlush(AbstractChannelHandlerContext.java:794) ~[AbstractChannelHandlerContext.class:4.1.9.Final] at io.netty.channel.AbstractChannelHandlerContext.writeAndFlush(AbstractChannelHandlerContext.java:831) ~[AbstractChannelHandlerContext.class:4.1.9.Final] at io.netty.channel.DefaultChannelPipeline.writeAndFlush(DefaultChannelPipeline.java:1032) ~[DefaultChannelPipeline.class:4.1.9.Final] at io.netty.channel.AbstractChannel.writeAndFlush(AbstractChannel.java:296) ~[AbstractChannel.class:4.1.9.Final] at net.minecraft.network.NetworkManager$4.run(NetworkManager.java:245) [NetworkManager$4.class:?] at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163) [AbstractEventExecutor.class:4.1.9.Final] at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:403) [SingleThreadEventExecutor.class:4.1.9.Final] at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:442) [NioEventLoop.class:4.1.9.Final] at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:858) [SingleThreadEventExecutor$5.class:4.1.9.Final] at java.lang.Thread.run(Thread.java:748) [?:1.8.0_281] Caused by: java.lang.NullPointerException at net.minecraft.network.play.client.CPacketPlayerDigging.writePacketData(CPacketPlayerDigging.java:41) ~[CPacketPlayerDigging.class:?] at net.minecraft.network.NettyPacketEncoder.encode(NettyPacketEncoder.java:51) ~[NettyPacketEncoder.class:?] at net.minecraft.network.NettyPacketEncoder.encode(NettyPacketEncoder.java:12) ~[NettyPacketEncoder.class:?] at io.netty.handler.codec.MessageToByteEncoder.write(MessageToByteEncoder.java:107) ~[MessageToByteEncoder.class:4.1.9.Final] How can I fix it?
    • I just realized you are using a non-supported version of Forge based on the parameters of the render method as they are pre-1.15. The only versions supported are those in the blue banner at the top of the screen. Please update to one of the above versions to receive more support.
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.