Fixes various bugs in commands

This commit is contained in:
Kristian Knarvik 2022-02-27 12:47:05 +01:00
parent 0f958f0908
commit 91d477b45a
9 changed files with 153 additions and 46 deletions

View File

@ -22,7 +22,7 @@ public class AddCommand extends TokenizedCommand {
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
@NotNull String[] args) {
super.onCommand(sender, command, label, args);
if (args.length < 3) {
if (argumentSize < 2) {
return false;
}
@ -34,7 +34,10 @@ public class AddCommand extends TokenizedCommand {
sender.sendMessage("You provided an invalid number");
return false;
}
String permission = arguments.get(2);
String permission = "";
if (argumentSize > 2) {
permission = arguments.get(2);
}
OptionState ignoreCase = OptionState.DEFAULT;
OptionState ignoreColor = OptionState.DEFAULT;
if (argumentSize > 3) {

View File

@ -2,11 +2,17 @@ package net.knarcraft.paidsigns.command;
import net.knarcraft.paidsigns.PaidSigns;
import net.knarcraft.paidsigns.container.PaidSign;
import net.knarcraft.paidsigns.manager.PaidSignManager;
import net.knarcraft.paidsigns.property.OptionState;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* A representation of the command for adding a new match condition for a sign
*/
@ -40,12 +46,22 @@ public class AddConditionCommand extends TokenizedCommand {
if (argumentSize > 5) {
ignoreColor = OptionState.getFromBoolean(Boolean.parseBoolean(arguments.get(5)));
}
PaidSign sign = PaidSigns.getInstance().getSignManager().getPaidSign(name);
PaidSignManager signManager = PaidSigns.getInstance().getSignManager();
PaidSign sign = signManager.getPaidSign(name);
if (sign == null) {
sender.sendMessage("No such paid sign exists");
return false;
}
sign.addCondition(lineNumber, stringToMatch, executeRegEx, ignoreCase, ignoreColor);
try {
signManager.saveSigns();
} catch (IOException e) {
Logger logger = PaidSigns.getInstance().getLogger();
logger.log(Level.SEVERE, "Exception encountered while trying to write to the data file");
logger.log(Level.SEVERE, Arrays.toString(e.getStackTrace()));
sender.sendMessage("An exception occurred. Please notify the server administrator or check the server log.");
return false;
}
sender.sendMessage("Condition added");
return true;
}

View File

@ -94,7 +94,7 @@ public class ListCommand extends TokenizedCommand {
sender.sendMessage("Sign conditions: ");
Map<Short, PaidSignCondition> conditions = paidSign.getConditions();
for (short lineIndex : conditions.keySet()) {
sender.sendMessage(" | " + lineIndex + ". " + conditions.get(lineIndex));
sender.sendMessage(" | " + lineIndex + ". " + conditions.get(lineIndex).getStringToMatch());
}
}

View File

@ -1,5 +1,7 @@
package net.knarcraft.paidsigns.command;
import net.knarcraft.paidsigns.PaidSigns;
import net.knarcraft.paidsigns.container.PaidSign;
import net.knarcraft.paidsigns.utility.TabCompleteHelper;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
@ -21,9 +23,19 @@ public class ListTabCompleter extends TokenizedTabCompleter {
if (argumentSize < 1) {
return TabCompleteHelper.getPaidSignNames();
} else if (argumentSize < 2) {
return TabCompleteHelper.getSignLines();
PaidSign sign = PaidSigns.getInstance().getSignManager().getPaidSign(arguments.get(0));
if (sign != null) {
List<String> availableConditions = new ArrayList<>();
for (Short signLine : sign.getConditions().keySet()) {
availableConditions.add(String.valueOf(signLine + 1));
}
return availableConditions;
} else {
return TabCompleteHelper.getSignLines();
}
} else {
return new ArrayList<>();
}
return new ArrayList<>();
}
}

View File

@ -2,10 +2,16 @@ package net.knarcraft.paidsigns.command;
import net.knarcraft.paidsigns.PaidSigns;
import net.knarcraft.paidsigns.container.PaidSign;
import net.knarcraft.paidsigns.manager.PaidSignManager;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* A representation of the command for removing a condition from a sign
*/
@ -21,13 +27,33 @@ public class RemoveConditionCommand extends TokenizedCommand {
String name = arguments.get(0);
short line;
try {
line = Short.parseShort(arguments.get(1));
line = (short) (Short.parseShort(arguments.get(1)) - 1);
} catch (NumberFormatException exception) {
sender.sendMessage("Invalid line number given");
return false;
}
PaidSign sign = PaidSigns.getInstance().getSignManager().getPaidSign(name);
PaidSignManager signManager = PaidSigns.getInstance().getSignManager();
PaidSign sign = signManager.getPaidSign(name);
if (sign == null) {
sender.sendMessage("Invalid sign name given");
return false;
}
if (!sign.getConditions().containsKey(line)) {
sender.sendMessage("No condition registered for line " + (line + 1));
return false;
}
sign.removeCondition(line);
try {
signManager.saveSigns();
} catch (IOException e) {
Logger logger = PaidSigns.getInstance().getLogger();
logger.log(Level.SEVERE, "Exception encountered while trying to write to the data file");
logger.log(Level.SEVERE, Arrays.toString(e.getStackTrace()));
sender.sendMessage("An exception occurred. Please notify the server administrator or check the server log.");
return false;
}
sender.sendMessage("Condition removed");
return true;
}

View File

@ -1,5 +1,7 @@
package net.knarcraft.paidsigns.command;
import net.knarcraft.paidsigns.PaidSigns;
import net.knarcraft.paidsigns.container.PaidSign;
import net.knarcraft.paidsigns.utility.TabCompleteHelper;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
@ -28,7 +30,16 @@ public class RemoveConditionTabCompleter extends TokenizedTabCompleter {
if (argumentSize < 1) {
return TabCompleteHelper.getPaidSignNames();
} else if (argumentSize < 2) {
return TabCompleteHelper.getSignLines();
PaidSign sign = PaidSigns.getInstance().getSignManager().getPaidSign(arguments.get(0));
if (sign != null) {
List<String> availableConditions = new ArrayList<>();
for (Short signLine : sign.getConditions().keySet()) {
availableConditions.add(String.valueOf(signLine + 1));
}
return availableConditions;
} else {
return TabCompleteHelper.getSignLines();
}
}
return new ArrayList<>();
}

View File

@ -19,11 +19,7 @@ public class TokenizedCommand implements CommandExecutor {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
arguments = Tokenizer.tokenize(String.join(" ", args));
if (args.length == 0) {
argumentSize = 0;
} else {
argumentSize = args[args.length - 1].isEmpty() ? arguments.size() : arguments.size() - 1;
}
argumentSize = arguments.size();
return true;
}

View File

@ -38,7 +38,7 @@ public final class PaidSignManager {
*/
public void addPaidSign(PaidSign paidSign) throws IOException {
this.paidSigns.put(paidSign.getName(), paidSign);
saveSigns(this.paidSigns);
saveSigns();
}
/**
@ -53,7 +53,7 @@ public final class PaidSignManager {
if (!removed) {
return false;
} else {
saveSigns(this.paidSigns);
saveSigns();
return true;
}
}
@ -103,6 +103,44 @@ public final class PaidSignManager {
return paidSigns;
}
/**
* Saves all signs registered to this paid sign manager
*
* @throws IOException <p>If unable to write to the signs file</p>
*/
public void saveSigns() throws IOException {
saveSigns(this.paidSigns);
}
/**
* Saves the given paid signs to the signs file
*
* @param signs <p>The signs to save</p>
* @throws IOException <p>If unable to write to the signs file</p>
*/
public static void saveSigns(Map<String, PaidSign> signs) throws IOException {
YamlConfiguration configuration = YamlConfiguration.loadConfiguration(signsFile);
ConfigurationSection signSection = configuration.createSection("paidSigns");
for (PaidSign sign : signs.values()) {
String name = sign.getName();
signSection.set(name + ".cost", sign.getCost());
signSection.set(name + ".permission", sign.getPermission());
signSection.set(name + ".ignoreCase", sign.getIgnoreCase());
signSection.set(name + ".ignoreColor", sign.getIgnoreColor());
ConfigurationSection conditionsSection = signSection.createSection(name + ".conditions");
Map<Short, PaidSignCondition> signConditions = sign.getConditions();
for (short lineIndex : signConditions.keySet()) {
PaidSignCondition condition = signConditions.get(lineIndex);
conditionsSection.set(lineIndex + ".stringToMatch", condition.getStringToMatch());
conditionsSection.set(lineIndex + ".executeRegEx", condition.executeRegex());
conditionsSection.set(lineIndex + ".ignoreCase", condition.ignoreCase());
conditionsSection.set(lineIndex + ".ignoreColor", condition.ignoreColor());
}
}
configuration.save(signsFile);
}
/**
* Loads any saved paid sign conditions and applies them to the given sign
*
@ -125,33 +163,4 @@ public final class PaidSignManager {
}
}
/**
* Saves the given paid signs to the signs file
*
* @param signs <p>The signs to save</p>
* @throws IOException <p>If unable to write to the signs file</p>
*/
public static void saveSigns(Map<String, PaidSign> signs) throws IOException {
YamlConfiguration configuration = YamlConfiguration.loadConfiguration(signsFile);
ConfigurationSection signSection = configuration.createSection("paidSigns");
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());
signSection.set(name + ".ignoreColor", sign.getIgnoreColor());
ConfigurationSection conditionsSection = signSection.createSection(name + ".conditions");
Map<Short, PaidSignCondition> signConditions = sign.getConditions();
for (short lineIndex : signConditions.keySet()) {
PaidSignCondition condition = signConditions.get(lineIndex);
conditionsSection.set(lineIndex + ".stringToMatch", condition.getStringToMatch());
conditionsSection.set(lineIndex + ".executeRegEx", condition.executeRegex());
conditionsSection.set(lineIndex + ".ignoreCase", condition.ignoreCase());
conditionsSection.set(lineIndex + ".ignoreColor", condition.ignoreColor());
}
}
configuration.save(signsFile);
}
}

View File

@ -15,6 +15,40 @@ public final class TabCompleteHelper {
}
/**
* Finds tab complete values that contain the typed text
*
* @param values <p>The values to filter</p>
* @param typedText <p>The text the player has started typing</p>
* @return <p>The given string values that contain the player's typed text</p>
*/
public static List<String> filterMatchingContains(List<String> values, String typedText) {
List<String> configValues = new ArrayList<>();
for (String value : values) {
if (value.toLowerCase().contains(typedText.toLowerCase())) {
configValues.add(value);
}
}
return configValues;
}
/**
* Finds tab complete values that match the start of the typed text
*
* @param values <p>The values to filter</p>
* @param typedText <p>The text the player has started typing</p>
* @return <p>The given string values that start with the player's typed text</p>
*/
public static List<String> filterMatchingStartsWith(List<String> values, String typedText) {
List<String> configValues = new ArrayList<>();
for (String value : values) {
if (value.toLowerCase().startsWith(typedText.toLowerCase())) {
configValues.add(value);
}
}
return configValues;
}
/**
* Gets the available boolean values for tab completion
*
@ -49,7 +83,7 @@ public final class TabCompleteHelper {
for (String paidSignName : paidSignNames) {
quotedNames.add("\"" + paidSignName + "\"");
}
return new ArrayList<>(quotedNames);
return quotedNames;
}
/**