Jump to content

[1.8] Chest contents not being placed


JaredBGreat

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);
		break;
	case 1:
		fillChest(contents, LootType.GEAR, random);
		break;
	}
}

 

	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.

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.



×
×
  • Create New...

Important Information

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