Get an int value from a string

Discussion in 'Plugin Development' started by Signatured, Feb 1, 2015.

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

    Signatured

    I'm trying to make a PlayerDeathEvent where when a player dies, it gives the killer a set reward in the config. Here is my code:
    Code:java
    1.  
    2. package me.signatured.moneyondeath;
    3.  
    4. import net.milkbowl.vault.economy.Economy;
    5. import net.milkbowl.vault.economy.EconomyResponse;
    6.  
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.entity.PlayerDeathEvent;
    12.  
    13. public class PlayerKillEvent implements Listener {
    14.  
    15. SettingsManager settings = SettingsManager.getInstance();
    16. public static Economy econ = null;
    17.  
    18. @SuppressWarnings("deprecation")
    19. @EventHandler
    20. public void onKill(PlayerDeathEvent e) {
    21.  
    22. Player killed = e.getEntity();
    23.  
    24. if (killed.isDead()) {
    25. Player killer = e.getEntity().getKiller();
    26.  
    27. if (killer instanceof Player) {
    28.  
    29. int reward = settings.getConfig().getInt("Money per kill");
    30. EconomyResponse r = econ.depositPlayer(killer.getName(), reward);
    31.  
    32. if (r.transactionSuccess()) {
    33. killer.sendMessage(ChatColor.AQUA + "Killing " + ChatColor.GOLD + killed + ChatColor.AQUA + " got you " + ChatColor.GREEN + reward);
    34. killer.sendMessage(String.format(ChatColor.AQUA + "Killing " + ChatColor.GOLD + killed + ChatColor.AQUA + " got you " + "$" + "%s", econ.format(r.amount)));
    35. }
    36. else {
    37. killer.sendMessage(String.format("An error occured: %s", r.errorMessage));
    38. }
    39. }
    40. }
    41. }
    42. }
    43.  


    Here's my config:
    Code:
    Money per kill: 1
    
    Here is the error I'm getting: http://paste.md-5.net/yubesolede.avrasm

    Any idea why?
     
  2. Offline

    SuperOriginal

    Show your settingsmanager class
     
  3. Offline

    sirrus86

    Well, it's an NPE so I'd say either settings, settings.getConfig(), or settings.getConfig().getInt("Money per kill") is returning null. I'd try debugging each of those.
     
  4. Offline

    Signatured

    @SuperOriginal
    Code:java
    1.  
    2. package me.signatured.moneyondeath;
    3.  
    4. import java.io.File;
    5. import java.io.IOException;
    6.  
    7. import org.bukkit.Bukkit;
    8. import org.bukkit.ChatColor;
    9. import org.bukkit.configuration.file.FileConfiguration;
    10. import org.bukkit.configuration.file.YamlConfiguration;
    11. import org.bukkit.plugin.Plugin;
    12. import org.bukkit.plugin.PluginDescriptionFile;
    13.  
    14. public class SettingsManager {
    15.  
    16. private SettingsManager() { }
    17.  
    18. static SettingsManager instance = new SettingsManager();
    19.  
    20. public static SettingsManager getInstance() {
    21. return instance;
    22. }
    23.  
    24. Plugin p;
    25.  
    26. FileConfiguration config;
    27. File cfile;
    28.  
    29. FileConfiguration data;
    30. File dfile;
    31.  
    32. public void setup(Plugin p) {
    33. cfile = new File(p.getDataFolder(), "config.yml");
    34. config = p.getConfig();
    35. //config.options().copyDefaults(true);
    36. //saveConfig();
    37.  
    38. if (!p.getDataFolder().exists()) {
    39. p.getDataFolder().mkdir();
    40. }
    41.  
    42. dfile = new File(p.getDataFolder(), "data.yml");
    43.  
    44. if (!dfile.exists()) {
    45. try {
    46. dfile.createNewFile();
    47. }
    48. catch (IOException e) {
    49. Bukkit.getServer().getLogger().severe(ChatColor.RED + "Could not create data.yml!");
    50. }
    51. }
    52.  
    53. data = YamlConfiguration.loadConfiguration(dfile);
    54. }
    55.  
    56. public FileConfiguration getData() {
    57. return data;
    58. }
    59.  
    60. public void saveData() {
    61. try {
    62. data.save(dfile);
    63. }
    64. catch (IOException e) {
    65. Bukkit.getServer().getLogger().severe(ChatColor.RED + "Could not save data.yml!");
    66. }
    67. }
    68.  
    69. public void reloadData() {
    70. data = YamlConfiguration.loadConfiguration(dfile);
    71. }
    72.  
    73. public FileConfiguration getConfig() {
    74. return config;
    75. }
    76.  
    77. public void saveConfig() {
    78. try {
    79. config.save(cfile);
    80. }
    81. catch (IOException e) {
    82. Bukkit.getServer().getLogger().severe(ChatColor.RED + "Could not save config.yml!");
    83. }
    84. }
    85.  
    86. public void reloadConfig() {
    87. data = YamlConfiguration.loadConfiguration(cfile);
    88. }
    89.  
    90. public PluginDescriptionFile getDesc() {
    91. return p.getDescription();
    92. }
    93. }
    94.  
     
  5. Offline

    Skionz

    @Signatured Get rid of your setup method. That completely defeats the purpose of a constructor.
     
  6. Offline

    Signatured

    Thanks for the tip, removed the setup method. Happen to know why I'm getting the error I posted?
     
  7. Offline

    Skionz

    @Signatured Post the new class. 'getConfig()' was returning null because you were never invoking 'setup()'
     
  8. Offline

    Signatured

    @Skionz
    Code:java
    1.  
    2. package me.signatured.moneyondeath;
    3.  
    4. import net.milkbowl.vault.economy.Economy;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.plugin.RegisteredServiceProvider;
    8. import org.bukkit.plugin.java.JavaPlugin;
    9.  
    10. public class MoneyOnDeath extends JavaPlugin {
    11.  
    12. SettingsManager settings = SettingsManager.getInstance();
    13. public static Economy econ = null;
    14.  
    15. public void onEnable() {
    16. if (!setupEconomy() ) {
    17. getLogger().severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
    18. getServer().getPluginManager().disablePlugin(this);
    19. return;
    20. }
    21.  
    22. saveDefaultConfig();
    23. Bukkit.getServer().getPluginManager().registerEvents(new PlayerKillEvent(), this);
    24. }
    25.  
    26. private boolean setupEconomy() {
    27. if (getServer().getPluginManager().getPlugin("Vault") == null) {
    28. return false;
    29. }
    30. RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
    31. if (rsp == null) {
    32. return false;
    33. }
    34. econ = rsp.getProvider();
    35. return econ != null;
    36. }
    37. }
    38.  
     
Thread Status:
Not open for further replies.

Share This Page