Solved Spamming messages?

Discussion in 'Plugin Development' started by shohouku, May 1, 2014.

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

    shohouku

    I get no errors, but I'm not sure if I should be putting doubles in a Random.

    Because when I kill an entity it spams this:

    0.1 Xp Recieved
    0.2 Xp Recieved
    0.5 Xp Recieved


    It should be only playing 1 message..

    Code:java
    1. @EventHandler
    2. public void onKill(EntityDeathEvent e){
    3. if(e.getEntity() instanceof Monster){
    4. Monster m = (Monster) e.getEntity();
    5. if(m.getKiller() instanceof Player){
    6.  
    7. Player p = m.getKiller();
    8. Random random = new Random();
    9. int randomNum = random.nextInt(100) + 1;
    10. if (randomNum <= 30) {
    11. giveXp(p, 0.1D);
    12. }
    13. if (randomNum <= 20) {
    14. giveXp(p, 0.2D);
    15. }
    16. if (randomNum <= 90) {
    17. giveXp(p, 0.5D);
    18. }
    19. if (randomNum <= 60) {
    20. giveXp(p, 0.1D);
    21. }
    22. if (randomNum <= 10) {
    23. giveXp(p, 0.6D);
    24. }
    25.  
    26. }
    27. }
    28. }
    29.  
    30. public void giveXp(Player p, double d){
    31. ExperienceManager expMan = new ExperienceManager(p);
    32. expMan.changeExp(0 + d);
    33. p.sendMessage("ยง3" + d + " Xp recieved!");
    34.  
    35. }
    36. }
     
  2. shohouku Hypothetical situation:

    "An entity died!"
    "Was it a Monster?"
    "Yes."
    "Okay, was the killer a Player?"
    "Yes."
    "Okay, generate a random number between 1 and 100 inclusively."
    "Done. Number is 1"
    "Okay, is 1 less than or equal to 30?"
    "Yes"
    "Then give the player 0.1 exp. Is 1 less than or equal to 20?"
    "Yes"
    "Then give the player 0.2 exp. Is 1 less than or equal to 90?"
    Okay, I hope you see the problem by now :p Solution: Stop checking as soon as you hit one of them
     
    shohouku likes this.
  3. Offline

    shohouku


    Okay fixed my solution, I added else statements to each of them and now it works :p
     
    AdamQpzm likes this.
  4. Offline

    MordorKing78

    Return.
    You just could do return true;
     
  5. MordorKing78 In a void method? Let me know how that works out.
     
  6. Offline

    DrEinsteinium

    AdamQpzm You can return in a void method.. just not true :p I'm sure he understood what MordorKing78 meant.
     
  7. DrEinsteinium I know you can, but the exact words were to use return true ;)

    And maybe he did, maybe he didn't. But in either case, he'd solved the issue before MordorKing78 posted
     
  8. Offline

    MordorKing78

    AdamQpzm likes this.
Thread Status:
Not open for further replies.

Share This Page