Solved Leveling System Help

Discussion in 'Plugin Development' started by NinjaRoyal, Aug 9, 2014.

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

    NinjaRoyal

    Hello bukkit, I have recently been working on a server and found a bug that I cannot squash:

    This is how I make my config.yml:
    Code:java
    1. @EventHandler
    2. public void playerJoin(PlayerJoinEvent e) {
    3. if(!getConfig().isSet(e.getPlayer().getName() + ".WoodXp")) {
    4. getConfig().set(e.getPlayer().getName() + ".WoodXp", 0);
    5. saveConfig();
    6. reloadConfig();
    7. }
    8. if(!getConfig().isSet(e.getPlayer().getName() + ".WoodLevel")) {
    9. getConfig().set(e.getPlayer().getName() + ".WoodLevel", 0);
    10. }
    11. }

    This makes the config look like so:

    NinjaRoyal:
    WoodXp: 1600
    WoodLevel: 0
    This is my code for leveling up woodcutting xp:
    Code:java
    1. @EventHandler
    2. public void onWoodcutting(BlockBreakEvent e) {
    3. int w1 = 100;
    4. if (e.getBlock().getType() == Material.LOG) {
    5. Player p = (Player) e.getPlayer();
    6. e.setCancelled(true);
    7. Random r = new Random();
    8. double chance = r.nextDouble();
    9. if (chance <= 0.10 && p instanceof Player) {
    10. p.sendMessage(ChatColor.DARK_AQUA + "100" + ChatColor.AQUA + " XP has been added to: " + ChatColor.GREEN.toString() + ChatColor.BOLD + "Woodcutting");
    11. getConfig().set(p.getName() + ".WoodXp", getConfig().getInt(p.getName() + ".WoodXp", 0) + 100);
    12. saveConfig();
    13. return;
    14. }
    15. if (chance <= 0.20 && p instanceof Player) {
    16. p.sendMessage(ChatColor.DARK_AQUA + "110" + ChatColor.AQUA + " XP has been added to: " + ChatColor.GREEN.toString() + ChatColor.BOLD + "Woodcutting");
    17. getConfig().set(p.getName() + ".WoodXp", getConfig().getInt(p.getName() + ".WoodXp", 0) + 110);
    18. saveConfig();
    19. return;
    20. }
    21. if (chance <= 0.30 && p instanceof Player) {
    22. p.sendMessage(ChatColor.DARK_AQUA + "120" + ChatColor.AQUA + " XP has been added to: " + ChatColor.GREEN.toString() + ChatColor.BOLD + "Woodcutting");
    23. getConfig().set(p.getName() + ".WoodXp", getConfig().getInt(p.getName() + ".WoodXp", 0) + 120);
    24. saveConfig();
    25. return;
    26. }
    27. if (chance <= 0.40 && p instanceof Player) {
    28. p.sendMessage(ChatColor.DARK_AQUA + "130" + ChatColor.AQUA + " XP has been added to: " + ChatColor.GREEN.toString() + ChatColor.BOLD + "Woodcutting");
    29. getConfig().set(p.getName() + ".WoodXp", getConfig().getInt(p.getName() + ".WoodXp", 0) + 130);
    30. saveConfig();
    31. return;
    32. }
    33. if (chance <= 0.50 && p instanceof Player) {
    34. p.sendMessage(ChatColor.DARK_AQUA + "140" + ChatColor.AQUA + " XP has been added to: " + ChatColor.GREEN.toString() + ChatColor.BOLD + "Woodcutting");
    35. getConfig().set(p.getName() + ".WoodXp", getConfig().getInt(p.getName() + ".WoodXp", 0) + 140);
    36. saveConfig();
    37. return;
    38. }
    39. if (chance <= 0.60 && p instanceof Player) {
    40. p.sendMessage(ChatColor.DARK_AQUA + "150" + ChatColor.AQUA + " XP has been added to: " + ChatColor.GREEN.toString() + ChatColor.BOLD + "Woodcutting");
    41. getConfig().set(p.getName() + ".WoodXp", getConfig().getInt(p.getName() + ".WoodXp", 0) + 150);
    42. saveConfig();
    43. return;
    44. }
    45. if (chance <= 0.70 && p instanceof Player) {
    46. p.sendMessage(ChatColor.DARK_AQUA + "160" + ChatColor.AQUA + " XP has been added to: " + ChatColor.GREEN.toString() + ChatColor.BOLD + "Woodcutting");
    47. getConfig().set(p.getName() + ".WoodXp", getConfig().getInt(p.getName() + ".WoodXp", 0) + 160);
    48. saveConfig();
    49. return;
    50. }
    51. if (chance <= 0.80 && p instanceof Player) {
    52. p.sendMessage(ChatColor.DARK_AQUA + "170" + ChatColor.AQUA + " XP has been added to: " + ChatColor.GREEN.toString() + ChatColor.BOLD + "Woodcutting");
    53. getConfig().set(p.getName() + ".WoodXp", getConfig().getInt(p.getName() + ".WoodXp", 0) + 170);
    54. saveConfig();
    55. return;
    56. }
    57. if (chance <= 0.90 && p instanceof Player) {
    58. p.sendMessage(ChatColor.DARK_AQUA + "180" + ChatColor.AQUA + " XP has been added to: " + ChatColor.GREEN.toString() + ChatColor.BOLD + "Woodcutting");
    59. getConfig().set(p.getName() + ".WoodXp", getConfig().getInt(p.getName() + ".WoodXp", 0) + 180);
    60. saveConfig();
    61. return;
    62. }
    63. if (chance <= 1 && p instanceof Player) {
    64. p.sendMessage(ChatColor.DARK_AQUA + "190" + ChatColor.AQUA + " XP has been added to: " + ChatColor.GREEN.toString() + ChatColor.BOLD + "Woodcutting");
    65. getConfig().set(p.getName() + ".WoodXp", getConfig().getInt(p.getName() + ".WoodXp", 0) + 190);
    66. saveConfig();
    67. return;
    68. }
    69.  
    70.  
    71. }
    72. }

    All of this works perfectly fine except for 1 thing that I want to add.
    Basicly I want to detect if WoodXp is equal to a certain integer (say 500) then it would send me a message saying you have leveled up and maybe play a sound effect. Everything I try seems to give me errors like cannot go from string to int or something like that.
    How it should work:
    After my WoodXp reachs a certain int such as 500 it would change my WoodLevel to 2.
    Sort of like this:
    Code:java
    1. if (config.getInt(p.getName + ".WoodXp") == 500) {
    2. config.set(p.getName + ".WoodLevel", 2);
    3. }


    but that seems to give me an error? If there is a better way to do this please tell me or if you know why it wont work.
     
  2. Offline

    Skionz

    whats the error your getting?
     
  3. Offline

    LavaisWatery

    You should look into using SQL instead of configs. As for your problem you should first go about posting the StackTrace.
     
  4. Offline

    IkBenHarm

    NinjaRoyal
    also, use saveConfig();
    (if you using default one)
     
  5. Offline

    NinjaRoyal

    Skionz
    IkBenHarm
    LavaisWatery

    Code:
    if (config.getInt(p.getName() + ".WoodXp") == 500 && config.getInt(p.getName() + ".WoodXp") < 1000) {
                    config.set(p.getName() + ".WoodLevel", 2);
                    Bukkit.getServer().broadcastMessage("debug");
                }
    That code seems to work except how would I make it so it doesn't broadcast a debug msg everytime? Because I want it to do some code ONLY ONE when the WoodXp reachs 500 or more. If I do it this way it would only do it if the xp is 500 and if it is above then it would do nothing. So something like WoodXp >= 500 but then it would broadcast everytime I mine wood from 500-1000 which would be extremely annoying :p
     
  6. Offline

    Skionz

    I'm not sure if im understanding correctly but you want it to only broadcast when you are at level 1 and at 500 xp? If this is the case then you could do something like this
    Code:
    if(config.getInt(p.getName() + ".WoodXp") >= 500 && config.getInt(p.getName() + ".WoodLevel") == 1) {
        //do stuff
    }
     
  7. Offline

    NinjaRoyal

    Skionz
    Ah! Didn't think about that one :p Now I feel like I'm stupid. Thanks so much for the help though. Still yet to test so will post results and the working code soon.

    Skionz
    http://prntscr.com/4binp7

    Getting a weird exception whenever I punch :confused:?

    Code:java
    1. @EventHandler
    2. public void onInteract(PlayerInteractEvent e) {
    3. final Player p = (Player) e.getPlayer();
    4. if (p.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.WHITE + "Rag") && e.getAction().equals(Action.RIGHT_CLICK_AIR)) {
    5. if (!(bleed.contains(p.getName()))) {
    6. p.sendMessage(ChatColor.RED + "I am not wounded!");
    7. return;
    8. }
    9. p.sendMessage(ChatColor.GREEN + "You begin to bandage yourself.");
    10. patching.add(p.getName());
    11. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    12. public void run() {
    13. if (bleed.contains(p.getName()) && patching.contains(p.getName())){
    14. ItemStack is = p.getItemInHand();
    15. if(is != null) {
    16. if(is.getAmount() > 1)
    17. is.setAmount(is.getAmount() - 1);
    18. else p.setItemInHand(null);
    19. }
    20. p.updateInventory();
    21. bleed.remove(p.getName());
    22. patching.remove(p.getName());
    23. p.sendMessage(ChatColor.GREEN + "You temporarily patch up your wounds.");
    24. }
    25. }
    26. }, 200L);

    That is my code for where the error might be? Idk it says something about line 149 and this is the code there. Sorry not so good at reading stack traces.

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

    Skionz

    NinjaRoyal
    It looks like it is just a null pointer error which means you forgot to check if something was null before checking for a value or something around those lines. Anyway which line is 149? From what I'm seeing you should probably check if the item hasItemMeta before checking for the display name
    Code:
     if (p.getItemInHand().hasItemMeta && p.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.WHITE + "Rag") && e.getAction().equals(Action.RIGHT_CLICK_AIR)) {
      //stuff
    }
     
  9. Offline

    NinjaRoyal

    Skionz
    Ok I will test this later but in a more important matter another thing I am having trouble with is:
    Code:java
    1. //give xp\\
    2. //woodcutting
    3. if (cmd.getName().equalsIgnoreCase("woodcutting")) {
    4. if (args.length < 2) {
    5. sender.sendMessage(ChatColor.RED + "/woodcutting [player] [amount]");
    6. return true;
    7. }
    8. @SuppressWarnings("deprecation")
    9. Player xp = (Player) Bukkit.getServer().getPlayer(args[1]);
    10. Player p = (Player) sender;
    11. int newxp = config.getInt(xp.getName() + ".WoodXp" + args[3]);
    12. if (!(xp instanceof Player)) {
    13. return true;
    14. }
    15. if (!(p instanceof Player)) {
    16. return true;
    17. }
    18. getConfig().set(p.getName() + ".WoodXp", newxp);
    19. saveConfig();
    20. reloadConfig();
    21. return true;
    22. }

    That is my code for setting the WoodXp on a player. But when i do the command it gives me a error in the console:
    http://prntscr.com/4bj763
    A unhandled exception error.
    Also the code for the error when I punch doesn't work either. Still giving me an exception when I punch.
     
  10. Offline

    Skionz

    Code:
    /woodcutting [player] [amount]
    /woodcutting is the command, [player] is args[0] and amount is args[1]. the arguments start at 0 not 1.

    But one question, currently you are setting the woodcutting xp to the player who sent the command, shouldn't you be setting the woodcutting xp to whoever is args[0]? Sorry if this is confusing ill give an example

    player1 executes the command /woodcutting player2 500. Right now this would give player1 500 xp not player2.
    so to fix this you would change
    Code:
     getConfig().set(p.getName() + ".WoodXp", newxp);
    to
    Code:
     getConfig().set(xp.getName() + ".WoodXp", newxp);
    right now p is the player who sent the command and xp is args[0]

    EDIT: and i believe this line
    Code:
    int newxp = config.getInt(xp.getName() + ".WoodXp" + args[3]);
    should be this
    Code:
    int newxp = config.getInt(xp.getName() + ".WoodXp") + args[1];
     
  11. Offline

    NinjaRoyal

    Skionz
    Oooooh ok I understand. Let me test this out

    Skionz Ok so it works but it always sets it to 0?
    code:
    Code:java
    1. //give xp\\
    2. //woodcutting
    3. if (cmd.getName().equalsIgnoreCase("woodcutting")) {
    4. if (args.length < 1) {
    5. sender.sendMessage(ChatColor.RED + "/woodcutting [player] [amount]");
    6. return true;
    7. }
    8. @SuppressWarnings("deprecation")
    9. Player xp = (Player) Bukkit.getServer().getPlayer(args[0]);
    10. Player p = (Player) sender;
    11. int newxp = config.getInt(xp.getName() + ".WoodXp" + args[1]);
    12. if (!(xp instanceof Player)) {
    13. return true;
    14. }
    15. if (!(p instanceof Player)) {
    16. return true;
    17. }
    18. getConfig().set(xp.getName() + ".WoodXp", newxp);
    19. p.sendMessage(ChatColor.DARK_AQUA + ");
    20. saveConfig();
    21. reloadConfig();
    22. return true;
    23. }


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

    Skionz

    What do you mean it always sets it to 0?
    and you have a little error right here, you forgot an end quote
    Code:
    p.sendMessage(ChatColor.DARK_AQUA + ");
    so the quote is stopping your config from saving and reloading.
     
  13. Offline

    NinjaRoyal

    Skionz
    I just did that to test but it isn't working without it. And basicly when ever I do the command it sets my "WoodXp" In the config to 0 no matter what. So if I do the command /woodcutting ninjaboy323 100 it would set it to 0 rather then 100.
     
  14. Offline

    xTigerRebornx

    NinjaRoyal
    Code:
    config.getInt(xp.getName() + ".WoodXp" + args[1]);
    Not sure what you are doing here, but I am guessing this path doesn't exist, returning 0.
     
  15. Offline

    NinjaRoyal

    xTigerRebornx
    If you are talking about the WoodXp path it exists because it is setting it to 0. But it should be setting it to what I specify in arg 1.
     
  16. Offline

    Skionz

    NinjaRoyal
    I'm not sure but I think args[1] is currently a string so you would have to use Integer.parseInt(args[1]) to change it to a int. Although im not sure
     
  17. Offline

    NinjaRoyal

    Skionz
    xTigerRebornx
    I tryed parsing and did some debug test and it seems that it thinks arg[1] is equal to 0. I did some message tests and that was the result.
    Code:java
    1. //give xp\\
    2. //woodcutting
    3. if (cmd.getName().equalsIgnoreCase("woodcutting")) {
    4. if (args.length < 1) {
    5. sender.sendMessage(ChatColor.RED + "/woodcutting [player] [amount]");
    6. return true;
    7. }
    8. @SuppressWarnings("deprecation")
    9. Player xp = (Player) Bukkit.getServer().getPlayer(args[0]);
    10. Player p = (Player) sender;
    11. Integer.parseInt(args[1]);
    12. int newxp = config.getInt(xp.getName() + ".WoodXp" + args[1]);
    13. if (!(xp instanceof Player)) {
    14. return true;
    15. }
    16. if (!(p instanceof Player)) {
    17. return true;
    18. }
    19. getConfig().set(xp.getName() + ".WoodXp", newxp);
    20. p.sendMessage(ChatColor.DARK_AQUA.toString() + args[1] + ChatColor.AQUA + " has been added to " + ChatColor.GREEN.toString() + ChatColor.BOLD + args[0]);
    21. saveConfig();
    22. reloadConfig();
    23. return true;
    24. }


    This is all my current code. I need someone to help me please :(
     
  18. Offline

    Skionz

    I believe you have to set parseInt to a variable, try this.
    Code:
    Int xpToAdd = Integer.parseInt(args[1]);
    int newxp = config.getInt(xp.getName() + ".WoodXp" + xpToAdd);
    
     
  19. Offline

    NinjaRoyal

    Skionz
    Int cannot be resolved to a type. But integer works shall I do that?
     
  20. Offline

    Skionz

    NinjaRoyal it was a typo sorry "Int" should be "int" so change it to
    Code:
    int xpToAdd = Integer.parseInt(args[1]);
     
  21. Offline

    NinjaRoyal

    Skionz
    Oh ok, integer didn't work so let me try that and I will get back to you

    Skionz
    Still sets it to 0 :(

    Skionz
    I don't really need this command is there a way I can change this in the config? Because if I try to change the values in the config it just resets it. I also tryed making a command with the code:
    Code:java
    1. saveConfig();
    2. reloadConfig();

    But doesn't seem to work

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

    Skionz

    NinjaRoyal do you have saveConfig() on disable?
     
  23. Offline

    NinjaRoyal

  24. Offline

    Skionz

  25. Offline

    NinjaRoyal

    Skionz
    Code:java
    1. @EventHandler
    2. public void playerJoin(PlayerJoinEvent e) {
    3. if(!getConfig().isSet(e.getPlayer().getName() + ".WoodXp")) {
    4. getConfig().set(e.getPlayer().getName() + ".WoodXp", 0);
    5. saveConfig();
    6. reloadConfig();
    7. }
    8. if(!getConfig().isSet(e.getPlayer().getName() + ".WoodLevel")) {
    9. getConfig().set(e.getPlayer().getName() + ".WoodLevel", 1);
    10. }
    11. }

    I have that code but when I reset the config.yml and relogged it only created WoodXp and not WoodLevel?
    Also if I remove saveConfig(); on disable wouldn't that make it so it never saves if I disable server?

    Skionz
    hmmm it seems that It makes WoodLevel when I run the command that saves and reloads config.

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

    Skionz

    Save whenever you change something something with getConfig.set() but don't save onDisable() because if you save onDisable() it will get rid of any changes you made to the config by hand
     
  27. Offline

    NinjaRoyal

    Everything is solved. Thanks for everyone who commented and helped me :D
     
Thread Status:
Not open for further replies.

Share This Page