Solved Help with Experience

Discussion in 'Plugin Development' started by AoH_Ruthless, Dec 24, 2013.

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

    AoH_Ruthless

    Alright, so I'm trying to get a player's experience level when they die and then add it to them when they respawn.

    My PlayerDeathEvent:
    Code:java
    1. @EventHandler (priority = EventPriority.HIGHEST)
    2. public void onDeath(PlayerDeathEvent e) {
    3. Player p = e.getEntity();
    4. if (p.hasPermission("pvp.vip")) {
    5. Random rand = new Random();
    6. int xp = rand.nextInt(100);
    7. if (xp <= 55) {
    8. int level = p.getLevel();
    9. exp.put(p.getName(), level);
    10. p.setExp(0);
    11. }
    12. Bukkit.broadcastMessage(xp + " XP; " + item + " ITEM;");
    13. }
    14. }


    The Respawn listener:
    Code:java
    1. @EventHandler (priority = EventPriority.HIGHEST)
    2. public void onRespawn(PlayerRespawnEvent e) {
    3. Player p = e.getPlayer();
    4. String name = p.getName();
    5. if (contents.containsKey(name)) {
    6. p.getInventory().setContents(contents.get(name));
    7. contents.remove(name);
    8. }
    9.  
    10. if (exp.containsKey(name)) {
    11. p.setLevel(exp.get(name));
    12. exp.remove(name);
    13. }
    14. }


    And now, the HashMap in which the level is stored:
    Code:java
    1. public HashMap<String, Integer> exp = new HashMap<String, Integer>();


    So, if I say had 10levels, and when I died the integer was 24 (less than 55), it broadcasts 24 correctly so I know I am doing that correctly. The code I have is either not storing the exp or it is not retrieving it properly. There isn't a stacktrace... Maybe I'm just too tired to think properly :/ and it could be an obvious error.

    Thank you for your assistance :).

    EDIT: And yes, the events are registered, plugin.yml is proper, all of that good stuff is setup.
     
  2. Offline

    jimuskin

  3. Offline

    AoH_Ruthless

    jimuskin
    Yes, I was testing it alone and I gave myself the permission. I thought I wrote to ignore it but I suppose not :eek:
     
  4. Offline

    jimuskin

    AoH_Ruthless Try adding debug lines to where you save the exp on death and retrieve it on respawn. From there, work out where the code isn't executing

    Ahh wait, I see. You are selecting a random integer and then saying there is only 55% chance of the thing to work. You are probably getting the 45% chance. Remove the line where it checks if the random int "x" is less or equal to 55
     
  5. Offline

    AoH_Ruthless

    jimuskin
    That second part didn't make sense to me. I want it to have a 55% chance of working. It is supposed to pick a random integer between 1 to 100, and then if the integer is less than 55 I want it to execute the code. The server broadcasts the integer as debug. In cases where it is less than 55, nothing executes.
     
  6. Offline

    jimuskin

    AoH_Ruthless Still, debug your code to see where your issues are at. For example:
    Code:java
    1. if (xp <= 55) {
    2. int level = p.getLevel();
    3. exp.put(p.getName(), level);
    4. System.out.println(exp.get(p.getName()));
    5. p.setExp(0);
    6. }
     
  7. Offline

    AoH_Ruthless

    jimuskin

    Code:
    [22:34:33 INFO] AoH_Ruthless issued server command: /suicide
    [22:34:33 INFO] 14
    [22:34:33 INFO] 53 XP;
    [22:34:33 INFO] AoH_Ruthless died
    [22:34:33 INFO] 14
    [22:34:33 INFO] 14
    14 is where the level is printing in ... 3 times like it is supposed to (updated code):
    Code:java
    1. @EventHandler (priority = EventPriority.HIGHEST)
    2. public void onDeath(PlayerDeathEvent e) {
    3. Player p = e.getEntity();
    4. if (p.hasPermission("pvp.vip")) {
    5. Random rand = new Random();
    6. int xp = rand.nextInt(100);
    7. if (xp <= 55) {
    8. int level = p.getLevel();
    9. exp.put(p.getName(), level);
    10. System.out.println(exp.get(p.getName())); //First println
    11. p.setExp(0);
    12. }
    13. Bukkit.broadcastMessage(xp + " XP");
    14. }
    15. }
    16.  
    17. @EventHandler (priority = EventPriority.HIGHEST)
    18. public void onRespawn(PlayerRespawnEvent e) {
    19. Player p = e.getPlayer();
    20. String name = p.getName();
    21. if (exp.containsKey(name)) {
    22. System.out.println(exp.get(p.getName())); //Second println
    23. p.setLevel(exp.get(name));
    24. System.out.println(p.getLevel()); //Third println
    25. exp.remove(name);
    26. }
    27. }
     
  8. Offline

    jimuskin

    AoH_Ruthless And the level's not showing up? Try making the debug code
    Code:
    System.out.println(p.getName() + ":" + p.getLevel())
    to make sure that the player isn't the wrong player (Yes, it sounds stupid but it happens).
     
  9. Offline

    AoH_Ruthless

    jimuskin
    Alright ... the println displayed "AoH_Ruthless:1" like it should.
     
  10. Offline

    jimuskin

    AoH_Ruthless hmmm, maybe instead of
    Code:
    p.setLevel(exp.get(name));
    it might need to be
    Code:
    p.setLevel(exp.get(p.getName()));
     
  11. Offline

    AoH_Ruthless

    jimuskin
    name is a cast for p.getName();
     
  12. Offline

    jimuskin

    AoH_Ruthless I can see, but try debugging one of the messages with name instead of p.getName()
     
  13. Offline

    AoH_Ruthless

    jimuskin
    I tried it.. nothing changed, and the println still showed AoH_Ruthless:(Xp level)
     
  14. Offline

    jimuskin

    AoH_Ruthless I'll have a little fiddle around with it - see what I can back to ya ;)

    AoH_Ruthless Fiddled with it a bit - come up with a solution:
    Code:java
    1. @EventHandler (priority = EventPriority.HIGHEST)
    2. public void onDeath(PlayerDeathEvent e) {
    3. Player p = e.getEntity();
    4. if (p.hasPermission("pvp.vip")) {
    5. Random rand = new Random();
    6. int xp = rand.nextInt(100);
    7. if (xp <= 55) {
    8. e.setKeepLevel(true);
    9. }
    10. Bukkit.broadcastMessage(xp + " XP");
    11. }
    12. }


    The respawn listener is not needed.

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

    AoH_Ruthless

    jimuskin
    I feel so stupid for not realizing there was such a thing as that. Thank you for your help!
     
Thread Status:
Not open for further replies.

Share This Page