Jump to content

Recommended Posts

Posted

Hello,

I am trying to have an event play a custom sound when it fires. Specifically, it is a LivingHurtEvent. Calling the playSound() method of the event plays the sound on the server. This works fine, however the sound is not played in singleplayer as a result. What would I need to do in order to handle the case in singleplayer?

Posted

I was taking a look at that. How would I get an instance of PlayerClientEntity in my EventHandler? I tried casting and checking the instance of the LivingEntity that the event passes in, but that didn't work.

Posted
51 minutes ago, skip999 said:

Will that return null if it's a server world?

No but it will return null if you use it in a only server side code part, because Minecraft class can only use on client

Posted

I'm not in front of my computer right now, but I take it there is a Minecraft.getInstance().isSinglePlayer() call of some kind you can do to check.

Posted
3 minutes ago, skip999 said:

I'm not in front of my computer right now, but I take it there is a Minecraft.getInstance().isSinglePlayer() call of some kind you can do to check.

this will only check if you play in a single player world so it will not include lan servers/normal servers like hypixel
in which event do you want to play the sound

Posted

It's an EntityHurtEvent, and I have the entity (which in this case is the player) emit a sound using the .playSound() method. However according to the documentation this will only be heard by other players on a server. I want the sound to be heard by the player as well in single player.

Posted
19 minutes ago, skip999 said:

It's an EntityHurtEvent, and I have the entity (which in this case is the player) emit a sound using the .playSound() method. However according to the documentation this will only be heard by other players on a server. I want the sound to be heard by the player as well in single player.

i think you can use Minecraft#getInstance()#player

Posted

So I would need to call something along the lines of

event.getEntity().getWorld().playSound()

(I know that's not the exact method call but I'm not at my computer rn)

Would this not play it for everyone in the world regardless of location like the wither spawn sound? I want this to be a localized sound.

Posted (edited)

I tried doing the getWorld() option and it still is refusing to play. I have put a link to the code here on my github. Am I being a potato?

https://github.com/skiprocks999/Electrodynamics-JEI-Integration/blob/2dda66479361c3739b69264e6c6942c1640c5db7/src/main/java/electrodynamics/api/capability/compositearmor/CapabilityCeramicPlateHandler.java#L63

 

The multiple playSound() calls are there due to my testing. I feel I should point that out.

 

Edited by skip999
Posted

Okay so I got the sound to play in the OnRightClickEvent handler. However, I cannot get it to play in the LivingHurtEvent handler. I have confirmed the sound itself plays correctly. However, it will not play, and I can't figure out why. I am out of ideas on how to get it to work, so I would appreciate some help. Here is the code for the handler methods:

public static void takeDamageWithArmor(LivingHurtEvent event) {
        
        ItemStack[] ARMOR_PIECES = new ItemStack[] {
                new ItemStack(DeferredRegisters.COMPOSITE_ARMOR_PIECES.get(EquipmentSlotType.HEAD).get()),
                new ItemStack(DeferredRegisters.COMPOSITE_ARMOR_PIECES.get(EquipmentSlotType.CHEST).get()),    
                new ItemStack(DeferredRegisters.COMPOSITE_ARMOR_PIECES.get(EquipmentSlotType.LEGS).get()),
                new ItemStack(DeferredRegisters.COMPOSITE_ARMOR_PIECES.get(EquipmentSlotType.FEET).get())
        };
        
        List<ItemStack> armorPieces = new ArrayList<>();
        event.getEntityLiving().getArmorInventoryList().forEach(armorPieces::add);
        
        if(ItemStack.areItemsEqualIgnoreDurability(armorPieces.get(0), ARMOR_PIECES[3])
            && ItemStack.areItemsEqualIgnoreDurability(armorPieces.get(1), ARMOR_PIECES[2])
            && ItemStack.areItemsEqualIgnoreDurability(armorPieces.get(2), ARMOR_PIECES[1])
            && ItemStack.areItemsEqualIgnoreDurability(armorPieces.get(3), ARMOR_PIECES[0])
        ) {
            
            if(event.getAmount() <= NO_DAMAGE_DEALT) {
                if(!event.getSource().equals(DamageSource.OUT_OF_WORLD)) {
                    event.setCanceled(true);
                }
            }
            
            
            ItemStack stack = armorPieces.get(2);
            stack.getCapability(CapabilityCeramicPlate.CERAMIC_PLATE_HOLDER_CAPABILITY).ifPresent(h ->{
                if(event.getAmount() >= LETHAL_DAMAGE_AMOUNT && h.getPlateCount() > 0) {
                    event.getEntityLiving().getEntityWorld().playSound(
                            event.getEntityLiving().chunkCoordX,
                            event.getEntityLiving().chunkCoordY,
                            event.getEntityLiving().chunkCoordZ,
                            SoundRegister.SOUND_CERAMICPLATEBREAKING.get(),
                            SoundCategory.PLAYERS, 1, 1, false);


                    event.setAmount((float) Math.sqrt(event.getAmount()));
                    h.setPlateCount(h.getPlateCount() - 1);
                    
                }
            });
            
        }
    }

    public static void addPlateToArmor(RightClickItem event) {
        if(ItemStack.areItemsEqualIgnoreDurability(event.getItemStack(), new ItemStack(DeferredRegisters.SUBTYPEITEM_MAPPINGS.get(SubtypeCeramic.plate)))){
            
            List<ItemStack> armorPieces = new ArrayList<>();
            event.getEntityLiving().getArmorInventoryList().forEach(armorPieces::add);
            
            ItemStack chestplate = armorPieces.get(2);
            if(ItemStack.areItemsEqualIgnoreDurability(chestplate, new ItemStack(DeferredRegisters.COMPOSITE_ARMOR_PIECES.get(EquipmentSlotType.CHEST).get()))) {
                chestplate.getCapability(CapabilityCeramicPlate.CERAMIC_PLATE_HOLDER_CAPABILITY).ifPresent(h -> {
                    if(h.getPlateCount() < 2) {
                        event.getEntityLiving().getEntityWorld().playSound(
                                event.getEntityLiving().chunkCoordX,
                                event.getEntityLiving().chunkCoordY,
                                event.getEntityLiving().chunkCoordZ,
                                SoundRegister.SOUND_CERAMICPLATEADDED.get(),
                                SoundCategory.PLAYERS, 1, 1, false);
                        h.setPlateCount(h.getPlateCount() + 1);
                        event.getItemStack().shrink(1);
                    }
                });
            }
            
            
        }
    }

 

Posted

This is the if statement in the LivingHurtEvent method

 

if(event.getAmount() >= LETHAL_DAMAGE_AMOUNT && h.getPlateCount() > 0) {
                    
                    event.setAmount((float) Math.sqrt(event.getAmount()));
                    h.setPlateCount(h.getPlateCount() - 1);
                    event.getEntityLiving().getEntityWorld().playSound(
                            null,
                            event.getEntityLiving().chunkCoordX,
                            event.getEntityLiving().chunkCoordY,
                            event.getEntityLiving().chunkCoordZ,
                            SoundRegister.SOUND_CERAMICPLATEBREAKING.get(),
                            SoundCategory.PLAYERS, 1, 1);
                }

Posted

I swapped it to use the getPosition() option instead of the chunk coords and that worked. I appreciate the help. Events and Sounds are things I haven't worked with before. so I'm learning as I go as it were :D

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.