Solved My Stats Plugin doesn't work!

Discussion in 'Plugin Development' started by Protom1234, Sep 6, 2015.

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

    Protom1234

    Hey there,

    I made my own /stats plugin, but it doesn't work properly. The /resetstats work but the /stats doesn't. Could someone take a look?

    Code:
    package server.feelthepowerpvp.stats;
    
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.Statistic;
    import org.bukkit.block.Block;
    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.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.permissions.Permission;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener {
       
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this); }
    
          public boolean onCommand(CommandSender sender, Command cmd, String commandlable, String[] args) {
                if (commandlable.equalsIgnoreCase("resetstats")) {
                    if (args.length == 0)
                    {
                      if ((sender instanceof Player))
                      {
                        Player p = (Player)sender;
                        p.setStatistic(Statistic.PLAYER_KILLS, 0);
                        p.setStatistic(Statistic.DEATHS, 0);
                        p.sendMessage(ChatColor.GREEN + "You have successfully reset your stats.!");
                      }
                      else
                      {
                        sender.sendMessage(ChatColor.RED + "This version of the command is not enabled for console!");
                      }
                    }
                    else if (args.length == 1) {
                      if (sender.hasPermission(new Permission("feelthepowerpvp.resetstats")))
                      {
                        Player p2 = Bukkit.getPlayer(args[0]);
                        if (p2 != null)
                        {
                          p2.setStatistic(Statistic.PLAYER_KILLS, 0);
                          p2.setStatistic(Statistic.DEATHS, 0);
                          sender.sendMessage(ChatColor.GREEN + "You have successfully reset " + p2.getName() + "'s stats!");
                        }
                        else
                        {
                          sender.sendMessage(ChatColor.RED + "That player is not online!");
                        }
                      }
                      else
                      {
                        sender.sendMessage(ChatColor.RED + "You do not have permission to use this command!");
                      }
                    }
                  }
              if (commandlable.equalsIgnoreCase("stats"))
                {
                  Player p = (Player)sender;
                  if (args.length == 0)
                  {
                    p.sendMessage(ChatColor.RED + "---------\2476\247l[ Stats of " + p.getName() + " ]\247c---------");
                    p.sendMessage("");
                    p.sendMessage(ChatColor.GREEN + "Kills: " + ChatColor.YELLOW + getKills(p));
                    p.sendMessage("");
                    p.sendMessage(ChatColor.GREEN + "Deaths: " + ChatColor.YELLOW + getDeaths(p));
                    p.sendMessage("");
                    p.sendMessage(ChatColor.GRAY + "To reset your own stats, use /resetstats");
                    p.sendMessage(ChatColor.RED + "--------------------------------------------");
                    return true;
                  }
                  if (args.length == 1)
                  {
                    Player p1 = Bukkit.getPlayer(args[0]);
                    if (p1 == null)
                    {
                      p.sendMessage("┬žcPlayer does not exist");
                      return true;
                    }
                    p.sendMessage(ChatColor.RED + "---------\2476\247l[ Stats of " + args[0] + " ]\247c---------");
                    p.sendMessage("");
                    p.sendMessage(ChatColor.GREEN + "Kills: " + ChatColor.YELLOW + getKills(p1));
                    p.sendMessage("");
                    p.sendMessage(ChatColor.GREEN + "Deaths: " + ChatColor.YELLOW + getDeaths(p1));
                    p.sendMessage("");
                    p.sendMessage(ChatColor.GRAY + "To reset your own stats, use /resetstats");
                    p.sendMessage(ChatColor.RED + "--------------------------------------------");
                    return true;
                  }
                }
            return false;
              }
              public static int getKills(Player player) {
                  return player.getStatistic(Statistic.PLAYER_KILLS);
              }
              public static int getDeaths(Player player) {
                  return player.getStatistic(Statistic.DEATHS);
              }
              
              
        }
     
  2. Offline

    Lolmewn Retired Staff

    I don't even know where to start... My god that formatting. How can you live with that? :O
     
  3. Offline

    timtower Moderator Moderator

    Moved to plugin development.
    @Protom1234 What is /stats doing then instead of what you want it to do?
     
  4. Offline

    mine-care

    Always check before casting, you do for the first command but not the second one :/

    Dont use label string, use the Command#getName(); to allow compatibility with alias'es

    Err is this plugin decompiled or something? Why do you use the unicode colorcodes?

    Have you registered your commands in the plugin.yml?

    Also as @Lolmewn said, your code is not formated in such a way that it can be read easily. If you use eclipse, press Ctrl+Shift+F to allow eclipse to format your code.
     
  5. Offline

    RoboticPlayer

    Also, why are you even using a permissions API? You don't need to if all you are going to do is check if they have a permission. Just do
    Code:
    if (p.hasPermission("/*node*/") {
          //Do stuff
    }
     
  6. Offline

    Protom1234

    Easier to code, plus I understand it more :)

    Fixed my problem, thank you!
     
Thread Status:
Not open for further replies.

Share This Page