Speed Error

Discussion in 'Plugin Development' started by Cooper PluginDev, Jan 2, 2014.

Thread Status:
Not open for further replies.
  1. I'm trying to make it so when you right click with a 'hyper axe' you'll be given speed II for 6 seconds and then it goes on a 20 second cool-down.
    The error is, when you right click it does what it's ment to but when it goes on the cool-down (after the speed buff is removed) it glitches. Everytime you right click it makes you really fast and glitches you around.
    Not sure how to do the {code} thingo so i'll just put it in a paste bin :p
    Code:
    http://pastebin.com/pz4dSW6A
     
  2. Offline

    zeeveener

    For future reference (Don't use the quotes obviously)

    ["syntax=java"]//Code in here[/syntax]
     
  3. ok. any information on my question?

    I think it might be a misplaced semi-column?

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

    Btw it doesn't give you a effect it just makes you go really really fast xD

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

    Tster

    do you have a variable boolean controlling if it's on cooldown, and the effect can only be used if the variable is false
     
  6. Tster
    What do you mean? Line of code please :)
     
  7. Offline

    Tster

    hold on, let me have a look through your code.

    I don't see an else,
    You have your 'if' statement checking if they are on cooldown, but then you seem to add the effect anyway

    You need something along the lines of:
    if they're on cooldown {
    tell them they're on cooldown
    } else {
    put the effect on
    }

    BTW I don't know why everything has a line break between it -.-

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  8. Tster
    Added a }else{ under the cooldown, now it aint even having the effect!
     
  9. Offline

    BillyGalbreath

    1) Dont overuse "this"
    2) Organize your code
    3) Separate your if statement for better readability
    4) Dont double check the axe's display name
    5) Dont .put() the player into the cooldown twice (this was the biggest part of the problem)
    6) Dont repeatedly call Systen.currentTimeMillis(), you should call it once and store that value into a variable. Not needed in this case, as you only need it once anyway.
    7) Probably other things I fixed here but dont remember..

    Code:java
    1.  
    2. @EventHandler
    3. public void onLegacyWeaponClick(PlayerInteractEvent event) {
    4. final Player player = event.getPlayer();
    5. Sound gh = Sound.WITHER_HURT;
    6. if (!event.getAction().equals(Action.RIGHT_CLICK_AIR) && !event.getAction().equals(Action.RIGHT_CLICK_BLOCK))
    7. return;
    8. if (player.getItemInHand().getType() != Material.IRON_AXE)
    9. return;
    10. if (!player.getItemInHand().getItemMeta().hasDisplayName())
    11. return;
    12. if (!player.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.RED + "Hyper Axe")))
    13. return;
    14. if (cooldown.containsKey(player.getName())) {
    15. long diff = (System.currentTimeMillis() - cooldown.get(player.getName())) / 1000;
    16. if (diff < cooldownTime) {
    17. player.sendMessage(ChatColor.AQUA + "[Hyper Axe]" + ChatColor.LIGHT_PURPLE + " Hyper Speed" + ChatColor.YELLOW + " is still on a " + ChatColor.LIGHT_PURPLE + (cooldownTime - diff) + "s" + ChatColor.YELLOW + " cooldown!");
    18. return;
    19. }
    20. }
    21. cooldown.put(player.getName(), Long.valueOf(System.currentTimeMillis()));
    22. Sound sound = Sound.WITHER_HURT;
    23. player.getWorld().playEffect(player.getLocation(), Effect.STEP_SOUND, Material.IRON_BLOCK.getId());
    24. player.getWorld().playEffect(player.getLocation(), Effect.STEP_SOUND, Material.LAPIS_BLOCK.getId());
    25. player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED,120, 3));
    26. player.sendMessage(ChatColor.AQUA + "[Hyper Axe] " + ChatColor.GRAY + "You used " + ChatColor.LIGHT_PURPLE + "Hyper Speed!");
    27. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    28. public void run() {
    29. if (LegacyWeapons.cooldown.containsKey(player.getName())) {
    30. LegacyWeapons.cooldown.remove(player.getName());
    31. player.sendMessage(ChatColor.AQUA + "[Hyper Axe] " + ChatColor.YELLOW + "[Recharged]" + ChatColor.GREEN + " Hyper Speed");
    32. }
    33. }
    34. },400);
    35. }
    36.  
     
  10. BillyGalbreath
    ahaha, still errors -_-
    Same things happenning

    Tster
    Well idk

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

    Tster

    Can you try to describe the issue in more detail?
     
  12. Tster
    Ok. So basically when you have a "hyper axe" you will be given buffs. So when you right click you will get speed 4 for a few seconds. What the problem is, is when you've right clicked the potion effect gets added and all is well but when the potion effect is removed, you can spam right click and it makes you super fast (more than speed 4, maybe speed 6).
    Not sure what is going on! Arghh.
     
Thread Status:
Not open for further replies.

Share This Page