From 86cb1c0fedfbe71cef10948680a6a7fca23a6712 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Fri, 25 Feb 2022 18:18:52 +0100 Subject: [PATCH] Removes some redundancy between commands --- .../paidsigns/command/AddCommand.java | 8 ++---- .../command/AddConditionCommand.java | 9 ++----- .../paidsigns/command/ListCommand.java | 4 +-- .../command/ListConditionsCommand.java | 4 +-- .../command/RemoveConditionCommand.java | 9 ++----- .../paidsigns/command/TokenizedCommand.java | 26 +++++++++++++++++++ 6 files changed, 36 insertions(+), 24 deletions(-) create mode 100644 src/main/java/net/knarcraft/paidsigns/command/TokenizedCommand.java diff --git a/src/main/java/net/knarcraft/paidsigns/command/AddCommand.java b/src/main/java/net/knarcraft/paidsigns/command/AddCommand.java index 3a6c5ef..91f9ed5 100644 --- a/src/main/java/net/knarcraft/paidsigns/command/AddCommand.java +++ b/src/main/java/net/knarcraft/paidsigns/command/AddCommand.java @@ -4,32 +4,28 @@ import net.knarcraft.paidsigns.PaidSigns; import net.knarcraft.paidsigns.container.PaidSign; import net.knarcraft.paidsigns.manager.PaidSignManager; 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.io.IOException; import java.util.Arrays; -import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; /** * A representation of the command for adding a new paid sign */ -public class AddCommand implements CommandExecutor { +public class AddCommand extends TokenizedCommand { @Override 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) { return false; } PaidSignManager manager = PaidSigns.getInstance().getSignManager(); - List 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; diff --git a/src/main/java/net/knarcraft/paidsigns/command/AddConditionCommand.java b/src/main/java/net/knarcraft/paidsigns/command/AddConditionCommand.java index 6a1677f..36c5804 100644 --- a/src/main/java/net/knarcraft/paidsigns/command/AddConditionCommand.java +++ b/src/main/java/net/knarcraft/paidsigns/command/AddConditionCommand.java @@ -3,23 +3,18 @@ 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 { +public class AddConditionCommand extends TokenizedCommand { @Override public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { - List arguments = Tokenizer.tokenize(String.join(" ", args)); - int argumentSize = args[args.length - 1].isEmpty() ? arguments.size() : arguments.size() - 1; + super.onCommand(sender, command, label, args); if (argumentSize < 3) { return false; } diff --git a/src/main/java/net/knarcraft/paidsigns/command/ListCommand.java b/src/main/java/net/knarcraft/paidsigns/command/ListCommand.java index 6088036..d950d72 100644 --- a/src/main/java/net/knarcraft/paidsigns/command/ListCommand.java +++ b/src/main/java/net/knarcraft/paidsigns/command/ListCommand.java @@ -1,14 +1,14 @@ package net.knarcraft.paidsigns.command; import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; -public class ListCommand implements CommandExecutor { +public class ListCommand extends TokenizedCommand { @Override 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 an argument is given, output extensive information about the sign return false; diff --git a/src/main/java/net/knarcraft/paidsigns/command/ListConditionsCommand.java b/src/main/java/net/knarcraft/paidsigns/command/ListConditionsCommand.java index 8ea5398..e416ed6 100644 --- a/src/main/java/net/knarcraft/paidsigns/command/ListConditionsCommand.java +++ b/src/main/java/net/knarcraft/paidsigns/command/ListConditionsCommand.java @@ -1,14 +1,14 @@ package net.knarcraft.paidsigns.command; import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; -public class ListConditionsCommand implements CommandExecutor { +public class ListConditionsCommand extends TokenizedCommand { @Override 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 and a line is given, display everything about the condition return false; diff --git a/src/main/java/net/knarcraft/paidsigns/command/RemoveConditionCommand.java b/src/main/java/net/knarcraft/paidsigns/command/RemoveConditionCommand.java index 4fd1585..23a7e8b 100644 --- a/src/main/java/net/knarcraft/paidsigns/command/RemoveConditionCommand.java +++ b/src/main/java/net/knarcraft/paidsigns/command/RemoveConditionCommand.java @@ -2,23 +2,18 @@ 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 { +public class RemoveConditionCommand extends TokenizedCommand { @Override public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { - List arguments = Tokenizer.tokenize(String.join(" ", args)); - int argumentSize = args[args.length - 1].isEmpty() ? arguments.size() : arguments.size() - 1; + super.onCommand(sender, command, label, args); if (argumentSize < 2) { return false; } diff --git a/src/main/java/net/knarcraft/paidsigns/command/TokenizedCommand.java b/src/main/java/net/knarcraft/paidsigns/command/TokenizedCommand.java new file mode 100644 index 0000000..bbd6802 --- /dev/null +++ b/src/main/java/net/knarcraft/paidsigns/command/TokenizedCommand.java @@ -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 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; + } + +}