Solved Code is not changing my Walkspeed!

Discussion in 'Plugin Development' started by CodingEyes, Oct 30, 2013.

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

    CodingEyes

    Hello,
    I'm trying to make some code so that when you hold a blaze rod in your hand, you can walk faster. This is what I've got in the listener file.

    Code:java
    1. public class ItemListener implements Listener {
    2. static ChatColor goodMsgColour = ChatColor.GREEN;
    3. static ChatColor bracketColor = ChatColor.BLUE;
    4. static ChatColor pluginNameColor = ChatColor.GOLD;
    5. public static String name= "WalkTheWalk";
    6.  
    7. public static void sendPlayerGoodMessage(Player player, String msg) {
    8. player.sendMessage(bracketColor + "[" + pluginNameColor + name + bracketColor + "] " + goodMsgColour + msg);
    9. }
    10. @EventHandler(priority = EventPriority.HIGHEST)
    11. public void onPlayerInteract(PlayerInteractEvent e) {
    12. final Player player = e.getPlayer();
    13. if (player.hasPermission("wtw.run") || player.isOp()) {
    14. if(player.getItemInHand().getType() == Material.BLAZE_ROD){
    15. ((Player) e).setWalkSpeed(5); //This line doesn't work for some reason.
    16. ItemListener.sendPlayerGoodMessage(player, "Run faster than the wind!");
    17. return;
    18. }
    19. else {
    20. }
    21. }
    22. }
    23. }



    Please help me solve this problem. Thank you.
     
  2. Offline

    TheUpdater

    do Player p = (Player) e.getPlayer():
    float speed = 0.3;
    then p.setwalkspeed(speed);
    if its 5 he will run so fast it wont work lol, walk is a float =)
     
  3. Offline

    se1by

    You check for a PlayerInteractEvent.
    If he just holds the blaze rod, this event doesn't get fired.
    Check for PlayerMoveEvent.
     
    CodingEyes likes this.
  4. Offline

    TheUpdater

    then check only x and z cus you dont want to overload it lol =)
     
  5. Offline

    Quantix

    Even better, listen to the PlayerItemHeldEvent and check if the slot the player switched to contains a blaze rod. If so, change his walk speed to a higher value and if not change it back to normal.
     
    se1by likes this.
  6. Offline

    EcMiner

    Instead of doing setSpeed(5); you have to do setSpeed(0.5); because 1 is the highest value that is accepted.
     
    CodingEyes likes this.
  7. Offline

    sgavster

    YOu did "e.setWalkSpeed();" you need to do "p.setWalkSpeed();".
     
  8. I hope you noticed the fact that you tried to cast Player to the PlayerInteractEvent, there is no need for that, simply do player.setWalkSpeed(5);

    Next time you feel something isn't working as expected have a look at the console and see if there are any errors, in this case there should've been an error every time you interacted with a blaze rod
    Edit: Of course, paste that error along with your code.

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

    CodingEyes

    There were actually no errors in the console before :)

    Also, Thank you to everyone who has helped me! :D I've got it working.
     
  10. Offline

    _Filip

    Also don't make static variables/methods.
     
Thread Status:
Not open for further replies.

Share This Page