Check if String in List equals a mesage on server

Discussion in 'Plugin Development' started by CRAZYxMUNK3Y, Aug 13, 2012.

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

    CRAZYxMUNK3Y

    I was wondering if/how you can check if you can check for a String in a List.

    How i have it setup;
    Server starts and adds all commands in config to configCommands List.
    As player types a command, server will check List, if String is in List, things will happen

    Code:
    Code:java
    1.  
    2. package me.crazy.commanddelay;
    3.  
    4. import java.awt.List;
    5. import java.util.ArrayList;
    6.  
    7. import org.bukkit.configuration.file.FileConfiguration;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.player.AsyncPlayerChatEvent;
    10. import org.bukkit.plugin.PluginManager;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class CommandDelay extends JavaPlugin implements Listener {
    14.  
    15. FileConfiguration config = getConfig();
    16. List configCommands = new List();
    17. ArrayList<String> commandToDelay = new ArrayList<String>();
    18. //Adds commnds to delay with scheduler
    19.  
    20. public void onEnable() {
    21. PluginManager pm = getServer().getPluginManager();
    22. pm.registerEvents(this, this);
    23. getCommandsFromConfig();
    24. }
    25.  
    26. public void playerChat(AsyncPlayerChatEvent e) {
    27. String message = e.getMessage();
    28. if (message.startsWith("/")) {
    29. if (message.equalsIgnoreCase(message/*replace with returned String from CheckForCommand Method*/));
    30.  
    31. } else {
    32. return;
    33. }
    34. }
    35.  
    36. // Adds each command from config to List on start
    37. public void getCommandsFromConfig() {
    38. for (String commands : config.getStringList("commands")) {
    39. configCommands.add(commands);
    40. }
    41. }
    42.  
    43. // Check if command typed is in configCommands List
    44. public void checkForCommand() {
    45. //For loop of some sort
    46. }
    47. }
    48.  
    49.  


    Thanks in advanced.
     
  2. Offline

    Courier

    Code:java
    1. if(commandToDelay.contains(command))
    2. {
    3. doStuff();
    4. }
    It would be better to use a HashSet though, Lists have linear search time (the more items in the list, the longer it takes to search), HashSets have essentially a constant search rate.
     
    dark navi likes this.
  3. Offline

    dark navi

    First of all, you probably shouldn't do this via a chat handler. Commands should be properly registered through Bukkit or a vast number of unexpected things could go wrong.

    Secondly, you would just need to iterate through your 'commands' list, passing it your command that you handled in your chat event.
     
  4. Offline

    CRAZYxMUNK3Y

    Courier
    I tried that but i am trying to get something like;
    Code:java
    1.  
    2. HashSet<String, Player> commandsToDelay = new HashSet<String, Player>();
    3.  

    But I noticed that you can't, what would be another way of doing that? I also tried ArrayList and HashMap and still nothing.

    New Code If You Want (open)

    Code:java
    1.  
    2. public void playerChat(PlayerCommandPreprocessEvent e) {
    3. String message = e.getMessage();
    4. if(message.equals(CheckForCommand())){
    5.  
    6. }
    7. }
    8.  
    9. // Adds each command from config to List on start
    10. public void GetCommandsFromConfig() {
    11. for (String commands : config.getStringList("commands")) {
    12. configCommands.add(commands);
    13. }
    14. }
    15.  
    16. // Check if command typed is in configCommands List
    17. public String CheckForCommand() {
    18. String message = null;
    19. String player = null;
    20. if (configCommands.contains(message)) {
    21. commandsToDelay.add(message);
    22. commandsToDelay.add(player);
    23. }
    24. return message;
    25. }
    26.  

     
  5. Offline

    Courier

    The HashSet is for your configCommands. A HashSet is a type of Set. A Set is basically like a list, except that there cannot be duplicates, and (in most sets) the elements are in no particular order. A HashSet works great for configCommands, but I'm not exactly sure what you are trying to do with commandsToDelay. It sounds like you want something more like this:
    Code:java
    1. ArrayList<String> comandsToDelay = new ArrayList<String>();
    2. ArrayList<Player> playersToDelay = new ArrayList<Player>();
    3.  
    4. private void delayCommand(String cmd, Player player)
    5. {
    6. this.commandsToDelay.add(cmd);
    7. this.playersToDelay.add(player);
    8. }
    9.  
    10. private void executeAllDelayedCommands()
    11. {
    12. for(int i = 0; i < commandsToDelay.size(); ++i)
    13. {
    14. String command = commandsToDelay.get(i);
    15. Player player = playersToDelay.get(i);
    16. //TODO execute the command on player, or whatever it is you wanted to do
    17. }
    18. //reset the lists, since the commands have been executed
    19. commandsToDelay.clear();
    20. playersToDelay.clear();
    21. }

    Or could use an Object that contains a Player and a String, so you'd only need one list.
    Code:java
    1. class DelayedCommand
    2. {
    3. public String command;
    4. public Player player;
    5. public DelayedCommand(String command, Player player)
    6. {
    7. this.command = command;
    8. this.player = player;
    9. }
    10. }
    11.  
    12. ...
    13.  
    14. ArrayList<DelayedCommand> commandsToDelay = new ArrayList<DelayedCommand>();
    15. ...
     
  6. Offline

    CRAZYxMUNK3Y

    Courier
    Sorry about the late reply, been busy.

    I have a rough idea about what is going on in the code, but just to confirm(so the code isn't wrong), what i am attempted to do is;
    As the command is typed, add the command and player to an Array/HashMap/HashSet
    Every x amount of seconds a Scheduler will remove everything from the arrays(players and commands). If the player tries to execute the same command within the clear time, they will either be denied or charged(money or item wise).

    Sorry if that doesn't make sense.
     
  7. Offline

    Courier

    It makes sense. Are you sure that is what you want exactly? You want all commands to be cleared together, instead of each command having its own cooldown period?
     
Thread Status:
Not open for further replies.

Share This Page