Jump to content

[SOLVED] [1.10.2] Intercepting block changes in a world


TheMasterGabriel

Recommended Posts

Hi,

I was wondering if there was a system in place that would allow me to intercept all blocks changes from the player, commands, etc. Basically, an event or something that is called when the blocks in a world are changed at all. I have looked at intercepting SPacketBlockChange in NetHandlerPlayServer#sendPacket, but the packet gets sent after the changes happen on the server-side, so manipulations I do on the packet data are reflected only on the client (and are undone when I reload the world). From some digging, the only place I can see something like this existing would be in World#setBlockState, but I'm not sure how to proceed.

Link to comment
Share on other sites

Well after intercepting the change and all the parameters match up you can just send another packet back with the new data.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

Well after intercepting the change and all the parameters match up you can just send another packet back with the new data.

 

From what I found, I don't think the server listens for block update packets, only distributes them. Would I need to make a custom packet and have the server listen and update accordingly for it?

Link to comment
Share on other sites

Well after intercepting the change and all the parameters match up you can just send another packet back with the new data.

 

From what I found, I don't think the server listens for block update packets, only distributes them. Would I need to make a custom packet and have the server listen and update accordingly for it?

Yes you wouldnhave to make your own packet and send it from the client.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

Well after intercepting the change and all the parameters match up you can just send another packet back with the new data.

 

From what I found, I don't think the server listens for block update packets, only distributes them. Would I need to make a custom packet and have the server listen and update accordingly for it?

Yes you wouldnhave to make your own packet and send it from the client.

 

Alright I'll have a go at it, Thanks

Link to comment
Share on other sites

Okay, I need to ask - what packets have anything to do with block changes. Packets are used to sync sides, not to listen to changes (even if you would listen to packet that says "this block changed" - it doesn't mean the packet would be sent with every change, thus - you miss out on changes).

 

Question here is not a matter of syncing - it's a matter of detecting change on one of sides (probably server because server saves world).

Now - Forge doesn't provide event for ALL block changes, it only gives you few hooks such as BlockBreak and PlaceBlock which are called in very specific circumstances (mining/building/explosions) - so again - not all of them.

 

Firing change block event (if such would exist) from #setBlockState would be not only expensive (not really biggest problem), but wouldn't really solve issue either - why? Because world data (blocks) can and is modified from many other places, including external (e.g: other mods).

 

So the answer to thread: No, it's impossible to track ALL block changes in world.

 

More: There is solution to this - its a core-mod. But WAIT! Don't write one (we don't need another core-mod/api)! I strongly belive SpongeAPI has such event (and MANY more) - if you don't know what that is - basically server-sided API (core-mod) that can be installed on top of forge (using all forge systems), an extension that replaced old dead-bukkit.

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

Link to comment
Share on other sites

Okay, I need to ask - what packets have anything to do with block changes. Packets are used to sync sides, not to listen to changes (even if you would listen to packet that says "this block changed" - it doesn't mean the packet would be sent with every change, thus - you miss out on changes).

 

Question here is not a matter of syncing - it's a matter of detecting change on one of sides (probably server because server saves world).

Now - Forge doesn't provide event for ALL block changes, it only gives you few hooks such as BlockBreak and PlaceBlock which are called in very specific circumstances (mining/building/explosions) - so again - not all of them.

 

Firing change block event (if such would exist) from #setBlockState would be not only expensive (not really biggest problem), but wouldn't really solve issue either - why? Because world data (blocks) can and is modified from many other places, including external (e.g: other mods).

 

So the answer to thread: No, it's impossible to track ALL block changes in world.

 

More: There is solution to this - its a core-mod. But WAIT! Don't write one (we don't need another core-mod/api)! I strongly belive SpongeAPI has such event (and MANY more) - if you don't know what that is - basically server-sided API (core-mod) that can be installed on top of forge (using all forge systems), an extension that replaced old dead-bukkit.

 

Thanks, I've heard of Sponge before and have looked at it a few times, but haven't developed anything for it yet so I'll be sure to check it out. The only reason I asked this question was because I'm attempting to create a "mirror" dimension to the overworld, where block changes in the overworld are reflected in my new dimension as well (ie. place a furnace in overworld @ 0,0,0 and a furnace gets placed in my new dimension @ 0,0,0).

Link to comment
Share on other sites

Why not just make your mirror dimension look at the overworld data?

 

I didn't realize this was possible. How would I go about doing it? Also, I suspect that this approach won't work if I want changes to reflect in both directions (overworld affects dimension and vice versa), but I'm not sure.

 

--EDIT:

 

Also, the furnace problem was just an example. I would prefer if the blocks weren't the exact same blocks in the overworld as in the mirror dimension, by which I mean placing a furnace @ 0,0,0 places a mirrored furnace (or something along those lines) @ 0,0,0 in the mirror dimension.

Link to comment
Share on other sites

This should be possible by writing a custom chunk provider implementation that ensures the same chunks are used for both overworld and your dimension (possibly applying some minor transformations between them on the fly).

 

Thanks, although I'm a bit unclear on how to proceed. I've made a world provider for my new dimension (for lighting changes and sky color changes), but in terms of forcing the chunk provider to share chunks with the overworld I'm lost. I've tried setting my dimensions chunk generator and biome provider to the overworld's like so but that doesn't seem to do anything. Any pointers on where I should being or what I should look at would be helpful, thanks.

 

    public IChunkGenerator createChunkGenerator()
    {
        return FMLCommonHandler.instance().getMinecraftServerInstance().worldServerForDimension(0).getChunkProvider().chunkGenerator;
    }
    
    /**
     * creates a new world chunk manager for WorldProvider
     */
    protected void createBiomeProvider()
    {
        this.biomeProvider = FMLCommonHandler.instance().getMinecraftServerInstance().worldServerForDimension(0).getBiomeProvider();
    }

Link to comment
Share on other sites

What you need is not the chunk generator, but rather the chunk loader (IChunkLoader interface).

I am not really at my IDE at the moment so I cannot tell you how feasable it is to get your custom IChunkLoader interface to be used by the World though.

 

Oh gotcha. Alright, I'll take a look into it. On a side note, while I was waiting for someone to respond earlier I tried to tackle this problem the way I had originally asked about (by intercepting block packets). I managed to come up with this:

 

public class InterceptedNetHandler extends NetHandlerPlayServer
{
public InterceptedNetHandler(NetHandlerPlayServer connection)
{
	super(connection.playerEntity.getServer(), connection.netManager, connection.playerEntity);
}

    public void sendPacket(final Packet<?> packetIn)
    {
    	if(this.netManager.isChannelOpen())
    	{
    		int dimension = this.playerEntity.getEntityWorld().provider.getDimension();
        	int dimVOS = TheUnderside.THE_VEIL_OF_SHADOWS.getId();

        	if(dimension == 0 || dimension == dimVOS)
        	{
            	WorldServer world = this.playerEntity.getServer().worldServerForDimension(dimension == 0 ? dimVOS : 0);
            	boolean flag = false;
            	
        		if(packetIn instanceof SPacketChunkData)
        		{
        			SPacketChunkData spacketchunkdata = (SPacketChunkData)packetIn;

        			if(!spacketchunkdata.doChunkLoad())
        			{
        				Chunk chunk = world.getChunkFromChunkCoords(spacketchunkdata.getChunkX(), spacketchunkdata.getChunkZ());
        		        
        				chunk.fillChunk(spacketchunkdata.getReadBuffer(), spacketchunkdata.getExtractedSize(), false);
        		        world.markBlockRangeForRenderUpdate(spacketchunkdata.getChunkX() << 4, 0, spacketchunkdata.getChunkZ() << 4, (spacketchunkdata.getChunkX() << 4) + 15, 256, (spacketchunkdata.getChunkZ() << 4) + 15);
    		            chunk.resetRelightChecks();

        		        for(NBTTagCompound nbttagcompound : spacketchunkdata.getTileEntityTags())
        		        {
        		            BlockPos blockpos = new BlockPos(nbttagcompound.getInteger("x"), nbttagcompound.getInteger("y"), nbttagcompound.getInteger("z"));
        		            TileEntity tileentity = world.getTileEntity(blockpos);

        		            if (tileentity != null)
        		            {
        		                tileentity.handleUpdateTag(nbttagcompound);
        		            }
        		        }
        		        
        				flag = true;	
        			}
        		} 
        		else if(packetIn instanceof SPacketMultiBlockChange)
        		{
        			SPacketMultiBlockChange spacketmultiblockchange = (SPacketMultiBlockChange)packetIn;

        			for(SPacketMultiBlockChange.BlockUpdateData spacketmultiblockchange$blockupdatedata : spacketmultiblockchange.getChangedBlocks())
        	        {
                    	world.setBlockState(spacketmultiblockchange$blockupdatedata.getPos(), spacketmultiblockchange$blockupdatedata.getBlockState());
        	        }
        	        
                	flag = true;
        		}
        		else if(packetIn instanceof SPacketBlockChange)
                {
                	SPacketBlockChange spacketblockchange = (SPacketBlockChange)packetIn; 
                	world.setBlockState(spacketblockchange.getBlockPosition(), spacketblockchange.blockState);
                	flag = true;
                }
        	}	
    	}
    	
    	super.sendPacket(packetIn);
    }
}

 

When a player logs in (via EntityJoinWorldEvent and checking if the entity is an instance of EntityPlayerMP), I replace the player's network manager with my new class. On the surface, it appears to work as intended, however I seem to get a repeated error thrown with an occasional crash. From the crash log and console output, it seems like a packet size is too big. However, I'm not really well versed in packet and PacketBuffer stuff so I don't really know what's causing the errors. In case you know some more about it, here is the console log. I've trimmed it to where the error first appears. After the first throw, it just seems to repeat itself.

 

[18:06:09] [server thread/INFO] [The Underside]: Applying intercepted packet handler for Player509....

[18:06:10] [Client thread/FATAL]: Error executing task

java.util.concurrent.ExecutionException: java.lang.IndexOutOfBoundsException: readerIndex(15180) + length(2048) exceeds writerIndex(16689): UnpooledHeapByteBuf(ridx: 15180, widx: 16689, cap: 16689/16689)

at java.util.concurrent.FutureTask.report(Unknown Source) ~[?:1.8.0_102]

at java.util.concurrent.FutureTask.get(Unknown Source) ~[?:1.8.0_102]

at net.minecraft.util.Util.runTask(Util.java:26) [util.class:?]

at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1108) [Minecraft.class:?]

at net.minecraft.client.Minecraft.run(Minecraft.java:406) [Minecraft.class:?]

at net.minecraft.client.main.Main.main(Main.java:118) [Main.class:?]

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_102]

at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_102]

at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_102]

at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_102]

at net.minecraft.launchwrapper.Launch.launch(Launch.java:135) [launchwrapper-1.12.jar:?]

at net.minecraft.launchwrapper.Launch.main(Launch.java:28) [launchwrapper-1.12.jar:?]

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_102]

at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_102]

at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_102]

at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_102]

at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:97) [start/:?]

at GradleStart.main(GradleStart.java:26) [start/:?]

Caused by: java.lang.IndexOutOfBoundsException: readerIndex(15180) + length(2048) exceeds writerIndex(16689): UnpooledHeapByteBuf(ridx: 15180, widx: 16689, cap: 16689/16689)

at io.netty.buffer.AbstractByteBuf.checkReadableBytes(AbstractByteBuf.java:1175) ~[AbstractByteBuf.class:4.0.23.Final]

at io.netty.buffer.AbstractByteBuf.readBytes(AbstractByteBuf.java:676) ~[AbstractByteBuf.class:4.0.23.Final]

at io.netty.buffer.AbstractByteBuf.readBytes(AbstractByteBuf.java:684) ~[AbstractByteBuf.class:4.0.23.Final]

at net.minecraft.network.PacketBuffer.readBytes(PacketBuffer.java:876) ~[PacketBuffer.class:?]

at net.minecraft.world.chunk.Chunk.fillChunk(Chunk.java:1227) ~[Chunk.class:?]

at net.minecraft.client.network.NetHandlerPlayClient.handleChunkData(NetHandlerPlayClient.java:788) ~[NetHandlerPlayClient.class:?]

at net.minecraft.network.play.server.SPacketChunkData.processPacket(SPacketChunkData.java:110) ~[sPacketChunkData.class:?]

at net.minecraft.network.play.server.SPacketChunkData.processPacket(SPacketChunkData.java:20) ~[sPacketChunkData.class:?]

at net.minecraft.network.PacketThreadUtil$1.run(PacketThreadUtil.java:15) ~[PacketThreadUtil$1.class:?]

at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) ~[?:1.8.0_102]

at java.util.concurrent.FutureTask.run(Unknown Source) ~[?:1.8.0_102]

at net.minecraft.util.Util.runTask(Util.java:25) ~[util.class:?]

... 15 more

[18:06:10] [Client thread/FATAL]: Error executing task

 

Also worth mentioning that the error doesn't show up until I visit the other dimension and then return to the overworld. Perhaps it has something to do with both dimensions being loaded at the same time? I'm not sure how that would happen, however, as I have no chunk loaders and the dimension isn't registered to stay loaded.

Link to comment
Share on other sites

I am not here with straight answer, just wondering about some stuff.

 

I don't know THAT much about stuff you do in your prev post (tbh - only chunks part), but I think I know enough to say - drop this approach.

That is unless I am not really seeing what you are trying to do - I will call upon my prev post and say that this way you won't be able to catch all block changes (hell, you will be probably in lesser half if not less).

 

I'd personally make worlds copy each other by using same chunks (or rather "connected" chunks), like I think d7 suggested.

 

EDIT:

As for replacing chunk loaders - you could probably do that from world load event, I am not sure if vanilla's chunk loader is already being used there.

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

Link to comment
Share on other sites

I am not here with straight answer, just wondering about some stuff.

 

I don't know THAT much about stuff you do in your prev post (tbh - only chunks part), but I think I know enough to say - drop this approach.

That is unless I am not really seeing what you are trying to do - I will call upon my prev post and say that this way you won't be able to catch all block changes (hell, you will be probably in lesser half if not less).

 

I'd personally make worlds copy each other by using same chunks (or rather "connected" chunks), like I think d7 suggested.

 

Yea I plan on investigating what diesieben07 said, I just tried this approach first. From what I could tell, all the changes I made to the world seemed to get reflected in my new dimension (placing blocks, all the block commands, endermen, fire, water/lava, growing trees/plants, explosions, etc), but I see what you mean by there is no concrete way to tell if it works 100% of the time.

Link to comment
Share on other sites

What I mean is that you rely on player to "tell" you when stuff happens.

Chunks can be loaded by non-players, can be forced to load and basically anything in world can happen when there is 0 players online.

This is also what I meant by saying this is not syncing issue, but tracking server changes.

Still tho - chunk loaders are out of my expertise since 1.6, so I am useless here, cheers! 8)

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

Link to comment
Share on other sites

What I mean is that you rely on player to "tell" you when stuff happens.

Chunks can be loaded by non-players, can be forced to load and basically anything in world can happen when there is 0 players online.

This is also what I meant by saying this is not syncing issue, but tracking server changes.

Still tho - chunk loaders are out of my expertise since 1.6, so I am useless here, cheers! 8)

 

Ahh I see. The problem is that I have built the manager around a single player world (where its impossible to have 0 players online). It would fail on a dedicated server. Alright, to d7's suggestion, which, unfortunately, seems significantly harder :(

Link to comment
Share on other sites

You can still chunk-load in SSP and the player doesn't need to be in the same dimension, which is the same as 0 players online.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

You can still chunk-load in SSP and the player doesn't need to be in the same dimension, which is the same as 0 players online.

 

Yes I know you can load chunks in SSP. I'm not trying to make a chunk loader. I meant that my network handler is attached to a player, so it's useless if there is no player on the server. (In SSP, there is always a player on the Integrated Server, while dedicated server's don't need to have a player online.) On a dedicated server, manipulating server packets via players won't work because there can be 0 players.

Link to comment
Share on other sites

Yes I know you can load chunks in SSP. I'm not trying to make a chunk loader.

 

YOU aren't, but SOME OTHER MOD COULD BE.  That's the distinction: someone else might chunkload something, and the interaction with your code causes it to break.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Yes I know you can load chunks in SSP. I'm not trying to make a chunk loader.

 

YOU aren't, but SOME OTHER MOD COULD BE.  That's the distinction: someone else might chunkload something, and the interaction with your code causes it to break.

 

Alright, I don't know exactly what part is broken, but it doesn't matter, as I've trashed the idea in favor of diesieben07's "linked chunks" idea anyway. Although, I'm kinda lost on that part too. I managed to force the overworld and my dimension to use the same IChunkLoader, but that doesn't really work as the chunks are not saved and loaded every time someone makes a change to them. Because of this, if I edit a chunk in the overworld, the changes aren't instantly reflected in my new dimension. And when I save the chunks manually after building something (by logging out), the game can't really decide what chunk data it load when I join again (because in 1 world there are blocks where I placed them and in the other there aren't, which seems to cause conflicting data sets). I think the game tends to favor no change to a chunk rather than changing the chunk in both dimensions, but that's just a guess based on what I saw.

 

Also, there is a conflict in lighting levels using this approach as well (meaning if it's night in 1 dimension and day in the other, the game meshes the lighting levels together so in some chunks it appears to be nighttime while in others it's day).

 

So basically, rather than sharing an IChunkLoader, I think the approach would be to ensure the actual chunk data (not the chunks) is synced across the dimensions. Now the trouble is where do I even start looking to achieve this.

 

--EDIT:

 

So it looks like I would need to fetch the BlockStateContainer data variable in each all the instances of ExtendedBlockStorage in a modified chunk's storageArrays variable. This takes me back to the main problem here, though, which is how to detect chunk/block changes independently of the player.

Link to comment
Share on other sites

Alright! I've managed to figure out an alternative method without having to deal with the chunk provider and packet intercepting and such. Consider the question solved

 

You should post your solution so other people can find it.

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

Alright! I've managed to figure out an alternative method without having to deal with the chunk provider and packet intercepting and such. Consider the question solved

 

You should post your solution so other people can find it.

 

Basically, I actually sat down and thought about what I wanted to do and realized the answer was surprisingly simple. Minecraft already implements a block listener: the one used by entity pathing. I through the WorldEvent.Load event, I add a new instance of IWorldEventListener. IWorldEventListener provides a method called notifyBlockUpdate which gets called every time a block changes (at least for my purposes, only when it matters). Through that method, I get access to a world instance, block pos and block state. From that, I just fetch the world server instance of my dimension and setblock at the right position. I have to manipulate the flags parameter in World#setBlockState, however, to ensure I don't cause infinite notification loops.

Link to comment
Share on other sites

Alright! I've managed to figure out an alternative method without having to deal with the chunk provider and packet intercepting and such. Consider the question solved

 

You should post your solution so other people can find it.

 

Basically, I actually sat down and thought about what I wanted to do and realized the answer was surprisingly simple. Minecraft already implements a block listener: the one used by entity pathing. I through the WorldEvent.Load event, I add a new instance of IWorldEventListener. IWorldEventListener provides a method called notifyBlockUpdate which gets called every time a block changes (at least for my purposes, only when it matters). Through that method, I get access to a world instance, block pos and block state. From that, I just fetch the world server instance of my dimension and setblock at the right position. I have to manipulate the flags parameter in World#setBlockState, however, to ensure I don't cause infinite notification loops.

That will defiantly work for blocks, but it will not work for TileEntities don't know if you wanted that, but just to let you know.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

Alright! I've managed to figure out an alternative method without having to deal with the chunk provider and packet intercepting and such. Consider the question solved

 

You should post your solution so other people can find it.

 

Basically, I actually sat down and thought about what I wanted to do and realized the answer was surprisingly simple. Minecraft already implements a block listener: the one used by entity pathing. I through the WorldEvent.Load event, I add a new instance of IWorldEventListener. IWorldEventListener provides a method called notifyBlockUpdate which gets called every time a block changes (at least for my purposes, only when it matters). Through that method, I get access to a world instance, block pos and block state. From that, I just fetch the world server instance of my dimension and setblock at the right position. I have to manipulate the flags parameter in World#setBlockState, however, to ensure I don't cause infinite notification loops.

That will defiantly work for blocks, but it will not work for TileEntities don't know if you wanted that, but just to let you know.

 

I'm not sure what you mean. If I place a crafting table or furnace down in 1 dimension, it gets placed in the other one as well. If you mean the tile entities aren't synced (like sharing a chest inventory), no that doesn't matter.

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.