Solved Stats plugin no errors in eclipse or server console.

Discussion in 'Plugin Development' started by DamnHippo, Apr 25, 2014.

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

    DamnHippo

    I created a stats plugin today where a player types /stats and it would show them there Deaths and kills aswell as there XP. I also added a /stats (Player) command but for some reason when I type /stats in minecraft nothing pops up :( Here is my code.


    Code removed.
     
  2. Offline

    xTigerRebornx

    DamnHippo Debug your code. Print out checks, find out where it is stopping, and try and figure out why
     
  3. Offline

    itzrobotix

    Make sure you commands are registered, your command executor is set in your onEnable() etc. Also I'm sure your way of using a config works for storing information, but let me present a maybe easier and cleaner way to do it. Create a class like a settings manager and for kills, deaths etc create a file and file config like;
    Code:java
    1. private FileConfiguration killsconfig;
    2. private File killsfile;
    3. private FileConfiguration deathsconfig;
    4. private File deathsfile;
    5. private Plugin p;
    6.  

    Then create a setup method and in it put this;
    Code:java
    1. if(!p.getDataFolder().exists()) p.getDataFolder().mkdir();
    2. killsfile = new File(p.getDataFolder(), "kills.yml");
    3. if(!killsfile.exists()){
    4. try{ killsfile.createNewFile(); }
    5. catch(Exception e) { e.printStackTrace(); }
    6. }
    7. if(!p.getDataFolder().exists()) p.getDataFolder().mkdir();
    8. deathsfile = new File(p.getDataFolder(), "deaths.yml");
    9. if(!deathsfile.exists()){
    10. try{ deathsfile.createNewFile(); }
    11. catch(Exception e) { e.printStackTrace(); }
    12. }

    This basically creates the database folder if it already hasn't, then creates the file if it already hasn't.
    Then say below;
    Code:java
    1. killsconfig = YamlConfiguration.loadConfiguration(killsfile);
    2. deathsconfig = YamlConfiguration.loadConfiguration(deathsfile);

    Now call this setup method in the onEnable(), on a side note it should be something like this;
    Code:java
    1. public void setup(Plugin p){
    2. this.p = p;
    3. }

    Then create methods for getting and setting paths in the file like this;
    Code:java
    1.  
    2. public int getKills(String p){
    3. return killsconfig.getInt("kills." + p);
    4. }
    5. public void addKills(String p){
    6. setKills(p, getKills(p) + 1);
    7. }
    8. public void setKills(String p, int amount){
    9. killsconfig.set("kills." + p, amount);
    10. saveKills();
    11. }
    12. public int getDeaths(String p){
    13. return deathsconfig.getInt("deaths." + p);
    14. }
    15. public void addDeaths(String p){
    16. setDeaths(p, getDeaths(p) + 1);
    17. }
    18. public void setDeaths(String p, int amount){
    19. deathsconfig.set("deaths." + p, amount);
    20. saveDeaths();
    21. }
    22. public void saveKills(){
    23. try{ killsconfig.save(killsfile); }
    24. catch(Exception e) { e.printStackTrace(); }
    25. }
    26. public void saveDeaths(){
    27. try{ deathsconfig.save(deathsfile); }
    28. catch(Exception e) { e.printStackTrace(); }
    29. }
     
  4. Offline

    xTigerRebornx

    DamnHippo Debugging your code is a basic you must know how to do and it will help you fix a vast amount of problems you may have in the future, that is probably the best thing for you to do.
    Another thing I'd say is that we need your class that extends JavaPlugin aswell as the one provided
     
  5. Offline

    itzrobotix

    That is because using saveConfig() or saveDefaultConfig() does that for you, however my way is more, hmm controllable in a sense.

    Code:java
    1. getCommand("help").setExecutor(new Help());
    2. getCommand("promote").setExecutor(new Promote());
    3. getCommand("me").setExecutor(new Me());
    4. getCommand("msg").setExecutor(new Msg());
    5. getCommand("killme").setExecutor(new killme());
    6. getCommand("kill").setExecutor(new kill());
    7. getCommand("slap").setExecutor(new Slap());
    8. getCommand("rules").setExecutor(new rules());
    9. getCommand("banish").setExecutor(new banish());
    10. getCommand("kit").setExecutor(new kit());
    11. getCommand("potion").setExecutor(new potion());
    12. getCommand("stats").setExecutor(new stats(this));

    Maybe because it is given "this" and all the others aren't?

    Normally when you use "this" it refers to the body of text for example in the main class it could refer to a plugin or a listener, is that what you mean? What is the actual parameter it takes?

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

    xTigerRebornx

    itzrobotix 'this' refers to the current object it is in, which in the OPs case, his class that extends JavaPlugin. Since his constructor takes in the same object that he is referring to with this, it is a valid use. The keyword 'this' is not the problem.
    DamnHippo Like I've said before, you need to debug your code to see where it gets stuck. Whether this be a simple Bukkit.broadcastMessage() or a System.out.println(), you need something to indicate each step of the program and those messages will tell you where it gets stuck
     
    itzrobotix likes this.
  7. Offline

    itzrobotix

    Thanks for clearing that up. On a side note that is the first thing he should have done. To see where the code stops, very useful and obvious.
     
  8. Offline

    xTigerRebornx

    itzrobotix It is indeed the first thing that he should've done, and the first thing I suggested (which he may or may not have completely ignored my suggestion)
     
  9. Offline

    itzrobotix

    Then he wonders why he isn't getting anywhere.
     
  10. Offline

    Booshayy

    That's an issue. No curly bracket.
     
  11. Offline

    itzrobotix

    Are you trying to imply that I do? Also you asked us for help, if you think we are bugging you trying to teach you how to help yourself, sling your hook.
     
  12. Offline

    Chew

    Hippo again, I'm on 24/7 most of the time.~ I can always help.
     
Thread Status:
Not open for further replies.

Share This Page