Jump to content

[1.15.2][SOLVED] Listen for block break event on client


henne90gen

Recommended Posts

I'm creating a mod that keeps track of the items in the players chests and I want it to work on vanilla servers as well.

Whenever a player opens a chest I write the contents of the chest to a "database".

That's why I need to know when the player destroys a chest, so that I can delete that chest from the "database"

Link to comment
Share on other sites

I found a solution for my problem.

I'm now using the PlayerInteractEvent.LeftClickBlock to detect when a player clicks on a block.

Every time the event fires, I check the surrounding 3x3x3 blocks whether they are chests or not.

If a block is not a chest, I simply check my database, whether I have a chest saved for that position and delete it if that's the case.

The only downside to this is, that the player has to click on a neighboring block after breaking the chest block.

 

So if anyone knows about a better way of doing this, then please let me know.

Link to comment
Share on other sites

Yeah, it's more of a workaround. 

I didn't expect to find a client side solution that takes into account other players breaking chests. 

It would be nice though to at least be able to get notified when the current player (the one that is being played by the client) breaks a block. 

Link to comment
Share on other sites

Hi

 

If you can't modify the server, and there is only one or a few chests that you're trying to track, then you could consider the client tick event; once every (eg) 20 ticks you could check to see if the chest still exists at the expected location.  Likewise you could periodically check the chest contents to see if they have changed.  That would detect changes not caused by the local player.  I can't think of any other client events which are triggered on updates to changes to chest contents.

 

-TGG

  • Thanks 1
Link to comment
Share on other sites

Thanks for the tip.

 

Checking all chests every couple ticks works much better than my initial workaround.

However, I can't get the content of the chest this way. At least not the way I tried it.

TileEntity tileEntity = world.getTileEntity(pos);
if (tileEntity instanceof ChestTileEntity) {
    ChestTileEntity chestTileEntity = (ChestTileEntity) tileEntity;
    for (int i = 0; i < chestTileEntity.getSizeInventory(); i++) {
    	ItemStack stack = chestTileEntity.getStackInSlot(i);
      	// count items here
    }
}

This only gives me an empty inventory.

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.