Where can I put this method?

Discussion in 'Plugin Development' started by JTGaming2012, Jul 21, 2014.

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

    JTGaming2012

    Hey there, I have this coins plugin which keeps track of players coins, adds coins, removes coins, does a lot of stuff with coins. Point is, I need to save these coins in a config file. I have a method to do this, but I don't know where to put it. Because it needs access to the config file, it throws and error when it is in a static method. I also need like it to save on a regular basis, like auto-saving...

    Here is my whole plugin....
    Thanks for your help guys!

    Code:java
    1. package me.jack.coins;
    2.  
    3. import java.util.HashMap;
    4. import java.util.logging.Logger;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12. import org.bukkit.scheduler.BukkitRunnable;
    13.  
    14. public class Coins extends JavaPlugin {
    15.  
    16. public final Logger logger = Logger.getLogger("Minecraft");
    17. public static Coins plugin;
    18.  
    19. public final static HashMap<String, Integer> c = new HashMap<String, Integer>();
    20.  
    21. public final MyApi api = new MyApi();
    22.  
    23. public void onDisable() {
    24. plugin = null;
    25. }
    26.  
    27. public void onEnable() {
    28. plugin = this;
    29. getConfig().options().copyDefaults(true);
    30. saveConfig();
    31. for(String s : getConfig().getKeys(true)) {
    32. Player p = Bukkit.getServer().getPlayer(s);
    33. c.put(p.getName(), getConfig().getInt(p.getName()));
    34. }
    35. }
    36.  
    37.  
    38. @Override
    39. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    40. Player p = (Player) sender;
    41. if(commandLabel.equalsIgnoreCase("coins")) {
    42. if(c.containsKey(p.getName())) {
    43. ChatUtils.message("You have " + ChatColor.BLUE + c.get(p.getName()) + ChatColor.WHITE + " coins!", p);
    44. } else {
    45. ChatUtils.message("Have no coins!", p);
    46. }
    47. } else if(commandLabel.equalsIgnoreCase("addcoins") && p.hasPermission("coins.add")) {
    48. if(args.length == 2) {
    49. Player targetPlayer = this.getServer().getPlayer(args[0]);
    50. int a = Integer.parseInt(args[1]);
    51. MyApi.addCoins(p, a);
    52. ChatUtils.message("You have given " + ChatColor.BLUE + a + ChatColor.WHITE + " coins to " + ChatColor.BLUE + targetPlayer.getName(), p);
    53. ChatUtils.message("You have recieved " + ChatColor.BLUE + a + ChatColor.WHITE + " coins!", targetPlayer);
    54. } else {
    55. ChatUtils.message("Not enough arguments!", p);
    56. }
    57. } else if(commandLabel.equalsIgnoreCase("removecoins") && p.hasPermission("coins.remove")) {
    58. if(args.length == 2) {
    59. Player targetPlayer = this.getServer().getPlayer(args[0]);
    60. int a = Integer.parseInt(args[1]);
    61. MyApi.removeCoins(p, a);
    62. ChatUtils.message("You have removed " + ChatColor.BLUE + a + ChatColor.WHITE + " coins from " + ChatColor.BLUE + targetPlayer.getName(), p);
    63. ChatUtils.message(ChatColor.BLUE + "" + a + ChatColor.WHITE + " coins have been removed from you!", targetPlayer);
    64. } else {
    65. ChatUtils.message("Not enough arguments!", p);
    66. }
    67. }
    68. return false;
    69.  
    70. }
    71. }
    72.  
    73. package me.jack.coins;
    74.  
    75. import org.bukkit.entity.Player;
    76.  
    77.  
    78. public class MyApi {
    79.  
    80. public static boolean hasCoins(Player p, int a) {
    81. if(Coins.c.get(p.getName()) >= a) {
    82. return true;
    83. } else {
    84. return false;
    85. }
    86. }
    87.  
    88. public static void addCoins(Player p, int a) {
    89. if(Coins.c.containsKey(p.getName())) {
    90. Coins.c.put(p.getName(), Coins.c.get(p.getName()) + a);
    91. } else {
    92. Coins.c.put(p.getName(), a);
    93. }
    94. }
    95.  
    96. public static void removeCoins(Player p, int a) {
    97. if(hasCoins(p, a)) {
    98. Coins.c.put(p.getName(), Coins.c.get(p.getName()) - a);
    99. } else {
    100. Coins.c.put(p.getName(), 0);
    101. }
    102. }
    103.  
    104. }
     
  2. Offline

    DrEinsteinium

    JTGaming2012 You should make the API class an enum singleton :) I don't really see a lot of bukkit programmers that use this, but it's a really easy way to create an easy-access utility class in my opinion. It will remove the need for your static methods as well(which are a lot of times good to avoid using anyways). It's also easy to setup:

    Code:
    public enum MyAPI
    {
        INSTANCE;
     
        public void method1()
        {}
    }
    
    Now you can access any public, non-static methods in MyAPI by using this: MyAPI.INSTANCE.method1();
     
  3. Offline

    Zettelkasten

    Just call MyApp something like CoinManager, make all the methods non-static and make it take your plugin in the constructor: public CoinManager(Coins plugin) { .. }
    This way you can access the plugin with its configuration through a variable.
     
  4. Offline

    DrEinsteinium

    Zettelkasten It looks like he is doing that, but in the code he is just calling the static methods and not the instance of his API.

    In your onCommand method, instead of using MyAPI.method(), use api.method(). Also, remove static from all of the methods in your api class.
     
Thread Status:
Not open for further replies.

Share This Page