My mod is a client side only mod and must function on vanilla servers. I must stop the placement of blocks under certain conditions.
I've simplified my method to the following to make it easier to understand:
@SubscribeEvent(priority = EventPriority.LOWEST)
public void onItemUse(RightClickBlock e) {
if(e.getWorld().isRemote) { //only run on client side
e.setUseItem(Result.DENY);
}
}
Event though the client side hasn't placed the block, it still sends a packet to the server that it has placed a block, so the block is placed anyway. How can I stop the block from being placed?
Actually that might not work.. You need to cancel the packet somehow
Same thing, cancelling BlockPlaceEvent on the client side does nothing. I wonder if this is a bug with forge, or if it's intended. Regardless, you're correct, I will have to find a way to intercept the packet before it's sent to the server
The only way I have found to do this is to replace Minecraft.playercontroller with my own instance of PlayerControllerMP, and alter processRightClickBlock so it doesn't send the packet when my mod specifically cancels the event.
Rollback Post to RevisionRollBack
My Github ด้้้้้็็็็็้้้้้็็็็็้้้้้็็็็็้้้้้็็็็็้้้้้็็็็็้้้้้็็็็็้้้้้็็็็็้้้้้дด็็็็็้้้้้็็็็้้้้้็็็็็้้้้้็็็็็้้้้้็็็็็้้้้้
I am also looking to solve the same problem. How did you replace the object and only omit the packet when you cancel the event?
It seems that replacing the object will mean doing a new on PlayerController and overriding the method with some special content. But the new instance will not have the same data as the old one. Do you have to use reflection to load the new instance with the old data?
My mod is a client side only mod and must function on vanilla servers. I must stop the placement of blocks under certain conditions.
I've simplified my method to the following to make it easier to understand:
Event though the client side hasn't placed the block, it still sends a packet to the server that it has placed a block, so the block is placed anyway. How can I stop the block from being placed?
My Github ด้้้้้็็็็็้้้้้็็็็็้้้้้็็็็็้้้้้็็็็็้้้้้็็็็็้้้้้็็็็็้้้้้็็็็็้้้้้дด็็็็็้้้้้็็็็้้้้้็็็็็้้้้้็็็็็้้้้้็็็็็้้้้้
What version of mc is this? If it's 1.10.2 or later, you need to do e.setCanceled(true);
If you would like to get a hold of me somehow, then my discord is @Alex Couch#5275.
It's 1.12.2. It appears canceling cancelling RightClickBlock does not work if only canceling it on the client side.
Same thing, cancelling BlockPlaceEvent on the client side does nothing. I wonder if this is a bug with forge, or if it's intended. Regardless, you're correct, I will have to find a way to intercept the packet before it's sent to the server
EDIT: It does appear to be an issue with forge: https://github.com/MinecraftForge/MinecraftForge/issues/3272
My Github ด้้้้้็็็็็้้้้้็็็็็้้้้้็็็็็้้้้้็็็็็้้้้้็็็็็้้้้้็็็็็้้้้้็็็็็้้้้้дด็็็็็้้้้้็็็็้้้้้็็็็็้้้้้็็็็็้้้้้็็็็็้้้้้
The only way I have found to do this is to replace Minecraft.playercontroller with my own instance of PlayerControllerMP, and alter processRightClickBlock so it doesn't send the packet when my mod specifically cancels the event.
My Github ด้้้้้็็็็็้้้้้็็็็็้้้้้็็็็็้้้้้็็็็็้้้้้็็็็็้้้้้็็็็็้้้้้็็็็็้้้้้дด็็็็็้้้้้็็็็้้้้้็็็็็้้้้้็็็็็้้้้้็็็็็้้้้้
I am also looking to solve the same problem. How did you replace the object and only omit the packet when you cancel the event?
It seems that replacing the object will mean doing a new on PlayerController and overriding the method with some special content. But the new instance will not have the same data as the old one. Do you have to use reflection to load the new instance with the old data?
In 1.15, you can simply cancel InputEvent.ClickInputEvent. https://github.com/MinecraftForge/MinecraftForge/pull/6047
In older versions, you can do the following:
Replace the playercontroller (Make sure you're on the client side):
Then you will have to overwrite processRightClickBlock()
https://github.com/Gamebuster19901/MineJoy/blob/467a888bf7abcffc0ee956424f55c187654e10cc/src/main/java/com/gamebuster19901/minejoy/controller/PlayerControllerMPMinejoy.java#L77-L207
My Github ด้้้้้็็็็็้้้้้็็็็็้้้้้็็็็็้้้้้็็็็็้้้้้็็็็็้้้้้็็็็็้้้้้็็็็็้้้้้дด็็็็็้้้้้็็็็้้้้้็็็็็้้้้้็็็็็้้้้้็็็็็้้้้้