Hello! I'm a newbie at coding and am trying to kind of start coding my own plugins. So I've started coding a /Fly command, however I need some help with it! Code:java if(cmd.getName().equalsIgnoreCase("fly")) { sender.sendMessage(ChatColor.GREEN + "Fly has been enabled!"); player.setAllowFlight(true); player.setFlySpeed(1); player.setFlying(true); } if(cmd.getName().equalsIgnoreCase("fly")) { sender.sendMessage(ChatColor.GREEN + "Fly has been disabled!"); player.setFlying(false); player.setFlySpeed(0); player.setAllowFlight(false); As you can see we have both the enable and disable command. Now I'm wondering how I can make it like they don't run at the same time, that it's permanent until you use the command again to disable flight. This is what I get: http://prntscr.com/581hl2 . Thanks.
You're checking if the cmd is equal to "fly" in both if statements, so of course they will both be true at the same time. You can either make a separate command that disables flight, or you can use an array list to store the UUID of players that are in flight mode. Then, when they run the command, check if their UUID is in the list. If it is, turn off flight and remove them from the list. If it is not, then add them and turn on flight.
or you could add the check as the one in line 2 of the example below Code:java if(cmd.getName().equalsIgnoreCase("fly")){ if(player.getAllowFlight()){ sender.sendMessage(ChatColor.GREEN + "Fly has been disabled!"); player.setFlying(false); player.setFlySpeed(0); player.setAllowFlight(false); }else{ sender.sendMessage(ChatColor.GREEN + "Fly has been enabled!"); player.setAllowFlight(true); player.setFlySpeed(1); player.setFlying(true); } }
That's probably easier for this plugin^ But if you ever want to make a different plugin with a toggle in the future you can use my method.