Solved get a certain player in a console command

Discussion in 'Plugin Development' started by SoLID_HAX, May 3, 2020.

Thread Status:
Not open for further replies.
  1. Hi,
    I wanted to ask if somebody knows hot to execute a console command for a certain player!

    Code:
        @EventHandler
        public void onDeath(EntityDeathEvent e) {
            Entity LivingEntity = e.getEntity();
            Player k = ((org.bukkit.entity.LivingEntity) LivingEntity).getKiller();
            if(LivingEntity.hasMetadata("SkelettBoss")) {
                if(k instanceof Player) {
                    Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "money give " + k + " 200");
                    k.sendMessage("§aDu hast den Boss getötet und dafür 200 bekommen!");
                }
            }
        }
     
  2. Offline

    KarimAKL

  3. Offline

    Strahan

    You said "for a certain player". You could check k's name or UUID depending on how you want to designate the player. So like
    Code:
    @EventHandler
    public void onDeath(EntityDeathEvent e) {
      LivingEntity livingEntity = e.getEntity();
      if (livingEntity.getKiller() == null) return;
      if(!livingEntity.hasMetadata("SkelettBoss")) return;
    
      Player k = livingEntity.getKiller();
      if (!k.getName().equalsIgnoreCase("notch")) return;
      Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "money give " + k.getName() + " 200");
      k.sendMessage(ChatColor.GREEN + "Du hast den Boss getötet und dafür 200 bekommen!");
    }
    I also fixed a few other issues, namely that you weren't following Java naming conventions (livingEntity not LivingEntity). For the k var, an instanceof check isn't really required; killer with either be null or a Player so easier to just null check. Also don't embed color characters, use the ChatColor enum. I'd also suggest negative check and return, keeps things cleaner IMO. Lastly, you need to use the getName() method of k in the command, not just dumping a Player object in there.

    Also I personally hate hard coding things. If I were doing this and I wanted to filter rewards to certain player(s), I'd do:
    Code:
    @EventHandler
    public void onDeath(EntityDeathEvent e) {
      LivingEntity livingEntity = e.getEntity();
      Player k = livingEntity.getKiller();
    
      if (livingEntity.getKiller() == null) return;
      if(!livingEntity.hasMetadata("SkelettBoss")) return;
      if (!k.getName().equalsIgnoreCase("notch")) return;
    
      List<String> validPlayers = getConfig().getStringList("bosspay.players");
      int payAmount = plugin.getConfig().getInt("bosspay.amount", 200);
      String payMessage = getConfig().getString("messages.bosspay", "&aDu hast den Boss getötet und dafür %amount% bekommen!").replace("%amount%", String.valueOf(payAmount));
    
      for (String playerName : validPlayers) {
        Player p = plugin.getServer().getPlayerExact(playerName);
        if (p == null) continue;
    
        Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "money give " + k.getName() + " " + payAmount);
        k.sendMessage(ChatColor.translateAlternateColorCodes('&', payMessage));
      }
    }
    Also I'd look to see if whatever economy system I am using has an API and just hook that instead of dispatching console commands.
     
    Last edited: May 3, 2020
Thread Status:
Not open for further replies.

Share This Page