Check if it's a float error!

Discussion in 'Plugin Development' started by Dead_Skeleton, Oct 12, 2013.

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

    Dead_Skeleton

    Hey, I made a method to check if something was a float or not. It's getting an error. I'm trying to teleport a player to an X Y Z, and World on death from a config file, here's the code:

    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent e) {
    3. Player p = e.getPlayer();
    4. if(!(e.getAction() == Action.RIGHT_CLICK_BLOCK)) {
    5. return;
    6. }
    7. if (e.getClickedBlock().getState() instanceof Sign) {
    8. if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
    9. Sign s = (Sign) e.getClickedBlock().getState();
    10. if (s.getLine(0).equals("§1Checkpoint")
    11. && s.getLine(1).equals("§2Click here")
    12. && s.getLine(2).equals("§3to set your")
    13. && s.getLine(3).equals("§3checkpoint!")) {
    14. getConfig().set(p.getName() + ".stats.checkpoint.x",
    15. p.getLocation().getX());
    16. getConfig().set(p.getName() + ".stats.checkpoint.y",
    17. p.getLocation().getY());
    18. getConfig().set(p.getName() + ".stats.checkpoint.z",
    19. p.getLocation().getZ());
    20. getConfig().set(p.getName() + ".stats.checkpoint.world",
    21. p.getLocation().getWorld().getName());
    22. saveConfig();
    23. reloadConfig();
    24. p.sendMessage(ChatColor.GREEN + "Checkpoint set!");
    25. }
    26. }
    27. }
    28. }
    29.  
    30. public static boolean isFloat(String s) {
    31. try {
    32. Float.parseFloat(s);
    33. } catch (NumberFormatException nfe) {
    34. return false;
    35. }
    36. return true;
    37. }
    38.  
    39. @EventHandler
    40. public void onPlayerDeath(PlayerDeathEvent e) {
    41. if(e.getEntity() instanceof Player) {
    42. Player p = (Player) e.getEntity();
    43. EntityDamageEvent ede = p.getLastDamageCause();
    44. DamageCause dc = ede.getCause();
    45. if(!(dc == DamageCause.VOID))
    46. {
    47. return;
    48. }
    49. if(dc == DamageCause.VOID) {
    50. String world = (String) getConfig().get(p.getName() + ".stats.checkpoint.world");
    51. String x = (String) getConfig().get(p.getName() + ".stats.checkpoint.x");
    52. String y = (String) getConfig().get(p.getName() + ".stats.checkpoint.y");
    53. String z = (String) getConfig().get(p.getName() + ".stats.checkpoint.z");
    54. if(x == "nothing" || y == "nothing" || z == "nothing" || world == "nothing") {
    55. p.sendMessage(ChatColor.RED + "No checkpoint found, teleporting to spawn.");
    56. return;
    57. } else if (!(x == "nothing") && !(y == "nothing") && !(z == "nothing") && !(world == "nothing")) {
    58. if(isFloat(x) && isFloat(y) && isFloat(z)) {
    59. float xF = (float) getConfig().get(p.getName() + ".stats.checkpoint.x");
    60. float yF = (float) getConfig().get(p.getName() + ".stats.checkpoint.y");
    61. float zF = (float) getConfig().get(p.getName() + ".stats.checkpoint.z");
    62. World cWorld = (World) getConfig().get(p.getName() + ".stats.checkpoint.world");
    63. if(!(cWorld != null)) {
    64. return;
    65. } else if (cWorld != null) {
    66. Location loc = new Location(cWorld, xF, yF, zF);
    67. p.teleport(loc);
    68. p.sendMessage(ChatColor.GREEN + "You fell, teleporting to checkpoint!");
    69. return;
    70. }
    71. }
    72. }
    73. }
    74. }
    75. }


    Here's the error:

    Code:
    2013-10-12 19:46:53 [SEVERE] Could not pass event PlayerDeathEvent to Parkour v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
        at org.bukkit.craftbukkit.v1_6_R3.event.CraftEventFactory.callPlayerDeathEvent(CraftEventFactory.java:344)
        at net.minecraft.server.v1_6_R3.EntityPlayer.die(EntityPlayer.java:312)
        at net.minecraft.server.v1_6_R3.EntityLiving.damageEntity(EntityLiving.java:710)
        at net.minecraft.server.v1_6_R3.EntityHuman.damageEntity(EntityHuman.java:714)
        at net.minecraft.server.v1_6_R3.EntityPlayer.damageEntity(EntityPlayer.java:383)
        at net.minecraft.server.v1_6_R3.EntityLiving.C(EntityLiving.java:981)
        at net.minecraft.server.v1_6_R3.Entity.y(Entity.java:323)
        at net.minecraft.server.v1_6_R3.EntityLiving.y(EntityLiving.java:145)
        at net.minecraft.server.v1_6_R3.Entity.l_(Entity.java:230)
        at net.minecraft.server.v1_6_R3.EntityLiving.l_(EntityLiving.java:1243)
        at net.minecraft.server.v1_6_R3.EntityHuman.l_(EntityHuman.java:157)
        at net.minecraft.server.v1_6_R3.EntityPlayer.h(EntityPlayer.java:228)
        at net.minecraft.server.v1_6_R3.PlayerConnection.a(PlayerConnection.java:343)
        at net.minecraft.server.v1_6_R3.Packet10Flying.handle(SourceFile:136)
        at net.minecraft.server.v1_6_R3.NetworkManager.b(NetworkManager.java:296)
        at net.minecraft.server.v1_6_R3.PlayerConnection.e(PlayerConnection.java:116)
        at net.minecraft.server.v1_6_R3.ServerConnection.b(SourceFile:37)
        at net.minecraft.server.v1_6_R3.DedicatedServerConnection.b(SourceFile:30)
        at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:592)
        at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:227)
        at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:488)
        at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:421)
        at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
    Caused by: java.lang.NullPointerException
        at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
        at java.lang.Float.parseFloat(Unknown Source)
        at me.Dead_Skeleton.main.Main.isFloat(Main.java:243)
        at me.Dead_Skeleton.main.Main.onPlayerDeath(Main.java:269)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
        ... 26 more
    Thanks for the help! :D
     
  2. Offline

    adam753

    Are you setting the strings correctly in the config? It seems like you're doing isFloat() with a null value.
    Also try this:
    Code:
    Float.parseFloat(s.trim());
    
    trim() removes spaces and such from the string, which some stackoverflow thread said would help.
     
  3. Offline

    Jogy34

    I would suggest changing the NumberFormatException to just a plain Exception. You're getting an npe and since the Float class can't really be null (at least if it is you have a much bigger problem) the string that you're sending in is probably null so you would want to return false in that instance too.

    Also, when you're comparing strings you should use .equals(otherString) or .equalsIgnoreCase(someOtherString) as use '==' with strings will almost always return false. It will only return true in very specialized instances and with what you're doing, it most likely won't work.

    Another thing, your 'if' and 'else if' statements are really redundant. You're checking if any of them equal "nothing" and then you're checking if they don't equal "nothing"
     
  4. Offline

    Dead_Skeleton


    Sorry, I'm new and do not know the most efficient ways to make the code work. :/ I'm 11 years old... Thanks for the help, anyways! :D
     
Thread Status:
Not open for further replies.

Share This Page