Currency Command

Discussion in 'Plugin Development' started by MCJoshua345, Jan 31, 2015.

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

    MCJoshua345

    Hi! Today I don't have an error of any sort (Hallelujah! I stink at understanding bug fixes!). I have programmed a bit of currency. I have stored said currency for the player in the config. When you hit someone, it gives you one gold. (Have that in another class.) I want to make 2 commands. /givegold, and /takegold. The format is this:
    /givegold {playername} {amount}
    /takegold {playername} {amount}
    Here's the class:
    Code:
    package com.mcjlobby;
    
    import org.bukkit.Bukkit;
    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.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Currency extends JavaPlugin{
    
    @EventHandler
    public void onPJoin(PlayerJoinEvent e) {
      Player p = e.getPlayer();
      String pname = p.getName();     
           e.setJoinMessage(null);
           if(p.hasPlayedBefore() ==false){
               Bukkit.broadcastMessage("" + ChatColor.AQUA + ChatColor.BOLD + pname + ChatColor.RESET + ChatColor.GOLD + " has joined the server for the first time!");
               p.sendMessage("" + ChatColor.GOLD + "Welcome to the server " + ChatColor.AQUA + ChatColor.BOLD + pname + ChatColor.RESET + ChatColor.GOLD + "! You were given 500 gold for your first time joining!");
               p.sendMessage("" + ChatColor.GOLD + "You can earn gold by popping people! To pop someone, go up to them and hit them!");
               p.sendMessage("" + ChatColor.GOLD + "You can use gold to buy cosmetics like swords, armor, hats! To see this message again, type /tutorial!");
               if(!getConfig().contains(p.getName())){
                      getConfig().set(p.getName() + ".Gold", 500);}
           else if(p.hasPlayedBefore() ==true){
                    p.sendMessage("" + ChatColor.GOLD + "Welcome back to " + ChatColor.AQUA + "AtlantisPVP" + ChatColor.BOLD + pname + ChatColor.RESET + ChatColor.GOLD + "!" );
           }
           }
      }
    
    
          
    public void giveGold(Player p, int i){
        getConfig().set(p.getName() + ".Gold", getConfig().getInt(p.getName() + ".Gold", 0) + i);
        saveConfig();
        p.sendMessage("§6$" + i + " gold received.");
      
    }
    public void takeGold(Player p, int i){
        getConfig().set(p.getName() + ".Gold", getConfig().getInt(p.getName() + ".Gold", 0) - i);
        saveConfig();
        p.sendMessage("§4$" + i + " gold taken.");
      
    }
    @SuppressWarnings("deprecation")
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            Player p = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("givegold")) {
                int length = args.length;
                if(length == 1) {
                    boolean pFound = false;
                    for(Player pToGive : Bukkit.getServer(). getOnlinePlayers()) {
                        if(getConfig().equals(p.getName() + ".Gold")) {
                            pToGive.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("GiveGoldMessage") + p.getName() + (getConfig().getString("GiveGoldMessageEnd"))));
                            pFound = true;
                            break;
                              
                            }
    
                    }
                  
                    if(pFound == false){
                        p.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("OfflinePlayerMessage")));
                      
                    }
                  
                }
                        }
            return true;
                      
                      
                    }{
    
                }
    {
          
    }
    }
    Hope you can help!
    If there's any other files you need, like the Pop class, just tell me!
     
    Last edited: Jan 31, 2015
  2. Offline

    timtower Administrator Moderator

  3. Offline

    MCJoshua345

    @timtower
    Because this is the Plugin Development section...
    And I need help making that command...
     
  4. Offline

    SuperOriginal

    @MCJoshua345
    What is that?
     
  5. Offline

    MCJoshua345

    @SuperOriginal
    p.getName() = Player's Name
    ".Gold" = New Line and on that new Line the text is this "Gold: {number}"
     
  6. Offline

    SuperOriginal

    @MCJoshua345 I know. But you're checking if getConfig() (which returns a YamlConfiguration) is equal to a string. That's what confuses me.
     
  7. Offline

    MCJoshua345

    Learned it from a tutorial: www.youtube.com/watch?v=BjyJvm1KL4g
    Would this be better?:
    Code:
    package com.mcjlobby;
    
    import java.util.HashMap;
    import java.util.UUID;
    import java.util.Map.Entry;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Gold extends JavaPlugin implements Listener {
    
        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() + ".Gold", entry.getValue());
            }
    
            saveConfig();
        }
    
        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
            Player p = e.getPlayer();
    
            if (!getConfig().contains(p.getUniqueId().toString())) {
                getConfig().set(p.getUniqueId() + ".Gold", 0);
                money.put(p.getUniqueId(), 0);
            } else {
                money.put(p.getUniqueId(), getConfig().getInt(p.getUniqueId() + ".Gold"));
            }
        }
    
           public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
                Player p = (Player) sender;
                if(cmd.getName().equalsIgnoreCase("tsgold")) {
                    if(p.hasPermission("lobby.tsgold"))
                    takeGold(p, 500);
                        return true;}
                if(cmd.getName().equalsIgnoreCase("gsgold")) {
                    if(p.hasPermission("lobby.gsgold"))
                    giveGold(p, 500);
                    return true;
                }
                {
                return false;
                  
                }
              
                  
    
              
              
                  
        }
              
          
      
    
        private void giveGold(Player p, int i) {
            UUID uuid = p.getUniqueId();
            money.put(uuid, money.get(uuid) + i);
            p.sendMessage("§6$" + i + " Gold received!");
        }
    
        private void takeGold(Player p, int i) {
            UUID uuid = p.getUniqueId();
            money.put(uuid, money.get(uuid) - i);
            p.sendMessage("§4$" + i + " Gold taken!");
        }
    
    @SuppressWarnings("deprecation")
    @EventHandler
    public void onAttemptPop(EntityDamageByEntityEvent event){
        Entity attacker = event.getDamager();
        Entity entity = event.getEntity();
        if (entity instanceof Player && attacker instanceof Player){
            final Player target = (Player) entity;
            final Player popper = (Player) attacker;
            try {
            } catch (Exception e) {
                e.printStackTrace();
            }
            popper.hidePlayer(target);
            popper.sendMessage(ChatColor.GOLD + "" + "You just popped " + target.getName() + " and recieved 1 Gold!");
            event.setCancelled(true);
            popper.sendMessage(ChatColor.GOLD + "" + "You just popped " + target.getName() + " and recieved 1 Gold!");
            giveGold(popper, 1);
            Bukkit.getServer().getScheduler().scheduleAsyncDelayedTask(this, new Runnable(){
                public void run(){
                    popper.showPlayer(target);
                }
    
            },60L);
    
        }
    
    }
    }
    Still need help with the actual command though.
     
    Last edited: Jan 31, 2015
  8. Offline

    uksspy

    This try-catch is empty.

    Also, you should change the "§4" to ChatColor.RED.
    I see you used ChatColor later in the code so I'm confused as to why you used the color codes in a different place.
     
  9. Offline

    Skionz

    @MCJoshua345 Look into command arguments, but make sure to check before casting. The CommandSender may not be a Player instance resulting in a ClassCastException.
     
  10. Offline

    Synic

    I'm going to give you some psuedo code.

    1) Check if the command's name is "givegold".
    2) Check if the user has the permission node to execute this command.
    3) Find out how many arguments this command will have.
    - I'm assuming there would be two arguments: ./givegold <player> <amount>
    4) Check if the args.length equals "2".
    5) You can set your first argument, "<player>" with this:
    - Player target = Bukkit.getPlayer(args[0]);
    6) Check if the player is online and not null.
    7) Check if the second argument, "<amount>" is an integer with this:
    - try {Integer.parseInt(args[1]) }
    catch(NumberFormatException) { <send the player a message to tell them that they must enter a number> }
    8) Set a variable to the <amount>:
    - int amount = Integer.parseInt(args[1]);
    9) Check if the amount is greater than zero.
    10) Use your public void giveGold(Player player, int amount) method to give them the desired gold.
    11) Repeat for "takegold".
     
  11. Offline

    MCJoshua345

    @uksspy I just follow whatever the person does in the tutorial, soooooo yeah. Fixed it, new code that includes everything to do with Gold:

    Code:
             @SuppressWarnings("deprecation")
            @EventHandler
             public void onAttemptPop(EntityDamageByEntityEvent event){
                 Entity attacker = event.getDamager();
                 Entity entity = event.getEntity();
                 if (entity instanceof Player && attacker instanceof Player){
                     final Player target = (Player) entity;
                     final Player popper = (Player) attacker;
                     popper.hidePlayer(target);
                     popper.sendMessage(ChatColor.GOLD + "" + "You just popped " + target.getName() + " and recieved 1 Gold!");
                     event.setCancelled(true);
                     popper.sendMessage(ChatColor.GOLD + "" + "You just popped " + target.getName() + " and recieved 1 Gold!");
                     giveGold(popper, 1);
                     Bukkit.getServer().getScheduler().scheduleAsyncDelayedTask(this, new Runnable(){
                         public void run(){
                             popper.showPlayer(target);
                         }
    
                     },60L);
    
                 }
    
             }
    
                {}
                 public void giveGold(Player p, int i){
                         getConfig().set(p.getName() + ".Gold", getConfig().getInt(p.getName() + ".Gold", 0) + i);
                         saveConfig();
                         p.sendMessage(ChatColor.GOLD + "$" + i + " gold received.");
                       
                      }
                     public void takeGold(Player p, int i){
                         getConfig().set(p.getName() + ".Gold", getConfig().getInt(p.getName() + ".Gold", 0) - i);
                         saveConfig();
                         p.sendMessage(ChatColor.DARK_RED + "$" + i + " gold taken.");
               
            
             {
            
              String pname = p.getName();     
                  if(p.hasPlayedBefore() ==false){
                      Bukkit.broadcastMessage("" + ChatColor.AQUA + ChatColor.BOLD + pname + ChatColor.RESET + ChatColor.GOLD + " has joined the server for the first time!");
                      p.sendMessage("" + ChatColor.GOLD + "Welcome to the server " + ChatColor.AQUA + ChatColor.BOLD + pname + ChatColor.RESET + ChatColor.GOLD + "! You were given 500 gold for your first time joining!");
                      p.sendMessage("" + ChatColor.GOLD + "You can earn gold by popping people! To pop someone, go up to them and hit them!");
                      p.sendMessage("" + ChatColor.GOLD + "You can use gold to buy cosmetics like swords, armor, hats! To see this message again, type /tutorial!");
                      if(!getConfig().contains(p.getName())){
                             getConfig().set(p.getName() + ".Gold", 500);}
                             saveConfig(); }
                  else if(p.hasPlayedBefore() ==true){
                           p.sendMessage("" + ChatColor.GOLD + "Welcome back to " + ChatColor.AQUA + "AtlantisPVP" + ChatColor.BOLD + pname + ChatColor.RESET + ChatColor.GOLD + "!" ); }}
                         
              }
    @Skionz Thanks! I'll look into that!
     
  12. Offline

    Skionz

    @MCJoshua345 You should figure out what everything does before blindly following someone.
     
  13. Offline

    uksspy

    @MCJoshua345
    @Skionz is right. Following tutorials are fine, just make sure you know what everything does. Otherwise it will come back to haunt you.
     
Thread Status:
Not open for further replies.

Share This Page