Not Working?

Discussion in 'Spigot Plugin Development' started by Mhalllz, May 28, 2018.

  1. I'm trying to make a plugin that gives a reward upon killing mobs but its not giving the reward or sending the message. Heres what i have

    Code (Java):
    package me.MC_Hoxen.mobmoney;

    import org.bukkit.Bukkit;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDeathEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    public class main extends JavaPlugin implements Listener{

        @Override
        public void onEnable(){
            Bukkit.getServer().getLogger().info("Mob Money has been enabled!");
            Bukkit.getServer().getLogger().info("Version 1.0 By MC_Hoxen");
            this.getConfig().options().copyDefaults(true);
            this.saveConfig();
        }
       
        @Override
        public void onDisable(){
            Bukkit.getServer().getLogger().info("Mob Money has been disabled!");
        }
       
        @EventHandler
          public void onDeath(EntityDeathEvent e){
            Entity killer = e.getEntity().getKiller();
            Entity dead = e.getEntity();
         
            if (killer instanceof Player){
                Player p = (Player) e.getEntity().getKiller();
                String s = getConfig().getString("Prefix").replace('&', '§');
                if (dead.getLastDamageCause() instanceof Player) {
                if (dead.getType().equals(EntityType.ZOMBIE)) {
                    getServer().dispatchCommand(getServer().getConsoleSender(), "eco give user" + this.getConfig().getInt("Zombie-Kill-Reward"));
                    p.sendMessage(s + getConfig().getString("Zombie-Reward-Message").replace('&', '§'));
                }
                else if (dead.getType().equals(EntityType.CREEPER)) {
                    getServer().dispatchCommand(getServer().getConsoleSender(), "eco give user" + this.getConfig().getInt("Creeper-Kill-Reward"));
                    p.sendMessage(s + getConfig().getString("Creeper-Reward-Message").replace('&', '§'));
                }
                else if (dead.getType().equals(EntityType.SPIDER)) {
                    getServer().dispatchCommand(getServer().getConsoleSender(), "eco give user" + this.getConfig().getInt("Spider-Kill-Reward"));
                    p.sendMessage(s + getConfig().getString("Spider-Reward-Message").replace('&', '§'));
                }
                else if (dead.getType().equals(EntityType.SKELETON)) {
                    getServer().dispatchCommand(getServer().getConsoleSender(), "eco give user" + this.getConfig().getInt("Skeleton-Kill-Reward"));
                    p.sendMessage(s + getConfig().getString("Skeleton-Reward-Message").replace('&', '§'));
                }
                else if (dead.getType().equals(EntityType.ENDERMAN)) {
                    getServer().dispatchCommand(getServer().getConsoleSender(), "eco give user" + this.getConfig().getInt("Enderman-Kill-Reward"));
                    p.sendMessage(s + getConfig().getString("Enderman-Reward-Message").replace('&', '§'));
                }
                if (dead.getType().equals(EntityType.SLIME)) {
                    getServer().dispatchCommand(getServer().getConsoleSender(), "eco give user" + this.getConfig().getInt("Slime-Kill-Reward"));
                    p.sendMessage(s + getConfig().getString("Slime-Reward-Message").replace('&', '§'));
                }
                }  
    }
            }
    }
     
    Code (YAML):
    Prefix: '&4&lPrefix'

    Zombie-Kill-Reward
    : 10
    Zombie-Reward-Message
    : '&6You have recieved 10$'

    Creeper-Kill-Reward
    : 10
    Creeper-Reward-Message
    : '&6You have recieved 10$'

    Skeleton-Kill-Reward
    : 10
    Skeleton-Reward-Message
    : '&6You have recieved 10$'

    Enderman-Kill-Reward
    : 10
    Enderman-Reward-Message
    : '&6You have recieved 10$'

    Slime-Kill-Reward
    : 10
    Slime-Reward-Message
    : '&6You have recieved 10$'
    Code (YAML):
    main: me.MC_Hoxen.mobmoney.main
    name
    : MobMoney
    version
    : 1.0
     
  2. Need to register your main class as a Event Handler with Bukkit#getServer()#getPluginManager()#registerEvents(this, this);
     
  3. where do I put that? I tried in the OnEnable
     
  4. In the onEnable.
     
  5. This is what I have and it doesnt work

    Code (Java):
    package me.MC_Hoxen.mobmoney;

    import net.md_5.bungee.api.ChatColor;

    import org.bukkit.Bukkit;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDeathEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    public class main extends JavaPlugin implements Listener{

        @Override
        public void onEnable(){
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            Bukkit.getServer().getLogger().info("Mob Money has been enabled!");
            Bukkit.getServer().getLogger().info("Version 1.0 By MC_Hoxen");
            this.getConfig().options().copyDefaults(true);
            this.saveConfig();
        }

        @Override
        public void onDisable(){
            Bukkit.getServer().getLogger().info("Mob Money has been disabled!");
        }  

        @EventHandler
        public void onDeath(EntityDeathEvent e){
            Entity killer = e.getEntity().getKiller();
            Entity dead = e.getEntity();

            if (killer instanceof Player){
                Player p = (Player) e.getEntity().getKiller();
                String s = ChatColor.translateAlternateColorCodes('&', getConfig().getString("Prefix"));
                if (dead.getLastDamageCause() instanceof Player) {
                    if (dead.getType().equals(EntityType.ZOMBIE)) {
                        getServer().dispatchCommand(getServer().getConsoleSender(), "eco give user" + this.getConfig().getInt("Zombie-Kill-Reward"));
                        p.sendMessage(s + getConfig().getString("Zombie-Reward-Message").replace('&', '§'));
                    }
                    else if (dead.getType().equals(EntityType.CREEPER)) {
                        getServer().dispatchCommand(getServer().getConsoleSender(), "eco give user" + this.getConfig().getInt("Creeper-Kill-Reward"));
                        p.sendMessage(s + ChatColor.translateAlternateColorCodes('&', getConfig().getString("Creeper-Reward-Message")));
                    }
                    else if (dead.getType().equals(EntityType.SPIDER)) {
                        getServer().dispatchCommand(getServer().getConsoleSender(), "eco give user" + this.getConfig().getInt("Spider-Kill-Reward"));
                        p.sendMessage(s + ChatColor.translateAlternateColorCodes('&', getConfig().getString("Spider-Reward-Message")));
                    }
                    else if (dead.getType().equals(EntityType.SKELETON)) {
                        getServer().dispatchCommand(getServer().getConsoleSender(), "eco give user" + this.getConfig().getInt("Skeleton-Kill-Reward"));
                        p.sendMessage(s + ChatColor.translateAlternateColorCodes('&', getConfig().getString("Skeleton-Reward-Message")));
                    }
                    else if (dead.getType().equals(EntityType.ENDERMAN)) {
                        getServer().dispatchCommand(getServer().getConsoleSender(), "eco give user" + this.getConfig().getInt("Enderman-Kill-Reward"));
                        p.sendMessage(s + ChatColor.translateAlternateColorCodes('&', getConfig().getString("Enderman-Reward-Message")));
                    }
                    if (dead.getType().equals(EntityType.SLIME)) {
                        getServer().dispatchCommand(getServer().getConsoleSender(), "eco give user" + this.getConfig().getInt("Slime-Kill-Reward"));
                        p.sendMessage(s + ChatColor.translateAlternateColorCodes('&', getConfig().getString("Slime-Reward-Message")));
                    }
                }  
            }
        }
    }
     
  6. I tested out your code and I figured out the issue -

    Firstly, if I could make a suggestion I would implement the VaultAPI into your plugin, it makes handling economy a lot cleaner, more can be found here: https://github.com/MilkBowl/VaultAPI

    The reason why it's not sending you a message/running the command is because of the following line (remove it):
    Code (Text):

    if (dead.getLastDamageCause() instanceof Player) {
     
    Also another suggestion I'd like to make is just change the import you're using for ChatColor the current import you're using is for bungeecord:
    Code (Text):

    import net.md_5.bungee.api.ChatColor;
     
    Change it to:
    Code (Text):

    import org.bukkit.ChatColor;
     
     
    • Agree Agree x 1
  7. dead.getType().equals(EntityType.ZOMBIE) should be -> dead.getType() == Entify...
     
    • Agree Agree x 1
  8. Either works for enums so...

    The reason this line is work working as expected is because it returns EntityDamageEvent which is not a Player object and hence the check always fails, you already have killer which will be null if it wasn't a player kill.