115 lines
4.8 KiB
Java
115 lines
4.8 KiB
Java
package net.knarcraft.permissionsigns.command;
|
|
|
|
import net.knarcraft.permissionsigns.PermissionSigns;
|
|
import net.knarcraft.permissionsigns.container.PermissionSign;
|
|
import net.knarcraft.permissionsigns.formatting.StringFormatter;
|
|
import net.knarcraft.permissionsigns.formatting.TranslatableMessage;
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.command.Command;
|
|
import org.bukkit.command.CommandExecutor;
|
|
import org.bukkit.command.CommandSender;
|
|
import org.bukkit.entity.Player;
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
import java.util.List;
|
|
|
|
/**
|
|
* The command used to create a new permission sign
|
|
*/
|
|
public class CreateCommand implements CommandExecutor {
|
|
|
|
@Override
|
|
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
|
|
@NotNull String[] args) {
|
|
// /ps create <name> <permission,permission> <cost> <duration> to create a new permission-sign
|
|
//Name and permission(s) required, but duration and cost optional
|
|
String usage = "/ps create <name> <description> <permission,permission> [cost] [duration] - Used for creating" +
|
|
" a new permission sign";
|
|
if (!(sender instanceof Player)) {
|
|
sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.COMMAND_PLAYER_ONLY));
|
|
return false;
|
|
}
|
|
if (!sender.hasPermission("permissionsigns.admin.create")) {
|
|
sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.COMMAND_PERMISSION_DENIED));
|
|
return false;
|
|
}
|
|
if (args.length < 3) {
|
|
sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.MISSING_CREATION_INFO));
|
|
sender.sendMessage(usage);
|
|
return true;
|
|
}
|
|
|
|
PermissionSign newSign = parseSign(sender, args);
|
|
if (newSign == null) {
|
|
sender.sendMessage(usage);
|
|
return true;
|
|
}
|
|
PermissionSigns.addSignCreationRequest((Player) sender, newSign);
|
|
|
|
sender.sendMessage(StringFormatter.getTranslatedInfoMessage(TranslatableMessage.CREATION_REQUEST_CREATED));
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Parses the permission sign given in user input
|
|
*
|
|
* @param sender <p>The sender that initiated the command</p>
|
|
* @param args <p>The given command arguments</p>
|
|
* @return <p>The parsed sign, or null if it could not be parsed</p>
|
|
*/
|
|
private PermissionSign parseSign(@NotNull CommandSender sender, @NotNull String[] args) {
|
|
String name = args[0];
|
|
String description = replaceUnderscoresWithSpaces(args[1]);
|
|
String[] permissions = args[2].replaceAll("\\?", " ").split(",");
|
|
for (String permission : permissions) {
|
|
if (permission.contains(":")) {
|
|
String world = permission.split(":")[0];
|
|
if (!world.equalsIgnoreCase("all") && !world.equalsIgnoreCase("any") &&
|
|
Bukkit.getWorld(world) == null) {
|
|
sender.sendMessage(StringFormatter.replacePlaceholder(StringFormatter.getTranslatedErrorMessage(
|
|
TranslatableMessage.PERMISSION_WORLD_INVALID), "{world}", world));
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
|
|
double cost = 0;
|
|
int duration = 0;
|
|
if (args.length > 3) {
|
|
try {
|
|
cost = Double.parseDouble(args[3]);
|
|
} catch (NumberFormatException exception) {
|
|
sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.COST_INVALID_NUMBER));
|
|
return null;
|
|
}
|
|
}
|
|
if (args.length > 4) {
|
|
try {
|
|
duration = Integer.parseInt(args[4]);
|
|
} catch (NumberFormatException exception) {
|
|
sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.DURATION_INVALID_NUMBER));
|
|
return null;
|
|
}
|
|
}
|
|
|
|
return new PermissionSign(name, description, List.of(permissions), duration, cost);
|
|
}
|
|
|
|
/**
|
|
* Replaces underscores with spaces in the given description
|
|
*
|
|
* <p>This basically replaces all underscores with spaces, but keeps escaped underscores (\_) as underscores.</p>
|
|
*
|
|
* @param description <p>The description to replace underscores in</p>
|
|
* @return <p>The string with underscores replaced</p>
|
|
*/
|
|
private @NotNull String replaceUnderscoresWithSpaces(@NotNull String description) {
|
|
String temporaryString = "\\|ESCAPED-UNDERSCORE\\|";
|
|
description = description.replaceAll("\\\\_", temporaryString);
|
|
description = description.replaceAll("_", " ");
|
|
description = description.replaceAll(temporaryString, "_");
|
|
return description;
|
|
}
|
|
|
|
}
|