Speed command Isn't working right

Discussion in 'Plugin Development' started by random_username, Dec 19, 2013.

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

    random_username

    Hello, I am trying to make a speed command for my plugin, but It isn't working right. What happens is that after setting my speed to something, when I change it again, I cannot move. i.e. I set my flight speed to 10, then change it to 4. After changing it, I can only move up and down. This is happening with both fly speed and walk speed. The console doesn't display any errors. Any ideas on how to solve this, and could I also please know what am I doing wrong? :) Code:
    Code:java
    1. private Main main;
    2.  
    3. public speed(Main main)
    4. {
    5. this.main = main;
    6. }
    7.  
    8. @Override
    9. public boolean onCommand(CommandSender Sender, Command cmd, String commandLabel, String[] args){
    10. String noperm = main.getConfig().getString("NoPermission").replace("&", "§");
    11. String prefix = main.getConfig().getString("Prefix").replace("&", "§");
    12. if(cmd.getName().equalsIgnoreCase("speed")){
    13.  
    14. if(!(args.length == 1 || args.length == 2)){
    15.  
    16. }if(args.length == 1){
    17. if(Sender instanceof Player){
    18. Player player = (Player) Sender;
    19. if(player.hasPermission("pcommands.speed")){
    20. int speed = 0;
    21.  
    22. try{
    23. speed = Integer.parseInt(args[0]);
    24. Sender.sendMessage(prefix + "§4Speed must be a whole number!");
    25. return true;
    26. }
    27. if(speed >10 || speed < 1){
    28. Sender.sendMessage(prefix + "§4Speed can only be from 1-10!");
    29. return true;
    30. }else{
    31. Float f = Float.valueOf(speed / 10);
    32. if(player.isFlying()){
    33. player.setFlySpeed(f);
    34. player.sendMessage(prefix + "§3Set flight speed to " + args[0]);
    35. }else{
    36. player.setWalkSpeed(f);
    37. player.sendMessage(prefix + "§3Set walk speed to " + args[0]);
    38. }
    39.  
    40.  
    41. }
    42. }else{
    43. Sender.sendMessage(prefix + noperm);
    44. }
    45. }else{
    46. Sender.sendMessage("Console usage: /speed <number> <player>");
    47. }
    48. }if(args.length == 2){
    49. if(Sender.hasPermission("pcommands.speed.others")){
    50. Player target = Bukkit.getPlayer(args[1]);
    51. if(target == null){
    52. Sender.sendMessage(prefix + "§4Player not found!");
    53. }else{
    54. int speed = 0;
    55.  
    56. try{
    57. speed = Integer.parseInt(args[0]);
    58. Sender.sendMessage(prefix + "§4Speed must be a whole number!");
    59. return true;
    60. }
    61. if(speed >10 || speed < 1){
    62. Sender.sendMessage(prefix + "§4Speed can only be from 1-10!");
    63. return true;
    64. }else{
    65.  
    66. Float f = Float.valueOf(speed / 10);
    67.  
    68. if(target.isFlying()){
    69. target.setFlySpeed(f);
    70. target.sendMessage(prefix + "§3Set your flight speed to " + args[0]);
    71. Sender.sendMessage(prefix + "§3Set " + args[1] + "'s flight speed to " + args[0]);
    72. }else{
    73. target.setWalkSpeed(f);
    74. target.sendMessage(prefix + "§3Set your walk speed to " + args[0]);
    75. Sender.sendMessage(prefix + "§3Set " + args[1] + "'s walk speed to " + args[0]);
    76. }
    77. }
    78. }
    79. }else{
    80. Sender.sendMessage(prefix + noperm);
    81. }
    82. }
    83. }
    84. return false;
    85. }

    Thanks for helping :D
     
  2. Offline

    random_username

    bump, any Ideas? :)
     
  3. Offline

    lordbobby104

    random_username hmmm interesting. Try removing the division and just enter in the .whatever because I know that works for me.
     
  4. Offline

    random_username

    bump, any other Ideas?
     
  5. Offline

    random_username

    bump. Any ideas on how to solve it?
     
  6. Offline

    random_username

  7. Offline

    xize

    random_username
    I had the same problem though what I did whas make a public variable outside the command holding the default speed before changing it and then when I wanted to go back to normal speed I used that value, you may want to check what the default value is by printing it out;)
     
    random_username likes this.
  8. Offline

    random_username

    Hello, thanks for the reply :D This problem isn't happening just while going to the normal speed, but while changing your speed after using the command once. Would that work for this as well? Maybe While changing speed, set it to that default value, and changing it to the desired value?
     
  9. Offline

    xize

    random_username
    it will work, basicly the problem probably is if you set setWalkSpeed(); or setFlySpeed() you override the default value this means when you set it back to 0 you can't walk but if you store the default value in a other variable before changing it you are safe:p

    a example what I should do is:
    Code:
    public Float walkSpeed;
     
    public boolean onCommand(CommandSender sender, Command cmd, CommandLabel label, String[] args) {
    if(cmd.getName().equalsIgnoreCase("someCommand")) {
        if(sender instanceof Player) {
          Player p = (Player) sender;
          if(args.length == 1) {
             if(args[0].equalsIgnoreCase("on")) {
             walkSpeed = p.getWalkSpeed();
             p.setWalkSpeed(200);
          } else if(args[0].equalsIgnoreCase("off")) {
                  p.setWalkSpeed(walkSpeed);
          }
      }
       }
    }
    return false;
    }
    
    its just a psuodo not really safe to use though, but what you can also do is printing the getWalkSpeed() before changing it so you don't have to store its value to set it back but you can just use that printed value instead;), the default walk speed is not 0 :p
     
  10. Offline

    random_username

    xize
    It's still not working, I'm trying this:
    Code:java
    1. private Main main;
    2.  
    3. public speed(Main main)
    4. {
    5. this.main = main;
    6. }
    7.  
    8. public Float walkspeed = 0.2F;
    9. public Float flyspeed = 0.1F;
    10.  
    11. @Override
    12. public boolean onCommand(CommandSender Sender, Command cmd, String commandLabel, String[] args){
    13. String noperm = main.getConfig().getString("NoPermission").replace("&", "§");
    14. String prefix = main.getConfig().getString("Prefix").replace("&", "§");
    15. if(cmd.getName().equalsIgnoreCase("speed")){
    16.  
    17. if(!(args.length == 1 || args.length == 2)){
    18.  
    19. }if(args.length == 1){
    20. if(Sender instanceof Player){
    21. Player player = (Player) Sender;
    22. if(player.hasPermission("pcommands.speed")){
    23. int speed = 0;
    24.  
    25. try{
    26. speed = Integer.parseInt(args[0]);
    27. Sender.sendMessage(prefix + "§4Speed must be a whole number!");
    28. return true;
    29. }
    30. if(speed >10 || speed < 1){
    31. Sender.sendMessage(prefix + "§4Speed can only be from 1-10!");
    32. return true;
    33. }else{
    34. Float f = Float.valueOf(speed / 10);
    35. if(player.isFlying()){
    36. player.setFlySpeed(flyspeed);
    37. player.setFlySpeed(f);
    38. player.sendMessage(prefix + "§3Set flight speed to " + args[0]);
    39. }else{
    40. player.setWalkSpeed(walkspeed);
    41. player.setWalkSpeed(f);
    42. player.sendMessage(prefix + "§3Set walk speed to " + args[0]);
    43. }
    44.  
    45.  
    46. }
    47. }else{
    48. Sender.sendMessage(prefix + noperm);
    49. }
    50. }else{
    51. Sender.sendMessage("Console usage: /speed <number> <player>");
    52. }
    53. }if(args.length == 2){
    54. if(Sender.hasPermission("pcommands.speed.others")){
    55. Player target = Bukkit.getPlayer(args[1]);
    56. if(target == null){
    57. Sender.sendMessage(prefix + "§4Player not found!");
    58. }else{
    59. int speed = 0;
    60.  
    61. try{
    62. speed = Integer.parseInt(args[0]);
    63. Sender.sendMessage(prefix + "§4Speed must be a whole number!");
    64. return true;
    65. }
    66. if(speed >10 || speed < 1){
    67. Sender.sendMessage(prefix + "§4Speed can only be from 1-10!");
    68. return true;
    69. }else{
    70.  
    71. Float f = Float.valueOf(speed / 10);
    72.  
    73. if(target.isFlying()){
    74. target.setFlySpeed(flyspeed);
    75. target.setFlySpeed(f);
    76. target.sendMessage(prefix + "§3Set your flight speed to " + args[0]);
    77. Sender.sendMessage(prefix + "§3Set " + args[1] + "'s flight speed to " + args[0]);
    78. }else{
    79. target.setWalkSpeed(walkspeed);
    80. target.setWalkSpeed(f);
    81. target.sendMessage(prefix + "§3Set your walk speed to " + args[0]);
    82. Sender.sendMessage(prefix + "§3Set " + args[1] + "'s walk speed to " + args[0]);
    83. }
    84. }
    85. }
    86. }else{
    87. Sender.sendMessage(prefix + noperm);
    88. }
    89. }
    90. }
    91. return false;
    92. }

    Any ideas on what am I doing wrong? :)
     
  11. Offline

    xize

    random_username
    the problem is that you need to check if your fly speed is higher or lower than the default fly speed I recommend to make a toggle;)
    because now you do setFlySpeed(flyspeed); and then override it again with a faster fly speed.

    you could make a simple boolean to check whenever the player is flying faster or slower or equals with default.

    Code:
    public boolean isFlyingOtherThanDefault(Float speed) {
          if(speed != flyspeed) {
               return true;
          }
         return false;
    }
    
    then do something like this in your command:

    Code:
    if(player.isFlying()) {
         if(isFlyingOtherThanDefault(player.getFlySpeed())) {
               player.setFlySpeed(flyspeed);
               //toggled fly speed to normal
         } else {
              flyspeed = player.getFlySpeed();
              player.setFlySpeed(your custom value);
              //change speed, note that we have added the default speed to the public variable outside the command
        }
    }
    
     
  12. Offline

    random_username

    xize
    Hello, sorry for the late reply :p I have the following code now:
    Code:java
    1. private Main main;
    2.  
    3. public speed(Main main)
    4. {
    5. this.main = main;
    6. }
    7.  
    8. public Float walkspeed = 0.2F;
    9. public Float flyspeed = 0.1F;
    10.  
    11. public boolean isFlyingOtherThanDefault(Float speed) {
    12. if(speed != flyspeed) {
    13. return true;
    14. }
    15. return false;
    16. }
    17.  
    18. public boolean iswalkingOtherThanDefault(Float speed) {
    19. if(speed != walkspeed) {
    20. return true;
    21. }
    22. return false;
    23. }
    24.  
    25. @Override
    26. public boolean onCommand(CommandSender Sender, Command cmd, String commandLabel, String[] args){
    27. String noperm = main.getConfig().getString("NoPermission").replace("&", "§");
    28. String prefix = main.getConfig().getString("Prefix").replace("&", "§");
    29. if(cmd.getName().equalsIgnoreCase("speed")){
    30.  
    31. if(!(args.length == 1 || args.length == 2)){
    32.  
    33. }if(args.length == 1){
    34. if(Sender instanceof Player){
    35. Player player = (Player) Sender;
    36. if(player.hasPermission("pcommands.speed")){
    37. int speed = 0;
    38.  
    39. try{
    40. speed = Integer.parseInt(args[0]);
    41. Sender.sendMessage(prefix + "§4Speed must be a whole number!");
    42. return true;
    43. }
    44. if(speed >10 || speed < 1){
    45. Sender.sendMessage(prefix + "§4Speed can only be from 1-10!");
    46. return true;
    47. }else{
    48. Float f = Float.valueOf(speed / 10);
    49. if(player.isFlying()){
    50. if(isFlyingOtherThanDefault(player.getFlySpeed())){
    51. player.setFlySpeed(flyspeed);
    52. player.sendMessage(prefix + "§3Set flight speed to default. Try again to set to the desired value.");
    53. }else{
    54. player.setFlySpeed(f);
    55. player.sendMessage(prefix + "§3Set flight speed to " + args[0]);
    56. }
    57. }else{
    58. if(iswalkingOtherThanDefault(player.getWalkSpeed())){
    59. player.setWalkSpeed(walkspeed);
    60. player.sendMessage(prefix + "§3Set Walk speed to default. Try again to set to the desired value.");
    61. System.out.println("Walk speed = " + walkspeed);
    62. System.out.println("Current = " + player.getWalkSpeed());
    63. }else{
    64. player.setWalkSpeed(f);
    65. player.sendMessage(prefix + "§3Set walk speed to " + args[0]);
    66. }
    67. }
    68.  
    69.  
    70. }
    71. }else{
    72. Sender.sendMessage(prefix + noperm);
    73. }
    74. }else{
    75. Sender.sendMessage("Console usage: /speed <number> <player>");
    76. }
    77. }if(args.length == 2){
    78. if(Sender.hasPermission("pcommands.speed.others")){
    79. Player target = Bukkit.getPlayer(args[1]);
    80. if(target == null){
    81. Sender.sendMessage(prefix + "§4Player not found!");
    82. }else{
    83. int speed = 0;
    84.  
    85. try{
    86. speed = Integer.parseInt(args[0]);
    87. Sender.sendMessage(prefix + "§4Speed must be a whole number!");
    88. return true;
    89. }
    90. if(speed >10 || speed < 1){
    91. Sender.sendMessage(prefix + "§4Speed can only be from 1-10!");
    92. return true;
    93. }else{
    94.  
    95. Float f = Float.valueOf(speed / 10);
    96.  
    97. if(target.isFlying()){
    98. if(isFlyingOtherThanDefault(target.getFlySpeed())){
    99. target.setFlySpeed(flyspeed);
    100. Sender.sendMessage(prefix + "§3Set flight speed to default. Try again to set to the desired value.");
    101. }else{
    102. target.setFlySpeed(f);
    103. target.sendMessage(prefix + "§3Set your flight speed to " + args[0]);
    104. Sender.sendMessage(prefix + "§3Set " + args[1] + "'s flight speed to " + args[0]);
    105. }
    106. }else{
    107. if(iswalkingOtherThanDefault(target.getWalkSpeed())){
    108. target.setWalkSpeed(walkspeed);
    109. Sender.sendMessage(prefix + "§3Set walk speed to default. Try again to set to the desired value.");
    110. }else{
    111. target.setWalkSpeed(f);
    112. target.sendMessage(prefix + "§3Set your walk speed to " + args[0]);
    113. Sender.sendMessage(prefix + "§3Set " + args[1] + "'s walk speed to " + args[0]);
    114. }
    115. }
    116. }
    117. }
    118. }else{
    119. Sender.sendMessage(prefix + noperm);
    120. }
    121. }
    122. }
    123. return false;
    124. }

    Each time I try the speed command, it says my speed has been set to default. After that, when I try again, it still says the same. I tried printing the default and current values in the console each time the command is performed. When i join the game, my walking speed is 0.2 and my Flying speed is 0.1 . Each time I try the command, the speeds are equal to its default values. Any idea on what am I doing wrong? :)
     
  13. Offline

    xize

    You didn't change the fly speed on else :p
    random_username

    edit
    readed it wrong hmm you could try .equals(...) instead of != on the boolean
     
  14. Offline

    random_username

    xize
    I tried changing the methods to:
    Code:java
    1. public boolean isFlyingOtherThanDefault(Float speed) {
    2. if(!speed.equals(flyspeed)) {
    3. return true;
    4. }
    5. return false;
    6. }
    7.  
    8. public boolean iswalkingOtherThanDefault(Float speed) {
    9. if(!speed.equals(walkspeed)) {
    10. return true;
    11. }
    12. return false;
    13. }

    As you said. This time, the boolean check is working. The only problem is that after the speed is set to the default, when I use the command again, It won't let me move. Is there any other ways to solve it? Btw, thanks so much for the help! :D
    edit: Command is only working when the speed argument is 10. The fly speed I use is args[0] / 10, any idea why this is happening?
     
  15. Offline

    xize

    random_username
    hmm what happends when you change 10 to lower? because default speed is 2 so when you set the speed lower than 20 its always lower.
     
  16. Offline

    random_username

    xize When I change it to lower than 10, after setting it to default, I am no longer able to move, just as before :)
     
  17. Offline

    xize

    random_username
    I'm wondering if the float value 'f' is correct, could you print that value when it sets the speed and test it with different speed values inside the arguments?, I'm at my phone so its hard to read :p
     
Thread Status:
Not open for further replies.

Share This Page