Removes some redundancy between commands

This commit is contained in:
Kristian Knarvik 2022-02-25 18:18:52 +01:00
parent 5bc62d5bc0
commit 86cb1c0fed
6 changed files with 36 additions and 24 deletions

View File

@ -4,32 +4,28 @@ import net.knarcraft.paidsigns.PaidSigns;
import net.knarcraft.paidsigns.container.PaidSign; import net.knarcraft.paidsigns.container.PaidSign;
import net.knarcraft.paidsigns.manager.PaidSignManager; import net.knarcraft.paidsigns.manager.PaidSignManager;
import net.knarcraft.paidsigns.property.OptionState; 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.CommandSender; import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
/** /**
* A representation of the command for adding a new paid sign * A representation of the command for adding a new paid sign
*/ */
public class AddCommand implements CommandExecutor { public class AddCommand extends TokenizedCommand {
@Override @Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
@NotNull String[] args) { @NotNull String[] args) {
super.onCommand(sender, command, label, args);
if (args.length < 3) { if (args.length < 3) {
return false; return false;
} }
PaidSignManager manager = PaidSigns.getInstance().getSignManager(); 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); String signName = arguments.get(0);
double cost; double cost;

View File

@ -3,23 +3,18 @@ package net.knarcraft.paidsigns.command;
import net.knarcraft.paidsigns.PaidSigns; import net.knarcraft.paidsigns.PaidSigns;
import net.knarcraft.paidsigns.container.PaidSign; import net.knarcraft.paidsigns.container.PaidSign;
import net.knarcraft.paidsigns.property.OptionState; 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.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 * A representation of the command for adding a new match condition for a sign
*/ */
public class AddConditionCommand implements CommandExecutor { public class AddConditionCommand extends TokenizedCommand {
@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)); super.onCommand(sender, command, label, args);
int argumentSize = args[args.length - 1].isEmpty() ? arguments.size() : arguments.size() - 1;
if (argumentSize < 3) { if (argumentSize < 3) {
return false; return false;
} }

View File

@ -1,14 +1,14 @@
package net.knarcraft.paidsigns.command; package net.knarcraft.paidsigns.command;
import org.bukkit.command.Command; import org.bukkit.command.Command;
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;
public class ListCommand implements CommandExecutor { public class ListCommand extends TokenizedCommand {
@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) {
super.onCommand(sender, command, label, args);
//TODO: If no argument is given, list all paid sign names //TODO: If no argument is given, list all paid sign names
//TODO: If an argument is given, output extensive information about the sign //TODO: If an argument is given, output extensive information about the sign
return false; return false;

View File

@ -1,14 +1,14 @@
package net.knarcraft.paidsigns.command; package net.knarcraft.paidsigns.command;
import org.bukkit.command.Command; import org.bukkit.command.Command;
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;
public class ListConditionsCommand implements CommandExecutor { public class ListConditionsCommand extends TokenizedCommand {
@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) {
super.onCommand(sender, command, label, args);
//TODO: If a paid sign name is given, print a short overview over the conditions //TODO: If a paid sign name is given, print a short overview over the conditions
//TODO: If a paid sign name and a line is given, display everything about the condition //TODO: If a paid sign name and a line is given, display everything about the condition
return false; return false;

View File

@ -2,23 +2,18 @@ package net.knarcraft.paidsigns.command;
import net.knarcraft.paidsigns.PaidSigns; import net.knarcraft.paidsigns.PaidSigns;
import net.knarcraft.paidsigns.container.PaidSign; import net.knarcraft.paidsigns.container.PaidSign;
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.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 removing a condition from a sign * A representation of the command for removing a condition from a sign
*/ */
public class RemoveConditionCommand implements CommandExecutor { public class RemoveConditionCommand extends TokenizedCommand {
@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)); super.onCommand(sender, command, label, args);
int argumentSize = args[args.length - 1].isEmpty() ? arguments.size() : arguments.size() - 1;
if (argumentSize < 2) { if (argumentSize < 2) {
return false; return false;
} }

View File

@ -0,0 +1,26 @@
package net.knarcraft.paidsigns.command;
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 command executor with tokenized arguments
*/
public class TokenizedCommand implements CommandExecutor {
protected List<String> arguments;
protected int argumentSize;
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
arguments = Tokenizer.tokenize(String.join(" ", args));
argumentSize = args[args.length - 1].isEmpty() ? arguments.size() : arguments.size() - 1;
return true;
}
}