Adds and option to match any paid sign condition instead of all conditions Adds checks for whether line indices are outside the allowed range Disallows any invalid regular expressions in sign conditions
91 lines
4.0 KiB
Java
91 lines
4.0 KiB
Java
package net.knarcraft.paidsigns.command;
|
|
|
|
import net.knarcraft.paidsigns.PaidSigns;
|
|
import net.knarcraft.paidsigns.container.PaidSign;
|
|
import net.knarcraft.paidsigns.formatting.StringFormatter;
|
|
import net.knarcraft.paidsigns.formatting.TranslatableMessage;
|
|
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) {
|
|
sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.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) {
|
|
sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.ERROR_NAME_DUPLICATE));
|
|
return false;
|
|
}
|
|
manager.addPaidSign(sign);
|
|
sender.sendMessage(StringFormatter.getTranslatedInfoMessage(TranslatableMessage.SUCCESS_ADDED_PAID_SIGN));
|
|
return true;
|
|
} catch (IOException e) {
|
|
sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.ERROR_EXCEPTION_OCCURRED));
|
|
return false;
|
|
} catch (IllegalArgumentException e) {
|
|
sender.sendMessage(StringFormatter.replacePlaceholder(StringFormatter.getTranslatedErrorMessage(
|
|
TranslatableMessage.ERROR_INVALID_INPUT), "{input}", e.getMessage()));
|
|
}
|
|
return false;
|
|
}
|
|
|
|
}
|