How to make one command do multiple things

Discussion in 'Plugin Development' started by 3nz, Mar 2, 2017.

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

    3nz

    So I am a new coder and I want to try to make it so when I type /incognito it puts me in vanish (I already did that) but then im trying to make it so when I type /incognito again it will take me out of vanish etc. Can someone please help me!
     
  2. @3nz
    When executing the incognito command, check if player isn't vanished, if so, vanish them, if not, unvanish them.
     
  3. Offline

    SeniorCluckers

    Use a if or/and else statement. Check if your invisible and if you are remove it and vice versa.
     
  4. Offline

    3nz

  5. Offline

    GayZoTe

    put the vanished players in a list
    ArrayList<Player> vanished = new ArrayList<Player>();


    check if he is in
    if(vanished.contains(player)) {
    vanished.remove(player);
    //remove the vanished
    }
    else {
    vanished.add(player);
    //vanish the player
    }
     
  6. Offline

    MrGeneralQ

    @3nz like @GayZoTe already said. Just make sure you initialize the ArrayList (vanished) only once!
     
  7. Offline

    3nz

    @GayZoTe
    When I type the command it just keeps telling me the 2nd part "You are Invisble!"
    Here is all my code, if you want my Main class just ask.

    Code:
    package incognito;
    
    import java.util.ArrayList;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class me3nz implements CommandExecutor{
    
       @Override
       public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
         if(sender.hasPermission("incognito.allow")) {
         if(sender instanceof Player) {
           Player p = (Player) sender;
           ArrayList<Player> vanished = new ArrayList<Player>();
           if(vanished.contains(p)) {
             vanished.remove(p);
             p.sendMessage(ChatColor.RED + "You are Visible!");
             //remove the vanished
             }
             else {
             vanished.add(p);
             p.sendMessage(ChatColor.RED + "You are Invisible!");
             //vanish the player
             }
           
           return true;
         }
         }
         if(!(sender.hasPermission("incognito.allow"))) {
           Player p = (Player) sender;
           p.sendMessage(ChatColor.RED + "You don't have permissions to use this command, if you think this is an error please contact an Administrator!");
           
         }
         return false;
       }
    }
     
  8. Offline

    Zombie_Striker

    Because it is inside the onCommand, You create a new array each time the command is sent. Move the array outside of the onCommand.
     
  9. Offline

    GayZoTe

    yes i don't have said but put the arraylist just after you declared your class to make a public arraylist
     
  10. Offline

    3nz

    Okay, thank you sooo much! I finally finished the plugin, you guys are awesome!
     
  11. Offline

    Zombie_Striker

    @3nz
    If your problem has been solved, mark this thread as solved.
     
Thread Status:
Not open for further replies.

Share This Page