95 lines
4.2 KiB
Java
95 lines
4.2 KiB
Java
package net.knarcraft.paidsigns.command;
|
|
|
|
import net.knarcraft.knarlib.formatting.StringFormatter;
|
|
import net.knarcraft.paidsigns.PaidSigns;
|
|
import net.knarcraft.paidsigns.container.PaidSign;
|
|
import net.knarcraft.paidsigns.formatting.PaidSignsTranslatableMessage;
|
|
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;
|
|
|
|
/**
|
|
* A representation of the command for adding a new paid sign
|
|
*/
|
|
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 (argumentSize < 2) {
|
|
return false;
|
|
}
|
|
|
|
String signName = arguments.get(0).trim();
|
|
double cost;
|
|
try {
|
|
cost = Double.parseDouble(arguments.get(1));
|
|
} catch (NumberFormatException exception) {
|
|
StringFormatter.displayErrorMessage(sender, PaidSigns.getTranslator(),
|
|
PaidSignsTranslatableMessage.ERROR_INVALID_NUMBER);
|
|
return false;
|
|
}
|
|
String permission = "";
|
|
if (argumentSize > 2) {
|
|
permission = arguments.get(2);
|
|
}
|
|
OptionState ignoreCase = OptionState.DEFAULT;
|
|
OptionState ignoreColor = OptionState.DEFAULT;
|
|
if (argumentSize > 3) {
|
|
ignoreCase = OptionState.fromString(arguments.get(3));
|
|
}
|
|
if (argumentSize > 4) {
|
|
ignoreColor = OptionState.fromString(arguments.get(4));
|
|
}
|
|
boolean matchAnyCondition = false;
|
|
if (argumentSize > 5) {
|
|
matchAnyCondition = Boolean.parseBoolean(arguments.get(5));
|
|
}
|
|
|
|
return createPaidSign(sender, signName, cost, permission, ignoreCase, ignoreColor, matchAnyCondition);
|
|
}
|
|
|
|
/**
|
|
* Creates a new paid sign with the given user input
|
|
*
|
|
* @param sender <p>The command sender that called the add command</p>
|
|
* @param signName <p>The name of the new paid sign</p>
|
|
* @param cost <p>The cost of the new paid sign</p>
|
|
* @param permission <p>The permission required for creating the sign represented by the paid sign</p>
|
|
* @param ignoreCase <p>Whether to ignore case for the paid sign's conditions</p>
|
|
* @param ignoreColor <p>Whether to ignore color for the paid sign's conditions</p>
|
|
* @param matchAnyCondition <p>Whether to treat any matching condition as a sign match</p>
|
|
* @return <p>True if the paid sign was successfully created and registered</p>
|
|
*/
|
|
private boolean createPaidSign(CommandSender sender, String signName, double cost, String permission,
|
|
OptionState ignoreCase, OptionState ignoreColor, boolean matchAnyCondition) {
|
|
PaidSignManager manager = PaidSigns.getInstance().getSignManager();
|
|
try {
|
|
PaidSign sign = new PaidSign(signName, cost, permission, ignoreCase, ignoreColor, matchAnyCondition);
|
|
if (manager.getPaidSign(signName) != null) {
|
|
StringFormatter.displayErrorMessage(sender, PaidSigns.getTranslator(),
|
|
PaidSignsTranslatableMessage.ERROR_NAME_DUPLICATE);
|
|
return false;
|
|
}
|
|
manager.addPaidSign(sign);
|
|
StringFormatter.displaySuccessMessage(sender, PaidSigns.getTranslator(),
|
|
PaidSignsTranslatableMessage.SUCCESS_ADDED_PAID_SIGN);
|
|
return true;
|
|
} catch (IOException e) {
|
|
StringFormatter.displayErrorMessage(sender, PaidSigns.getTranslator(),
|
|
PaidSignsTranslatableMessage.ERROR_EXCEPTION_OCCURRED);
|
|
return false;
|
|
} catch (IllegalArgumentException e) {
|
|
StringFormatter.displayErrorMessage(sender, StringFormatter.replacePlaceholder(PaidSigns.getTranslator(),
|
|
PaidSignsTranslatableMessage.ERROR_INVALID_INPUT, "{input}", e.getMessage()));
|
|
}
|
|
return false;
|
|
}
|
|
|
|
}
|