Topic category: User side tutorials
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!
Put this to custom code section :)
I don't know why there is no more a way to submit Custom Code. Just see it for yourself...
YAY! Thank you! My mod was needing it!
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)
@#4 Try to add it at the end of the structure generation
Advanced? pfff its just copying and pasting :v
@#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));
}
And How to not add items in the chest in every slot? :/
Excuse me but is this code still work at MCreator 1.7.9
How would I go about adding custom NBT for items (ie. enchanted books)?
That "world.setBlockState()" never shows up for me. Where in the code should I be able to find it?