Jump to content

Changing Block In World


Cephrus

Recommended Posts

I'm currently using the method World.setBlockState() to change blocks in the world.

 

However, these blocks disappear when you right click or reload the world.

I'm guessing this has something to do with server/client synchronization, but I don't know for sure.

 

I am using a Server-side Tickhandler.

Link to comment
Share on other sites

My Tick event:

@SubscribeEvent
public void onServerTick(TickEvent.ServerTickEvent event)
{
	EAPlugin.internalTick();

	for(EAPluginContainer plugin : EALoader.instance().loadedPlugins)
	{
		plugin.plugin.onTick();
	}
}

 

And the code that changes blocks:

@Override
public void onTick()
{
	if(day <= 4) phase = day;
	else phase = 4;

	try
	{
		minecraft.theWorld.setBlockState(new BlockPos(minecraft.thePlayer.posX, minecraft.thePlayer.posY, minecraft.thePlayer.posZ), Blocks.dirt.getDefaultState());
	}
	catch(Exception e)
	{
		System.err.println("Errorino");
	}

	Random random = new Random(1337);
	int randX = random.nextInt();
	int randZ = random.nextInt();
	int randY = EAToolkit.getTopBlock(minecraft.theWorld, randX, randZ);

	try
	{
		if(phase == 1)
		{
			blockBuffer++;

			if(blockBuffer >= 20)
			{
				Block block = minecraft.theWorld.getChunkFromChunkCoords(randX, randZ).getBlock(randX, randY, randZ);

				if(block == Blocks.grass)
				{
					minecraft.theWorld.setBlockState(new BlockPos(randX, randY, randZ), Blocks.dirt.getDefaultState());
				}

				if(block == Blocks.leaves)
				{
					minecraft.theWorld.setBlockState(new BlockPos(randX, randY, randZ), Blocks.air.getDefaultState());
				}
			}
		}
	}
	catch(Exception e)
	{
		e.printStackTrace();
	}
}

 

The first setBlockState call is for testing purposes, and right clicking the dirt that is created makes it disappear, breaking it in survival doesn't drop anything but still makes the sound, and world reload removes all dirt and resets my spawn.

Link to comment
Share on other sites

Okay, so basically - this code will crash on Dedicated server.

 

You can't access Minecraft.class inside ANY server methods, including ticks.

You are accesting MC.theWorld - this is single, one and only client-sided world (the one your client player is in).

 

Server has x worlds. You need to pick one or use WorldTickEvent to launch tick for all worlds (every world ticks separately).

 

Note: TickEvents have Phases - START and END! Pick one or code will run twice.

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

I have started using WorldServer instead of World for my tickhandler, but MinecraftServer.worldServers has nothing in it.

 

It returns ArrayIndexOutOfBounds for whatever number I put in, and if I use a for() loop to get the first WorldServer I can it always uses the default null variable.

Link to comment
Share on other sites

In WorldTIckEvent you don't need any of that. This event is launched ONLY ON SERVER SIDE for EVERY world there is.

event.world - will give you direct access.

 

Edit: Just a note: "event is launched ONLY ON SERVER SIDE" might have changed lately, just make sure. (I don't know really, it might also work for client)

1.7.10 is no longer supported by forge, you are on your own.

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.