Subtract money from a player when they're killed?

Discussion in 'Plugin Development' started by MaxNatural, Jun 21, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    MaxNatural

    I have coded this class and i can get the player kills another player money and the message comes up but when the other player dies their money is not taken away.

    Code:java
    1. package me.max;
    2.  
    3. import net.milkbowl.vault.economy.Economy;
    4. import net.milkbowl.vault.economy.EconomyResponse;
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.entity.PlayerDeathEvent;
    11. import org.bukkit.plugin.RegisteredServiceProvider;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14. public class Eco extends JavaPlugin implements Listener {
    15.  
    16. public static Economy econ = null;
    17. public static EconomyResponse r;
    18.  
    19. public void onEnable() {
    20. if (!setupEconomy() ) {
    21. getLogger().severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
    22. getServer().getPluginManager().disablePlugin(this);
    23. return;
    24. }
    25.  
    26. getLogger().info("Plugin has been enabled!");
    27. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    28. }
    29.  
    30. private boolean setupEconomy() {
    31. if (getServer().getPluginManager().getPlugin("Vault") == null) {
    32. return false;
    33. }
    34. RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
    35. if (rsp == null) {
    36. return false;
    37. }
    38. econ = rsp.getProvider();
    39. return econ != null;
    40. }
    41.  
    42. @EventHandler
    43. public void onPlayerDeath(PlayerDeathEvent e) {
    44. Player p = e.getEntity().getKiller();
    45.  
    46. if (e.getEntity().getKiller() instanceof Player) {
    47.  
    48. r = econ.depositPlayer(p.getName(), 5.00);
    49. if (r.transactionSuccess()) {
    50. p.sendMessage(ChatColor.GREEN + "You have recived 5 coins.");
    51. return;
    52. }
    53. else {
    54. p.sendMessage(ChatColor.RED + "An error occured when paying out your money. Please contact a server admin.");
    55. return;
    56. }
    57. }
    58.  
    59. r = econ.withdrawPlayer(p.getName(), 5.00);
    60. if (r.transactionSuccess()) {
    61. p.sendMessage(ChatColor.DARK_RED + "5 coins have been taken from your account!");
    62. return;
    63. }
    64. else {
    65. p.sendMessage(ChatColor.RED + "ECONOMY ISNT WORKING RIGHT! Please contact a server admin right away!");
    66. return;
    67. }
    68. }
    69.  
    70. }
     
  2. Offline

    St3venAU

    It's because after you deposit the money you have a return statement, it will never run the rest of your code. Take out all of the return statements from the onPlayerDeath method and it should work as intended.
     
  3. Offline

    MaxNatural

  4. Offline

    St3venAU

    Get the balance beforehand, if its 5 or more then deduct 5, if its less then 5 then deduct whatever balance they have.
     
  5. MaxNatural I dont have the api right now
    But Make A If Statement To Check If a player has more than 5 coins
     
  6. Offline

    MaxNatural

  7. MaxNatural Of Course I can.

    MaxNatural Try if(VaultEco.hasUnder(1) To Return nothing then make an else statement to take the money away

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  8. Offline

    MaxNatural

    Where would this go. This is my first plugin with Vault Herbert_The_Pervert

    St3venAU Code you show me with this code?

    Code:java
    1. package me.max;
    2.  
    3. import net.milkbowl.vault.economy.Economy;
    4. import net.milkbowl.vault.economy.EconomyResponse;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.entity.PlayerDeathEvent;
    11. import org.bukkit.plugin.RegisteredServiceProvider;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14. public class Eco extends JavaPlugin implements Listener {
    15.  
    16. public static Economy econ = null;
    17. public static EconomyResponse r;
    18.  
    19. public void onEnable() {
    20. if (!setupEconomy() ) {
    21. getLogger().severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
    22. getServer().getPluginManager().disablePlugin(this);
    23. return;
    24. }
    25.  
    26. getLogger().info("Plugin has been enabled!");
    27. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    28. }
    29.  
    30. private boolean setupEconomy() {
    31. if (getServer().getPluginManager().getPlugin("Vault") == null) {
    32. return false;
    33. }
    34. RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
    35. if (rsp == null) {
    36. return false;
    37. }
    38. econ = rsp.getProvider();
    39. return econ != null;
    40. }
    41.  
    42. @EventHandler
    43. public void onDeath(PlayerDeathEvent event) {
    44. //Get killed entity
    45. Player p = event.getEntity();
    46. //Get the killer
    47. Player killer = p.getKiller();
    48.  
    49. if(killer != null) {
    50. EconomyResponse playerResponse;
    51. EconomyResponse killerResponse;
    52.  
    53. //Withdraw money from the player
    54. playerResponse = econ.withdrawPlayer(p, 5D);
    55.  
    56. if(playerResponse.transactionSuccess()) {
    57. p.sendMessage("§7[§4-§7]5 coins!");
    58.  
    59. //Give the killer money
    60. killerResponse = econ.depositPlayer(killer, 5D);
    61.  
    62. if(killerResponse.transactionSuccess()) {
    63. killer.sendMessage("§7[§a+§7]5 coins!");
    64. }
    65. }
    66. }
    67. }
    68. }
    69.  


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  9. Offline

    mazentheamazin

    MaxNatural
    Most economy plugins do not allow players to go into the negative balances. If they do, its configurable.
     
  10. Offline

    MaxNatural

    mazentheamazin I will try it! :D

    mazentheamazin It worked thanks!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
Thread Status:
Not open for further replies.

Share This Page