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

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?

  • Author

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.

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

  • Author

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.

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

  • Author

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.

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

  • Author

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.

  • Author

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

  • Author

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);
                    }
                });
            }
            
            
        }
    }

 

  • Author

I've tried using variant where you pass in null for the player, but that also didn't work. I forgot to mention that.

  • Author

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);
                }

  • Author

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

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.