Jump to content

Playing a sound event when player is in range of an entity


Plgue

Recommended Posts

Title says a lot. Also, the  "System.out.println("PlayerTickEvent Working");" does not print every tick. Here's my code:

 

    @SubscribeEvent
    public void entitiesInRadius(PlayerTickEvent event)
    {
         EntityPlayer player = event.player;
        
         int monsterCount = player.world.getEntitiesWithinAABB(EntityMob.class, new AxisAlignedBB(player.posX - 16, player.posY - 8, player.posZ - 16, player.posX + 16, player.posY + 8, player.posZ + 16)).size();
         boolean isplayerinrange = false;
         
         System.out.println("PlayerTickEvent Working");
         if(monsterCount > 0) 
         {
             System.out.println("isplayerinrange is true");
             isplayerinrange = true;
             
             if(isplayerinrange = true) 
                {    
                 System.out.println("Battle Music Initiated");
                player.playSound(ModSounds.BATTLE_MUSIC, 1, 1);
                }
          }
         
         else
         {
             isplayerinrange = false;
         }
         
    }

Edited by Plgue
Link to comment
Share on other sites

1 hour ago, Plaigon said:

Hi, you can maybe try using the LivingSetAttackTargetEvent ?

So I updated my code:

 

    @SubscribeEvent
    public void livingSetAttacktargetEvent(LivingSetAttackTargetEvent event)
    {
        EntityLivingBase player = event.getTarget();
        
        System.out.println("Set attack target");
        if(event.getTarget() == player)
        {
            System.out.println("Battle Music Play");
            player.playSound(ModSounds.BATTLE_MUSIC, 100, 1);
        }
        
        else
        {
            
        } 
    }

 

It's still a work in progress, but no matter what I do I can't seem to get the event to start my method. The System.out.println's don't even go off, I'm really stumped here. 

 

Edit: This is my common proxy, just in case you wanna see it:

 

public class CommonProxy {


    public void preInit(FMLPreInitializationEvent e) 
    {
        ModItems.init();
        ModItems.register();;
        
        ModEntities.init();

    }

    public void init(FMLInitializationEvent e) 
    {
        MinecraftForge.EVENT_BUS.register(ModSounds.class);
        MinecraftForge.EVENT_BUS.register(EventHandler.class);
    }

    public void postInit(FMLPostInitializationEvent e) 
    {
        
    }
}

Edited by Plgue
Link to comment
Share on other sites

Use Eclipse to look into source code of vanilla entities . See what they do to make sounds, especially from server side decisions.

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

Link to comment
Share on other sites

So after everything I've seen in this thread, this is what I came up with:

 

    @SubscribeEvent(priority = EventPriority.HIGHEST)
    public void playBattleSong(LivingSetAttackTargetEvent event)
    {

          EntityLivingBase entity = event.getEntityLiving().getAttackingEntity();

          entity.playSound(ModSounds.BATTLE_MUSIC, 75, 1);
    }

 

It still does not play the sound.

Link to comment
Share on other sites

How does EventPriority.HIGHEST solve the client-vs-server issue?

I'll give you one hint:

It does fuck all about it.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

7 hours ago, diesieben07 said:

Don't "just mess around". Read the documentation about playing sounds that I linked.

But this code should work right? I'm playing a sound from an entity, vanilla Minecraft does the same thing.

So I don't know why it does not work.

Edited by Plgue
Link to comment
Share on other sites

Just now, diesieben07 said:

It does not work because you are calling a method that is designed to be called from both sides only on the server.

You would know this if you had read the documentation that I linked.

So in this case I should be using "World.playsound" ?

Link to comment
Share on other sites

Just now, diesieben07 said:

That is one solution, yes.

Again i'm really sorry if I sound like an idiot, but I don't know how to get the sound to play using "World.playsound()"

Because I don't know how to get my method non static, I did not set it as static though.

 

Link to comment
Share on other sites

1 minute ago, diesieben07 said:

Please clarify what you are talking about. Which method? Why do you want it to be non-static? Do you even know what that means?

Never mind, this is what i'm currently using

entity.world.playSound(null, entity.chunkCoordX, entity.chunkCoordY, entity.chunkCoordZ, ModSounds.BATTLE_MUSIC, SoundCategory.MUSIC, 100, 1);

But it still does not work

Link to comment
Share on other sites

4 hours ago, Plgue said:

Never mind, this is what i'm currently using

entity.world.playSound(null, entity.chunkCoordX, entity.chunkCoordY, entity.chunkCoordZ, ModSounds.BATTLE_MUSIC, SoundCategory.MUSIC, 100, 1);

But it still does not work

It's not working because you are passing chunk coordinates into a method that takes world coordinates. You want Entity#posXEntity#posY & Entity#posZ.

Link to comment
Share on other sites

3 hours ago, Leviathan143 said:

It's not working because you are passing chunk coordinates into a method that takes world coordinates. You want Entity#posXEntity#posY & Entity#posZ.

Did this and it's still not working

 

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.