Solved Command Not Doing Anything?

Discussion in 'Plugin Development' started by blok601, Jun 25, 2016.

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

    blok601

    Only two of the commands work:

    /kc stats
    and /kc

    Otherwise, when I run a command, nothing happens.

    Here is my code:
    Main Class:
    Code:
    package me.blok601.pandakc;
    
    import java.text.DecimalFormat;
    
    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.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin{
       
        private static Plugin plugin;
       
        public static Plugin getPlugin(){
            return plugin;
        }
       
        public static String prefix = ChatColor.WHITE + "" + ChatColor.BOLD + "[Panda]: ";
       
        public static int getKills(Player p){
            int kills  = getPlugin().getConfig().getInt(p.getUniqueId() + ".kills");
            return kills;
           
        }
       
        public static int getDeaths(Player p){
            int deaths  = getPlugin().getConfig().getInt(p.getUniqueId() + ".deaths");
            return deaths;
           
        }
       
        public static String getKD(Player p){
            double kd = getPlugin().getConfig().getDouble(p.getUniqueId() + ".kd");
            DecimalFormat df2 = new DecimalFormat("0.00");
            kd = getKills(p)/ (double) getDeaths(p);
            String fkills = df2.format(kd);
            return fkills;
        }
       
        public static void addKill(Player p){
            int kills = getKills(p);
            getPlugin().getConfig().set(p.getUniqueId() + ".kills", kills + 1);
            getPlugin().saveConfig();
        }
       
        public static void addDeath(Player p){
            int deaths = getDeaths(p);
            getPlugin().getConfig().set(p.getUniqueId() + ".deaths", deaths + 1);
            getPlugin().saveConfig();
        }
       
        public static void removeKill(Player p){
            int kills = getKills(p);
            getPlugin().getConfig().set(p.getUniqueId() + ".kills", kills - 1);
            getPlugin().saveConfig();
        }
       
        public static void removeDeath(Player p){
            int deaths = getDeaths(p);
            getPlugin().getConfig().set(p.getUniqueId() + ".deaths", deaths - 1);
            getPlugin().saveConfig();
        }
       
        @Override
        public void onEnable(){
            plugin = this;
            getServer().getPluginManager().registerEvents(new PlayerListener(), this);
            getConfig().options().copyDefaults(true);
            saveConfig();
            Bukkit.getLogger().info("Enabling Panda v1 - SideBar");
        }
       
        @Override
        @SuppressWarnings("deprecation")
        public boolean onCommand(CommandSender sender, Command cmd, String label, String args[]){
            if(cmd.getName().equalsIgnoreCase("kc")){
                if(sender instanceof Player){
                    Player p = (Player) sender;
                    if(args.length == 0){
                        if(p.hasPermission("panda.kc")){
                            p.sendMessage(prefix + "/kc stats <player> Leave blank for yourself");
                            p.sendMessage(ChatColor.GOLD + "-----|Admin Commands|-----");
                            p.sendMessage(prefix + "/kc addkill <player>");
                            p.sendMessage(prefix + "/kc removekill <player>");
                            p.sendMessage(prefix + "/kc adddeath <player>");
                            p.sendMessage(prefix + "/kc removedeath <player>");
                            return true;
                        }
                        p.sendMessage(prefix = "/kc stats <player> Leave blank for yourself");
                    }else if(args.length == 1){
                        if(args[0].equalsIgnoreCase("stats")){
                            p.sendMessage(ChatColor.GOLD + "-----|Stats|-----");
                            p.sendMessage(prefix  = "Player name: " + p.getName());
                            p.sendMessage(prefix  + "Kills: " + getKills(p));
                            p.sendMessage(prefix  + "Deaths: " + getDeaths(p));
                            p.sendMessage(prefix  + "K/D: " + getKD(p));
                        }
                    }else if(args.length == 2){
                        Player target = Bukkit.getPlayer(args[1]);
                        int kills = getKills(target);
                        int deaths = getDeaths(target);
                        String kd = getKD(target);
                        if(p.hasPermission("panda.kc")){
                            if(target == null){
                                p.sendMessage(ChatColor.RED + "That player couldn't be found!");
                                return false;
                            }
                            if(args[1].equalsIgnoreCase("addkill")){
                                addKill(target);
                                p.sendMessage(prefix + "Added a kill to " + target.getName() + "! They now have " + kills + " kills!");
                            }else if(args[1].equalsIgnoreCase("adddeath")){
                                addDeath(target);
                                p.sendMessage(prefix + "Added a death to " + target.getName() + "! They now have " + deaths + " deaths!");
                            }else if(args[1].equalsIgnoreCase("removedeath")){
                                removeDeath(target);
                                p.sendMessage(prefix + "Removed a death from " + target.getName() + "! They now have " + deaths + " deaths!");
                            }else if(args[1].equalsIgnoreCase("removekill")){
                                removeKill(target);
                                p.sendMessage(prefix + "Removed a kill from " + target.getName() + "! They now have " + kills + " kills!");
                            }
                        }
                    }
                }else{
                    sender.sendMessage(ChatColor.RED + "Only players can run this command!");
                    return false;
                }
            }
            return false;
           
        }
    
    }
    
    Listener Class:
    Code:
    package me.blok601.pandakc;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerKickEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.scoreboard.DisplaySlot;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Score;
    import org.bukkit.scoreboard.Scoreboard;
    import org.bukkit.scoreboard.ScoreboardManager;
    
    public class PlayerListener implements Listener{
       
        public static void SB(Player p){
            int kills = Main.getKills(p);
            int deaths = Main.getDeaths(p);
            ScoreboardManager manager = Bukkit.getScoreboardManager();
            String sname = ChatColor.WHITE + "" + ChatColor.BOLD + "Panda " + ChatColor.RED + "" + ChatColor.BOLD + "Arena";
            Scoreboard board = manager.getNewScoreboard();
            Objective obj = board.registerNewObjective("test", "dummy");
            obj.setDisplaySlot(DisplaySlot.SIDEBAR);
            obj.setDisplayName(sname);
           
            Score score = obj.getScore(ChatColor.GOLD + "Kills " + ChatColor.RED + "" + Main.getKills(p));
            score.setScore(0);
           
            Score s1 = obj.getScore(ChatColor.GOLD + "Deaths: " + ChatColor.RED + "" + Main.getDeaths(p));
            s1.setScore(-1);
           
            if(!(deaths == 0)){
                Score s2 = obj.getScore(ChatColor.GOLD + "K/D: " + ChatColor.RED + "" + Main.getKD(p));
                s2.setScore(-2);
            }else{
                Score s2 = obj.getScore(ChatColor.GOLD + "K/D: " + ChatColor.RED + "" + Main.getKD(p));
                s2.setScore(-2);
            }
           
            p.setScoreboard(board);
        }
       
        @EventHandler
        public void onJoin(PlayerJoinEvent e){
            for(Player p : Bukkit.getOnlinePlayers()){
                SB(p);
            }
        }
       
        @EventHandler
        public void onKick(PlayerKickEvent e){
            Player p = e.getPlayer();
            ScoreboardManager manager = Bukkit.getScoreboardManager();
            p.setScoreboard(manager.getNewScoreboard());
        }
       
        @EventHandler
        public void onLeave(PlayerQuitEvent e){
            Player p = e.getPlayer();
            ScoreboardManager manager = Bukkit.getScoreboardManager();
            p.setScoreboard(manager.getNewScoreboard());
        }
       
        @EventHandler
        public void onDeath(PlayerDeathEvent e){
            if(e.getEntity().getKiller() == null){
                return;
            }else{
                Player killer = e.getEntity().getKiller();
                Player p = e.getEntity();
               
                Main.addKill(killer);
                Main.addDeath(p);
               
            }
        }
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
    
    }
    
     
  2. Offline

    Zombie_Striker

    @blok601
    F=You need to null check target before you do anything with target. By getting the kills and deaths of a the target before nullchecking, you will get a npe if the player types in anything but a player that is online.

    Main problem: You are checking if args[1] is both a Player, and "addkill" or "addDeath". Most likely, you meant args[0].
     
  3. Offline

    blok601

    Yea I figured it out shortly after. Thanks for the help anyway though!
     
  4. Offline

    bwfcwalshy Retired Staff

    So much static abuse.... Why do you even have a getPlugin when you just static everything?
     
  5. Offline

    blok601

    So I can get the Config in the other class. As well as the other classes I am going to be adding.
     
  6. Offline

    bwfcwalshy Retired Staff

    @blok601 No need to use static for all that, just pass the instance.
     
Thread Status:
Not open for further replies.

Share This Page