Implements the add condition command and its tab completion
This commit is contained in:
		| @@ -1,6 +1,8 @@ | ||||
| package net.knarcraft.paidsigns; | ||||
|  | ||||
| import net.knarcraft.paidsigns.command.AddCommand; | ||||
| import net.knarcraft.paidsigns.command.AddConditionCommand; | ||||
| import net.knarcraft.paidsigns.command.AddConditionTabCompleter; | ||||
| import net.knarcraft.paidsigns.command.AddTabCompleter; | ||||
| import net.knarcraft.paidsigns.command.ReloadTabCommand; | ||||
| import net.knarcraft.paidsigns.command.RemoveTabCommand; | ||||
| @@ -105,6 +107,12 @@ public final class PaidSigns extends JavaPlugin { | ||||
|             addCommand.setTabCompleter(new AddTabCompleter()); | ||||
|         } | ||||
|  | ||||
|         PluginCommand addConditionCommand = this.getCommand("addPaidSignCondition"); | ||||
|         if (addConditionCommand != null) { | ||||
|             addConditionCommand.setExecutor(new AddConditionCommand()); | ||||
|             addConditionCommand.setTabCompleter(new AddConditionTabCompleter()); | ||||
|         } | ||||
|  | ||||
|         PluginCommand removeCommand = this.getCommand("removePaidSign"); | ||||
|         if (removeCommand != null) { | ||||
|             TabExecutor removeTabExecutor = new RemoveTabCommand(); | ||||
|   | ||||
| @@ -29,6 +29,7 @@ public class AddCommand implements CommandExecutor { | ||||
|         } | ||||
|         PaidSignManager manager = PaidSigns.getInstance().getSignManager(); | ||||
|         List<String> arguments = Tokenizer.tokenize(String.join(" ", args)); | ||||
|         int argumentSize = args[args.length - 1].isEmpty() ? arguments.size() : arguments.size() - 1; | ||||
|  | ||||
|         String signName = arguments.get(0); | ||||
|         double cost; | ||||
| @@ -41,20 +42,18 @@ public class AddCommand implements CommandExecutor { | ||||
|         String permission = arguments.get(2); | ||||
|         OptionState ignoreCase = OptionState.DEFAULT; | ||||
|         OptionState ignoreColor = OptionState.DEFAULT; | ||||
|         if (arguments.size() > 3) { | ||||
|         if (argumentSize > 3) { | ||||
|             ignoreCase = OptionState.fromString(arguments.get(3)); | ||||
|         } | ||||
|         if (arguments.size() > 4) { | ||||
|         if (argumentSize > 4) { | ||||
|             ignoreColor = OptionState.fromString(arguments.get(4)); | ||||
|         } | ||||
|  | ||||
|         try { | ||||
|             PaidSign sign = new PaidSign(signName, cost, permission, ignoreCase, ignoreColor); | ||||
|             for (PaidSign otherSign : manager.getAllPaidSigns()) { | ||||
|                 if (sign.getName().equals(otherSign.getName())) { | ||||
|                     sender.sendMessage("A paid sign with the same name already exists"); | ||||
|                     return false; | ||||
|                 } | ||||
|             if (manager.getPaidSign(signName) != null) { | ||||
|                 sender.sendMessage("A paid sign with the same name already exists"); | ||||
|                 return false; | ||||
|             } | ||||
|             manager.addPaidSign(sign); | ||||
|             sender.sendMessage("Successfully added new paid sign"); | ||||
|   | ||||
| @@ -1,16 +1,56 @@ | ||||
| package net.knarcraft.paidsigns.command; | ||||
|  | ||||
| import net.knarcraft.paidsigns.PaidSigns; | ||||
| import net.knarcraft.paidsigns.container.PaidSign; | ||||
| import net.knarcraft.paidsigns.property.OptionState; | ||||
| import net.knarcraft.paidsigns.utility.Tokenizer; | ||||
| import org.bukkit.command.Command; | ||||
| import org.bukkit.command.CommandExecutor; | ||||
| import org.bukkit.command.CommandSender; | ||||
| import org.jetbrains.annotations.NotNull; | ||||
|  | ||||
| import java.util.List; | ||||
|  | ||||
| /** | ||||
|  * A representation of the command for adding a new match condition for a sign | ||||
|  */ | ||||
| public class AddConditionCommand implements CommandExecutor { | ||||
|  | ||||
|     @Override | ||||
|     public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { | ||||
|         List<String> arguments = Tokenizer.tokenize(String.join(" ", args)); | ||||
|         int argumentSize = args[args.length - 1].isEmpty() ? arguments.size() : arguments.size() - 1; | ||||
|         if (argumentSize < 3) { | ||||
|             return false; | ||||
|         } | ||||
|  | ||||
|  | ||||
|         String name = arguments.get(0); | ||||
|         short lineNumber; | ||||
|         try { | ||||
|             lineNumber = (short) (Short.parseShort(arguments.get(1)) - 1); | ||||
|         } catch (NumberFormatException exception) { | ||||
|             sender.sendMessage("Invalid line number given"); | ||||
|             return false; | ||||
|         } | ||||
|         String stringToMatch = arguments.get(2); | ||||
|         boolean executeRegEx = false; | ||||
|         if (argumentSize > 3) { | ||||
|             executeRegEx = Boolean.parseBoolean(arguments.get(3)); | ||||
|         } | ||||
|         OptionState ignoreCase = OptionState.DEFAULT; | ||||
|         if (argumentSize > 4) { | ||||
|             ignoreCase = OptionState.getFromBoolean(Boolean.parseBoolean(arguments.get(4))); | ||||
|         } | ||||
|         OptionState ignoreColor = OptionState.DEFAULT; | ||||
|         if (argumentSize > 5) { | ||||
|             ignoreColor = OptionState.getFromBoolean(Boolean.parseBoolean(arguments.get(5))); | ||||
|         } | ||||
|         PaidSign sign = PaidSigns.getInstance().getSignManager().getPaidSign(name); | ||||
|         if (sign == null) { | ||||
|             sender.sendMessage("No such paid sign exists"); | ||||
|             return false; | ||||
|         } | ||||
|         sign.addCondition(lineNumber, stringToMatch, executeRegEx, ignoreCase, ignoreColor); | ||||
|         return false; | ||||
|     } | ||||
|  | ||||
|   | ||||
| @@ -0,0 +1,57 @@ | ||||
| package net.knarcraft.paidsigns.command; | ||||
|  | ||||
| import net.knarcraft.paidsigns.utility.TabCompleteHelper; | ||||
| import net.knarcraft.paidsigns.utility.Tokenizer; | ||||
| import org.bukkit.command.Command; | ||||
| import org.bukkit.command.CommandSender; | ||||
| import org.bukkit.command.TabCompleter; | ||||
| import org.jetbrains.annotations.NotNull; | ||||
| import org.jetbrains.annotations.Nullable; | ||||
|  | ||||
| import java.util.List; | ||||
|  | ||||
| /** | ||||
|  * The tab completer for the add paid sign condition command | ||||
|  */ | ||||
| public class AddConditionTabCompleter implements TabCompleter { | ||||
|  | ||||
|     private List<String> lineIndices; | ||||
|     private List<String> stringsToMatch; | ||||
|     private List<String> booleans; | ||||
|     private List<String> optionStates; | ||||
|  | ||||
|     @Nullable | ||||
|     @Override | ||||
|     public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, | ||||
|                                       @NotNull String[] args) { | ||||
|         if (lineIndices == null) { | ||||
|             initializeValues(); | ||||
|         } | ||||
|  | ||||
|         List<String> arguments = Tokenizer.tokenize(String.join(" ", args)); | ||||
|         int argumentSize = args[args.length - 1].isEmpty() ? arguments.size() : arguments.size() - 1; | ||||
|         if (argumentSize < 1) { | ||||
|             return TabCompleteHelper.getPaidSignNames(); | ||||
|         } else if (argumentSize < 2) { | ||||
|             return this.lineIndices; | ||||
|         } else if (argumentSize < 3) { | ||||
|             return stringsToMatch; | ||||
|         } else if (argumentSize < 4) { | ||||
|             return booleans; | ||||
|         } else if (argumentSize < 6) { | ||||
|             return optionStates; | ||||
|         } | ||||
|         return null; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Initializes the values available for tab completion | ||||
|      */ | ||||
|     private void initializeValues() { | ||||
|         lineIndices = TabCompleteHelper.getSignLines(); | ||||
|         stringsToMatch = TabCompleteHelper.getExampleConditionStrings(); | ||||
|         booleans = TabCompleteHelper.getBooleans(); | ||||
|         optionStates = TabCompleteHelper.getOptionStates(); | ||||
|     } | ||||
|  | ||||
| } | ||||
| @@ -1,5 +1,6 @@ | ||||
| package net.knarcraft.paidsigns.command; | ||||
|  | ||||
| import net.knarcraft.paidsigns.utility.TabCompleteHelper; | ||||
| import net.knarcraft.paidsigns.utility.Tokenizer; | ||||
| import org.bukkit.Bukkit; | ||||
| import org.bukkit.command.Command; | ||||
| @@ -128,21 +129,9 @@ public class AddTabCompleter implements TabCompleter { | ||||
|      * Initializes the values available for tab completion | ||||
|      */ | ||||
|     private static void initializeValues() { | ||||
|         names = new ArrayList<>(); | ||||
|         names.add("sign1"); | ||||
|         names.add("\"lift up sign\""); | ||||
|         names.add("\"lift down sign\""); | ||||
|  | ||||
|         costs = new ArrayList<>(); | ||||
|         costs.add("1"); | ||||
|         costs.add("5"); | ||||
|         costs.add("10"); | ||||
|         costs.add("15"); | ||||
|  | ||||
|         options = new ArrayList<>(); | ||||
|         options.add("default"); | ||||
|         options.add("true"); | ||||
|         options.add("false"); | ||||
|         names = TabCompleteHelper.getExamplePaidSignNames(); | ||||
|         costs = TabCompleteHelper.getCosts(); | ||||
|         options = TabCompleteHelper.getOptionStates(); | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -13,6 +13,7 @@ import java.io.IOException; | ||||
| import java.util.ArrayList; | ||||
| import java.util.Arrays; | ||||
| import java.util.List; | ||||
| import java.util.Map; | ||||
| import java.util.logging.Level; | ||||
| import java.util.logging.Logger; | ||||
|  | ||||
| @@ -55,12 +56,12 @@ public class RemoveTabCommand implements TabExecutor { | ||||
|         int argumentSize = args[args.length - 1].isEmpty() ? arguments.size() : arguments.size() - 1; | ||||
|  | ||||
|         if (argumentSize < 1) { | ||||
|             List<PaidSign> allPaidSigns = PaidSigns.getInstance().getSignManager().getAllPaidSigns(); | ||||
|             List<String> signIds = new ArrayList<>(); | ||||
|             for (PaidSign sign : allPaidSigns) { | ||||
|                 signIds.add("\"" + sign.getName() + "\""); | ||||
|             Map<String, PaidSign> allPaidSigns = PaidSigns.getInstance().getSignManager().getAllPaidSigns(); | ||||
|             List<String> signNames = new ArrayList<>(); | ||||
|             for (String name : allPaidSigns.keySet()) { | ||||
|                 signNames.add("\"" + name + "\""); | ||||
|             } | ||||
|             return signIds; | ||||
|             return signNames; | ||||
|         } else { | ||||
|             return new ArrayList<>(); | ||||
|         } | ||||
|   | ||||
| @@ -9,7 +9,7 @@ import org.bukkit.event.EventPriority; | ||||
| import org.bukkit.event.Listener; | ||||
| import org.bukkit.event.block.SignChangeEvent; | ||||
|  | ||||
| import java.util.List; | ||||
| import java.util.Map; | ||||
|  | ||||
| /** | ||||
|  * A listener for listening to registered paid signs | ||||
| @@ -24,8 +24,8 @@ public class SignListener implements Listener { | ||||
|         } | ||||
|  | ||||
|         String[] lines = event.getLines(); | ||||
|         List<PaidSign> matchingSigns = PaidSigns.getInstance().getSignManager().getAllPaidSigns(); | ||||
|         for (PaidSign paidSign : matchingSigns) { | ||||
|         Map<String, PaidSign> matchingSigns = PaidSigns.getInstance().getSignManager().getAllPaidSigns(); | ||||
|         for (PaidSign paidSign : matchingSigns.values()) { | ||||
|             //If a match is found, just return | ||||
|             if (matchSign(paidSign, lines, event)) { | ||||
|                 return; | ||||
|   | ||||
| @@ -9,8 +9,7 @@ import org.bukkit.configuration.file.YamlConfiguration; | ||||
|  | ||||
| import java.io.File; | ||||
| import java.io.IOException; | ||||
| import java.util.ArrayList; | ||||
| import java.util.List; | ||||
| import java.util.HashMap; | ||||
| import java.util.Map; | ||||
| import java.util.logging.Level; | ||||
|  | ||||
| @@ -19,7 +18,7 @@ import java.util.logging.Level; | ||||
|  */ | ||||
| public final class PaidSignManager { | ||||
|  | ||||
|     private final List<PaidSign> paidSigns; | ||||
|     private final Map<String, PaidSign> paidSigns; | ||||
|     private static final File signsFile = new File(PaidSigns.getInstance().getDataFolder(), "data.yml"); | ||||
|  | ||||
|     /** | ||||
| @@ -27,7 +26,7 @@ public final class PaidSignManager { | ||||
|      * | ||||
|      * @param paidSigns <p>The paid signs this manager should manage</p> | ||||
|      */ | ||||
|     public PaidSignManager(List<PaidSign> paidSigns) { | ||||
|     public PaidSignManager(Map<String, PaidSign> paidSigns) { | ||||
|         this.paidSigns = paidSigns; | ||||
|     } | ||||
|  | ||||
| @@ -38,7 +37,7 @@ public final class PaidSignManager { | ||||
|      * @throws IOException <p>If unable to write to the signs file</p> | ||||
|      */ | ||||
|     public void addPaidSign(PaidSign paidSign) throws IOException { | ||||
|         this.paidSigns.add(paidSign); | ||||
|         this.paidSigns.put(paidSign.getName(), paidSign); | ||||
|         saveSigns(this.paidSigns); | ||||
|     } | ||||
|  | ||||
| @@ -50,7 +49,7 @@ public final class PaidSignManager { | ||||
|      * @throws IOException <p>If unable to write to the signs file</p> | ||||
|      */ | ||||
|     public boolean removePaidSign(String name) throws IOException { | ||||
|         boolean removed = this.paidSigns.removeIf((sign) -> sign.getName().equals(name)); | ||||
|         boolean removed = this.paidSigns.remove(name) != null; | ||||
|         if (!removed) { | ||||
|             return false; | ||||
|         } else { | ||||
| @@ -59,13 +58,23 @@ public final class PaidSignManager { | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Gets the paid sign with the given name | ||||
|      * | ||||
|      * @param name <p>The paid sign with the given name</p> | ||||
|      * @return <p>The paid sign with the given name, or null if it does not exist</p> | ||||
|      */ | ||||
|     public PaidSign getPaidSign(String name) { | ||||
|         return paidSigns.get(name); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Gets a copy of all registered paid signs | ||||
|      * | ||||
|      * @return <p>All registered paid signs</p> | ||||
|      */ | ||||
|     public List<PaidSign> getAllPaidSigns() { | ||||
|         return new ArrayList<>(paidSigns); | ||||
|     public Map<String, PaidSign> getAllPaidSigns() { | ||||
|         return new HashMap<>(paidSigns); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
| @@ -73,15 +82,15 @@ public final class PaidSignManager { | ||||
|      * | ||||
|      * @return <p>The loaded paid signs</p> | ||||
|      */ | ||||
|     public static List<PaidSign> loadSigns() { | ||||
|     public static Map<String, PaidSign> loadSigns() { | ||||
|         YamlConfiguration configuration = YamlConfiguration.loadConfiguration(signsFile); | ||||
|         ConfigurationSection signSection = configuration.getConfigurationSection("paidSigns"); | ||||
|         if (signSection == null) { | ||||
|             PaidSigns.getInstance().getLogger().log(Level.WARNING, "Signs section not found in data.yml"); | ||||
|             return new ArrayList<>(); | ||||
|             return new HashMap<>(); | ||||
|         } | ||||
|  | ||||
|         List<PaidSign> paidSigns = new ArrayList<>(); | ||||
|         Map<String, PaidSign> paidSigns = new HashMap<>(); | ||||
|         for (String name : signSection.getKeys(false)) { | ||||
|             double cost = signSection.getDouble(name + ".cost"); | ||||
|             String permission = signSection.getString(name + ".permission"); | ||||
| @@ -89,7 +98,7 @@ public final class PaidSignManager { | ||||
|             OptionState ignoreColor = OptionState.getFromBoolean(signSection.getBoolean(name + ".ignoreColor")); | ||||
|             PaidSign sign = new PaidSign(name, cost, permission, ignoreCase, ignoreColor); | ||||
|             loadConditions(signSection, sign); | ||||
|             paidSigns.add(sign); | ||||
|             paidSigns.put(name, sign); | ||||
|         } | ||||
|         return paidSigns; | ||||
|     } | ||||
| @@ -122,12 +131,12 @@ public final class PaidSignManager { | ||||
|      * @param signs <p>The signs to save</p> | ||||
|      * @throws IOException <p>If unable to write to the signs file</p> | ||||
|      */ | ||||
|     public static void saveSigns(List<PaidSign> signs) throws IOException { | ||||
|     public static void saveSigns(Map<String, PaidSign> signs) throws IOException { | ||||
|         YamlConfiguration configuration = YamlConfiguration.loadConfiguration(signsFile); | ||||
|         ConfigurationSection signSection = configuration.createSection("paidSigns"); | ||||
|  | ||||
|         for (PaidSign sign : signs) { | ||||
|             String name = sign.getName(); | ||||
|         for (PaidSign sign : signs.values()) { | ||||
|             String name = signSection.getName(); | ||||
|             signSection.set(name + ".cost", sign.getCost()); | ||||
|             signSection.set(name + ".permission", sign.getPermission()); | ||||
|             signSection.set(name + ".ignoreCase", sign.getIgnoreCase()); | ||||
|   | ||||
| @@ -0,0 +1,98 @@ | ||||
| package net.knarcraft.paidsigns.utility; | ||||
|  | ||||
| import net.knarcraft.paidsigns.PaidSigns; | ||||
|  | ||||
| import java.util.ArrayList; | ||||
| import java.util.List; | ||||
|  | ||||
| /** | ||||
|  * A helper class for providing common tab complete options | ||||
|  */ | ||||
| public class TabCompleteHelper { | ||||
|  | ||||
|     /** | ||||
|      * Gets the available boolean values for tab completion | ||||
|      * | ||||
|      * @return <p>The available boolean values</p> | ||||
|      */ | ||||
|     public static List<String> getBooleans() { | ||||
|         List<String> booleans = new ArrayList<>(); | ||||
|         booleans.add("true"); | ||||
|         booleans.add("false"); | ||||
|         return booleans; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Gets the available option states for tab completion | ||||
|      * | ||||
|      * @return <p>The available option states</p> | ||||
|      */ | ||||
|     public static List<String> getOptionStates() { | ||||
|         List<String> optionStates = getBooleans(); | ||||
|         optionStates.add("default"); | ||||
|         return optionStates; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Gets the names of all registered paid signs | ||||
|      * | ||||
|      * @return <p>The names of all registered paid signs</p> | ||||
|      */ | ||||
|     public static List<String> getPaidSignNames() { | ||||
|         return new ArrayList<>(PaidSigns.getInstance().getSignManager().getAllPaidSigns().keySet()); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Gets all the choices for lines on a sign | ||||
|      * | ||||
|      * @return <p>All the line indices of a sign</p> | ||||
|      */ | ||||
|     public static List<String> getSignLines() { | ||||
|         List<String> lines = new ArrayList<>(); | ||||
|         for (int i = 1; i < 5; i++) { | ||||
|             lines.add(String.valueOf(i)); | ||||
|         } | ||||
|         return lines; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Gets some valid costs as example values | ||||
|      * | ||||
|      * @return <p>Some valid costs</p> | ||||
|      */ | ||||
|     public static List<String> getCosts() { | ||||
|         List<String> costs = new ArrayList<>(); | ||||
|         costs.add("1"); | ||||
|         costs.add("5"); | ||||
|         costs.add("10"); | ||||
|         costs.add("15"); | ||||
|         return costs; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Gets some example paid sign names for auto-completion | ||||
|      * | ||||
|      * @return <p>Some example paid sign names</p> | ||||
|      */ | ||||
|     public static List<String> getExamplePaidSignNames() { | ||||
|         List<String> names = new ArrayList<>(); | ||||
|         names.add("sign1"); | ||||
|         names.add("\"lift up sign\""); | ||||
|         names.add("\"lift down sign\""); | ||||
|         return names; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Gets some example condition matching strings for tab completion | ||||
|      * | ||||
|      * @return <p>Some example "stringToMatch" values</p> | ||||
|      */ | ||||
|     public static List<String> getExampleConditionStrings() { | ||||
|         List<String> conditionStrings = new ArrayList<>(); | ||||
|         conditionStrings.add("[Gate]"); | ||||
|         conditionStrings.add("\"[Lift Up]\""); | ||||
|         conditionStrings.add("\"[Lift Down]\""); | ||||
|         return conditionStrings; | ||||
|     } | ||||
|  | ||||
| } | ||||
		Reference in New Issue
	
	Block a user