Initial commit
This commit is contained in:
@ -0,0 +1,76 @@
|
||||
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 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 {
|
||||
|
||||
@Override
|
||||
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
|
||||
@NotNull String[] args) {
|
||||
if (args.length < 3) {
|
||||
return false;
|
||||
}
|
||||
PaidSignManager manager = PaidSigns.getInstance().getSignManager();
|
||||
List<String> arguments = Tokenizer.tokenize(String.join(" ", args));
|
||||
|
||||
String id = arguments.get(0);
|
||||
short line;
|
||||
double cost;
|
||||
try {
|
||||
line = (short) (Short.parseShort(arguments.get(1)) - 1);
|
||||
cost = Double.parseDouble(arguments.get(2));
|
||||
} catch (NumberFormatException exception) {
|
||||
sender.sendMessage("You provided an invalid number");
|
||||
return false;
|
||||
}
|
||||
OptionState ignoreCase = OptionState.DEFAULT;
|
||||
OptionState ignoreColor = OptionState.DEFAULT;
|
||||
if (arguments.size() > 3) {
|
||||
ignoreCase = OptionState.fromString(arguments.get(3));
|
||||
}
|
||||
if (arguments.size() > 4) {
|
||||
ignoreColor = OptionState.fromString(arguments.get(4));
|
||||
}
|
||||
|
||||
try {
|
||||
PaidSign sign = new PaidSign(id, line, cost, ignoreCase, ignoreColor);
|
||||
for (PaidSign similarSign : manager.getPaidSigns(sign.getCleanId(), sign.getLineIndex())) {
|
||||
if (sign.matches(similarSign)) {
|
||||
sender.sendMessage("A paid sign with the same id and line already exists");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
manager.addPaidSign(sign);
|
||||
sender.sendMessage("Successfully added new paid sign");
|
||||
return true;
|
||||
} 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;
|
||||
} catch (IllegalArgumentException e) {
|
||||
sender.sendMessage("Invalid input: " + e.getMessage());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user