Jump to content

Event is not called on client after being canceled on server


GrigLog

Recommended Posts

What I want: under certain circumstances a player is considered immune to mobs' attacks. When he is supposed to get hit, he avoids it and a special sound is played. Ive subscribed to a LivingAttackEvent and made all the checks (for immunity), now I need to cancel the event and play the sound, something like this:

SoundEvent sound = new SoundEvent(new ResourceLocation("soul", "sword_clash"));
player.world.playSound(player, player.getPosition(), sound, SoundCategory.PLAYERS, 1, 1);
event.setCanceled(true);

But here is a problem. The event is first fired at server side, and if it gets canceled there, its not called on client. And the sound can only be called on client. How can I do both the cancel and the sound? Do I have to use packets or there is an easier way?

Edited by GrigLog
some grammar mistakes
Link to comment
Share on other sites

Well, actually it doesnt help much, because there are some other things that I have to do there... At this moment Ive managed to sync client and server with packets, but its very clumsy and there should be a better way. I just need to fire the event on client too and execute the same code there, there must be a way

Link to comment
Share on other sites

I have to 1) change player's capability, 2) stop player from using an item. I do both things in a packet handle method, but it... takes too much space, and it would be better if the code was shared between both sides.
Btw, do you know a way to do the second thing? Ive tried a player.stopActiveHand(), but it has a problem - if I hold the right button, item is used immediately after being stopped. (To be more specific, I want to add a special type of shield which you have to raise after each blocked attack, so that player cant just hold it. And with this method used its always up and player dont have to right-click.)

Link to comment
Share on other sites

I was able to do the second thing. If someone is interested:
1) Subscribe to a right-click input event

@SubscribeEvent
public static void onEvent(InputEvent.MouseInputEvent event)
    {
        KeyBinding keyUse = Minecraft.getInstance().gameSettings.keyBindUseItem;
        if (event.getButton() == 1 && event.getAction() == 1) {  //right button pressed down
            SoulCap soulCap = Minecraft.getInstance().player.getCapability(SoulProvider.SOUL_CAP, null).resolve().get();
            soulCap.setRightClicked(true);   //use a capability to save the info
            PacketSender.INSTANCE.sendToServer(new SoulPacket(soulCap));   //sync with server
        }
    }

2) Override an item onItemRightClick method to return ActionResult.resultPass when neccessary

@Override
    public ActionResult<ItemStack> onItemRightClick(World world, PlayerEntity player, Hand handIn) {
        ItemStack itemstack = player.getHeldItem(handIn);
        SoulCap soulCap = player.getCapability(SoulProvider.SOUL_CAP, null).resolve().get();
        if (soulCap.getRightClicked()){
            soulCap.setRightClicked(false);
      	    player.setActiveHand(handIn);
            //item is used as its supposed to
            return ActionResult.resultConsume(itemstack);
        }
        return ActionResult.resultPass(itemstack);
    }

 

Edited by GrigLog
some grammar mistakes
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.