[Advanced] Add items to a chest (for custom structures)

Started by hacheipe399 on

Topic category: User side tutorials

Last seen on 23:51, 21. Mar 2020
Joined Jun 2013
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
[Advanced] Add items to a chest (for custom structures)

Hello! As you know, MCreator transforms each block of a schematic into a simple world.setBlockState() function. So that's the reason why your items are gone when your structure generates into Minecraft. MCreator can't read the data so only the blocks are placed. To add treasure to the chest of our structures, we need to do these things:

  • Get the Tile Entity of the chest. Because the inventory functions are only for the tile entity and not for the block.
  • Add a for loop to check each slot of the inventory and add items.

Well, let's start!

1. Create though MCreator your custom structure with a chest inside. Then in your workspace edit the code of your structure:

2. As you know, the decimal block ID of the chest is 54, so find the world.setBlockState() function with Block.getBlockbyId(54) inside.

3. Look carefully to the coordinates of the block. They're within the new BlockPos() instance of the same line. In the new structure of my mod, the chest is at i+2, j+1 and k+2.

4. We'll get the Tile Entity of that chest using this line:

TileEntity chest = world.getTileEntity(new BlockPos(i+2, j+1, k+2));

This will obtain the entity of the chest at the position of the block.

5. Add a for loop for each slot of the container. Pay attention: the chest is a special Tile Entity, it's a TileEntityChest, so you need to specify that when you're working with chest's functions. Instead of only chest, we need to write ((TileEntityChest) chest). When you're ready, start to write the loop:

for (int slot = 0; slot < ((TileEntityChest) chest).getSizeInventory(); slot++) {

((TileEntityChest) chest).setInventorySlotContents(slot, new ItemStack(Items.diamond, 1));

}

If you save, recompile and then test your Minecraft, you'll get 1 diamond on each slot. That's because:

  • setInventorySlotContents(slot, item) sets the content of a slot. The arguments are: the slot to add, the item for that slot.
  • This is a for loop which gets the size of chest's inventoy and runs for every slot in that inventory, so that function will add the same items in every slot.
  • To place an item you create a new ItemStack(item, quantity, type). Its arguments are: the item (you can look at the names in MCreator's Items Dialog when crafting a recipe), the number of items to add and the type of item. I'll explain this now:

Well I want to place an enchanted apple to my chest. The item id is Item.golden_apple. But if I put:

((TileEntityChest) chest).setInventorySlotContents(slot, new ItemStack(Items.golden_apple, 1));

I'll get a common golden apple, and not the enchanted one. So within ItemStack() we need to add a 3rd parameter, which will be the type of the apple: 0 is common, 1 is enchanted:

((TileEntityChest) chest).setInventorySlotContents(slot, new ItemStack(Items.golden_apple, 1, 1));

The 3rd parameter is useful if you need to place into your chest a specific kind of potion, wool, wooden plank, etc.

HOW TO ADD RANDOMNESS:

If you know java and think with simple logic, you can create algorithms with only one variable and some if/else if statements. Remember that your variable must be a random number:

for (int slot = 0; slot < ((TileEntityChest) chest).getSizeInventory(); slot++) {

int probability = random.nextInt(50);

if (probability > 48) {

((TileEntityChest) chest).setInventorySlotContents(slot, new ItemStack(Items.diamond, 1));

}

else if (probability == 25 || probability == 20) {

((TileEntityChest) chest).setInventorySlotContents(slot, new ItemStack(Items.rotten_flesh, 3));

}

}

nextInt(number) generates a random number from 0 to the number within brackets. You can also add a random item quantity with another variable, just replace the number parameter with the name of your variable:

for (int slot = 0; slot < ((TileEntityChest) chest).getSizeInventory(); slot++) {

int probability = random.nextInt(50);

int quantity = random.nextInt(2) + 1;

if (probability > 48) {

((TileEntityChest) chest).setInventorySlotContents(slot, new ItemStack(Items.diamond, quantity));

}

else if (probability == 25 || probability == 20) {

((TileEntityChest) chest).setInventorySlotContents(slot, new ItemStack(Items.rotten_flesh, quantity));

}

}

I implemented custom loot within chests with my last mod: Check it out:

https://mcreator.pylo.co/modification/19384/h399s-defqon1-hard-discs-mo…

Comment and rate my mod. Comment this topic if you have doubts or suggestions!

Regards!

    Last seen on 03:03, 6. Feb 2024
    Joined Apr 2014
    Points:

    User statistics:

    • Modifications:
    • Forum topics:
    • Wiki pages:
    • MCreator plugins:
    • Comments:
    Put this to custom code
    Fri, 03/04/2016 - 22:27

    Put this to custom code section :)

    Last seen on 23:51, 21. Mar 2020
    Joined Jun 2013
    Points:

    User statistics:

    • Modifications:
    • Forum topics:
    • Wiki pages:
    • MCreator plugins:
    • Comments:
    I don't know why there is no
    Sat, 03/12/2016 - 16:48

    I don't know why there is no more a way to submit Custom Code. Just see it for yourself...

    Last seen on 03:45, 14. Jun 2016
    Joined Apr 2016
    Points:

    User statistics:

    • Modifications:
    • Forum topics:
    • Wiki pages:
    • MCreator plugins:
    • Comments:
    YAY! Thank you! My mod was
    Tue, 04/26/2016 - 13:15

    YAY! Thank you! My mod was needing it!

    Last seen on 12:58, 23. Sep 2019
    Joined Aug 2014
    Points:

    User statistics:

    • Modifications:
    • Forum topics:
    • Wiki pages:
    • MCreator plugins:
    • Comments:
    I really need this thing!
    Wed, 04/27/2016 - 17:29

    I really need this thing!

    But When I wrote the code, recompile went well but when I try to create a new world, minecraft crashes :/

    My code is this: 

    if(place){
    world.setBlockState(new BlockPos(i+0, j+1, k+0), Block.getBlockById(54).getStateFromMeta(2), 3);
    TileEntity chest = world.getTileEntity(new BlockPos(i+2, j+1, k+2));
    for (int slot = 0; slot < ((TileEntityChest) chest).getSizeInventory(); slot++) {

    ((TileEntityChest) chest).setInventorySlotContents(slot, new ItemStack(Items.diamond, 1));

    }
    }

    Did I write it in the wrong line? (surely)

    Last seen on 03:45, 14. Jun 2016
    Joined Apr 2016
    Points:

    User statistics:

    • Modifications:
    • Forum topics:
    • Wiki pages:
    • MCreator plugins:
    • Comments:
    RE:I really need this thing!
    Wed, 04/27/2016 - 17:47

    @#4 Try to add it at the end of the structure generation

    Last seen on 03:45, 14. Jun 2016
    Joined Apr 2016
    Points:

    User statistics:

    • Modifications:
    • Forum topics:
    • Wiki pages:
    • MCreator plugins:
    • Comments:
    Advanced? pfff its just
    Wed, 04/27/2016 - 22:45

    Advanced? pfff its just copying and pasting :v

    Last seen on 12:58, 23. Sep 2019
    Joined Aug 2014
    Points:

    User statistics:

    • Modifications:
    • Forum topics:
    • Wiki pages:
    • MCreator plugins:
    • Comments:
    RE:RE:I really need this thing!
    Thu, 04/28/2016 - 15:00

    @#4.1 Nope...

    Recompilation error for all this string: 

    for (int slot = 0; slot < ((TileEntityChest) chest).getSizeInventory(); slot++) {

    ((TileEntityChest) chest).setInventorySlotContents(slot, new ItemStack(Items.diamond, 1));

    }

    Last seen on 12:58, 23. Sep 2019
    Joined Aug 2014
    Points:

    User statistics:

    • Modifications:
    • Forum topics:
    • Wiki pages:
    • MCreator plugins:
    • Comments:
    And How to not add items in
    Sun, 05/01/2016 - 10:24

    And How to not add items in the chest in every slot? :/

    Last seen on 13:41, 21. Jun 2018
    Joined Jan 2017
    Points:

    User statistics:

    • Modifications:
    • Forum topics:
    • Wiki pages:
    • MCreator plugins:
    • Comments:
    Excuse me but is this code…
    Tue, 06/19/2018 - 06:51

    Excuse me but is this code still work at MCreator 1.7.9

     

    Last seen on 00:30, 24. Aug 2023
    Joined May 2014
    Points:

    User statistics:

    • Modifications:
    • Forum topics:
    • Wiki pages:
    • MCreator plugins:
    • Comments:
    How would I go about adding…
    Mon, 07/16/2018 - 20:55

    How would I go about adding custom NBT for items (ie. enchanted books)?

    Last seen on 19:43, 25. Feb 2019
    Joined Feb 2019
    Points:

    User statistics:

    • Modifications:
    • Forum topics:
    • Wiki pages:
    • MCreator plugins:
    • Comments:
    That "world.setBlockState()"…
    Sun, 02/24/2019 - 22:02

    That "world.setBlockState()" never shows up for me. Where in the code should I be able to find it?