Solved Currency & ChatManger Help

Discussion in 'Plugin Development' started by ItsJorden, Apr 20, 2015.

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

    ItsJorden

    Goal
    I am trying to make it so when a player speaks, it fetches their player coin amount. I am trying to do this by creating a custom command to add and remove coins.

    Problem
    Right now the command (/coins) currently will not add or remove coins. Also, when the coins are set in the config (Players are stored as UUID's) the first player that logs in has their set amount, as soon as player 2 - 10 joins everyone's coin amount is set to the newest joining player.

    Main Command: /coins add/remove [player] [amount]

    This is the code string that will be getting the player coins supposedly.
    Code:
    getConfig().getString(p.getUniqueId()
    /coins command
    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            @SuppressWarnings("deprecation")
            Player target = getServer().getPlayer(args[1]);
            System.out.println("Label: " + label);
            for (int i = 0; i < args.length; i++) {
                System.out.println("String at : " + i + " " + args[i]);
            }
            System.out.println("Command: " + cmd.getName());
            if (cmd.getName().equalsIgnoreCase("coins")) {
                if (args[0].equalsIgnoreCase("add")) {
                        giveCoins(target, money.get(target.getUniqueId()) + Integer.parseInt(args[2]));
                        saveConfig();
                }
          
                else if (args[0].equalsIgnoreCase("remove")) {
                        money.put(target.getUniqueId(), money.get(target.getUniqueId())-Integer.parseInt(args[2]));
                        saveConfig();
                }
                return true;
            }
            return false;       
        } 
    When the new player joins
    Code:
        public void onJoin(PlayerJoinEvent e) {
            p = e.getPlayer();
            if (!getConfig().contains(p.getUniqueId().toString())) {
                getConfig().set(p.getUniqueId() + ".Coins", 0);
                money.put(p.getUniqueId(), 0);
            } else {
                money.put(p.getUniqueId(), getConfig().getInt(p.getUniqueId() + ".Coins"));
            }
        }
    onEnable, onDisable, Variables and Hashmap
    Code:
        private Player p;
        private HashMap<UUID, Integer> money = new HashMap<>();
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this);
        }
        public void onDisable() {
            for (Entry<UUID, Integer> entry : money.entrySet()) {
                getConfig().set(entry.getKey() + ".Coins", entry.getValue());
            }
            saveConfig();
        }
    I would like to fetch the correct number of a players coins when they chat and get the adding and removeing of the coins fixed if possible.


    *Note*: Yes, I have been told I am recreating the wheel.

    EDIT: No console error at all.

    Just needing some code fixes that a beginner, like me, wouldn't quite know.

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

    Koobaczech

    Hey bud quick thing i see, is OnJoin you are not doing saveConfig(); after setting a players money. Next what is GiveCoins doing? Also do you need hash maps? An easy way of using the config would be
    Code:
    //Get the coin value in Integer
    getConfig().getInt(p.getUniqueId()+".Coins");
    
    //Set a coin value to existing value + new value
    getConfig().set(p.getUniqueId()+".Coins", getConfig().getInt(p.getUniqueId()+".Coins")+NewValue);
    
    //Set a coin value to a value
    getConfig().set(p.getUniqueId()+".Coins",  NewValue);
    
    //Proper reload. Reload First before save
    reloadConfig();
    saveConfig();
    
     
    Last edited: Apr 22, 2015
    ItsJorden likes this.
  3. Offline

    ItsJorden

    It should be getting Player UUID and sending a message to that user that the coins have been recieved. However, it never completes this for some reason.
     
  4. Offline

    Koobaczech

    Post up the code buddy! So i can see it. The GiveCoins. Try this out anyways
    Code:
    if (args[0].equalsIgnoreCase("add")) {
    if (args.length==3) {
    Player target = getServer().getPlayer(args[1]);
    getConfig().set(getConfig.getString(target.getUniqueId())+".Coins", getConfig().getInt(target.getUniqueId()+".Coins")+Integer.parseInt(args[2]));
    target.sendMessage("You have been given "+args[2]+" coins");
    saveConfig();
    }
    }
    
     
    Last edited: Apr 20, 2015
  5. Offline

    ItsJorden

    NEW CODE




    Code:
    package me.ItsJorden.ChatManager;
    
    import java.util.HashMap;
    import java.util.UUID;
    import java.util.Map.Entry;
    
    
    
    
    
    
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Chat extends JavaPlugin implements Listener {
    
        private Player p;
    
        private HashMap<UUID, Integer> money = new HashMap<>();
    
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this);
        }
    
        public void onDisable() {
            for (Entry<UUID, Integer> entry : money.entrySet()) {
                getConfig().set(entry.getKey() + ".Coins", entry.getValue());
            }
    
            saveConfig();
        }
    
        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
            p = e.getPlayer();
            if (!getConfig().contains(p.getUniqueId().toString())) {
                getConfig().set(p.getUniqueId() + ".Coins", 0);
                money.put(p.getUniqueId(), 0);
            } else {
                money.put(p.getUniqueId(), getConfig().getInt(p.getUniqueId() + ".Coins"));
                //Save
                saveConfig();
            }
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            @SuppressWarnings("deprecation")
            Player target = getServer().getPlayer(args[1]);
            System.out.println("Label: " + label);
            for (int i = 0; i < args.length; i++) {
                System.out.println("String at : " + i + " " + args[i]);
            }
            System.out.println("Command: " + cmd.getName());
            if (cmd.getName().equalsIgnoreCase("coins")) {
                if (args[0].equalsIgnoreCase("add")) {
                    if (args.length==3) {
                        Player target = getServer().getPlayer(args[1]);
                        getConfig().set(getConfig.getString(target.getUniqueId())+".Coins", getConfig().getInt(target.getUniqueId()+".Coins")+Integer.parseInt(args[2]));
                        target.sendMessage("You have been given the coins");
                        saveConfig();
                    }
                }
               
                else if (args[0].equalsIgnoreCase("remove")) {
                    Player target = getServer().getPlayer(args[1]);
                    money.put(target.getUniqueId(), money.get(target.getUniqueId())-Integer.parseInt(args[2]));
                    saveConfig();
                }
               
                else if (args[0].equalsIgnoreCase("set")) {
                    Player target = getServer().getPlayer(args[1]);
                    getConfig().set(getConfig.getString(p.getUniqueId())+".Coins",  NewValue);
                    saveConfig();
                }
                return true;
            } 
            return false;            
        } 
       
        @EventHandler
        public void onChat(AsyncPlayerChatEvent event) {
            // p is not identified as a player D:
            if (event.getPlayer().hasPermission("chat.normal")) {
                event.setFormat("" + ChatColor.YELLOW + getConfig().getInt(p.getUniqueId()+".Coins") + ChatColor.DARK_GRAY + ChatColor.BOLD + " | " + ChatColor.WHITE + "%s" + ChatColor.GOLD + ChatColor.BOLD + " > " + ChatColor.WHITE + "%s");
            }
            if (event.getPlayer().hasPermission("chat.donor")) {
                event.setFormat("" + ChatColor.YELLOW + getConfig().getInt(p.getUniqueId()+".Coins") + ChatColor.DARK_GRAY + ChatColor.BOLD + " | " + ChatColor.AQUA + "Donor" + ChatColor.DARK_GRAY + ChatColor.BOLD + " | " + ChatColor.DARK_AQUA + "%s" + ChatColor.GOLD + ChatColor.BOLD + " > " + ChatColor.WHITE + "%s");
            }
            if (event.getPlayer().hasPermission("chat.mod")) {
                event.setFormat("" + ChatColor.YELLOW + getConfig().getInt(p.getUniqueId()+".Coins") + ChatColor.DARK_GRAY + ChatColor.BOLD + " | " + ChatColor.GREEN + "Mod" + ChatColor.DARK_GRAY + ChatColor.BOLD + " | " + ChatColor.DARK_GREEN + "%s" + ChatColor.GOLD + ChatColor.BOLD + " > " + ChatColor.WHITE + "%s");
            }
            if (event.getPlayer().hasPermission("chat.admin")) {
                event.setFormat("" + ChatColor.YELLOW + getConfig().getInt(p.getUniqueId()+".Coins") + ChatColor.DARK_GRAY + ChatColor.BOLD + " | " + ChatColor.DARK_RED + "Admin" + ChatColor.DARK_GRAY + ChatColor.BOLD + " | " + ChatColor.RED + "%s" + ChatColor.GOLD + ChatColor.BOLD + " > " + ChatColor.WHITE + "%s");
            }
            if (event.getPlayer().hasPermission("chat.owner")) {
                event.setFormat("" + ChatColor.YELLOW + getConfig().getInt(p.getUniqueId()+".Coins") + ChatColor.DARK_GRAY + ChatColor.BOLD + " | " + ChatColor.RED + "Owner" + ChatColor.DARK_GRAY + ChatColor.BOLD + " | " + ChatColor.DARK_RED + "%s" + ChatColor.GOLD + ChatColor.BOLD + " > " + ChatColor.WHITE + "%s");
            }
        }
    }
    
     
    Last edited: Apr 20, 2015
  6. Offline

    Koobaczech

    Ok so first
    Code:
     public void onJoin(PlayerJoinEvent e) {
            p = e.getPlayer();
            if (!getConfig().contains(p.getUniqueId().toString())) {
                getConfig().set(p.getUniqueId() + ".Coins", 0);
                money.put(p.getUniqueId(), 0);
            } else {
                money.put(p.getUniqueId(), getConfig().getInt(p.getUniqueId() + ".Coins"));
    //Save
    saveConfig();
            }
        }
    Next
    Code:
    if (args[0].equalsIgnoreCase("add")) {
        if (args.length==3) {
           Player target = getServer().getPlayer(args[1]);
           getConfig().set(getConfig.getString(target.getUniqueId())+".Coins", getConfig().getInt(target.getUniqueId()+".Coins")+.Integer.parseInt(args[2]));
           target.sendMessage("You have been given coins");
            saveConfig();
         }
    }
    
     
    Last edited: Apr 20, 2015
  7. Offline

    ItsJorden

    Shouldn't it be
    Code:
        if (args.length==2) { 
    because it is referencing off of args [0] args[1] and args [2] or have I mistaken?
     
  8. Offline

    Koobaczech

    Well those are three arguments! Argument 1 is args[0], 2 is args[1], and three is args[2]
     
  9. Offline

    ItsJorden

    Okies moving on....
     
  10. Offline

    Koobaczech

    Ok. So basically whenever you want to give or remove or set coins, get the amount from the configs, and if you do make a change save the config. Heres the remove. For reference /coins is the command, args[0] is remove, args[1] is the playername, args[2] is the amount
    Code:
    if (args[0].equalsIgnoreCase("remove")) {
        if (args.length==3) {
           Player target = getServer().getPlayer(args[1]);
           getConfig().set(getConfig.getString(target.getUniqueId())+".Coins",               getConfig().getInt(target.getUniqueId()+".Coins")-.Integer.parseInt(args[2]));
           target.sendMessage("You have lost "+args[2]+" coins");
            saveConfig();
         }
    }
     
  11. Offline

    ItsJorden

  12. @ItsJorden Hm. What is the problem anyway? Is it still the same problem? I'm sure I'll be able to help.
     
  13. Offline

    ItsJorden

    Im going to test it with the code provided by Koob, still has many errors.

    First off, it will not allow .getString to use UUID's? Second off, your free to help me out here.
     
    Last edited by a moderator: Apr 20, 2015
  14. Offline

    Koobaczech

    Let know if you need anything bud. Shouldn't be so difficult!
     
  15. Offline

    ItsJorden

    The method ggetString won't work with getConfig().set(getConfig().getString(target.getUniqueId())+".Coins", getConfig().getInt(target.getUniqueId()+".Coins")+Integer.parseInt(args[2]));
     
  16. Offline

    Koobaczech

    O. Um. @ItsJorden Maybe try using target.getUniqueId().toString()
    Code:
    getConfig().set(getConfig().getString(target.getUniqueId().toString())+".Coins", getConfig().getInt(target.getUniqueId().toString()+".Coins")+Integer.parseInt(args[2])); 
     
  17. Offline

    ItsJorden

    The attached picture are the errors I get that I quite don't know how to solve.

    Code:
    Code:
    package me.ItsJorden.ChatManager;
    import java.util.HashMap;
    import java.util.UUID;
    import java.util.Map.Entry;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    public class Chat extends JavaPlugin implements Listener {
        private Player p;
        private HashMap<UUID, Integer> money = new HashMap<>();
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this);
        }
        public void onDisable() {
            for (Entry<UUID, Integer> entry : money.entrySet()) {
                getConfig().set(entry.getKey() + ".Coins", entry.getValue());
            }
            saveConfig();
        }
        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
            p = e.getPlayer();
            if (!getConfig().contains(p.getUniqueId().toString())) {
                getConfig().set(p.getUniqueId() + ".Coins", 0);
                money.put(p.getUniqueId(), 0);
            } else {
                money.put(p.getUniqueId(), getConfig().getInt(p.getUniqueId() + ".Coins"));
                //Save
                saveConfig();
            }
        }
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("coins")) {
                if (args[0].equalsIgnoreCase("add")) {
                    if (args.length==3) {
                        Player target = getServer().getPlayer(args[1]);
                        getConfig().set(getConfig().getString(target.getUniqueId())+".Coins", getConfig().getInt(target.getUniqueId()+".Coins")+Integer.parseInt(args[2]));
                        target.sendMessage("You have recieved coins!");
                        saveConfig();
                    }
                }
             
                else if (args[0].equalsIgnoreCase("remove")) {
                    Player target = getServer().getPlayer(args[1]);
                    money.put(target.getUniqueId(), money.get(target.getUniqueId())-Integer.parseInt(args[2]));
                       target.sendMessage("You have lost "+args[2]+" coins");
                    saveConfig();
                }
             
                else if (args[0].equalsIgnoreCase("set")) {
                    Player target = getServer().getPlayer(args[1]);
                    getConfig().set(getConfig.getString(p.getUniqueId())+".Coins",  NewValue);
                    target.sendMessage("Your coins have been set to " + args[2] + "");
                    saveConfig();
                }
                return true;
            }
            return false;          
        }
     
        @EventHandler
        public void onChat(AsyncPlayerChatEvent event) {
            // p is not identified as a player D:
            if (event.getPlayer().hasPermission("chat.normal")) {
                event.setFormat("" + ChatColor.YELLOW + getConfig().getInt(p.getUniqueId()+".Coins") + ChatColor.DARK_GRAY + ChatColor.BOLD + " | " + ChatColor.WHITE + "%s" + ChatColor.GOLD + ChatColor.BOLD + " > " + ChatColor.WHITE + "%s");
            }
            if (event.getPlayer().hasPermission("chat.donor")) {
                event.setFormat("" + ChatColor.YELLOW + getConfig().getInt(p.getUniqueId()+".Coins") + ChatColor.DARK_GRAY + ChatColor.BOLD + " | " + ChatColor.AQUA + "Donor" + ChatColor.DARK_GRAY + ChatColor.BOLD + " | " + ChatColor.DARK_AQUA + "%s" + ChatColor.GOLD + ChatColor.BOLD + " > " + ChatColor.WHITE + "%s");
            }
            if (event.getPlayer().hasPermission("chat.mod")) {
                event.setFormat("" + ChatColor.YELLOW + getConfig().getInt(p.getUniqueId()+".Coins") + ChatColor.DARK_GRAY + ChatColor.BOLD + " | " + ChatColor.GREEN + "Mod" + ChatColor.DARK_GRAY + ChatColor.BOLD + " | " + ChatColor.DARK_GREEN + "%s" + ChatColor.GOLD + ChatColor.BOLD + " > " + ChatColor.WHITE + "%s");
            }
            if (event.getPlayer().hasPermission("chat.admin")) {
                event.setFormat("" + ChatColor.YELLOW + getConfig().getInt(p.getUniqueId()+".Coins") + ChatColor.DARK_GRAY + ChatColor.BOLD + " | " + ChatColor.DARK_RED + "Admin" + ChatColor.DARK_GRAY + ChatColor.BOLD + " | " + ChatColor.RED + "%s" + ChatColor.GOLD + ChatColor.BOLD + " > " + ChatColor.WHITE + "%s");
            }
            if (event.getPlayer().hasPermission("chat.owner")) {
                event.setFormat("" + ChatColor.YELLOW + getConfig().getInt(p.getUniqueId()+".Coins") + ChatColor.DARK_GRAY + ChatColor.BOLD + " | " + ChatColor.RED + "Owner" + ChatColor.DARK_GRAY + ChatColor.BOLD + " | " + ChatColor.DARK_RED + "%s" + ChatColor.GOLD + ChatColor.BOLD + " > " + ChatColor.WHITE + "%s");
            }
        }
    }
     

    Attached Files:

  18. Offline

    ItsJorden

    NewValue has nothing to reference it off of.
    Code:
        @SuppressWarnings("deprecation")
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("coins")) {
                if (args[0].equalsIgnoreCase("add")) {
                    if (args.length==3) {
                        Player target = getServer().getPlayer(args[1]);
                        getConfig().set(getConfig().getString(target.getUniqueId())+".Coins", getConfig().getInt(target.getUniqueId()+".Coins")+Integer.parseInt(args[2]));
                        target.sendMessage("You have recieved coins!");
                        saveConfig();
                    }
                }
              
                else if (args[0].equalsIgnoreCase("remove")) {
                    Player target = getServer().getPlayer(args[1]);
                    money.put(target.getUniqueId(), money.get(target.getUniqueId())-Integer.parseInt(args[2]));
                      target.sendMessage("You have lost "+ args[2] + " coins");
                    saveConfig();
                }
              
                else if (args[0].equalsIgnoreCase("set")) {
                    Player target = getServer().getPlayer(args[1]);
                    getConfig().set(getConfig().getString(p.getUniqueId())+".Coins",  NewValue);
                    target.sendMessage("Your coins have been set to " + args[2] + "");
                    saveConfig();
                }
    
    Also getString is still not applicable for the argument of UUID in the type MemorySection.
    @CodePlaysMinecraft @Koobaczech
    Code:
                      getConfig().set(getConfig().getString(target.getUniqueId())+".Coins", getConfig().getInt(target.getUniqueId()+".Coins")+Integer.parseInt(args[2]));
    
    Help pls.
     
  19. Offline

    Koobaczech

    Sorry for the late reply. OG code had a lot of errors i missed. This new code works perfectly, and checks for correct input including the right numbers, and if that player is online. Gluck broski
    Code:
        if (cmd.getName().equalsIgnoreCase("coins")) {
            if (args.length==3) {
                try {
                  Integer.parseInt(args[2]);
                  } catch(NumberFormatException e) {
                        sender.sendMessage("Please use a number");
                        return false;
                  }        
                Player target = getServer().getPlayer(args[1]);
                if (target!=null&&target.isOnline()) {
                    if (args[0].equalsIgnoreCase("add")) {
                        if (Integer.parseInt(args[2])<0) {
                          sender.sendMessage("Please use a non-negative number");
                              return true;
                          }
                          getConfig().set(target.getUniqueId()+".Coins", getConfig().getInt(target.getUniqueId()+".Coins")+Integer.parseInt(args[2]));
                          target.sendMessage("You have recieved "+Integer.parseInt(args[2])+" coins!");
                          saveConfig();
                      }         
    
                      else if (args[0].equalsIgnoreCase("remove")) {
                          if (getConfig().getInt(target.getUniqueId()+".Coins")-Integer.parseInt(args[2])<=0) {
                              getConfig().set(target.getUniqueId()+".Coins", 0);
                              target.sendMessage("You have lost all your coins");
                              saveConfig();
                              return true;
                          }
                          else
                          getConfig().set(target.getUniqueId()+".Coins", getConfig().getInt(target.getUniqueId()+".Coins")-Integer.parseInt(args[2]));
                          target.sendMessage("You have lost "+Integer.parseInt(args[2])+" coins");
                          saveConfig();
                      }
              
    
                      else if (args[0].equalsIgnoreCase("set")) {
                          if (Integer.parseInt(args[2])<0) {
                              sender.sendMessage("Please use a non-negative number");
                              return true;
                          }
                          getConfig().set(target.getUniqueId().toString()+".Coins", Integer.parseInt(args[2]));
                          target.sendMessage("Your coin balance has been set to "+Integer.parseInt(args[2]));
                          saveConfig();
                      }   
                }
              }
              else
                  sender.sendMessage("Please do /coins add|remove|set <player> <amount>");
              return true;
          }
    
     
    Last edited: Apr 22, 2015
  20. Offline

    ItsJorden

    Thanks very much my problem has been solved so far
    @Koobaczech

    EDIT: I still have one more bug. Everytime I stop and start back up the server the number of coins defaults back to 0.

    Along with that, with multiple players online it shows everyone's as value of zero.

    The only function to set a player's value to zero is when they join.
    The command function shouldn't be activated on login.

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

    Koobaczech

    Alright, everytime you call a saveConfig(); all your values are written and safe. If you plan on going straight with configs, i don't see a need to also use hash maps. Thats like a backup for your backups, idk i like working with configs directly. Others might like to load all values into game variables and work off them, but i like the idea of using configs for everything. So id remove this hashmap so we don't use it, and then remove this for{ } code on disable, all your values don't get lost
    Code:
    private HashMap<UUID, Integer> money =new HashMap<>();//remove this
    for (Entry<UUID, Integer> entry : money.entrySet()) {
                getConfig().set(entry.getKey() + ".Coins", entry.getValue());
            }//remove all this too
    Then on PlayerJoin
    Code:
    @EventHandler
    public void onJoin(PlayerJoinEvent e) {
            p = e.getPlayer();
            if (getConfig().get(p.getUniqueId())==null) {
                getConfig().set(p.getUniqueId()+".Coins", 0);
                //money.put(p.getUniqueId(), 0);
                saveConfig();
                p.sendMessage("All your moneys are belong to server");
            }
    }
     
    Last edited: Apr 22, 2015
  22. Offline

    ItsJorden

    The part of (getConfig().get(p.getUniqueId())==null){ shows not applicable for UUID again UGH
     
  23. Offline

    Msrules123

    Well, from what I see, "p":

    p = e.getPlayer(); //In the onJoin method

    isn't ever defined, this could be causing you an error.
     
  24. Offline

    ItsJorden

    @Msrules123 True. But I am not seeing errors on the JavaDoc's part nor the Console

    What do you think @Koobaczech
     
    Last edited: Apr 24, 2015
  25. Offline

    ItsJorden

  26. Offline

    ItsJorden

    Anyone? I feel like this is dying.
     
  27. Offline

    Gater12

    @ItsJorden
    ConfigurationSection#get accepts a String as a parameter, not a UUID. Invoke toString on the UUID.

    What is your current code looking like?
     
  28. Offline

    ItsJorden

    Here's my current code:
    Code:
    package me.ItsJorden.ChatManager;
    
    import java.util.HashMap;
    import java.util.UUID;
    import java.util.Map.Entry;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Chat extends JavaPlugin implements Listener {
    
        private Player p;
    
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this);
        }
        public void onDisable() {
            saveConfig();
        }
        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
                p = e.getPlayer();
                if (getConfig().get(p.getUniqueId())==null) {
                    getConfig().set(p.getUniqueId()+".Coins", 0);
                    //money.put(p.getUniqueId(), 0);
                    saveConfig();
                    p.sendMessage("All your moneys are belong to server");
                }
        }
        @SuppressWarnings("deprecation")
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("coins")) {
                if (args.length==3) {
                    try {
                      Integer.parseInt(args[2]);
                      } catch(NumberFormatException e) {
                            sender.sendMessage("" + ChatColor.GOLD + ChatColor.BOLD + "CC ->" + ChatColor.YELLOW + "Please use a number");
                            return false;
                      }       
                    Player target = getServer().getPlayer(args[1]);
                    if (target!=null&&target.isOnline()) {
                        if (args[0].equalsIgnoreCase("add")) {
                            if (Integer.parseInt(args[2])<0) {
                              sender.sendMessage("" + ChatColor.GOLD + ChatColor.BOLD + "CC -> " + ChatColor.YELLOW + "Please use a non-negative number");
                                  return true;
                              }
                              getConfig().set(target.getUniqueId()+".Coins", getConfig().getInt(target.getUniqueId()+".Coins")+Integer.parseInt(args[2]));
                              target.sendMessage("" + ChatColor.GOLD + ChatColor.BOLD + "CC ->" + ChatColor.YELLOW + "You have recieved " + ChatColor.RED + Integer.parseInt(args[2]) + ChatColor.YELLOW + " coins!");
                              saveConfig();
                          }       
        
                          else if (args[0].equalsIgnoreCase("remove")) {
                              if (getConfig().getInt(target.getUniqueId()+".Coins")-Integer.parseInt(args[2])<=0) {
                                  getConfig().set(target.getUniqueId()+".Coins", 0);
                                  target.sendMessage("" + ChatColor.GOLD + ChatColor.BOLD + "CC ->" + ChatColor.RED + "You have lost all your coins");
                                  saveConfig();
                                  return true;
                              }
                              else
                              getConfig().set(target.getUniqueId()+".Coins", getConfig().getInt(target.getUniqueId()+".Coins")-Integer.parseInt(args[2]));
                              target.sendMessage("" + ChatColor.GOLD + ChatColor.BOLD + "CC ->" + ChatColor.YELLOW + "You have lost "+ ChatColor.RED +Integer.parseInt(args[2])+ ChatColor.YELLOW + " coins");
                              saveConfig();
                          }
                
        
                          else if (args[0].equalsIgnoreCase("set")) {
                              if (Integer.parseInt(args[2])<0) {
                                  sender.sendMessage("" + ChatColor.GOLD + ChatColor.BOLD + "CC ->" + ChatColor.RED + "Please use a non-negative number");
                                  return true;
                              }
                              getConfig().set(target.getUniqueId().toString()+".Coins", Integer.parseInt(args[2]));
                              target.sendMessage("" + ChatColor.GOLD + ChatColor.BOLD + "CC ->" + " Your coin balance has been set to "+Integer.parseInt(args[2]));
                              saveConfig();
                          } 
                    }
                  }
                  else
                      sender.sendMessage("" + ChatColor.YELLOW + ChatColor.BOLD + "Chat Coins");
                      sender.sendMessage("" + ChatColor.AQUA + ChatColor.BOLD + ChatColor.STRIKETHROUGH + "------------------");
                      sender.sendMessage("" + ChatColor.LIGHT_PURPLE + ChatColor.BOLD + "Main Command:" + ChatColor.GOLD + " /coins");
                      sender.sendMessage("" + ChatColor.RED + ChatColor.BOLD + "Args:" + ChatColor.BLUE + " add|remove|set" + ChatColor.GRAY + " [PlayerName] [Amount]");
                  return true;
              }
        
            return false;            
        }
       
        @EventHandler
        public void onChat(AsyncPlayerChatEvent event) {
            // p is not identified as a player D:
            if (event.getPlayer().hasPermission("chat.normal")) {
                event.setFormat("" + ChatColor.RED + "» " + ChatColor.YELLOW + getConfig().getInt(p.getUniqueId()+".Coins") + ChatColor.DARK_GRAY + " | " + ChatColor.GRAY + "%s" + ChatColor.AQUA + " » " + ChatColor.WHITE + "%s");
            }
            if (event.getPlayer().hasPermission("chat.donor")) {
                event.setFormat("" + ChatColor.RED + "» " + ChatColor.YELLOW + getConfig().getInt(p.getUniqueId()+".Coins") + ChatColor.DARK_GRAY + " | " + ChatColor.AQUA + "Donor" + ChatColor.DARK_GRAY + " | " + ChatColor.DARK_AQUA + "%s" + ChatColor.AQUA + " » " + ChatColor.WHITE + "%s");
            }
            if (event.getPlayer().hasPermission("chat.mod")) {
                event.setFormat("" + ChatColor.RED + "» " + ChatColor.YELLOW + getConfig().getInt(p.getUniqueId()+".Coins") + ChatColor.DARK_GRAY + " | " + ChatColor.GREEN + "Mod" + ChatColor.DARK_GRAY + " | " + ChatColor.DARK_GREEN + "%s" + ChatColor.AQUA + " » " + ChatColor.WHITE + "%s");
            }
            if (event.getPlayer().hasPermission("chat.admin")) {
                event.setFormat("" + ChatColor.RED + "» " + ChatColor.YELLOW + getConfig().getInt(p.getUniqueId()+".Coins") + ChatColor.DARK_GRAY + " | " + ChatColor.DARK_RED + "Admin" + ChatColor.DARK_GRAY + " | " + ChatColor.RED + "%s" + ChatColor.AQUA + " » " + ChatColor.WHITE + "%s");
            }
            if (event.getPlayer().hasPermission("chat.owner")) {
                event.setFormat("" + ChatColor.RED + "» " + ChatColor.YELLOW + getConfig().getInt(p.getUniqueId()+".Coins") + ChatColor.DARK_GRAY + " | " + ChatColor.RED + "Owner" + " | " + ChatColor.DARK_RED + "%s" + ChatColor.AQUA + " » " + ChatColor.WHITE + "%s");
            }
        }
    }
    Accidentally posted twice :p

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

    ItsJorden

    So. Um. My problem is still not fixed. It is continuing to say that it needs another value when getting the config for the player when they first join.
     
Thread Status:
Not open for further replies.

Share This Page