Jump to content

Prevent client event being sent to Server?


Timeraa

Recommended Posts

Hey, i am creating a mod for a plugin server where you can select blocks with right click, but i only want to use the right click on the client side and don't want to send the right click to the server. How can i achieve this?

Example:

 

User clicks at block, mod opens a GUI. (Server does as well but should not)

Link to comment
Share on other sites

@Mod.EventBusSubscriber
public class SelectionEventHandler {

    @SubscribeEvent
    public void onRightClickBlock(PlayerInteractEvent.RightClickBlock event) {}
}

 

Basically this is the event. I removed the if statements in the event because i don't think they help you in any way because its basically just location checking etc.

Link to comment
Share on other sites

33 minutes ago, Timeraa said:

Basically this is the event. I removed the if statements in the event because i don't think they help you in any way because its basically just location checking etc.

event.getSide() == Side.CLIENT

Though this is assuming that your method of opening a gui is client side only.

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

5 minutes ago, Animefan8888 said:

event.getSide() == Side.CLIENT

 Though this is assuming that your method of opening a gui is client side only.

Yes, but i want to prevent the server event like if i click the block the server plugin opens a gui but i want to prevent that event being sent to the server and just use the right click on the side end. 

Link to comment
Share on other sites

8 minutes ago, Timeraa said:

Yes, but i want to prevent the server event like if i click the block the server plugin opens a gui but i want to prevent that event being sent to the server and just use the right click on the side end.

Have you tried setting the event to cancelled?

event.setCanceled(true)

 

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

Just now, Animefan8888 said:

Have you tried setting the event to cancelled?


event.setCanceled(true)

 

Yes i tried that but that does not prevent the event. (Maybe because i have multiple right click events in multiple packages?)

Link to comment
Share on other sites

Just now, Timeraa said:

Yes i tried that but that does not prevent the event. (Maybe because i have multiple right click events in multiple packages?)

No, unless your other right click events set cancelled to false. It seems that there is no way for this to happen with forge currently.

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

2 minutes ago, Animefan8888 said:

No, unless your other right click events set cancelled to false. It seems that there is no way for this to happen with forge currently.

Hm thats unfortunate. Ill look through my packages to find out if i set it to false somewhere. Normally i develop plugins for Spigot but i needed a small change.

Edited by Timeraa
Link to comment
Share on other sites

No, wait it should work. This is the way you should do it.

 

In your proxy class create a method called something like showMyGui(). In the ClientProxy version of that method you would actually make the GUI. In the server version you would do nothing. In your event handler you just call that proxy method and it will behave differently on each side. Server would not put up a GUI.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

3 minutes ago, jabelar said:

No, wait it should work. This is the way you should do it.

 

In your proxy class create a method called something like showMyGui(). In the ClientProxy version of that method you would actually make the GUI. In the server version you would do nothing. In your event handler you just call that proxy method and it will behave differently on each side. Server would not put up a GUI.

? But i want to completely prevent the client to sent the right click to the server.

Link to comment
Share on other sites

5 minutes ago, Timeraa said:

? But i want to completely prevent the client to sent the right click to the server.

In the server version you would set cancelled. That should work. You mentioned handling the event in multiple places in your code? That is a bad idea because you don't know the order they will fire.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

1 minute ago, jabelar said:

In the server version you would set cancelled. That should work. You mentioned handling the event in multiple places in your code? That is a bad idea because you don't know the order they will fire.

The server is a normal server running plugins, i don't want to handle those items on the server i want to simply cancel the event on the client side, you hold the item with the mod and the mod does not send the right click to the server if you right click, i already tried just using setCancelled(true) but that only stops the block placement but it still sends the right click to the server. Or is the problem because the other right click events that are used from other items make it so that setCancelled sets itself to false?

Link to comment
Share on other sites

So, if I understand correctly, you are creating a client-side only mod? And when you right click a block something should happen on the client, but the server should not be notified of the right click? This is actually not that easy, because the client will always send the interaction package even if the event has been cancelled (in PlayerControllerMP#processRightClickBlock) :

if (event.isCanceled())
{
// Give the server a chance to fire event as well. That way server event is not dependant on client event.
this.connection.sendPacket(new CPacketPlayerTryUseItemOnBlock(pos, direction, hand, f, f1, f2));
return event.getCancellationResult();
}

 

Link to comment
Share on other sites

All this dilly dallying for something simple.

@EventBusSubscriber(value = Side.CLIENT)

This will make sure that the event is only registered for the client

Edited by Matryoshika

Also previously known as eAndPi.

"Pi, is there a station coming up where we can board your train of thought?" -Kronnn

Published Mods: Underworld

Handy links: Vic_'s Forge events Own WIP Tutorials.

Link to comment
Share on other sites

  • 1 year later...
  • Guest locked this topic
Guest
This topic is now closed to further replies.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • They were already updated, and just to double check I even did a cleanup and fresh update from that same page. I'm quite sure drivers are not the problem here. 
    • i tried downloading the drivers but it says no AMD graphics hardware has been detected    
    • Update your AMD/ATI drivers - get the drivers from their website - do not update via system  
    • As the title says i keep on crashing on forge 1.20.1 even without any mods downloaded, i have the latest drivers (nvidia) and vanilla minecraft works perfectly fine for me logs: https://pastebin.com/5UR01yG9
    • Hello everyone, I'm making this post to seek help for my modded block, It's a special block called FrozenBlock supposed to take the place of an old block, then after a set amount of ticks, it's supposed to revert its Block State, Entity, data... to the old block like this :  The problem I have is that the system breaks when handling multi blocks (I tried some fix but none of them worked) :  The bug I have identified is that the function "setOldBlockFields" in the item's "setFrozenBlock" function gets called once for the 1st block of multiblock getting frozen (as it should), but gets called a second time BEFORE creating the first FrozenBlock with the data of the 1st block, hence giving the same data to the two FrozenBlock :   Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=head] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@73681674 BlockEntityData : id:"minecraft:bed",x:3,y:-60,z:-6} Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=3, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=2, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} here is the code inside my custom "freeze" item :    @Override     public @NotNull InteractionResult useOn(@NotNull UseOnContext pContext) {         if (!pContext.getLevel().isClientSide() && pContext.getHand() == InteractionHand.MAIN_HAND) {             BlockPos blockPos = pContext.getClickedPos();             BlockPos secondBlockPos = getMultiblockPos(blockPos, pContext.getLevel().getBlockState(blockPos));             if (secondBlockPos != null) {                 createFrozenBlock(pContext, secondBlockPos);             }             createFrozenBlock(pContext, blockPos);             return InteractionResult.SUCCESS;         }         return super.useOn(pContext);     }     public static void createFrozenBlock(UseOnContext pContext, BlockPos blockPos) {         BlockState oldState = pContext.getLevel().getBlockState(blockPos);         BlockEntity oldBlockEntity = oldState.hasBlockEntity() ? pContext.getLevel().getBlockEntity(blockPos) : null;         CompoundTag oldBlockEntityData = oldState.hasBlockEntity() ? oldBlockEntity.serializeNBT() : null;         if (oldBlockEntity != null) {             pContext.getLevel().removeBlockEntity(blockPos);         }         BlockState FrozenBlock = setFrozenBlock(oldState, oldBlockEntity, oldBlockEntityData);         pContext.getLevel().setBlockAndUpdate(blockPos, FrozenBlock);     }     public static BlockState setFrozenBlock(BlockState blockState, @Nullable BlockEntity blockEntity, @Nullable CompoundTag blockEntityData) {         BlockState FrozenBlock = BlockRegister.FROZEN_BLOCK.get().defaultBlockState();         ((FrozenBlock) FrozenBlock.getBlock()).setOldBlockFields(blockState, blockEntity, blockEntityData);         return FrozenBlock;     }  
  • Topics

×
×
  • Create New...

Important Information

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