Add a fly boolean

Discussion in 'Plugin Development' started by C0lA_K1nG, Oct 7, 2012.

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

    C0lA_K1nG

    Hello, I need to find out how to add a fly boolean or any kind of boolean i am new.

    Here is my code
    Code:
    package me.colaking.FlyPlus;
     
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class FlyPlus extends JavaPlugin{
     
        @Override
        public void onEnable(){
            getLogger().info("FlyPlus Has Been Successfully been enabled!");
        }
         
     
        @Override
        public void onDisable() {
            getLogger().info("FlyPlus Has Been Successfuly Been Disabled!");
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player player = (Player) sender;
            String commandLabel = null;
            if(commandLabel.equalsIgnoreCase("fly")){
             
        }
            return false;
    }
    }
    
     
  2. Offline

    ZeusAllMighty11

    I don't understand why you need this?

    To define a boolean, I do at the top of main class:

    public static boolean booleanName = false;

    if you want to activate their fly, do :
    sender.allowFlight(true);

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  3. its better to store a hashmap so you can track the status of the fly for each player, and not for the global server. I recommend you look up tutorials for hashmaps
     
  4. Offline

    C0lA_K1nG

    Say okai i got it but now i get this any help

    Code:
    16:04:33 [INFO] C0lA_K1nG issued server command: /fly
    16:04:33 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'fly'
    in plugin FlyPlus v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:42)
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:16
    8)
            at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:4
    98)
            at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.
    java:880)
            at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:826)
     
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:808)
            at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:44)
            at net.minecraft.server.NetworkManager.b(NetworkManager.java:282)
            at net.minecraft.server.NetServerHandler.d(NetServerHandler.java:109)
            at net.minecraft.server.ServerConnection.b(SourceFile:35)
            at net.minecraft.server.DedicatedServerConnection.b(SourceFile:30)
            at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:577)
            at net.minecraft.server.DedicatedServer.q(DedicatedServer.java:213)
            at net.minecraft.server.MinecraftServer.p(MinecraftServer.java:473)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:405)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:539)
    Caused by: java.lang.NullPointerException
            at me.colaking.FlyPlus.FlyPlus.onCommand(FlyPlus.java:26)
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
            ... 15 more
    >
    Code
    Code:
    package me.colaking.FlyPlus;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class FlyPlus extends JavaPlugin{
       
        @Override
        public void onEnable(){
            getLogger().info("FlyPlus Has Been Successfully been enabled!");
        }
           
       
        @Override
        public void onDisable() {
            getLogger().info("FlyPlus Has Been Successfuly Been Disabled!");
        }
       
        @SuppressWarnings({ "null" })
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player player = (Player) sender;
            String commandLabel = null;
            if(commandLabel.equalsIgnoreCase("fly")){
                if (player.getAllowFlight() == false) {
                    player.setAllowFlight(true);
                    player.setFlying(true);
                    player.sendMessage(ChatColor.GREEN + "[FlyPlus]" + ChatColor.AQUA + "Flying Enabled");
                }
               
        }
            return false;
    }
    }
    
     
  5. Offline

    llamasaylol

    In your code you check weather the commandLabel is "fly", however you assign the commandLabel to be null. I think what you were meant to put is
    if (label.equalsIgnoreCase("fly")){
    player.setAllowFlight(!player.getAllowFlight());
    }
     
  6. Offline

    C0lA_K1nG

    Thank you it helped alot
     
  7. Offline

    llamasaylol

    No problem. Also, can you set this to solved now that you are ok.
     
  8. I dont this his isue is resolved, look at this part of his code:
    String commandLabel = null;
    if(commandLabel.equalsIgnoreCase("fly")){
    I am almost 99% sure that will triger an exception
     
  9. Offline

    C0lA_K1nG

    Now when i try to increase the fly speed with player.setFlySpeed i can't it just ruines /fly

    Code
    Code:
    package me.colaking.FlyPlus;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class FlyPlus extends JavaPlugin{
       
        @Override
        public void onEnable(){
            getLogger().info("FlyPlus Has Been Successfully been enabled!");
        }
           
       
        @Override
        public void onDisable() {
            getLogger().info("FlyPlus Has Been Successfuly Been Disabled!");
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player player = (Player) sender;
            if(label.equalsIgnoreCase("fly")){
                if (player.getAllowFlight() == false) {
                    player.setAllowFlight(true);
                    player.setFlying(true);
                    player.sendMessage(ChatColor.GREEN + "[FlyPlus]:" + ChatColor.AQUA + " Flying Enabled");
                } else if (player.getAllowFlight() == true) {
                    player.setAllowFlight(false);
                    player.setFlying(false);
                    player.sendMessage(ChatColor.GREEN + "[FlyPlus]:" + ChatColor.AQUA + " Flying Disabled");
                }
            if(label.equalsIgnoreCase("fly") && args[0].equalsIgnoreCase("speed") && args[1].equalsIgnoreCase(2)){
                if (player.getAllowFlight() == true) {
                    player.setAllowFlight(true);
                    player.setFlying(true);
                    player.setFlySpeed(2)
                }
               
        }
            return false;
    }
    }}
    
     
  10. Offline

    KeybordPiano459

  11. C0lA_K1nG Do yourself a flavor and learn Java/Programming basics. What you do is just hoorible! First off you assume that args[0] and args[1] are there, then you make no difference between "/fly" and "/fly speed 2": Both will toggle fly mode... Then what's the reason of hardcoding 2? You could just check if the given value isn't higher than a max. value and use this... Also you blindly cast sender to player.

    See how I would solve your problem, but keep in mind that I made erros into the code, so you can't copy&paste (it's some mix-up of Java and pseudocode):
    Code:java
    1. public class FlyPlus extends JavaPlugin{
    2. int max = 3;
    3.  
    4. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    5. if(sender isNotAnInstanceOf Player)
    6. return true;
    7. Player player = (Player) sender;
    8. if(the length of the args array < 1)
    9. {
    10. boolean to = !player.getAllowFlight();
    11. player.setAllowFlight(to);
    12. player.setFlying(to);
    13. player.sendMessage("ChatColor.GREEN + "[FlyPlus]: + ChatColor.AQUA + to ? "Enabled" : "Disabled");
    14. else if(the length of the args > 1 && args[0].equalsIgnoreCase("speed")
    15. {
    16. if (!player.getAllowFlight()) {
    17. player.sendMessage(ChatColor.RED+"You aren't flying!");
    18. return true;
    19. }
    20. try {
    21. int speed = ConvertStringToInt(args[0]);
    22. if(speed > max)
    23. player.setFlySpeed(speed);
    24. player.sendMessage(ChatColor.GREEN+"New speed: "+speed);
    25. } catch(NumberFormatException e) {
    26. player.sendMessage(ChatColor.RED+"Invalid speed: "+args[1]+" (only values between 0 and "+max+" are allowed)");
    27. }
    28. }
    29. else
    30. return false;
    31. return true;
    32. }
    33. }
     
    ferrybig likes this.
  12. Offline

    llamasaylol

    You miss read my post. I told him to use label not command label, thus solving the problem he did have. I know he has a new problem now.
     
  13. Offline

    gamerzap

    Not only that, but he didn't know how to define a boolean.
     
Thread Status:
Not open for further replies.

Share This Page