Solved setFlySpeed and setWalkSpeed

Discussion in 'Plugin Development' started by random_username, Nov 3, 2013.

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

    random_username

    Hello, I am making a plugin involving changing speed. I am trying to make a command /speed <flight|walk> <speed> [target] (Target is optional). Here is what I have (This is part of my on command, string prefix and noperm are further above):
    Code:java
    1. else if(cmd.getName().equalsIgnoreCase("speed")){
    2. if(args.length <= 1){
    3. if(Sender.hasPermission("pcommands.speed")){
    4. if(Sender instanceof Player){
    5. Sender.sendMessage(prefix + "§3Usage: /speed <Flying|Walking> <Speed level>.");
    6. }else{
    7. Sender.sendMessage("Correct console usage: /speed <flight|walk> <value> <player>");
    8. }
    9. }else{
    10. Sender.sendMessage(prefix + noperm);
    11. }
    12. }if(args.length == 2){
    13. if(Sender.hasPermission("pcommands.speed")){
    14. if(Sender instanceof Player){
    15. Player player = (Player) Sender;
    16. if(!(args[0].equalsIgnoreCase("Flight") || args[0].equalsIgnoreCase("Walk"))){
    17. player.sendMessage(prefix + "§3Speed type must be Flight or Walk!");
    18. }if(args[0].equalsIgnoreCase("Flight")){
    19. int id = 0;
    20.  
    21. try {
    22. id = Integer.parseInt(args[1]);
    23. } catch(NumberFormatException ex) {
    24. player.sendMessage(prefix + "§4Speed value MUST be a number!");
    25. return true;
    26. }
    27. player.setFlySpeed(id);
    28. }if(args[0].equalsIgnoreCase("walk")){
    29. int idd = 0;
    30.  
    31. try {
    32. idd = Integer.parseInt(args[1]);
    33. } catch(NumberFormatException ex) {
    34. Sender.sendMessage(prefix + "§4Speed MUST be a number!");
    35. return true;
    36. }
    37. player.setWalkSpeed(idd);
    38. }
    39. }else{
    40. Sender.sendMessage("Correct console usage: /speed <flight|walk> <value> <player>");
    41. }
    42. }else{
    43. Sender.sendMessage(prefix + noperm);
    44. }
    45. }if(args.length == 3){
    46. if(Sender.hasPermission("pcommands.speed.others")){
    47. if(!(args[0].equalsIgnoreCase("Flight") || args[0].equalsIgnoreCase("Walk"))){
    48. Sender.sendMessage(prefix + "§3Speed type must be Flight or Walk!");
    49. }if(args[0].equalsIgnoreCase("Flight")){
    50. Player target = Bukkit.getPlayer(args[2]);
    51. if(target == null){
    52. Sender.sendMessage(prefix + ChatColor.RED + args[0] + " could not be found.");
    53. }else{
    54. int idd = 0;
    55.  
    56. try {
    57. idd = Integer.parseInt(args[1]);
    58. } catch(NumberFormatException ex) {
    59. Sender.sendMessage(prefix + "§4Speed MUST be a number!");
    60. return true;
    61. }
    62. target.setFlySpeed(idd);
    63. }
    64. }if(args[0].equalsIgnoreCase("Walk")){
    65. Player target = Bukkit.getPlayer(args[2]);
    66. if(target == null){
    67. Sender.sendMessage(prefix + ChatColor.RED + args[0] + " could not be found.");
    68. }else{
    69. int idd = 0;
    70.  
    71. try {
    72. idd = Integer.parseInt(args[1]);
    73. } catch(NumberFormatException ex) {
    74. Sender.sendMessage(prefix + "§4Speed MUST be a number!");
    75. return true;
    76. }
    77. target.setWalkSpeed(idd);
    78. }
    79. }
    80. }else{
    81. Sender.sendMessage(prefix + noperm);
    82. }
    83. }
    84. }

    My problem is, I get an error when I try /speed 10 and I know the cause; Speed can only go up to 1, according to the apidocs. So, what I wanted is to replace 1 with 0.1, 2 with 0.2, etc. so I can change the speed in the way I want. Also, I can't write 0.1 In game, because of the:
    Code:java
    1. int idd = 0;
    2.  
    3. try {
    4. idd = Integer.parseInt(args[1]);
    5. } catch(NumberFormatException ex) {
    6. Sender.sendMessage(prefix + "§4Correct usage: Speed MUST be a number!");
    7. return true;
    8. }

    So, any idea on how to replace the speed values? Thanks for the help :)
     
  2. Offline

    Chinwe

    You can use Double.parseDouble(args[1]) to parse the decimal into a double :)
     
  3. Offline

    random_username

    and where would I add that? :)
     
  4. Offline

    Chinwe

    Instead of Integer.parseInt():
    Code:
    double idd = 0;
     
    try {
        idd = Double.parseDouble(args[1]);
    } catch(NumberFormatException ex) {
        Sender.sendMessage(prefix + "§4Correct usage: Speed MUST be a number!");
        return true;
    }
     
  5. Offline

    random_username

    I will try it out, Thanks!

    Eclipse doesn't like that, it says
    Code:java
    1. target.setWalkSpeed(idd);

    is wrong, underlines setWalkSpeed. Do I cast idd to float? So it would be
    Code:java
    1. target.setWalkSpeed((float) idd);

    ?

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

    Chinwe

    Ah derp, use Float.parseFloat() instead to get a float that you can use (although I don't think casting a double to float should make any problems)
     
  7. Offline

    random_username

    but how would I replace a 10 to 1, and a 1 to 0.1? so the player can make /speed 10?
     
  8. Offline

    Chinwe

    Divide by 10? Remember to cast to doubles:
    Code:
    // integer division
    float division = 10 / 1; // will equal 0
     
    // what you should do
    float division = theirInput / 10D; // will actually be theirInput / 10
     
  9. Offline

    random_username

    and how would I prevent an exception if args[1] isn't a number, but a string? Would it be this way?
    Code:java
    1. float idd = 0/10;
    2.  
    3. try {
    4. idd = Float.parseFloat(args[1]);
    5. } catch(NumberFormatException ex) {
    6. Sender.sendMessage(prefix + "§4Correct usage: Speed MUST be a number!");
    7. return true;
    8. }


    Nvm, this worked :D
    Code:java
    1. int idd = 0;
    2.  
    3. try {
    4. idd = Integer.parseInt(args[1]);
    5. } catch(NumberFormatException ex) {
    6. Sender.sendMessage(prefix + "§4Correct usage: Speed MUST be a number!");
    7. return true;
    8. }
    9. if(idd > 10 || idd < 1){
    10. Sender.sendMessage(prefix + "§cSpeed must be from 1-10!");
    11. }else{
    12. target.setFlySpeed(idd / 10);
    13. }

    Thanks for the help! :D

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
Thread Status:
Not open for further replies.

Share This Page