1

I am working on a plugin for my server for the KitPvP section.

Right now I am adding MoneyPerKill but have run into a problem.

My code is:

package com.lobbyist.junk.kitpvp;

import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.economy.EconomyResponse;

import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.plugin.RegisteredServiceProvider;

public class MoneyPerKill implements Listener {

    public static Economy econ = null;

    @EventHandler
    public void onPlayerDeathEvent(PlayerDeathEvent event) {
        Player player = event.getEntity();
        Player killer = player.getKiller();
        EconomyResponse r = econ.depositPlayer(killer, 10.00);
        if (r.transactionSuccess()) {
            killer.sendMessage(ChatColor.GOLD + "You recieved $10 for killing" +  player.getDisplayName());
        }
    }
}

In my main class Main.java I also registered the events.

And in Main.java I have:

public static Economy econ = null;
private boolean setupEconomy() {
    RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
    if (economyProvider != null) {
        econ = economyProvider.getProvider();
    }

    return (econ != null);
}

The problem is that it doesn't give the killer money when they kill a player.

Unihedron
  • 10,902
  • 13
  • 62
  • 72
User
  • 129
  • 2
  • 16
  • 2
    There's not enough information to diagnose the problem. I'm guessing your event handler crashes whenever a player dies, because somewhere in your code went wrong. If it's so, please post the stack trace, otherwise please add the relevant code used to load `setupEconomy()` and `MoneyPerKill`. Without sufficient compilable code for me to reproduce this, I can't help. Feel free to respond to this comment with `@Unihedron (comment text)` when you've done so. – Unihedron Oct 27 '14 at 08:10

2 Answers2

2

From what I can see, you're registering and referencing the Economy service object in Main.java's econ field, but in your MoneyPerKill.java PlayerDeathEvent listener, you're referencing econ as in MoneyPerKill's econ, which is null. So at this point in time, your code will throw NullPointerExceptions whenever a player dies.

To fix the issue, you need to reference Main.java's econ field. To do that, you type Main.econ.depositPlayer(killer, 10.00).

P.S. You should consider using your Main class instance instead of making static fields all over the place, but that's off topic.

Also consider what Rishaan Gupta said.

SupaHam
  • 1,125
  • 2
  • 12
  • 20
0

This may not be the solution to your problem but it should help prevent some errors. player.getKiller() returns the player if it is a player that killed the target player. If the killer isn't a player then it returns null.

Player getKiller() Gets the player identified as the killer of the living entity.

May be null.

Returns:

  • killer player, or null if none found

Source: https://hub.spigotmc.org/javadocs/spigot/org/bukkit/entity/LivingEntity.html#getKiller()

Try checking if the killer is null before proceding.

@EventHandler
public void onPlayerDeathEvent(PlayerDeathEvent event) {
    Player player = event.getEntity();
    Player killer = player.getKiller();
    /*
    * Checks if the killer is a player
    * If killer is null then the it is false
    * you can also use killer != null
    */
    if (killer instanceof Player) {
        EconomyResponse r = econ.depositPlayer(killer, 10.00);
        if (r.transactionSuccess()) {
            killer.sendMessage(ChatColor.GOLD + "You recieved $10 for killing" +  player.getDisplayName());
        }
    }

}
Rishaan Gupta
  • 563
  • 4
  • 18