Implements the add condition command and its tab completion

This commit is contained in:
Kristian Knarvik 2022-02-19 17:51:27 +01:00
parent d76d5cdf93
commit 90b5ff7304
9 changed files with 247 additions and 46 deletions

View File

@ -1,6 +1,8 @@
package net.knarcraft.paidsigns; package net.knarcraft.paidsigns;
import net.knarcraft.paidsigns.command.AddCommand; 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.AddTabCompleter;
import net.knarcraft.paidsigns.command.ReloadTabCommand; import net.knarcraft.paidsigns.command.ReloadTabCommand;
import net.knarcraft.paidsigns.command.RemoveTabCommand; import net.knarcraft.paidsigns.command.RemoveTabCommand;
@ -105,6 +107,12 @@ public final class PaidSigns extends JavaPlugin {
addCommand.setTabCompleter(new AddTabCompleter()); 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"); PluginCommand removeCommand = this.getCommand("removePaidSign");
if (removeCommand != null) { if (removeCommand != null) {
TabExecutor removeTabExecutor = new RemoveTabCommand(); TabExecutor removeTabExecutor = new RemoveTabCommand();

View File

@ -29,6 +29,7 @@ public class AddCommand implements CommandExecutor {
} }
PaidSignManager manager = PaidSigns.getInstance().getSignManager(); PaidSignManager manager = PaidSigns.getInstance().getSignManager();
List<String> arguments = Tokenizer.tokenize(String.join(" ", args)); 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); String signName = arguments.get(0);
double cost; double cost;
@ -41,21 +42,19 @@ public class AddCommand implements CommandExecutor {
String permission = arguments.get(2); String permission = arguments.get(2);
OptionState ignoreCase = OptionState.DEFAULT; OptionState ignoreCase = OptionState.DEFAULT;
OptionState ignoreColor = OptionState.DEFAULT; OptionState ignoreColor = OptionState.DEFAULT;
if (arguments.size() > 3) { if (argumentSize > 3) {
ignoreCase = OptionState.fromString(arguments.get(3)); ignoreCase = OptionState.fromString(arguments.get(3));
} }
if (arguments.size() > 4) { if (argumentSize > 4) {
ignoreColor = OptionState.fromString(arguments.get(4)); ignoreColor = OptionState.fromString(arguments.get(4));
} }
try { try {
PaidSign sign = new PaidSign(signName, cost, permission, ignoreCase, ignoreColor); PaidSign sign = new PaidSign(signName, cost, permission, ignoreCase, ignoreColor);
for (PaidSign otherSign : manager.getAllPaidSigns()) { if (manager.getPaidSign(signName) != null) {
if (sign.getName().equals(otherSign.getName())) {
sender.sendMessage("A paid sign with the same name already exists"); sender.sendMessage("A paid sign with the same name already exists");
return false; return false;
} }
}
manager.addPaidSign(sign); manager.addPaidSign(sign);
sender.sendMessage("Successfully added new paid sign"); sender.sendMessage("Successfully added new paid sign");
return true; return true;

View File

@ -1,16 +1,56 @@
package net.knarcraft.paidsigns.command; 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.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull; 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 { public class AddConditionCommand implements CommandExecutor {
@Override @Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { 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; return false;
} }

View File

@ -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();
}
}

View File

@ -1,5 +1,6 @@
package net.knarcraft.paidsigns.command; package net.knarcraft.paidsigns.command;
import net.knarcraft.paidsigns.utility.TabCompleteHelper;
import net.knarcraft.paidsigns.utility.Tokenizer; import net.knarcraft.paidsigns.utility.Tokenizer;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.Command; import org.bukkit.command.Command;
@ -128,21 +129,9 @@ public class AddTabCompleter implements TabCompleter {
* Initializes the values available for tab completion * Initializes the values available for tab completion
*/ */
private static void initializeValues() { private static void initializeValues() {
names = new ArrayList<>(); names = TabCompleteHelper.getExamplePaidSignNames();
names.add("sign1"); costs = TabCompleteHelper.getCosts();
names.add("\"lift up sign\""); options = TabCompleteHelper.getOptionStates();
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");
} }
} }

View File

@ -13,6 +13,7 @@ import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; 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; int argumentSize = args[args.length - 1].isEmpty() ? arguments.size() : arguments.size() - 1;
if (argumentSize < 1) { if (argumentSize < 1) {
List<PaidSign> allPaidSigns = PaidSigns.getInstance().getSignManager().getAllPaidSigns(); Map<String, PaidSign> allPaidSigns = PaidSigns.getInstance().getSignManager().getAllPaidSigns();
List<String> signIds = new ArrayList<>(); List<String> signNames = new ArrayList<>();
for (PaidSign sign : allPaidSigns) { for (String name : allPaidSigns.keySet()) {
signIds.add("\"" + sign.getName() + "\""); signNames.add("\"" + name + "\"");
} }
return signIds; return signNames;
} else { } else {
return new ArrayList<>(); return new ArrayList<>();
} }

View File

@ -9,7 +9,7 @@ import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.block.SignChangeEvent; import org.bukkit.event.block.SignChangeEvent;
import java.util.List; import java.util.Map;
/** /**
* A listener for listening to registered paid signs * A listener for listening to registered paid signs
@ -24,8 +24,8 @@ public class SignListener implements Listener {
} }
String[] lines = event.getLines(); String[] lines = event.getLines();
List<PaidSign> matchingSigns = PaidSigns.getInstance().getSignManager().getAllPaidSigns(); Map<String, PaidSign> matchingSigns = PaidSigns.getInstance().getSignManager().getAllPaidSigns();
for (PaidSign paidSign : matchingSigns) { for (PaidSign paidSign : matchingSigns.values()) {
//If a match is found, just return //If a match is found, just return
if (matchSign(paidSign, lines, event)) { if (matchSign(paidSign, lines, event)) {
return; return;

View File

@ -9,8 +9,7 @@ import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;
@ -19,7 +18,7 @@ import java.util.logging.Level;
*/ */
public final class PaidSignManager { 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"); 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> * @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; this.paidSigns = paidSigns;
} }
@ -38,7 +37,7 @@ public final class PaidSignManager {
* @throws IOException <p>If unable to write to the signs file</p> * @throws IOException <p>If unable to write to the signs file</p>
*/ */
public void addPaidSign(PaidSign paidSign) throws IOException { public void addPaidSign(PaidSign paidSign) throws IOException {
this.paidSigns.add(paidSign); this.paidSigns.put(paidSign.getName(), paidSign);
saveSigns(this.paidSigns); saveSigns(this.paidSigns);
} }
@ -50,7 +49,7 @@ public final class PaidSignManager {
* @throws IOException <p>If unable to write to the signs file</p> * @throws IOException <p>If unable to write to the signs file</p>
*/ */
public boolean removePaidSign(String name) throws IOException { 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) { if (!removed) {
return false; return false;
} else { } 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 * Gets a copy of all registered paid signs
* *
* @return <p>All registered paid signs</p> * @return <p>All registered paid signs</p>
*/ */
public List<PaidSign> getAllPaidSigns() { public Map<String, PaidSign> getAllPaidSigns() {
return new ArrayList<>(paidSigns); return new HashMap<>(paidSigns);
} }
/** /**
@ -73,15 +82,15 @@ public final class PaidSignManager {
* *
* @return <p>The loaded paid signs</p> * @return <p>The loaded paid signs</p>
*/ */
public static List<PaidSign> loadSigns() { public static Map<String, PaidSign> loadSigns() {
YamlConfiguration configuration = YamlConfiguration.loadConfiguration(signsFile); YamlConfiguration configuration = YamlConfiguration.loadConfiguration(signsFile);
ConfigurationSection signSection = configuration.getConfigurationSection("paidSigns"); ConfigurationSection signSection = configuration.getConfigurationSection("paidSigns");
if (signSection == null) { if (signSection == null) {
PaidSigns.getInstance().getLogger().log(Level.WARNING, "Signs section not found in data.yml"); 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)) { for (String name : signSection.getKeys(false)) {
double cost = signSection.getDouble(name + ".cost"); double cost = signSection.getDouble(name + ".cost");
String permission = signSection.getString(name + ".permission"); String permission = signSection.getString(name + ".permission");
@ -89,7 +98,7 @@ public final class PaidSignManager {
OptionState ignoreColor = OptionState.getFromBoolean(signSection.getBoolean(name + ".ignoreColor")); OptionState ignoreColor = OptionState.getFromBoolean(signSection.getBoolean(name + ".ignoreColor"));
PaidSign sign = new PaidSign(name, cost, permission, ignoreCase, ignoreColor); PaidSign sign = new PaidSign(name, cost, permission, ignoreCase, ignoreColor);
loadConditions(signSection, sign); loadConditions(signSection, sign);
paidSigns.add(sign); paidSigns.put(name, sign);
} }
return paidSigns; return paidSigns;
} }
@ -122,12 +131,12 @@ public final class PaidSignManager {
* @param signs <p>The signs to save</p> * @param signs <p>The signs to save</p>
* @throws IOException <p>If unable to write to the signs file</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); YamlConfiguration configuration = YamlConfiguration.loadConfiguration(signsFile);
ConfigurationSection signSection = configuration.createSection("paidSigns"); ConfigurationSection signSection = configuration.createSection("paidSigns");
for (PaidSign sign : signs) { for (PaidSign sign : signs.values()) {
String name = sign.getName(); String name = signSection.getName();
signSection.set(name + ".cost", sign.getCost()); signSection.set(name + ".cost", sign.getCost());
signSection.set(name + ".permission", sign.getPermission()); signSection.set(name + ".permission", sign.getPermission());
signSection.set(name + ".ignoreCase", sign.getIgnoreCase()); signSection.set(name + ".ignoreCase", sign.getIgnoreCase());

View File

@ -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;
}
}