Adds hex color code support and makes name, duration and cost formats customizable
This commit is contained in:
parent
14cff0e421
commit
967b533e55
@ -1,9 +1,9 @@
|
||||
package net.knarcraft.permissionsigns.container;
|
||||
|
||||
import net.knarcraft.permissionsigns.formatting.StringFormatter;
|
||||
import net.knarcraft.permissionsigns.formatting.TranslatableMessage;
|
||||
import net.knarcraft.permissionsigns.formatting.Translator;
|
||||
import net.knarcraft.permissionsigns.manager.EconomyManager;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Location;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@ -12,6 +12,9 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static net.knarcraft.permissionsigns.formatting.StringFormatter.replacePlaceholder;
|
||||
import static net.knarcraft.permissionsigns.formatting.StringFormatter.translateAllColorCodes;
|
||||
|
||||
/**
|
||||
* This class represents a placed and active permission sign
|
||||
*/
|
||||
@ -129,9 +132,9 @@ public class PermissionSign {
|
||||
*/
|
||||
public String[] getSignLines() {
|
||||
String[] lines = new String[4];
|
||||
lines[0] = ChatColor.translateAlternateColorCodes('&', Translator.getTranslatedMessage(
|
||||
TranslatableMessage.SIGN_PREFIX));
|
||||
lines[1] = getName();
|
||||
lines[0] = translateAllColorCodes(Translator.getTranslatedMessage(TranslatableMessage.SIGN_PREFIX));
|
||||
lines[1] = translateAllColorCodes(replacePlaceholder(Translator.getTranslatedMessage(
|
||||
TranslatableMessage.SIGN_NAME_FORMAT), "{name}", getName()));
|
||||
lines[2] = getDurationString();
|
||||
lines[3] = getCostString();
|
||||
return lines;
|
||||
@ -182,7 +185,10 @@ public class PermissionSign {
|
||||
return Translator.getTranslatedMessage(TranslatableMessage.SIGN_COST_FREE);
|
||||
} else {
|
||||
String currency = EconomyManager.getCurrency(cost != 1);
|
||||
return String.format("%.2f%s", cost, currency);
|
||||
String format = Translator.getTranslatedMessage(TranslatableMessage.SIGN_COST_FORMAT);
|
||||
String formatted = StringFormatter.replacePlaceholders(format, new String[]{"{cost}", "{currency}"},
|
||||
new String[]{"%.2f", currency});
|
||||
return String.format(formatted, cost);
|
||||
}
|
||||
}
|
||||
|
||||
@ -205,11 +211,11 @@ public class PermissionSign {
|
||||
* @return <p>The formatted duration string</p>
|
||||
*/
|
||||
private String formatDurationString(double duration, TranslatableMessage translatableMessage, boolean castToInt) {
|
||||
if (castToInt) {
|
||||
return (int) duration + " " + Translator.getTranslatedMessage(translatableMessage);
|
||||
} else {
|
||||
return duration + " " + Translator.getTranslatedMessage(translatableMessage);
|
||||
}
|
||||
String durationFormat = Translator.getTranslatedMessage(TranslatableMessage.SIGN_DURATION_FORMAT);
|
||||
durationFormat = replacePlaceholder(durationFormat, "{unit}",
|
||||
Translator.getTranslatedMessage(translatableMessage));
|
||||
return replacePlaceholder(durationFormat, "{duration}", castToInt ? String.valueOf((int) duration) :
|
||||
String.valueOf(duration));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,6 +1,9 @@
|
||||
package net.knarcraft.permissionsigns.formatting;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* A formatter for formatting displayed messages
|
||||
@ -81,9 +84,24 @@ public class StringFormatter {
|
||||
* @return <p>The formatted message</p>
|
||||
*/
|
||||
private static String formatMessage(String message) {
|
||||
return ChatColor.translateAlternateColorCodes('&',
|
||||
Translator.getTranslatedMessage(TranslatableMessage.PREFIX)) + " " + ChatColor.RESET +
|
||||
ChatColor.translateAlternateColorCodes('&', message);
|
||||
return translateAllColorCodes(Translator.getTranslatedMessage(TranslatableMessage.PREFIX)) + " " +
|
||||
ChatColor.RESET + translateAllColorCodes(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Translates all found color codes to formatting in a string
|
||||
*
|
||||
* @param message <p>The string to search for color codes</p>
|
||||
* @return <p>The message with color codes translated</p>
|
||||
*/
|
||||
public static String translateAllColorCodes(String message) {
|
||||
message = ChatColor.translateAlternateColorCodes('&', message);
|
||||
Pattern pattern = Pattern.compile("(#[a-fA-F0-9]{6})");
|
||||
Matcher matcher = pattern.matcher(message);
|
||||
while (matcher.find()) {
|
||||
message = message.replace(matcher.group(), "" + ChatColor.of(matcher.group()));
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -65,6 +65,21 @@ public enum TranslatableMessage {
|
||||
*/
|
||||
SIGN_PERMANENT,
|
||||
|
||||
/**
|
||||
* The format for displaying the name on a permission sign
|
||||
*/
|
||||
SIGN_NAME_FORMAT,
|
||||
|
||||
/**
|
||||
* The format for displaying the cost unit on a permission sign
|
||||
*/
|
||||
SIGN_COST_FORMAT,
|
||||
|
||||
/**
|
||||
* The format for displaying the duration on a permission sign
|
||||
*/
|
||||
SIGN_DURATION_FORMAT,
|
||||
|
||||
/**
|
||||
* The error message to display if the given cost is not a valid number
|
||||
*/
|
||||
|
@ -9,7 +9,6 @@ import net.knarcraft.permissionsigns.formatting.Translator;
|
||||
import net.knarcraft.permissionsigns.manager.EconomyManager;
|
||||
import net.knarcraft.permissionsigns.manager.PermissionManager;
|
||||
import net.knarcraft.permissionsigns.manager.SignManager;
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
import org.bukkit.GameMode;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Tag;
|
||||
@ -110,7 +109,7 @@ public class SignListener implements Listener {
|
||||
String replacedString = StringFormatter.replacePlaceholders(rawSignInfo, new String[]{"{Name}", "{Permissions}",
|
||||
"{Cost}", "{Duration}"}, new String[]{sign.getName(), permissionString.toString(), sign.getCostString(),
|
||||
sign.getDurationString()});
|
||||
return ChatColor.translateAlternateColorCodes('&', replacedString);
|
||||
return StringFormatter.translateAllColorCodes(replacedString);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -10,8 +10,11 @@ en:
|
||||
UNIT_HOURS: "hours"
|
||||
UNIT_DAY: "day"
|
||||
UNIT_DAYS: "days"
|
||||
SIGN_NAME_FORMAT: "{name}"
|
||||
SIGN_DURATION_FORMAT: "{duration} {unit}"
|
||||
SIGN_PERMANENT: "Permanent"
|
||||
SIGN_COST_FREE: "Free"
|
||||
SIGN_COST_FORMAT: "{cost}#66ff00{currency}"
|
||||
COST_INVALID_NUMBER: "&7The given cost is not a valid number"
|
||||
DURATION_INVALID_NUMBER: "&7The given duration is not a valid number"
|
||||
COMMAND_PLAYER_ONLY: "&7This command is only available to players"
|
||||
@ -42,8 +45,11 @@ nb-no:
|
||||
UNIT_HOURS: "timer"
|
||||
UNIT_DAY: "dag"
|
||||
UNIT_DAYS: "dager"
|
||||
SIGN_NAME_FORMAT: "{name}"
|
||||
SIGN_DURATION_FORMAT: "{duration} {unit}"
|
||||
SIGN_PERMANENT: "Permanent"
|
||||
SIGN_COST_FREE: "Gratis"
|
||||
SIGN_COST_FORMAT: "{cost}#66ff00{currency}"
|
||||
COST_INVALID_NUMBER: "&7Den gitte kostnaden er ikke et gyldig nummer"
|
||||
DURATION_INVALID_NUMBER: "&7Den gitte varigheten er ikke et gyldig nummer"
|
||||
COMMAND_PLAYER_ONLY: "&7Denne kommandoen kan bare brukes av spillere"
|
||||
|
Loading…
Reference in New Issue
Block a user