Jump to content

[1.8] Chest contents not being placed


Recommended Posts

I've discovered a problem with my mod that I don't quite understand.  The following code places loot items in chests, and almost the same code works in Minecraft 1.5, 1.6, and 1.7 it work consistently:


	public void place(World world, int x, int y, int z, Random random) {
	BlockPos pos = new BlockPos(x, y, z);
	level += random.nextInt(2);
	if(level >= LootCategory.LEVELS) level = LootCategory.LEVELS - 1;
	DBlock.placeChest(world, x, y, z);
	if(world.getChunkFromChunkCoords(x / 16, z / 16).getBlock(pos) != DBlock.chest) return;
	TileEntityChest contents = (TileEntityChest)world.getTileEntity(pos);
	if(ConfigHandler.vanillaLoot && (!ConfigHandler.stingyLoot || random.nextBoolean())) 
		vanillaChest(contents, random);
	int which = random.nextInt(2);
	switch (which) {
	case 0:
		fillChest(contents, LootType.HEAL, random);
	case 1:
		fillChest(contents, LootType.GEAR, random);


	protected void fillChest(TileEntityChest chest, LootType kind, Random random) {		
	int num;
	if(ConfigHandler.stingyLoot) num = random.nextInt(2 + (level / 2)) + 1;
	else num = random.nextInt(3 + (level / 2)) + 2;
	for(int i = 0; i < num; i++) {
		ItemStack treasure = LootCategory.getLoot(kind, level, random).getStack(random);
		if(treasure != null) chest.setInventorySlotContents(random.nextInt(27), treasure);
	if(!ConfigHandler.vanillaLoot) {
		ItemStack treasure = LootCategory.getLoot(LootType.HEAL, level, random).getStack(random);
		if(treasure != null) chest.setInventorySlotContents(random.nextInt(27), treasure);


Again, almost the same code worked consistently prior to 1.8.  However, in 1.8 it works inconsistently, producing long runs of successfully filling chests mixed with long runs (over several dungeons / many chunks, very many chests) of leaving chests empty.


I have found the removing the line "if(world.getChunkFromChunkCoords(x / 16, z / 16).getBlock(pos) != DBlock.chest) return;" leads to crashes with a null pointer exception.  My best guess is that concurrency might be involved, perhaps threads involved in placing blocks lagging behind my code so that the chest isn't there yet when it tries to add loot (or at least comes to the failsafe code) -- but that is purely an educated guess.


Does anyone have any other idea of an explanation?  Or a fix that doesn't involve radical changes (I have thought of spitting block and TileEntity placement into separate iterations? -- or, if its allowed, creating a separate thread that can sleep?)  If so I'd be very grateful to hear any relevant info or suggestions.

Developer of Doomlike Dungeons.

Link to comment
Share on other sites

When in the world gen process is your 'place' method being called? I've generated lots of chests and none of them have had this problem (at least that I've found / heard so far).


Aside: why are you using world.getChunkFromChunkCoords to check the block at the position instead of the world method directly? I.e. world.getBlockState(pos).getBlock().


Speaking of block states, can you show your DBlock.placeChest method? Perhaps something is going awry in there?

Link to comment
Share on other sites

  • 2 months later...

Thanks.  Sorry for the long delay, I've been busy with other things and forgot to check back here.  I guess I was using the get chunk method because of adapting from older code.  I was trying to update and didn't know all the methods that take "pos" as a parameter.


The chest placing code is as follows:


public static final Block spawner = (Block)Block.getBlockFromName("mob_spawner");
public static final Block chest   = (Block)Block.getBlockFromName("chest");


public static void placeBlock(World world, int x, int y, int z, Block block) {
	// This wrapper is a protection against possible changes in block representation,
	// e.g., abandoning the ID system, allowing any needed changes to be made here
	// instead of elsewhere. 
	if(isProtectedBlock(world, x, y, z)) return;
	world.setBlockState(new BlockPos(x, y, z), block.getDefaultState());


public static void placeChest(World world, int x, int y, int z) {
	if(!isProtectedBlock(world, x, y, z))
		placeBlock(world, x, y, z, chest);		


I didn't originally show the code for placing the chest block because the chest itself was always there, it was only the contents that were missing.


Then, for all I know this was a Forge bug that might already have been fixed.  I play mostly 1.7 and 1.6 (for MineFantasy), and only used this with 1.8 enough for some general testing, so I wouldn't know if its suddenly works now.

Developer of Doomlike Dungeons.

Link to comment
Share on other sites

Well, placeChest() would have originally called world.setBlock() directly, and may be different in different versions (I maintains versions back to 1.6.4 and have started using git to port bit directly).  I not that worried about minor optimizations until I'm sure it fundamentally works though, so I haven't worried about little things like this for now.

Developer of Doomlike Dungeons.

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.

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

    • There's a scheme I got into where they promised to trade Bitcoin for me and take a cut as a commission. Seemed like a good idea at the time. But then, things went south real fast. They ended up transferring   $190,000 worth of my Bitcoin. I was devastated and felt completely helpless. That's when I stumbled upon the Wizard Web Recovery Tool. It was like a beacon of hope amid chaos. With this tool, I could finally start digging into what went wrong and hopefully get my Bitcoin back. Using Wizard Web was surprisingly easy. I just had to plug in some details about my Bitcoin account and let it do its thing. It started scanning the internet, looking for any clues about what happened to my Bitcoin. It felt like having a detective on my side, searching for answers. And guess what? Wizard Web found some leads. It uncovered evidence of the scheme's shady dealings and helped me track down the people responsible for losing my Bitcoin. Armed with this information, I took the case to court. After a long and hard-fought legal battle, the court ruled in my favor. The perpetrators were held accountable for their actions and faced criminal charges for their involvement in the scheme. It was a victory not just for me, but for anyone who's been taken advantage of by these kinds of scams. Thanks to Wizard Web Recovery, I was able to get justice and reclaim what was rightfully mine. It showed me that even in the face of adversity, there's always a way to fight back. And with the right tools and determination, anything is possible.   The following is the contact information for Wizard Web Recovery.   Email: wizard web recovery((@))programmer . net
    • Hello, good morning. I know some programming and I'm interested in mod creation. That's why I've decided to follow a tutorial guide on YouTube by TurtyWurty. https://www.youtube.com/watch?v=DhoX9cmAZqA&t=160s&ab_channel=TurtyWurty I've followed the tutorial perfectly. The problem is that when checking the food, the texture doesn't load for me. However, everything seems fine no matter how much I check. I'm sure it's something trivial, the problem is that I can't find it. Could you help me solve it, please? I leave a zip of my file so you can edit it freely. forge-1.20-Civicraft.rar
    • If you have nvidia graphics, it's important that you make sure Minecraft (and anything Minecraft-related) is set to prefer high performance graphics first. If you only update your AMD drivers it might fix the issue but cause severe performance loss vs Vanilla as it'll be running on the integrated graphics instead of dedicated nvidia graphics
    • Update your drivers: https://www.amd.com/en/support/downloads/drivers.html/processors/ryzen/ryzen-5000-series/amd-ryzen-5-5600g.html
  • Topics

  • Create New...

Important Information

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