Setting fly speed....

Discussion in 'Plugin Development' started by stuntguy3000, Mar 1, 2013.

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

    stuntguy3000

    Hi, This one is really getting me :/

    Basically i want to set a players fly speed..to the normal creative speed, but.. no matter what i put they either go super fast or can't move..

    p.setAllowFlight(true);
    p.setFlying(true);
    p.setFlySpeed(1);

    ^ Superfast.
     
  2. Offline

    Tirelessly

    It takes a number between -1 and 1.
     
  3. Offline

    chasechocolate

    I think p.setAllowFlight(true) and p.setFlying(true) will automatically set them to go as fast as you would in creative.
     
  4. Offline

    stuntguy3000

    ^ I tried that, and i couldn't move .-.
     
  5. Offline

    Tirelessly

    What numbers have you tried inputting?
     
  6. Offline

    stuntguy3000

    1, 0 and 5. It doesn't take decimals sooo
     
  7. Offline

    Tirelessly

    It takes a float, so.. Yes. It does.
     
  8. Offline

    stuntguy3000

    Eclipse says otherwise...
     
  9. Offline

    Jeyge

  10. It takes float but stuff like 0.5 are by default doubles, you need to use 0.5f.

    Default fly speed is 0.1f, so adjust accordingly.
     
    tomecki1 likes this.
  11. Offline

    kreashenz

    I'm getting this error.. :/
    Code:
    [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'speed' in plugin Hugeness v0.2
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:186)
    at org.bukkit.craftbukkit.v1_4_R1.CraftServer.dispatchCommand(CraftServer.java:514)
    at net.minecraft.server.v1_4_R1.PlayerConnection.handleCommand(PlayerConnection.java:980)
    at net.minecraft.server.v1_4_R1.PlayerConnection.chat(PlayerConnection.java:898)
    at net.minecraft.server.v1_4_R1.PlayerConnection.a(PlayerConnection.java:853)
    at net.minecraft.server.v1_4_R1.Packet3Chat.handle(Packet3Chat.java:44)
    at net.minecraft.server.v1_4_R1.NetworkManager.b(NetworkManager.java:290)
    at net.minecraft.server.v1_4_R1.PlayerConnection.d(PlayerConnection.java:113)
    at net.minecraft.server.v1_4_R1.ServerConnection.b(SourceFile:39)
    at net.minecraft.server.v1_4_R1.DedicatedServerConnection.b(SourceFile:30)
    at net.minecraft.server.v1_4_R1.MinecraftServer.r(MinecraftServer.java:598)
    at net.minecraft.server.v1_4_R1.DedicatedServer.r(DedicatedServer.java:224)
    at net.minecraft.server.v1_4_R1.MinecraftServer.q(MinecraftServer.java:494)
    at net.minecraft.server.v1_4_R1.MinecraftServer.run(MinecraftServer.java:427)
    at net.minecraft.server.v1_4_R1.ThreadServerApplication.run(SourceFile:849)
    Caused by: java.lang.NumberFormatException: For input string: "0.1f"
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at me.kreashenz.minecraftrp.Commands.Speed.onCommand(Speed.java:40)
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    ... 15 more
    
     
  12. You failed to mention you're using a string parser :)
    <num>f is for Eclipse and other IDEs.
    If you want to parse a string then use the proper type that it wants: Float.valueOf(string) and the string should contain 0.1 or whatever but without the f.

    Also, you should use try-catch on Integer/Double/Float/etc.valueOf() because they throw NumberFormatException if the string isn't a number, you need to catch that error and warn the user in a more friendly manner... instead of letting it throw it in console and user recieving "internal error" thing.
     
  13. Offline

    kreashenz

    Digi
    Float.valueOf(parseInt);
    OR
    Float.valueOf(args[0]);?
     
  14. Offline

    kreashenz

    Digi wut ... A string what?
     
  15. Offline

    kreashenz

    Digi Little code thingy so far :
    Code:java
    1. import java.io.File;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandExecutor;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.configuration.file.FileConfiguration;
    8. import org.bukkit.configuration.file.YamlConfiguration;
    9. import org.bukkit.entity.Player;
    10.  
    11. public class Speed implements CommandExecutor {
    12.  
    13. @Override
    14. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    15. File file = new File( "plugins/MinecraftRP/config.yml" );
    16. FileConfiguration config = YamlConfiguration.loadConfiguration(file);
    17. if(cmd.getName().equalsIgnoreCase("speed")){
    18. if(sender instanceof Player){
    19. if(sender.hasPermission("mcrp.speed")){
    20. if(args.length == 0){
    21. try
    22. {
    23. Player player = (Player)sender;
    24. float speed = Float.valueOf(Integer.parseInt(args[0]));
    25. player.setFlySpeed(speed);
    26. sender.sendMessage(config.getString("messages.prefix") + "§cUsage : §f/speed <amount>");
    27. {
    28. sender.sendMessage("§cUsage : §f/speed <player> <amount>");
    29. }
    30. }
    31. else
    32. {
    33. try
    34. {
    35. sender.sendMessage(config.getString("messages.prefix") + "§cUsage : §f/speed <player> <amount>");
    36. Player target = Bukkit.getServer().getPlayer(args[0]);
    37. float speed = Float.valueOf(Integer.parseInt(args[1]));
    38. target.setFlySpeed(speed);
    39. {
    40. sender.sendMessage("§cUsages : §f/speed <player> <amount>");
    41. }
    42. }
    43. }
    44. }
    45. }
    46. return true;
    47. }
    48. }
     
  16. Apart from you totally ignoring everything I said and jumping to posting the code... you've made the code pointlessly complicated.
    - Use getConfig() instead of manually loading the config file each time the command is used, getConfig() loads only once.... but to access it you need your main class pointer, make a constructor and pass it through there then store it in a field.
    - Very bad usage of try-catch to get rid of index out of bounds errors, you need to check if args.length is the propper size before getting indexes from it, catching errors isn't great you know, and it shouldn't be done on any error you don't understand, so just get rid of them and fix the errors properly.
    - Then I see you are checking args.length but you're not checking if it has arguments, you're checking if it's empty.
    - I've told you like 3 times that Float.valueOf() takes a String as argument, you're still giving it an integer which is the source of your previous problem since it can't parse decimal numbers since it's an integer, not float.
    - Use ChatColor enums instead of those weird characters, it's much easier
    - I don't really get why you have duplicate code around there, you're getting the speed in 2 separate code blocks.
    - Indentation is unexistent, hard to read the code...

    So if you want help, start by indenting your code so other people and even yourself can read it easily. Ctrl+A and Ctrl+i if you're using Eclipse.
     
  17. Offline

    kreashenz

    Digi I do indent the code, just whenever I'm posting it in 'syntax=java' it doesn't EVER do the actual dents.. I honestly find it much easier to use § instead of ChatColor.. Should I remove the try - catch or just move them somewhere else? And the args.length == 0 , what should I set that to? I was looking onto another code I previously used for that, and it worked fine.. This is literally the first time I'm using try - catch.. And I don't see what I'm meant to be changing the
    Code:java
    1. float speed = Float.valueOf(Integer.parseInt(args[0]));
    to..
     
  18. You should use [code] tag then if syntax is screwing your indentation.
    Yes, remove that try-catch. You will need one covering Float.valueOf() however, because that exception is unavoidable.
    The args.length var is the size of the args array, which is how many arguments you have.... you can't get an argument that doesn't exist so you must check if there are how many arguments you're getting... and use > instead of absolute equality because if args.length == 1 then it's logical that it won't be anything else since it's exacly 1, but if you're using args.length >= 1 then it means there may be other arguments and you can check further.

    You're parsing a string into an integer then you're giving that integer to Float.valueOf()...
    The point of Float.valueOf(string) was to be able to use decimals, since integer does NOT have decimals...
    Code:
    float speed = Float.valueOf(args[0]);
    0 is the 1st argument btw, you had diferent argument layout in the help message.
     
Thread Status:
Not open for further replies.

Share This Page