Jump to content

Help with TileEntity/Container communication?


Big_Bad_E

Recommended Posts

I am trying to make a Tile Entity that stores one item and its amount.

I have a Container which has one slot, that is where you should put the item. The item goes up to the 32 bit integer limit.

My problems are:

Shift clicking is weird, slots 9-10 just go to each other 2-8 goes to 19-26, slot 27-35 and 1 crashes game with this error:

Caused by: java.lang.NullPointerException
	at net.minecraft.inventory.Container.slotClick(Container.java:271) ~[Container.class:?]
	at net.minecraft.client.multiplayer.PlayerControllerMP.windowClick(PlayerControllerMP.java:610) ~[PlayerControllerMP.class:?]
	at net.minecraft.client.gui.inventory.GuiContainer.handleMouseClick(GuiContainer.java:693) ~[GuiContainer.class:?]
	at net.minecraft.client.gui.inventory.GuiContainer.mouseClicked(GuiContainer.java:430) ~[GuiContainer.class:?]
	at net.minecraft.client.gui.GuiScreen.handleMouseInput(GuiScreen.java:611) ~[GuiScreen.class:?]
	at net.minecraft.client.gui.GuiScreen.handleInput(GuiScreen.java:576) ~[GuiScreen.class:?]
	at net.minecraft.client.Minecraft.runTick(Minecraft.java:1884) ~[Minecraft.class:?]
	... 15 more

I'm just gonna post a video cause it's easier to explain.

(Ima add textures/models later ofc)

Container:

public class QuantumContainer extends Container {

    QuantumContainer(InventoryPlayer inventory, TEQuantumStorage tile) {
        SlotItemHandler slot = new SlotQuantum(new QuantumHandler(tile), 36, 80, 40);
        addSlotToContainer(slot);
        slot.putStack(new ItemStack(tile.getItem(), tile.getAmount()));
        int i;
        for (i = 0; i < 3; ++i) {
            for (int j = 0; j < 9; ++j) {
                addSlotToContainer(new Slot(inventory, j + i * 9 + 9,
                        8 + j * 18, 84 + i * 18));
            }
        }
        // add hotbar slots
        for (i = 0; i < 9; ++i) {
            addSlotToContainer(new Slot(inventory, i, 8 + i * 18,
                    142));
        }
    }

    @Override
    @ParametersAreNonnullByDefault
    public boolean canInteractWith(EntityPlayer playerIn) {
        return true;
    }

    @Override
    @MethodsReturnNonnullByDefault
    public ItemStack transferStackInSlot(EntityPlayer playerIn, int slotIndex) {
        ItemStack itemStack1 = null;
        Slot slot = inventorySlots.get(slotIndex);

        if (slot != null && slot.getHasStack()) {
            ItemStack itemStack2 = slot.getStack();
            itemStack1 = itemStack2.copy();
            // player inventory slots
            if (slotIndex >= 9 && slotIndex < 36) {
                if (!mergeItemStack(itemStack2, 9, 35, false))
                    return null;
                // hotbar slots
            } else if (slotIndex < 9 && !mergeItemStack(itemStack2, 2, 28, false)) {
                if (!mergeItemStack(itemStack2, 0, 8, false))
                    return null;
            } else if (!mergeItemStack(itemStack2, 0, 37, false)) {
                return null;
            }
            if (itemStack2.getCount() == 0) {
                slot.putStack(ItemStack.EMPTY);
            } else {
                slot.onSlotChanged();
            }
            if (itemStack2.getCount() == itemStack1.getCount()) {
                return null;
            }
        }
        return itemStack1;
    }
}

QuantumHandler:

private ItemStack stack;
    private TEQuantumStorage tile;

    public QuantumHandler(TEQuantumStorage tile) {
        this.tile = tile;
    }

    @Override
    public int getSlots() {
        return 1;
    }

    @Nonnull
    @Override
    public ItemStack getStackInSlot(int slot) {
        return stack;
    }

    @Nonnull
    @Override
    public ItemStack insertItem(int slot, @Nonnull ItemStack stack, boolean simulate) {
        tile.setItem(stack.getItem());
        tile.addAmount(stack.getCount());
        this.stack = stack;
        this.stack.setCount(tile.getAmount());
        tile.markDirty();
        ItemStack stack1 = stack.copy();
        if(stack.getCount() == 1) {
            stack1.setCount(stack1.getCount() - 1);
            return stack1;
        } else
            return ItemStack.EMPTY;
    }

    @Nonnull
    @Override
    public ItemStack extractItem(int slot, int amount, boolean simulate) {
        ItemStack stack = this.stack.copy();
        if (stack.getCount() > 64) {
            stack.setCount(64);
        }
        if(amount == 1 && tile.getAmount() > 0)
            stack.setCount(1);
        else
            if(tile.getAmount() < 64)
                stack.setCount(tile.getAmount());
        tile.addAmount(-amount);
        this.stack.setCount(this.stack.getCount() - amount);
        tile.markDirty();
        return stack;
    }

    @Override
    public int getSlotLimit(int slot) {
        return 2147483647;
    }

    @Override
    public void setStackInSlot(int slot, @Nonnull ItemStack stack) {
        this.stack = stack;
        tile.markDirty();
    }

SlotQantum:

public class SlotQuantum extends SlotItemHandler {
    public SlotQuantum(IItemHandler handler, int index, int xPosition, int yPosition) {
        super(handler, index, xPosition, yPosition);
    }

    @Override
    public int getSlotStackLimit() {
        return 2147483647;
    }
}

Tile Entity:

public class TEQuantumStorage extends TileEntity {

    private Item item;
    private int amount;

    @Override
    public void readFromNBT(NBTTagCompound nbt) {
        super.readFromNBT(nbt);
        this.item = Item.getItemById(nbt.getInteger("item"));
        this.amount = nbt.getInteger("amount");
    }


    @Override
    public NBTTagCompound writeToNBT(NBTTagCompound nbt) {
        super.writeToNBT(nbt);
        nbt.setInteger("item", Item.getIdFromItem(item));
        nbt.setInteger("amount", amount);
        return nbt;
    }

    public int getAmount() {
        return amount;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }

    public Item getItem() {
        return item;
    }

    public void setItem(Item item) {
        this.item = item;
    }
}

Sorry for code dump, but I've been at this for hours and have no idea what I'm doing wrong.

Edited by Big_Bad_E
Link to comment
Share on other sites

Override getMaxStack size or something?

have a look at how AppliedEnergistics or another open source storage mod does it

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

14 hours ago, Cadiboo said:

Override getMaxStack size or something?

have a look at how AppliedEnergistics or another open source storage mod does it

No, I am not trying to have a super large item stack, I am trying to store it as a number in the tile entity's NBT data along with the item. Then I can use that to let the player get the item. It's literally the Deep Storage Unit.

Link to comment
Share on other sites

2 hours ago, Big_Bad_E said:

I'm just gonna bump this because Draco is online

Lol

20 hours ago, Big_Bad_E said:

GenericSlotHolder implements IInventory

Dont do this use the IItenHandler capability instead.

 

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

20 hours ago, Animefan8888 said:

Lol

Dont do this use the IItenHandler capability instead.

 

How would I use IItemHandler? I can't set slot's inventory to it because it doesn't implement IInventory.

It looks nice but I can't use it.

 

Also back to the original question what's wrong with my code?

Link to comment
Share on other sites

22 hours ago, Big_Bad_E said:

I'm just gonna bump this because Draco is online

I will then proceed to ignore the fuck out of it because I was on an airplane, and got home at 1am after having almost missed my connecting flight in Phoenix because my first leg was 45 60 minutes late leaving and the gate attendant told me "You'll have 34 minutes between flights, you'll be fine." I had 22 minutes, minus the "doors close 10 minutes before the departure time" so 12 minutes to get from gate B4 to A25.

 

Storing large stacks? You're going to need two inventory slots, an input and an output. The output always contains a stack of size Max(available, 64). The input slot is always empty. When a stack is placed into the input and it matches the contents, destroy the stack and add its size to the TE's integer value of contents.

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.

Link to comment
Share on other sites

1 hour ago, Draco18s said:

I will then proceed to ignore the fuck out of it because I was on an airplane, and got home at 1am after having almost missed my connecting flight in Phoenix because my first leg was 45 60 minutes late leaving and the gate attendant told me "You'll have 34 minutes between flights, you'll be fine." I had 22 minutes, minus the "doors close 10 minutes before the departure time" so 12 minutes to get from gate B4 to A25.

 

Storing large stacks? You're going to need two inventory slots, an input and an output. The output always contains a stack of size Max(available, 64). The input slot is always empty. When a stack is placed into the input and it matches the contents, destroy the stack and add its size to the TE's integer value of contents.

Oh, didn't know, sorry about ur airplane problems. How come we can mass produce nuclear weapons but we cant move from point A to point B in an airplane easily.

 

Input and output is a good idea i'm going to try it.

 

Still being weird, see updated code/errors above.

Edited by Big_Bad_E
Link to comment
Share on other sites

4 hours ago, Big_Bad_E said:

How would I use IItemHandler? I can't set slot's inventory to it because it doesn't implement IInventory.

It looks nice but I can't use it.

Use SlotItemHandler instead of Slot.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

6 hours ago, Big_Bad_E said:

I just gave up, I've spent way too long working on this.

Move on to something else for a couple days, then come back to this. This should be very easy to achieve, and when you come back to it in a couple days with a new perspective, it shouldn’t take you very much time at all to do it! Good luck!

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

23 minutes ago, Cadiboo said:

Move on to something else for a couple days, then come back to this. This should be very easy to achieve, and when you come back to it in a couple days with a new perspective, it shouldn’t take you very much time at all to do it! Good luck!

My main problem is that I can't figure out what is wrong with my code, and when I use one solution, tons more pop up. Like implements IItemHandler now the input slot only takes one item which won't disappear, and I can't test output, and for some reason when I close the GUI with an item the amount gets set to -22 and it crashes. I've updated my code but I've just run into a lack of solutions. For some reason slot events aren't being called and I'm just frustrated.

 

Now that I'm thinking about it i'm just going to make a slot with a max item limit of the 32 bit integer limit.

Link to comment
Share on other sites

1 minute ago, Big_Bad_E said:

My main problem is that I can't figure out what is wrong with my code, and when I use one solution, tons more pop up. Like implements IItemHandler now the input slot only takes one item which won't disappear, and I can't test output, and for some reason when I close the GUI with an item the amount gets set to -22 and it crashes. I've updated my code but I've just run into a lack of solutions. For some reason slot events aren't being called and I'm just frustrated.

 

Now that I'm thinking about it i'm just going to make a slot with a max item limit of the 32 bit integer limit.

Use forges IItemHandler system with 2 slots input & output, when a stack goes into the input delete it and add it’s value to your tiles data. Write the code for output later

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

1 hour ago, Cadiboo said:

Use forges IItemHandler system with 2 slots input & output, when a stack goes into the input delete it and add it’s value to your tiles data. Write the code for output later

I'd rather just make a single stack. I think it would generally be easier.

Link to comment
Share on other sites

1 minute ago, Big_Bad_E said:

I'd rather just make a single stack. I think it would generally be easier.

Use an IItemHandler that has one slot and access to the TEs amount field and override insertItem and extractItem as well as getStack or getStacks(whatever that method is called in IItemHandler) and return the appropriate values and update the stack inside the IItemHandler based on the amount filed. No need for a fancy slot.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

7 minutes ago, Animefan8888 said:

Use an IItemHandler that has one slot and access to the TEs amount field and override insertItem and extractItem as well as getStack or getStacks(whatever that method is called in IItemHandler) and return the appropriate values and update the stack inside the IItemHandler based on the amount filed. No need for a fancy slot.

That's what i'm doing.

Link to comment
Share on other sites

1 hour ago, Big_Bad_E said:

That's what i'm doing.

Well considering you have not posted your updated code how was I supposed to know that or help you anymore than that.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

11 hours ago, Animefan8888 said:

Well considering you have not posted your updated code how was I supposed to know that or help you anymore than that.

I haven't updated my code cause I haven't coded it yet. And:

12 hours ago, Big_Bad_E said:

I'd rather just make a single stack. I think it would generally be easier.

 

On 8/11/2018 at 8:10 PM, Big_Bad_E said:

Will look into it.

 

I am now using SlotItemHandler, good idea with that one.

I did say what I was going to do.

Link to comment
Share on other sites

3 hours ago, Big_Bad_E said:

How do I update the Container/GUI to display the updated item?

Send a packet to the player there are multiple ways to do this, but show your code and I can tell you which on to use. 

3 hours ago, Big_Bad_E said:

How do I stop the block from being placed when I open the GUI?

Return true in onBlockActivated

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

15 minutes ago, Animefan8888 said:

Send a packet to the player there are multiple ways to do this, but show your code and I can tell you which on to use. 

Return true in onBlockActivated

My code is updated, I've never used packets with Forge so it will be fun to try :)

Link to comment
Share on other sites

Are you sure packets are the way to go?

I made some packets, and it updated the server, but not the client's slot.

 

How do I get the slot itself to update? I think there is another ItemStack list somewhere that is called to draw the itemstacks. I am gonna look through the itemstack draw method and the container close/update method cause that also updates it.

Link to comment
Share on other sites

28 minutes ago, Big_Bad_E said:

Are you sure packets are the way to go?

I made some packets, and it updated the server, but not the client's slot.

Are you sending the data to the client or to the server?

29 minutes ago, Big_Bad_E said:

How do I get the slot itself to update?

If changing the IItemHandlers stack on the client doesnt update the container/gui have your packet also update the container.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

9 minutes ago, Animefan8888 said:

Are you sending the data to the client or to the server?

If changing the IItemHandlers stack on the client doesnt update the container/gui have your packet also update the container.

The data was sent client to server.

 

My problem was the container had updated data but for some reason the GUI's instance of the data wasn't updated, so I manually just got the itemstack from the TileEntity and used it to update the GUI.

Link to comment
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.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
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

    • Update your AMD/ATI drivers - get the drivers from their website - do not update via system
    • Hola buenas, Quería saber porque me da este error apt cache successfully updated Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remount=True). /content/drive/My Drive/Minecraft-server addons config installer.log run.sh att-debug crash-reports libraries scripts 'att-debug (1)' defaultconfigs local server.jar 'att-debug (2)' 'eula (1).txt' logs server.properties banned-ips.json 'eula (2).txt' modernfix usercache.json banned-players.json eula.txt mods user_jvm_args.txt 'colabconfig (1).json' fabricloader.log ops.json whitelist.json 'colabconfig (2).json' forge-installer.jar rhino.local.properties world colabconfig.json forge.jar run.bat Yay! Openjdk17 has been successfully installed. Se esta utilizando JAVA 17 - You are using JAVA 17. Usando playit Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead (see apt-key(8)). OK Iniciando servidor... Playit.gg instalado /content/drive/My Drive/Minecraft-server/libraries/net/minecraftforge/forge/1.20.1-47.2.19/unix_args.txt 8no command provided, doing auto runWARNING: Unknown module: cpw.mods.securejarhandler specified to --add-exports WARNING: Unknown module: cpw.mods.securejarhandler specified to --add-opens WARNING: Unknown module: cpw.mods.securejarhandler specified to --add-opens Error: Could not find or load main class cpw.mods.bootstraplauncher.BootstrapLauncher Caused by: java.lang.ClassNotFoundException: cpw.mods.bootstraplauncher.BootstrapLauncher 8checking if secret key is valid8secret key valid, agent has 1 tunnels8starting up tunnel connection8tunnel running8playit (v0.15.26): 1731348046735 tunnel running, 1 tunnels registered TUNNELS are-lasting.gl.joinmc.link => 127.0.0.1:25565 (minecraft-java) 8playit (v0.15.26): 1731348050080 tunnel running, 1 tunnels registered TUNNELS are-lasting.gl.joinmc.link => 127.0.0.1:25565 (minecraft-java) 8playit (v0.15.26): 1731348053321 tunnel running, 1 tunnels registered TUNNELS are-lasting.gl.joinmc.link => 127.0.0.1:25565 (minecraft-java) 8playit (v0.15.26): 1731348056568 tunnel running, 1 tunnels registered TUNNELS are-lasting.gl.joinmc.link => 127.0.0.1:25565 (minecraft-java) 8playit (v0.15.26): 1731348059809 tunnel running, 1 tunnels registered TUNNELS are-lasting.gl.joinmc.link => 127.0.0.1:25565 (minecraft-java) 8playit (v0.15.26): 1731348063176 tunnel running, 1 tunnels registered TUNNELS are-lasting.gl.joinmc.link => 127.0.0.1:25565 (minecraft-java) 8playit (v0.15.26): 1731348066476 tunnel running, 1 tunnels registered TUNNELS are-lasting.gl.joinmc.link => 127.0.0.1:25565 (minecraft-java) Iniciando servidor... /content/drive/My Drive/Minecraft-server/libraries/net/minecraftforge/forge/1.20.1-47.2.19/unix_args.txt WARNING: Unknown module: cpw.mods.securejarhandler specified to --add-exports WARNING: Unknown module: cpw.mods.securejarhandler specified to --add-opens WARNING: Unknown module: cpw.mods.securejarhandler specified to --add-opens Error: Could not find or load main class cpw.mods.bootstraplauncher.BootstrapLauncher Caused by: java.lang.ClassNotFoundException: cpw.mods.bootstraplauncher.BootstrapLauncher  
    • I'm completely new to Forge, and what I'm trying to do is extremely simple : I want to create a block that damages the player when they break it. But when testing my mod, the world crashes every time I break my block. The error is " io.netty.handler.codec.EncoderException: Failed to encode packet 'clientbound/minecraft:damage_event' " Here is my code : public class DamagerBlock extends Block { public DamagerBlock(BlockBehaviour.Properties properties) { super(properties); } @Override public void playerDestroy(Level level, Player p, BlockPos pos, BlockState state, @Nullable BlockEntity bentity, ItemStack stack) { p.awardStat(Stats.BLOCK_MINED.get(this)); p.causeFoodExhaustion(0.005F); System.out.println("The player is taking damage !"); //Just to check that the method is called if (!level.isClientSide()) { p.hurt(new DamageSource(new Holder.Direct<DamageType>(new DamageType("magic", 0.F, DamageEffects.HURT))), 5); } dropResources(state, level, pos, bentity, p, stack, false); } } I think I'm actually missing a lot of knowledge about client/server communication, but I just don't know how to properly tell the client that the player takes damage...
    • https://we.tl/t-w16BtUQcxb i send you the minecraft logs folder it's not the same site you sent me because it won't let me upload the logs folder, I hope it's not a problem
    • Please read the FAQ on how to post logs.   You have client mods trying to load on a server, which will not work. The one that stuck out was sodium client extras, there may be others, they would probably be easier to find in a log post the way the FAQ describes.
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.