Jump to content

Recommended Posts

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.

Posted

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

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.

Posted

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.

Posted

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

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

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • When I first heard about Bitcoin back in 2018, I was skeptical. The idea of a decentralized, digital currency seemed too good to be true. But I was intrigued as I learned more about the technology behind it and its potential. I started small, investing just a few hundred dollars, dipping my toes into the cryptocurrency waters. At first, it was exhilarating to watch the value of my investment grow exponentially. I felt like I was part of the future, an early adopter of this revolutionary new asset. But that euphoria was short-lived. One day, I logged into my digital wallet only to find it empty - my Bitcoin had vanished without a trace. It turned out that the online exchange I had trusted had been hacked, and my funds were stolen. I was devastated, both financially and emotionally. All the potential I had seen in Bitcoin was tainted by the harsh reality that with decentralization came a lack of regulation and oversight. My hard-earned money was gone, lost to the ether of the digital world. This experience taught me a painful lesson about the price of trust in the uncharted territory of cryptocurrency. While the technology holds incredible promise, the risks can be catastrophic if you don't approach it with extreme caution. My Bitcoin investment gamble had failed, and I was left to pick up the pieces, wiser but poorer for having placed my faith in the wrong hands. My sincere appreciation goes to MUYERN TRUST HACKER. You are my hero in recovering my lost funds. Send a direct m a i l ( muyerntrusted ( @ ) mail-me ( . )c o m ) or message on whats app : + 1 ( 4-4-0 ) ( 3 -3 -5 ) ( 0-2-0-5 )
    • You could try posting a log (if there is no log at all, it may be the launcher you are using, the FAQ may have info on how to enable the log) as described in the FAQ, however this will probably need to be reported to/remedied by the mod author.
    • So me and a couple of friends are playing with a shitpost mod pack and one of the mods in the pack is corail tombstone and for some reason there is a problem with it, where on death to fire the player will get kicked out of the server and the tombstone will not spawn basically deleting an entire inventory, it doesn't matter what type of fire it is, whether it's from vanilla fire/lava, or from modded fire like ice&fire/lycanites and it's common enough to where everyone on the server has experienced at least once or twice and it doesn't give any crash log. a solution to this would be much appreciated thank you!
    • It is 1.12.2 - I have no idea if there is a 1.12 pack
  • Topics

  • Who's Online (See full list)

    • There are no registered users currently online
×
×
  • Create New...

Important Information

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