Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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

  • Author

Found a solution. Just pass a player as null and the sound gets played for everyone.

  • Author

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

  • Author

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.)

  • Author

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

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.