Solved Command Locking

Discussion in 'Plugin Development' started by ProSl3nderMan, Jul 15, 2015.

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

    ProSl3nderMan

    Hey guys, I need help on how to lock a command from being used. *Mind Blown* lol. What I mean by command locking is not allowing a person to do a command after they've done another command.

    For an example:
    Someone does "/game join 1". I want to prevent them from doing "/game join 2" until they do "/game leave".

    So can anyone help me in doing so?
     
  2. Online

    timtower Moderator Moderator

    @ProSl3nderMan Well, if you log that they joined something then you see that they are in something when they perform commands, if(isInGame){ no join cmd }
     
  3. Offline

    DoggyCode™

    Umm, I know that this might have some downturns, but can't you add every player to different Array Lists- then check if the Array List contains the player? But do this in an event, like a on command perform event thing, I know that exists- and then cancel the event or something.
     
  4. Offline

    Adamki11s

    Use the command preprocess event to cancel commands if the player doesn't have permission.
     
  5. Offline

    ProSl3nderMan

    So I used your idea, but it doesn't seem to work when I do /cr leave, which is the leaving command I was talking about.

    Can someone help me figure out what's wrong with my code?:
    Code:
    package me.ProSl3nderMan.AccessControl;
    
    import java.util.ArrayList;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class AccessControlCrJoinExecutor implements CommandExecutor {
    
        @SuppressWarnings("unused")
        private AccessControl plugin;
      
        public AccessControlCrJoinExecutor(AccessControl plugin){
            this.plugin = plugin;
        }
      
        ArrayList<String> cr1 = new ArrayList<String>();
        ArrayList<String> cr2 = new ArrayList<String>();
        ArrayList<String> cr3 = new ArrayList<String>();
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (args.length < 0){
                sender.sendMessage(ChatColor.RED + "Usage: /cr join (1, 2, 3)");
                return true;
            }
          
            Player p = (Player) sender;
      
            if (args[0].equalsIgnoreCase("join")) {
            }
            if (args[1].equalsIgnoreCase("1")){
              
                // ARRAY LIST CR1
                cr1.add(sender.getName());
                          
                if (cr1.contains(p.getName())){
                    p.sendMessage(ChatColor.RED + "You are already in a game! Do /cr leave to leave.");
                    return (Boolean) false;
                }
                if (cr2.contains(p.getName())){
                    p.sendMessage(ChatColor.RED + "You are already in a game! Do /cr leave to leave.");
                    return (Boolean) false;
                }
                if (cr3.contains(p.getName())){
                    p.sendMessage(ChatColor.RED + "You are already in a game! Do /cr leave to leave.");
                    return (Boolean) false;
                }
              
                int random = (int) (Math.random() * 10 /* <- Put how much locations there are here */ + 1);
                if (random == 1) {
                    int x = 1594;
                    int y = 68;
                    int z = 1408;
                    p.teleport(new Location(Bukkit.getWorld("cr1"), x, y, z));
                }else if(random == 2) {
                    int x = 1605;
                    int y = 68;
                    int z = 1408;
                    p.teleport(new Location(Bukkit.getWorld("cr1"), x, y, z));
                }else if(random == 3) {
                    int x = 1616;
                    int y = 68;
                    int z = 1408;
                    p.teleport(new Location(Bukkit.getWorld("cr1"), x, y, z));
                }else if(random == 4) {
                    int x = 1627;
                    int y = 68;
                    int z = 1408;
                    p.teleport(new Location(Bukkit.getWorld("cr1"), x, y, z));
                }else if(random == 5) {
                    int x = 1638;
                    int y = 68;
                    int z = 1408;
                    p.teleport(new Location(Bukkit.getWorld("cr1"), x, y, z));
                }else if (random == 6) {
                    int x = 1594;
                    int y = 68;
                    int z = 1436;
                    p.teleport(new Location(Bukkit.getWorld("cr1"), x, y, z));
                }else if(random == 7) {
                    int x = 1605;
                    int y = 68;
                    int z = 1436;
                    p.teleport(new Location(Bukkit.getWorld("cr1"), x, y, z));
                }else if(random == 8) {
                    int x = 1616;
                    int y = 68;
                    int z = 1436;
                    p.teleport(new Location(Bukkit.getWorld("cr1"), x, y, z));
                }else if(random == 9) {
                    int x = 1627;
                    int y = 68;
                    int z = 1436;
                    p.teleport(new Location(Bukkit.getWorld("cr1"), x, y, z));
                }else if(random == 10) {
                    int x = 1638;
                    int y = 68;
                    int z = 1436;
                    p.teleport(new Location(Bukkit.getWorld("cr1"), x, y, z));
                }
                p.sendMessage(ChatColor.DARK_GREEN + "Hi " + sender.getName() + "! You just joined the cops and robbers game 1! If there is no cop yet, type /cops. Respect the rules and have fun!");
             
              
              
              
               }
          
            // ###### CR GAME 1 ENDS HERE ######
            // ###### CR GAME 2 STARTS HERE ######
          
            if (args[1].equalsIgnoreCase("2")){
              
                // ARRAY LIST CR1
                cr1.add(sender.getName());
                                      
                if (cr1.contains(p.getName())){
                    p.sendMessage(ChatColor.RED + "You are already in a game! Do /cr leave to leave.");
                    return (Boolean) false;
                }
                if (cr2.contains(p.getName())){
                    p.sendMessage(ChatColor.RED + "You are already in a game! Do /cr leave to leave.");
                    return (Boolean) false;
                }
                if (cr3.contains(p.getName())){
                    p.sendMessage(ChatColor.RED + "You are already in a game! Do /cr leave to leave.");
                    return (Boolean) false;
                }
              
              
                int random = (int) (Math.random() * 8 /* <- Put how much locations there are here */ + 1);
                if (random == 1) {
                    int x = 59;
                    int y = 134;
                    int z = -68;
                    p.teleport(new Location(Bukkit.getWorld("Alcatraz"), x, y, z));
                }else if(random == 2) {
                    int x = 47;
                    int y = 134;
                    int z = -68;
                    p.teleport(new Location(Bukkit.getWorld("Alcatraz"), x, y, z));
                }else if(random == 3) {
                    int x = 35;
                    int y = 134;
                    int z = -68;
                    p.teleport(new Location(Bukkit.getWorld("Alcatraz"), x, y, z));
                }else if(random == 4) {
                    int x = 23;
                    int y = 134;
                    int z = -68;
                    p.teleport(new Location(Bukkit.getWorld("Alcatraz"), x, y, z));
                }else if(random == 5) {
                    int x = 23;
                    int y = 134;
                    int z = -32;
                    p.teleport(new Location(Bukkit.getWorld("Alcatraz"), x, y, z));
                }else if(random == 6) {
                    int x = 35;
                    int y = 134;
                    int z = -32;
                    p.teleport(new Location(Bukkit.getWorld("Alcatraz"), x, y, z));
                }else if(random == 7) {
                    int x = 47;
                    int y = 134;
                    int z = -32;
                    p.teleport(new Location(Bukkit.getWorld("Alcatraz"), x, y, z));
                }else if(random == 8) {
                    int x = 59;
                    int y = 134;
                    int z = -32;
                    p.teleport(new Location(Bukkit.getWorld("Alcatraz"), x, y, z));
                }
                p.sendMessage(ChatColor.DARK_GREEN + "You just joined the cops and robbers game 2! If there is no cop yet, type /cops. Respect the rules and have fun!");
             
               }
          
            // ###### CR GAME 2 ENDS HERE ######
            // ###### CR GAME 3 STARTS HERE ######
          
            if (args[1].equalsIgnoreCase("3")){
              
                // ARRAY LIST CR1
                cr1.add(sender.getName());
                                      
                if (cr1.contains(p.getName())){
                    p.sendMessage(ChatColor.RED + "You are already in a game! Do /cr leave to leave.");
                    return (Boolean) false;
                }
                if (cr2.contains(p.getName())){
                    p.sendMessage(ChatColor.RED + "You are already in a game! Do /cr leave to leave.");
                    return (Boolean) false;
                }
                if (cr3.contains(p.getName())){
                    p.sendMessage(ChatColor.RED + "You are already in a game! Do /cr leave to leave.");
                    return (Boolean) false;
                }
              
                int random = (int) (Math.random() * 8 /* <- Put how much locations there are here */ + 1);
                if (random == 1) {
                    int x = 2324;
                    int y = 97;
                    int z = 1850;
                    p.teleport(new Location(Bukkit.getWorld("cr3"), x, y, z));
                }else if(random == 2) {
                    int x = 2316;
                    int y = 97;
                    int z = 1850;
                    p.teleport(new Location(Bukkit.getWorld("cr3"), x, y, z));
                }else if(random == 3) {
                    int x = 2308;
                    int y = 97;
                    int z = 1850;
                    p.teleport(new Location(Bukkit.getWorld("cr3"), x, y, z));
                }else if(random == 4) {
                    int x = 2300;
                    int y = 97;
                    int z = 1850;
                    p.teleport(new Location(Bukkit.getWorld("cr3"), x, y, z));
                }else if(random == 5) {
                    int x = 2324;
                    int y = 97;
                    int z = 1836;
                    p.teleport(new Location(Bukkit.getWorld("cr3"), x, y, z));
                }else if(random == 6) {
                    int x = 2316;
                    int y = 97;
                    int z = 1836;
                    p.teleport(new Location(Bukkit.getWorld("cr3"), x, y, z));
                }else if(random == 7) {
                    int x = 2308;
                    int y = 97;
                    int z = 1836;
                    p.teleport(new Location(Bukkit.getWorld("cr3"), x, y, z));
                }else if(random == 8) {
                    int x = 2300;
                    int y = 97;
                    int z = 1836;
                    p.teleport(new Location(Bukkit.getWorld("cr3"), x, y, z));
                }
                p.sendMessage(ChatColor.DARK_GREEN + "You just joined the cops and robbers game 3! If there is no cop yet, type /cops. Respect the rules and have fun!");
             
               }
          
            // ###### GAME 3 ENDS HERE ######
          
            if (args[0].equalsIgnoreCase("leave")) {
                cr2.remove(sender.getName());
            }
          
            return true;
        }
    
    }
    I get this error in the console when I do /cr leave:
    Error (open)

    [13:34:47 INFO]: CONSOLE: Reload complete.
    [13:34:49 INFO]: ProSl3nderMan issued server command: /cr leave
    [13:34:49 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'cr' in plugin AccessControl v0.1
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spi
    got.jar:git-Spigot-f928e7a-e91aed8]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-f928e7a-e91aed8]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:642) ~[spigot.jar:git-Spigot-f928e7a-e91aed8]
    at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1135) [spigot.jar:git-Spigot-f928e7a-e91aed8]
    at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:970) [spigot.jar:git-Spigot-f928e7a-e91aed8]
    at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-Spigot-f928e7a-e91aed8]
    at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot.jar:git-Spigot-f928e7a-e91aed8]
    at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-f928e7a-e91aed8]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_45]
    at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_45]
    at net.minecraft.server.v1_8_R3.SystemUtils.a(SystemUtils.java:19) [spigot.jar:git-Spigot-f928e7a-e91aed8]
    at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:718) [spigot.jar:git-Spigot-f928e7a-e91aed8]
    at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:367) [spigot.jar:git-Spigot-f928e7a-e91aed8]
    at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:657) [spigot.jar:git-Spigot-f928e7a-e91aed8]
    at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:560) [spigot.jar:git-Spigot-f928e7a-e91aed8]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_45]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
    at me.ProSl3nderMan.AccessControl.AccessControlCrJoinExecutor.onCommand(AccessControlCrJoinExecutor.java:36) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-f928e7a-e91aed8]
    ... 15 more
    >
     
  6. Offline

    guitargun

    Check your brackets and you find the problem. Also check the number of arguments.

    Store the players with their UUID in a set or list and use the contains to check if they are in it.. I have some sort of code somewhere but no access to it
     
  7. Offline

    ProSl3nderMan

    I've checked all the brackets, I can't find the problem :? Also, is storing UUID's in a list necessary for this situation?
     
  8. Offline

    guitargun


    Check closely to your if statement where you check for join

    Also check closely where you add and check this will cause a message that will always be send to you.. You forgot to do something there
     
  9. Offline

    ProSl3nderMan

    You know what? Your something LOL. Now what I mean by that is, you actually made me think more about it. You helped me fix a problem that I was stuck on yesterday that I thought I already fixed lol!

    Still trying to figure out the command locking thing though. I fixed my code up a little bit:

    Code:
    package me.ProSl3nderMan.AccessControl;
    
    import java.util.ArrayList;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    
    public class AccessControlCrJoinExecutor implements Listener, CommandExecutor {
    
        private AccessControl plugin;
      
        public AccessControlCrJoinExecutor(AccessControl plugin){
            this.plugin = plugin;
        }
      
        ArrayList<String> cr1 = new ArrayList<String>();
        ArrayList<String> cr2 = new ArrayList<String>();
        ArrayList<String> cr3 = new ArrayList<String>();
    
        @EventHandler
        public void onPlayerCommand(PlayerCommandPreprocessEvent e, String[] args) {
            Player p = e.getPlayer();
            if (cr2.contains(p.getName())) {
                if (args[0].equalsIgnoreCase("join")) {
                    if (args[1].equalsIgnoreCase("1")) {
                      
                        e.setCancelled(true);
                        p.sendMessage("Hi");
                      
                    }
                }
            }
        }
      
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, plugin);
        }
      
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (args.length < 0){
                sender.sendMessage(ChatColor.RED + "Usage: /cr join (1, 2, 3)");
                return true;
            }
          
            Player p = (Player) sender;
      
            if (args[0].equalsIgnoreCase("join")) {
          
                // ###### CR 1 STARTS HERE ######
              
            if (args[1].equalsIgnoreCase("1")){
              
                // ARRAY LIST CR1
                cr1.add(sender.getName());
              
                int random = (int) (Math.random() * 10 /* <- Put how much locations there are here */ + 1);
                if (random == 1) {
                    int x = 1594;
                    int y = 68;
                    int z = 1408;
                    p.teleport(new Location(Bukkit.getWorld("cr1"), x, y, z));
                }else if(random == 2) {
                    int x = 1605;
                    int y = 68;
                    int z = 1408;
                    p.teleport(new Location(Bukkit.getWorld("cr1"), x, y, z));
                }else if(random == 3) {
                    int x = 1616;
                    int y = 68;
                    int z = 1408;
                    p.teleport(new Location(Bukkit.getWorld("cr1"), x, y, z));
                }else if(random == 4) {
                    int x = 1627;
                    int y = 68;
                    int z = 1408;
                    p.teleport(new Location(Bukkit.getWorld("cr1"), x, y, z));
                }else if(random == 5) {
                    int x = 1638;
                    int y = 68;
                    int z = 1408;
                    p.teleport(new Location(Bukkit.getWorld("cr1"), x, y, z));
                }else if (random == 6) {
                    int x = 1594;
                    int y = 68;
                    int z = 1436;
                    p.teleport(new Location(Bukkit.getWorld("cr1"), x, y, z));
                }else if(random == 7) {
                    int x = 1605;
                    int y = 68;
                    int z = 1436;
                    p.teleport(new Location(Bukkit.getWorld("cr1"), x, y, z));
                }else if(random == 8) {
                    int x = 1616;
                    int y = 68;
                    int z = 1436;
                    p.teleport(new Location(Bukkit.getWorld("cr1"), x, y, z));
                }else if(random == 9) {
                    int x = 1627;
                    int y = 68;
                    int z = 1436;
                    p.teleport(new Location(Bukkit.getWorld("cr1"), x, y, z));
                }else if(random == 10) {
                    int x = 1638;
                    int y = 68;
                    int z = 1436;
                    p.teleport(new Location(Bukkit.getWorld("cr1"), x, y, z));
                }
                p.sendMessage(ChatColor.DARK_GREEN + "Hi " + sender.getName() + "! You just joined the cops and robbers game 1! If there is no cop yet, type /cops. Respect the rules and have fun!");
             
               }
          
            // ###### CR GAME 1 ENDS HERE ######
            // ###### CR GAME 2 STARTS HERE ######
          
            if (args[1].equalsIgnoreCase("2")){
              
                // ARRAY LIST CR2
                cr2.add(sender.getName());
              
                int random = (int) (Math.random() * 8 /* <- Put how much locations there are here */ + 1);
                if (random == 1) {
                    int x = 59;
                    int y = 134;
                    int z = -68;
                    p.teleport(new Location(Bukkit.getWorld("Alcatraz"), x, y, z));
                }else if(random == 2) {
                    int x = 47;
                    int y = 134;
                    int z = -68;
                    p.teleport(new Location(Bukkit.getWorld("Alcatraz"), x, y, z));
                }else if(random == 3) {
                    int x = 35;
                    int y = 134;
                    int z = -68;
                    p.teleport(new Location(Bukkit.getWorld("Alcatraz"), x, y, z));
                }else if(random == 4) {
                    int x = 23;
                    int y = 134;
                    int z = -68;
                    p.teleport(new Location(Bukkit.getWorld("Alcatraz"), x, y, z));
                }else if(random == 5) {
                    int x = 23;
                    int y = 134;
                    int z = -32;
                    p.teleport(new Location(Bukkit.getWorld("Alcatraz"), x, y, z));
                }else if(random == 6) {
                    int x = 35;
                    int y = 134;
                    int z = -32;
                    p.teleport(new Location(Bukkit.getWorld("Alcatraz"), x, y, z));
                }else if(random == 7) {
                    int x = 47;
                    int y = 134;
                    int z = -32;
                    p.teleport(new Location(Bukkit.getWorld("Alcatraz"), x, y, z));
                }else if(random == 8) {
                    int x = 59;
                    int y = 134;
                    int z = -32;
                    p.teleport(new Location(Bukkit.getWorld("Alcatraz"), x, y, z));
                }
                p.sendMessage(ChatColor.DARK_GREEN + "You just joined the cops and robbers game 2! If there is no cop yet, type /cops. Respect the rules and have fun!");
             
               }
          
            // ###### CR GAME 2 ENDS HERE ######
            // ###### CR GAME 3 STARTS HERE ######
          
            if (args[1].equalsIgnoreCase("3")){
              
                // ARRAY LIST CR3
                cr3.add(sender.getName());
              
                int random = (int) (Math.random() * 8 /* <- Put how much locations there are here */ + 1);
                if (random == 1) {
                    int x = 2324;
                    int y = 97;
                    int z = 1850;
                    p.teleport(new Location(Bukkit.getWorld("cr3"), x, y, z));
                }else if(random == 2) {
                    int x = 2316;
                    int y = 97;
                    int z = 1850;
                    p.teleport(new Location(Bukkit.getWorld("cr3"), x, y, z));
                }else if(random == 3) {
                    int x = 2308;
                    int y = 97;
                    int z = 1850;
                    p.teleport(new Location(Bukkit.getWorld("cr3"), x, y, z));
                }else if(random == 4) {
                    int x = 2300;
                    int y = 97;
                    int z = 1850;
                    p.teleport(new Location(Bukkit.getWorld("cr3"), x, y, z));
                }else if(random == 5) {
                    int x = 2324;
                    int y = 97;
                    int z = 1836;
                    p.teleport(new Location(Bukkit.getWorld("cr3"), x, y, z));
                }else if(random == 6) {
                    int x = 2316;
                    int y = 97;
                    int z = 1836;
                    p.teleport(new Location(Bukkit.getWorld("cr3"), x, y, z));
                }else if(random == 7) {
                    int x = 2308;
                    int y = 97;
                    int z = 1836;
                    p.teleport(new Location(Bukkit.getWorld("cr3"), x, y, z));
                }else if(random == 8) {
                    int x = 2300;
                    int y = 97;
                    int z = 1836;
                    p.teleport(new Location(Bukkit.getWorld("cr3"), x, y, z));
                }
                p.sendMessage(ChatColor.DARK_GREEN + "You just joined the cops and robbers game 3! If there is no cop yet, type /cops. Respect the rules and have fun!");
             
               }
          
            // ###### GAME 3 ENDS HERE ######
            }
            if (args[1].equalsIgnoreCase("leave")) {
                cr2.remove(sender.getName());
                p.sendMessage(ChatColor.DARK_GREEN + "You just left the game!");
            }
          
            return true;
        }
    
    }
    
    I'm not getting "hi" and it is not cancelling: "e.setCancelled(true);"

    Solved it, I just had to do a lot more work.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
Thread Status:
Not open for further replies.

Share This Page