Posted June 11, 20214 yr 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 June 11, 20214 yr by GrigLog some grammar mistakes
June 11, 20214 yr Author Found a solution. Just pass a player as null and the sound gets played for everyone.
June 12, 20214 yr 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
June 12, 20214 yr 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.)
June 13, 20214 yr 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 June 13, 20214 yr 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.