Jump to content

[Solved][1.9] Blocks placed using setBlockState disappear on load


vipuri

Recommended Posts

I'm trying to make a simple block that just makes a pillar of itself when used. Everything is working when I run it, however problems arise when I save the world and reload it. All of the new blocks placed by the block disappear when the world is reloaded. I haven't been able to find much information on this and I'm relatively new to Minecraft modding and Java my background is in C/C++ for research and robotics.

        @Override
public boolean onBlockActivated(
            World world,
            BlockPos pos,
            IBlockState state,
            EntityPlayer player,
            EnumHand hand,
            @Nullable ItemStack heldItem,
            EnumFacing side,
            float hitX,
            float hitY,
            float hitZ) {
	if (!world.isRemote){
		return false;
	}
	Random rand = new Random();
	IBlockState block = world.getBlockState(pos);
	for(int i = 0; i < 7; i++){
		world.setBlockState(pos.add(0, i, 0), block);
	}
	for(int k = 0; k < 20; k++){
		int x, z;
		x= rand.nextInt(16)-8;
		z= rand.nextInt(16)-8;
		EntityLightningBolt lightning = new EntityLightningBolt(world, pos.getX()+x, pos.getY(), pos.getZ()+z, false);
		world.addWeatherEffect(lightning);
	}
	return true;
}

 

I'm working by deconstructing examples but I seem to be missing something. Any advice is appreciated, thanks in advance.

Link to comment
Share on other sites

World#isRemote

is

true

on the client and

false

on the server, so you're doing everything on the client instead of the server. The server is in charge of the game state, so the changes you make disappear when the client next synchronises with the server.

 

You may want to use the

World#rand

field instead of creating your own

Random

each time.

 

Similarly, consider using the specialised methods like

BlockPos#up

instead of the more general

BlockPos#add

when only changing one coordinate of a

BlockPos

. There's no real performance difference, but your code will be a bit clearer.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

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.

Announcements



×
×
  • Create New...

Important Information

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