Implements the remove condition command and its tab completer

This commit is contained in:
Kristian Knarvik 2022-02-19 18:22:39 +01:00
parent 90b5ff7304
commit 2f559ce2af
6 changed files with 93 additions and 3 deletions

View File

@ -5,6 +5,8 @@ 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.RemoveConditionCommand;
import net.knarcraft.paidsigns.command.RemoveConditionTabCompleter;
import net.knarcraft.paidsigns.command.RemoveTabCommand;
import net.knarcraft.paidsigns.listener.SignListener;
import net.knarcraft.paidsigns.manager.EconomyManager;
@ -113,6 +115,12 @@ public final class PaidSigns extends JavaPlugin {
addConditionCommand.setTabCompleter(new AddConditionTabCompleter());
}
PluginCommand removeConditionCommand = this.getCommand("removePaidSignCondition");
if (removeConditionCommand != null) {
removeConditionCommand.setExecutor(new RemoveConditionCommand());
removeConditionCommand.setTabCompleter(new RemoveConditionTabCompleter());
}
PluginCommand removeCommand = this.getCommand("removePaidSign");
if (removeCommand != null) {
TabExecutor removeTabExecutor = new RemoveTabCommand();

View File

@ -51,7 +51,8 @@ public class AddConditionCommand implements CommandExecutor {
return false;
}
sign.addCondition(lineNumber, stringToMatch, executeRegEx, ignoreCase, ignoreColor);
return false;
sender.sendMessage("Condition added");
return true;
}
}

View File

@ -8,6 +8,7 @@ import org.bukkit.command.TabCompleter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
/**
@ -41,7 +42,7 @@ public class AddConditionTabCompleter implements TabCompleter {
} else if (argumentSize < 6) {
return optionStates;
}
return null;
return new ArrayList<>();
}
/**

View File

@ -1,15 +1,40 @@
package net.knarcraft.paidsigns.command;
import net.knarcraft.paidsigns.PaidSigns;
import net.knarcraft.paidsigns.container.PaidSign;
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 removing a condition from a sign
*/
public class RemoveConditionCommand 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 < 2) {
return false;
}
String name = arguments.get(0);
short line;
try {
line = Short.parseShort(arguments.get(1));
} catch (NumberFormatException exception) {
sender.sendMessage("Invalid line number given");
return false;
}
PaidSign sign = PaidSigns.getInstance().getSignManager().getPaidSign(name);
sign.removeCondition(line);
sender.sendMessage("Condition removed");
return true;
}
}

View File

@ -0,0 +1,46 @@
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.ArrayList;
import java.util.List;
/**
* The tab completer for the remove paid sign condition command
*/
public class RemoveConditionTabCompleter implements TabCompleter {
private List<String> lineIndices;
@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 TabCompleteHelper.getSignLines();
}
return new ArrayList<>();
}
/**
* Initializes the values available for tab completion
*/
private void initializeValues() {
lineIndices = TabCompleteHelper.getSignLines();
}
}

View File

@ -137,4 +137,13 @@ public class PaidSign {
this.conditions.put(line, new PaidSignCondition(stringToMatch, executeRegex, ignoreCaseBoolean, ignoreColorBoolean));
}
/**
* Removes a condition from this paid sign
*
* @param line <p>The sign line the condition belongs to</p>
*/
public void removeCondition(short line) {
this.conditions.remove(line);
}
}