Jump to content

[1.11] Trouble creating TileEntity with inventory.


Awesome_Spider

Recommended Posts

Ok. So I started to create a tile entity (and so far my mod in general) using ShadowFacts's tutorials for 1.11.

 

Then I decided I want to eventually include a GUI with my tile entity which this tutorial doesn't provide yet. So I used this tutorial for 1.8. This seems to outdated now, as it uses methods and fields in the ItemStack class which seem to not exist anymore or have been moved. These include ItemStack.stackSize and ItemStack.loadItemStackFromNBT().

 

I'm not sure what to do now. The only tutorial I have found for 1.10/1.11 is ShadowFacts's, and these tutorials don't include GUIs yet, which is my end goal. What has changed in Inventories/GUIs from 1.8 to 1.11? Which tutorial should I follow for these?

 

Btw, ShadowFacts's tutorials have been really great so far. I highly recommend them.

Link to comment
Share on other sites

Oh ya. I almost forgot. The code:

[spoiler=The code]My TileEntity class:

public class TileEntitySteamEngine extends TileEntity implements IInventory {

    private ItemStack[] inventory = new ItemStack[this.getSizeInventory()];
    private String customName;

    @Override
    public String getName() {
        return this.hasCustomName() ? this.customName : "container.tile_entity_steam_engine";
    }

    public String getCustomName() {
        return this.customName;
    }

    public void setCustomName(String customName) {
        this.customName = customName;
    }

    @Override
    public boolean hasCustomName() {
        return this.customName != null && !this.customName.equals("");;
    }

    @Override
    public IChatComponent getDisplayName() {
        return this.hasCustomName() ? new ChatComponentText(this.getName()) : new ChatComponentTranslation(this.getName());
    }

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

    @Override
    public ItemStack getStackInSlot(int index) {
        if (index < 0 || index >= this.getSizeInventory())
            return null;
        return this.inventory[index];
    }

    @Override
    public ItemStack decrStackSize(int index, int count) {
        if (this.getStackInSlot(index) != null) {
            ItemStack itemstack;

            if (this.getStackInSlot(index).stackSize <= count) {
                itemstack = this.getStackInSlot(index);
                this.setInventorySlotContents(index, null);
                this.markDirty();
                return itemstack;
            } else {
                itemstack = this.getStackInSlot(index).splitStack(count);

                if (this.getStackInSlot(index).stackSize <= 0) {
                    this.setInventorySlotContents(index, null);
                } else {
                    //Just to show that changes happened
                    this.setInventorySlotContents(index, this.getStackInSlot(index));
                }

                this.markDirty();
                return itemstack;
            }
        } else {
            return null;
        }
    }

    @Override
    public ItemStack getStackInSlotOnClosing(int index) {
        ItemStack stack = this.getStackInSlot(index);
        this.setInventorySlotContents(index, null);
        return stack;
    }

    @Override
    public void setInventorySlotContents(int index, ItemStack stack) {
        if (index < 0 || index >= this.getSizeInventory())
            return;

        if (stack != null && stack.stackSize > this.getInventoryStackLimit())
            stack.stackSize = this.getInventoryStackLimit();

        if (stack != null && stack.stackSize == 0)
            stack = null;

        this.inventory[index] = stack;
        this.markDirty();
    }

    @Override
    public int getInventoryStackLimit() {
        return 64;
    }

    @Override
    public boolean isUseableByPlayer(EntityPlayer player) {
        return this.worldObj.getTileEntity(this.getPos()) == this && player.getDistanceSq(this.pos.add(0.5, 0.5, 0.5)) <= 64;
    }

    @Override
    public void openInventory(EntityPlayer player) {
    }

    @Override
    public void closeInventory(EntityPlayer player) {
    }

    @Override
    public boolean isItemValidForSlot(int index, ItemStack stack) {
        return true;
    }

    @Override
    public int getField(int id) {
        return 0;
    }

    @Override
    public void setField(int id, int value) {
    }

    @Override
    public int getFieldCount() {
        return 0;
    }

    @Override
    public void clear() {
        for (int i = 0; i < this.getSizeInventory(); i++)
            this.setInventorySlotContents(i, null);
    }

    @Override
    public NBTTagCompound writeToNBT(NBTTagCompound nbt) {
        NBTTagList list = new NBTTagList();
        for (int i = 0; i < this.getSizeInventory(); ++i) {
            if (this.getStackInSlot(i) != null) {
                NBTTagCompound stackTag = new NBTTagCompound();
                stackTag.setByte("Slot", (byte) i);
                this.getStackInSlot(i).writeToNBT(stackTag);
                list.appendTag(stackTag);
            }
        }
        nbt.setTag("Items", list);

        if (this.hasCustomName()) {
            nbt.setString("CustomName", this.getCustomName());
        }

        return super.writeToNBT(nbt);
    }


    @Override
    public void readFromNBT(NBTTagCompound nbt) {
        super.readFromNBT(nbt);

        NBTTagList list = nbt.getTagList("Items", 10);
        for (int i = 0; i < list.tagCount(); ++i) {
            NBTTagCompound stackTag = list.getCompoundTagAt(i);
            int slot = stackTag.getByte("Slot") & 255;
            this.setInventorySlotContents(slot, ItemStack.loadItemStackFromNBT(stackTag));
        }
    }
}

 

Link to comment
Share on other sites

Am I supposed to use ItemStackHandler instead?

 

Yes.

 

That said, these:

ItemStack.stackSize and ItemStack.loadItemStackFromNBT()

Got tweaked a bit.

 

StackSize is now modified through getter and setter methods and loadItemStackFromNBT was renamed.

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

So, is the method to get the stack size this method: func_190916_E()? It is the only method I can see that returns stackSize.

 

That's the method, yes. It's been deobfuscated to

getCount

in newer mappings.

 

I suggest you update your mappings to the latest snapshot from the MCPBot website so this and other methods have deobfuscated names.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

I have also found Shadowfacts' tutorials to be very helpful. There's no more up-to-date tutorial on containers with a GUI that I have been able to find. I created a tile entity with inventory and a GUI in 1.8.9 and recently updated to 1.10.2. I think I used BedrockMiner's tutorial that you linked to, and it worked great back then. Everything worked fine after updating to 1.10.2, other than a few minor tweaks to methods like you're seeing. The update to using ItemStackHandlers wasn't strictly necessary, but I went ahead since that seems to be the direction of the future. It was fairly painful to me, as you can see if you look up some of my more recent threads here on the forum. You can find a lot of my source code in those threads, which may also be helpful.

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.

Announcements



×
×
  • Create New...

Important Information

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