Improves output formatting and adds some missing messages
This commit is contained in:
parent
52b033c005
commit
5c2831bbb7
@ -3,7 +3,6 @@ package net.knarcraft.permissionsigns.command;
|
||||
import net.knarcraft.permissionsigns.PermissionSigns;
|
||||
import net.knarcraft.permissionsigns.formatting.StringFormatter;
|
||||
import net.knarcraft.permissionsigns.formatting.TranslatableMessage;
|
||||
import net.knarcraft.permissionsigns.formatting.Translator;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
@ -20,11 +19,12 @@ public class CancelCommand implements CommandExecutor {
|
||||
if (sender instanceof Player player) {
|
||||
if (sender.hasPermission("permissionsigns.admin.create")) {
|
||||
PermissionSigns.cancelSignCreationRequest(player.getUniqueId());
|
||||
sender.sendMessage(StringFormatter.getTranslatedInfoMessage(TranslatableMessage.CREATION_REQUEST_CANCELLED));
|
||||
} else {
|
||||
sender.sendMessage(StringFormatter.formatErrorMessage(Translator.getTranslatedMessage(TranslatableMessage.COMMAND_PERMISSION_DENIED)));
|
||||
sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.COMMAND_PERMISSION_DENIED));
|
||||
}
|
||||
} else {
|
||||
sender.sendMessage(StringFormatter.formatErrorMessage(Translator.getTranslatedMessage(TranslatableMessage.COMMAND_PLAYER_ONLY)));
|
||||
sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.COMMAND_PLAYER_ONLY));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
@ -4,7 +4,6 @@ import net.knarcraft.permissionsigns.PermissionSigns;
|
||||
import net.knarcraft.permissionsigns.container.PermissionSign;
|
||||
import net.knarcraft.permissionsigns.formatting.StringFormatter;
|
||||
import net.knarcraft.permissionsigns.formatting.TranslatableMessage;
|
||||
import net.knarcraft.permissionsigns.formatting.Translator;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
@ -23,15 +22,15 @@ public class CreateCommand implements CommandExecutor {
|
||||
// /ps create <name> <permission,permission> <cost> <duration> to create a new permission-sign
|
||||
//Name and permission(s) required, but duration and cost optional
|
||||
if (!(sender instanceof Player)) {
|
||||
sender.sendMessage(Translator.getTranslatedMessage(TranslatableMessage.COMMAND_PLAYER_ONLY));
|
||||
sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.COMMAND_PLAYER_ONLY));
|
||||
return false;
|
||||
}
|
||||
if (!sender.hasPermission("permissionsigns.admin.create")) {
|
||||
sender.sendMessage(StringFormatter.formatErrorMessage(Translator.getTranslatedMessage(TranslatableMessage.COMMAND_PERMISSION_DENIED)));
|
||||
sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.COMMAND_PERMISSION_DENIED));
|
||||
return false;
|
||||
}
|
||||
if (args.length < 2) {
|
||||
sender.sendMessage(Translator.getTranslatedMessage(TranslatableMessage.MISSING_CREATION_INFO));
|
||||
sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.MISSING_CREATION_INFO));
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -40,8 +39,8 @@ public class CreateCommand implements CommandExecutor {
|
||||
return false;
|
||||
}
|
||||
PermissionSigns.addSignCreationRequest((Player) sender, newSign);
|
||||
//TODO: Tell the user that the creation request has been created and that they need to right-click an empty
|
||||
// sign within 60 seconds to register the permissions sign
|
||||
|
||||
sender.sendMessage(StringFormatter.getTranslatedInfoMessage(TranslatableMessage.CREATION_REQUEST_CREATED));
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -60,13 +59,13 @@ public class CreateCommand implements CommandExecutor {
|
||||
try {
|
||||
cost = Double.parseDouble(args[2]);
|
||||
} catch (NumberFormatException exception) {
|
||||
sender.sendMessage(Translator.getTranslatedMessage(TranslatableMessage.COST_INVALID_NUMBER));
|
||||
sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.COST_INVALID_NUMBER));
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
duration = Integer.parseInt(args[3]);
|
||||
} catch (NumberFormatException exception) {
|
||||
sender.sendMessage(Translator.getTranslatedMessage(TranslatableMessage.DURATION_INVALID_NUMBER));
|
||||
sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.DURATION_INVALID_NUMBER));
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -2,7 +2,6 @@ package net.knarcraft.permissionsigns.command;
|
||||
|
||||
import net.knarcraft.permissionsigns.formatting.StringFormatter;
|
||||
import net.knarcraft.permissionsigns.formatting.TranslatableMessage;
|
||||
import net.knarcraft.permissionsigns.formatting.Translator;
|
||||
import net.knarcraft.permissionsigns.manager.SignManager;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
@ -20,7 +19,7 @@ public class ReloadCommand implements CommandExecutor {
|
||||
//TODO: Perform a reload from disk
|
||||
SignManager.loadSigns();
|
||||
} else {
|
||||
sender.sendMessage(StringFormatter.formatErrorMessage(Translator.getTranslatedMessage(TranslatableMessage.COMMAND_PERMISSION_DENIED)));
|
||||
sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.COMMAND_PERMISSION_DENIED));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
@ -7,6 +7,53 @@ import org.bukkit.ChatColor;
|
||||
*/
|
||||
public class StringFormatter {
|
||||
|
||||
/**
|
||||
* Replaces a placeholder in a string
|
||||
*
|
||||
* @param input <p>The input string to replace in</p>
|
||||
* @param placeholder <p>The placeholder to replace</p>
|
||||
* @param replacement <p>The replacement value</p>
|
||||
* @return <p>The input string with all placeholder instances replaced</p>
|
||||
*/
|
||||
public static String replacePlaceholder(String input, String placeholder, String replacement) {
|
||||
return input.replaceAll(placeholder, replacement);
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces placeholders in a string
|
||||
*
|
||||
* @param input <p>The input string to replace in</p>
|
||||
* @param placeholders <p>The placeholders to replace</p>
|
||||
* @param replacements <p>The replacement values</p>
|
||||
* @return <p>The input string with all placeholder instances replaced</p>
|
||||
*/
|
||||
public static String replacePlaceholders(String input, String[] placeholders, String[] replacements) {
|
||||
for (int i = 0; i < Math.min(placeholders.length, replacements.length); i++) {
|
||||
input = replacePlaceholder(input, placeholders[i], replacements[i]);
|
||||
}
|
||||
return input;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a translated and formatted info message
|
||||
*
|
||||
* @param translatableMessage <p>The translatable message to translate and format</p>
|
||||
* @return <p>The translated and formatted message</p>
|
||||
*/
|
||||
public static String getTranslatedInfoMessage(TranslatableMessage translatableMessage) {
|
||||
return formatInfoMessage(Translator.getTranslatedMessage(translatableMessage));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a translated and formatted error message
|
||||
*
|
||||
* @param translatableMessage <p>The translatable message to translate and format</p>
|
||||
* @return <p>The translated and formatted message</p>
|
||||
*/
|
||||
public static String getTranslatedErrorMessage(TranslatableMessage translatableMessage) {
|
||||
return formatErrorMessage(Translator.getTranslatedMessage(translatableMessage));
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats an information message by adding the prefix and text color
|
||||
*
|
||||
|
@ -63,6 +63,36 @@ public enum TranslatableMessage {
|
||||
/**
|
||||
* The message to be displayed when a player successfully destroys a permissions sign
|
||||
*/
|
||||
PERMISSION_SIGN_REMOVED
|
||||
PERMISSION_SIGN_REMOVED,
|
||||
|
||||
/**
|
||||
* The message displayed when a player has successfully used the /ps create command
|
||||
*/
|
||||
CREATION_REQUEST_CREATED,
|
||||
|
||||
/**
|
||||
* The message displayed when a creation request is manually cancelled
|
||||
*/
|
||||
CREATION_REQUEST_CANCELLED,
|
||||
|
||||
/**
|
||||
* The message to display if a player is unable to pay for using a permission sign
|
||||
*/
|
||||
CANNOT_AFFORD,
|
||||
|
||||
/**
|
||||
* The message to display is a player has all permissions given by a permission sign
|
||||
*/
|
||||
ALREADY_HAS_PERMISSIONS,
|
||||
|
||||
/**
|
||||
* The message to display when a permission sign successfully grants permissions to the player
|
||||
*/
|
||||
PERMISSIONS_GRANTED,
|
||||
|
||||
/**
|
||||
* The message to display when a permission sign has been successfully created
|
||||
*/
|
||||
PERMISSION_SIGN_CREATED
|
||||
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package net.knarcraft.permissionsigns.listener;
|
||||
import net.knarcraft.permissionsigns.PermissionSigns;
|
||||
import net.knarcraft.permissionsigns.container.PermissionSign;
|
||||
import net.knarcraft.permissionsigns.container.SignCreationRequest;
|
||||
import net.knarcraft.permissionsigns.formatting.StringFormatter;
|
||||
import net.knarcraft.permissionsigns.formatting.TranslatableMessage;
|
||||
import net.knarcraft.permissionsigns.formatting.Translator;
|
||||
import net.knarcraft.permissionsigns.manager.EconomyManager;
|
||||
@ -66,10 +67,10 @@ public class SignListener implements Listener {
|
||||
}
|
||||
if (!player.hasPermission("permissionsigns.admin")) {
|
||||
event.setCancelled(true);
|
||||
player.sendMessage(Translator.getTranslatedMessage(TranslatableMessage.PERMISSION_SIGN_DESTROY_DENY));
|
||||
player.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.PERMISSION_SIGN_DESTROY_DENY));
|
||||
} else {
|
||||
SignManager.removeSign(sign.getLocation());
|
||||
player.sendMessage(Translator.getTranslatedMessage(TranslatableMessage.PERMISSION_SIGN_REMOVED));
|
||||
player.sendMessage(StringFormatter.getTranslatedInfoMessage(TranslatableMessage.PERMISSION_SIGN_REMOVED));
|
||||
}
|
||||
}
|
||||
|
||||
@ -101,16 +102,49 @@ public class SignListener implements Listener {
|
||||
* @param player <p>The player that interacted with the permission sign</p>
|
||||
*/
|
||||
private void handlePermissionSignInteract(PermissionSign permissionSign, Player player) {
|
||||
//TODO: Check if the player has the permissions for sale. If it has, just explain that it does
|
||||
if (EconomyManager.canAfford(player, permissionSign.getCost())) {
|
||||
EconomyManager.withdraw(player, permissionSign.getCost());
|
||||
for (String permissionString : permissionSign.getPermissionNodes()) {
|
||||
PermissionManager.addPermission(player, permissionString, permissionSign.getDuration() == 0);
|
||||
//Don't allow players to pay for permissions they already own
|
||||
boolean hasAllPermissions = true;
|
||||
for (String permissionNode : permissionSign.getPermissionNodes()) {
|
||||
if (!player.hasPermission(permissionNode)) {
|
||||
hasAllPermissions = false;
|
||||
}
|
||||
//TODO: Tell the player that they've been granted the new permissions, possibly including the full nodes?
|
||||
} else {
|
||||
//TODO: Tell the player that they cannot afford the transaction
|
||||
}
|
||||
if (hasAllPermissions) {
|
||||
player.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.ALREADY_HAS_PERMISSIONS));
|
||||
return;
|
||||
}
|
||||
|
||||
if (EconomyManager.canAfford(player, permissionSign.getCost())) {
|
||||
performPermissionTransaction(player, permissionSign);
|
||||
} else {
|
||||
player.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.CANNOT_AFFORD));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs the permission transaction taking payment and granting permissions
|
||||
*
|
||||
* @param player <p>The player to give permissions to</p>
|
||||
* @param permissionSign <p>The permission sign the player clicked</p>
|
||||
*/
|
||||
private void performPermissionTransaction(Player player, PermissionSign permissionSign) {
|
||||
EconomyManager.withdraw(player, permissionSign.getCost());
|
||||
StringBuilder permissionsBuilder = new StringBuilder();
|
||||
for (String permissionNode : permissionSign.getPermissionNodes()) {
|
||||
//Only grant the permission if not already owned
|
||||
if (!player.hasPermission(permissionNode)) {
|
||||
permissionsBuilder.append(permissionNode);
|
||||
permissionsBuilder.append(", ");
|
||||
PermissionManager.addPermission(player, permissionNode, permissionSign.getDuration() == 0);
|
||||
}
|
||||
}
|
||||
|
||||
String successMessage = StringFormatter.getTranslatedInfoMessage(TranslatableMessage.PERMISSIONS_GRANTED);
|
||||
String grantedPermissions = permissionsBuilder.toString();
|
||||
grantedPermissions = grantedPermissions.substring(0, grantedPermissions.length() - 2);
|
||||
String timeUnit = Translator.getTranslatedMessage(TranslatableMessage.SIGN_TIME_UNIT);
|
||||
player.sendMessage(StringFormatter.replacePlaceholders(successMessage, new String[]{"{permissions}", "{time}"},
|
||||
new String[]{grantedPermissions, permissionSign.getDuration() + timeUnit}));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -139,7 +173,7 @@ public class SignListener implements Listener {
|
||||
sign.setLine(i, signLines[i]);
|
||||
}
|
||||
sign.update();
|
||||
//TODO: Display text in the chat explaining that the permission sign has been created
|
||||
player.sendMessage(StringFormatter.getTranslatedInfoMessage(TranslatableMessage.PERMISSION_SIGN_CREATED));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -10,4 +10,10 @@ en:
|
||||
COMMAND_PLAYER_ONLY: "&7This command is only available to players"
|
||||
PERMISSION_SIGN_DESTROY_DENY: "&7You do not have permissions to delete a permissions sign"
|
||||
PERMISSION_SIGN_REMOVED: "&7Permissions sign removed"
|
||||
COMMAND_PERMISSION_DENIED: "&7You do not have necessary permissions to perform this command"
|
||||
COMMAND_PERMISSION_DENIED: "&7You do not have necessary permissions to perform this command"
|
||||
CREATION_REQUEST_CREATED: "&7Permission Sign request created. Right-click an empty sign within 60 seconds to create the new permission sign"
|
||||
CANNOT_AFFORD: "&7You cannot afford to use this permission sign"
|
||||
ALREADY_HAS_PERMISSIONS: "&7You already have all permissions sold by this permission sign"
|
||||
PERMISSIONS_GRANTED: "&7You have been granted the following permission nodes: {permissions} for {time}"
|
||||
PERMISSION_SIGN_CREATED: "&7Permission sign successfully created!"
|
||||
CREATION_REQUEST_CANCELLED: "&7Your last permission sign creation request has been cancelled"
|
Loading…
Reference in New Issue
Block a user