Sound when Player hits Entity

Discussion in 'Plugin Development' started by MrLucTV, Jun 2, 2018.

Thread Status:
Not open for further replies.
  1. Hello,

    I'm new in coding, so I have a problem.

    I'm currently making a little Plugin, which plays a sound when a player hits an entity.
    But somebody with perms. has to type /troll PLAYERNAME or something like this.

    My problem is that I need an onCommand() and an onHit() method but in the onHit method I need the target which was made in the onCommand method. I also tried seperating them into two classes and inheriting but that didn't work. I also browsed through the internet and found nothing.

    Ask me if you want the code.
    Thanks.
     
  2. Offline

    critikull

    You need to store the target of the sound in onCommand and get it in onHit. Pseudo code example:

    Code:
    public final class MyPlugin extends JavaPlugin {
        private HashMap<UUID,Sound> sounds = new HashMap<UUID,Sound>();
    
        public onCommand(...) {
              String playerName = ...
             for (Player player : Bukkit.getOnlinePlayers()) {
                   if (player.getName().equalsIgnoreCase(playerName)) {
                                   this.sounds.put(player.getUniqueID(), Sound.ANVIL);
                                   break;
                    }
             }
        }
    
        public onHit() {
            Player player = e.getPlayer() // or whatever...
            Sound sound = this.sounds.get(player.getUniqueID());
            if (sounds != null) {
                  // play it
             }
        }
    }
     
  3. Thanks for the answer.
    What do I have to fill in in the ...?
    And dont you need an Eventhandler etc.?
    Your Code ist also in the Main class is that needed?
    And to be sure: I want to type for example: /troll MyName and then I should hear a sound everytime I hit an Entity.
     
  4. Offline

    DutchJellyV2

  5. Still doesn't work...
    In my Main I did the getCommand and the pm.registerEvents

    Code:
    public class TrollCmd implements CommandExecutor, Listener{
    
        private HashMap<UUID,Sound> sounds = new HashMap<UUID,Sound>();
    
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
                   
            if(args.length == 1 ) {
                Player target = Bukkit.getPlayer(args[0]);
                String targetName = target.getName();
                for (Player player : Bukkit.getOnlinePlayers()) {
                    if (player.getName().equals(targetName)) {
                        this.sounds.put(player.getUniqueId(), Sound.ENDERDRAGON_HIT);
                        target.sendMessage("Test");
                        break;
                    }
                   
                    }
                }
            return false;
        }
       
        @EventHandler
        public void onHit(EntityDamageByEntityEvent e) {
            Player player = (Player) e.getDamager();
            Sound sound = this.sounds.get(player.getUniqueId());
            if(e.getDamager().equals(sounds)) {
                Player whoHit = (Player) e.getDamager();
                World w = whoHit.getWorld();
                w.playSound(whoHit.getLocation(), Sound.ENDERDRAGON_HIT, 1, 1);
            }
        }
    }
     
  6. Online

    timtower Administrator Administrator Moderator

  7. Code:
    
    import org.bukkit.Bukkit;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import de.luca.test.commands.HealCmd;
    import de.luca.test.commands.TrollCmd;
    import de.luca.test.listener.TPCompass;
    public class Main extends JavaPlugin {
        public void onEnable() {
            getCommand("heal").setExecutor(new HealCmd());
            getCommand("troll").setExecutor(new TrollCmd());
           
            PluginManager pm = Bukkit.getPluginManager();
            pm.registerEvents(new TPCompass(), this);
            pm.registerEvents(new TrollCmd(), this);
        }
    }
     
    Last edited by a moderator: Jun 3, 2018
  8. Online

    timtower Administrator Administrator Moderator

    @MrLucTV You are making 2 TrollCmd instances.
    Make sure to only use 1.
     
  9. But I need to register the Event?
     
  10. Online

    timtower Administrator Administrator Moderator

    @MrLucTV Make a variable for a trollcmd, use it for both methods
     
  11. Sorry idk what I have to do
     
  12. @MrLucTV
    Instead of doing new TrollCmd() twice make a variable for it like so

    TrollCmd trollCmd = new TrollCmd();

    and then use trollCmd instead of new TrollCmd()
     
Thread Status:
Not open for further replies.

Share This Page