Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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.

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?

  • 2 months later...
  • Author

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.

In your place chest method, your isProtected method is redundant since it passes over to the placeBlock method which has the check. That's not why your problem is occurring, but I thought it's still worth pointing out.

  • Author

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.

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.