Search code examples
javaminecraftbukkit

Why not add money to killer and not subtract from victim? And the message is not sent


No errors, but does nothing if I kill somebody. I used the Essentials Economy for the Money. I don't know why it doesn't work. This is my Main class Code:

package me.ghostyy.killmoney;

import java.math.BigDecimal;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.plugin.java.JavaPlugin;
import com.earth2me.essentials.api.Economy;

public class Main extends JavaPlugin {

    @EventHandler
    public void deathEvent(PlayerDeathEvent e) {
        Player killer = e.getEntity().getKiller();
        Player victim = e.getEntity();
        if (killer == null)
            return; 
        try {
            double amount = Economy.getMoneyExact(victim.getName()).doubleValue() / 10.0D;
            victim.sendMessage("[§bKillMoney§f]§e You killed §b" + killer.getName() + "§e, you got " + (int)amount + "$!");
            Economy.substract(victim.getName(), BigDecimal.valueOf(amount));
            Economy.add(killer.getName(), BigDecimal.valueOf(amount));
            killer.sendMessage("[§bKillMoney§f]§b " + victim.getName() + "killed you, and lost " + (int)amount + "§e$!");
        } catch (Exception e1) {
            return;
        } 
    }

}

Thank you in advance for your help!


Solution

  • The problem is, that you are not implementing a Listener in the class and not telling the main class that there is a listener class.

    Also you should create a new Class as an EventListener, this gives you more overview. When you create a class named "DeathListener", your class would look like this:

    package me.ghostyy.killmoney;
    
    import java.math.BigDecimal;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import com.earth2me.essentials.api.Economy;
    
    import org.bukkit.event.Listener;   //importing the Listener
    
    public class DeathListener implements Listener {  //You have to implement the Listener to use it!
    
    @EventHandler
    public void deathEvent(PlayerDeathEvent e) {
        Player killer = e.getEntity().getKiller();
        Player victim = e.getEntity();
        if (killer == null)
            return; 
        try {
            double amount = Economy.getMoneyExact(victim.getName()).doubleValue() / 10.0D;
            victim.sendMessage("[§bKillMoney§f]§e You killed §b" + killer.getName() + "§e, you got " + (int)amount + "$!");
            Economy.substract(victim.getName(), BigDecimal.valueOf(amount));
            Economy.add(killer.getName(), BigDecimal.valueOf(amount));
            killer.sendMessage("[§bKillMoney§f]§b " + victim.getName() + "killed you, and lost " + (int)amount + "§e$!");
        } catch (Exception e1) {
            return;
        } 
    }
    
    }
    

    That's all you have to do in this class. For a new Event, create a new class. Last but not least, you have to tell your main class that there is a listener. put the following code into your onEnable() method in your main class:

    getServer().getPluginManager().registerEvents(new DeathEvent(), this);
    

    keep in mind that every class you are creating as an listener has to implement the Listener and has to be added to the PluginManager as shown above.

    [EDIT] As Jonas already mentioned, https://www.spigotmc.org/wiki/using-the-event-api/ explains it very good!