Toggle Command on Player

Discussion in 'Plugin Development' started by Mycrowut, Jun 26, 2013.

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

    Mycrowut

    Hello all! I'm very new to learning Bukkit and java in general. This is a very basic question but I am wondering how I would make a command on toggle. The plugin is /fly and I want it to simply toggle on and off flying for the player.

    I have a variable and I'm not sure if its a variable for the player to touch... so thats honestly the part that I'm stuck on.

    My Variable: (I am completely unsure about this one :S)
    Code:java
    1. boolean FlyEnable;
    2.  
    3. @Override
    4. public void onEnable() {
    5. this.logger.info("[Blockie] Portable has been enabled.");
    6. FlyEnable = false;
    7. }


    And here is my code down below which is a bit confusing because I'm trying to make it overcomplicated...:
    Code:java
    1. } else if (commandLabel.equalsIgnoreCase("fly")) {
    2. Player player = (Player) sender;
    3. if (player.hasPermission("blockie.fly") || player.isOp()) {
    4. if (FlyEnable = false) {
    5. player.setAllowFlight(true);
    6. FlyEnable = true;
    7. player.sendMessage(ChatColor.AQUA + "[Blockie] " + ChatColor.WHITE + "Flying Enabled. Do /fly to disable.");
    8. }else{
    9. player.setAllowFlight(false);
    10. FlyEnable = false;
    11. player.sendMessage(ChatColor.AQUA + "[Blockie] " + ChatColor.WHITE + "Flying Disabled. Do /fly to enable.");
    12. }
    13. }


    So in the end I'm looking for a player to be able to disable or enable fly for themselves. Very basic question.
    Thanks.
     
  2. Offline

    Ultimate_n00b

    Code:java
    1. if(player.isFlying())
    2. player.setFlying(false);
    3. else
    4. player.setFlying(true);
     
    Mycrowut likes this.
  3. Offline

    Mycrowut

    Ultimate_n00b What about if the player is on the ground. It only toggles if it finds that the player is in the air... Any other way? :S
     
  4. Offline

    Rhino390

    Create a simple array list of strings, then if they aren't in the list, add them to it, and setFlying to true. Then if they are in the list, remove them from it, and setflying to false
     
  5. Offline

    T31337

    This Is How I Toggle Flight... [creeper]

    public class flight implements CommandExecutor//class is named flight but the name doesn't really matter in the end...
    {
    T3 plugin;//T3 Is The Name Of My Main Class File....

    public flight(T3 plugin)
    {
    this.plugin = plugin;
    }

    public boolean onCommand(CommandSender sender, Command cmd, String cmdName, String[] args)
    {
    if(args.length==0)
    {
    if(!(sender instanceof Player))
    {
    sender.sendMessage("Usage: /flight <player>");
    return false;
    }
    else
    {

    Player p = (Player) sender;
    //was a player
    if(p.hasPermission("T3.Flight"))//T3.Flight Is MY Permissions Node, You May Remove Or Change It :)
    {
    boolean fly = p.getAllowFlight();
    if(fly)
    {
    p.setAllowFlight(false);
    p.sendMessage("Flight "+ChatColor.RED+"Disabled!");
    return true;
    }
    else
    {
    //!fly
    p.setAllowFlight(true);
    p.sendMessage("Flight "+ChatColor.GREEN+"Enabled!");
    return true;
    }
    }
    else
    {
    p.sendMessage(ChatColor.RED+"You Do Not Have Permission To Do That...");
    return false;
    }
    }

    }//args = 0
    if(args.length==1)
    {
    if(sender.hasPermission("T3.Flight.Other"))
    {
    Player t = (Player) Bukkit.getServer().getPlayer(args[0]);
    boolean tfly = t.getAllowFlight();
    if(!t.hasPermission("T3.Other.Denied"))
    {
    if(t.isOnline())
    {
    if(tfly)
    {
    t.setAllowFlight(false);
    t.sendMessage("Flight "+ChatColor.RED+"Disabled!");
    sender.sendMessage(t.getDisplayName()+" Has Had Thier Fly Mode "+ChatColor.RED+"Disabled!");
    return true;
    }
    else
    {
    //!tfly
    t.setAllowFlight(true);
    t.sendMessage("Flight Enabled!");
    sender.sendMessage(t.getDisplayName()+" Has Had Their Fly Mode "+ChatColor.GREEN+ "Enabled!");
    return true;
    }
    }
    else
    {
    sender.sendMessage(ChatColor.RED+"That Player Is Not Online...");
    return true;
    }
    }
    else
    {
    sender.sendMessage("You May Not Toggle The Flight Ablility Of That Player...");
    return true;
    }

    }
    else
    {
    sender.sendMessage(ChatColor.RED+"You Do Not Have Permission To Do That...");
    return false;
    }
    }//num args == 1
    if(args.length>=2)
    {
    sender.sendMessage("Usage: /flight (player)");
    return false;
    }
    return false;
    }

    }

    //Hope This Was Helpful :)
     
Thread Status:
Not open for further replies.

Share This Page