mirror of
https://github.com/Shiewk/SModeration.git
synced 2026-04-29 06:34:17 +02:00
Internationalization support
This commit is contained in:
@@ -7,7 +7,7 @@ import java.util.UUID;
|
||||
/**
|
||||
* Utility class for byte-based saving of integers, longs and UUIDs
|
||||
*/
|
||||
public abstract class ByteUtil {
|
||||
public final class ByteUtil {
|
||||
private ByteUtil(){}
|
||||
|
||||
public static byte[] longToBytes(long v){
|
||||
|
||||
@@ -12,13 +12,15 @@ import org.bukkit.command.ConsoleCommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import static de.shiewk.smoderation.paper.util.PlayerUtil.UUID_CONSOLE;
|
||||
import static net.kyori.adventure.text.Component.translatable;
|
||||
|
||||
public abstract class CommandUtil {
|
||||
public final class CommandUtil {
|
||||
private CommandUtil(){}
|
||||
|
||||
public static Predicate<CommandSourceStack> requirePermission(String permission) {
|
||||
@@ -30,7 +32,7 @@ public abstract class CommandUtil {
|
||||
if (sender instanceof Player player) {
|
||||
return player;
|
||||
} else {
|
||||
error("Only players can execute this command.");
|
||||
errorTranslatable("smod.command.fail.players");
|
||||
throw new UnknownError(); // can't happen
|
||||
}
|
||||
}
|
||||
@@ -42,7 +44,7 @@ public abstract class CommandUtil {
|
||||
} else if (sender instanceof ConsoleCommandSender){
|
||||
return UUID_CONSOLE;
|
||||
} else {
|
||||
error("Only players and the console can execute this command.");
|
||||
errorTranslatable("smod.command.fail.playersConsole");
|
||||
throw new UnknownError(); // can't happen
|
||||
}
|
||||
}
|
||||
@@ -50,17 +52,27 @@ public abstract class CommandUtil {
|
||||
public static Player getPlayerSingle(CommandContext<CommandSourceStack> context, String name) throws CommandSyntaxException {
|
||||
@NotNull List<Player> players = context.getArgument(name, PlayerSelectorArgumentResolver.class).resolve(context.getSource());
|
||||
if (players.isEmpty()){
|
||||
CommandUtil.error("Please provide a valid player.");
|
||||
errorTranslatable("smod.command.fail.invalidPlayer");
|
||||
}
|
||||
return players.getFirst();
|
||||
}
|
||||
|
||||
public static void error(String message) throws CommandSyntaxException {
|
||||
public static void error(Component message) throws CommandSyntaxException {
|
||||
throw new CommandSyntaxException(
|
||||
new SimpleCommandExceptionType(null),
|
||||
MessageComponentSerializer.message().serialize(
|
||||
Component.text(message)
|
||||
)
|
||||
MessageComponentSerializer.message().serialize(message)
|
||||
);
|
||||
}
|
||||
|
||||
public static void errorTranslatable(String key) throws CommandSyntaxException {
|
||||
error(translatable(key));
|
||||
}
|
||||
|
||||
public static void errorTranslatable(String key, Component...args) throws CommandSyntaxException {
|
||||
error(translatable(key, args));
|
||||
}
|
||||
|
||||
public static void errorTranslatable(String key, String...args) throws CommandSyntaxException {
|
||||
errorTranslatable(key, Arrays.stream(args).map(Component::text).toArray(Component[]::new));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,21 +1,14 @@
|
||||
package de.shiewk.smoderation.paper.util;
|
||||
|
||||
import de.shiewk.smoderation.paper.SModerationPaper;
|
||||
import de.shiewk.smoderation.paper.punishments.Punishment;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.util.StringUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public abstract class PlayerUtil {
|
||||
public final class PlayerUtil {
|
||||
private PlayerUtil(){}
|
||||
|
||||
public static final UUID UUID_CONSOLE = new UUID(0, 0);
|
||||
@@ -25,21 +18,7 @@ public abstract class PlayerUtil {
|
||||
return "CONSOLE";
|
||||
}
|
||||
OfflinePlayer player = Bukkit.getOfflinePlayer(uuid);
|
||||
return player.getName() == null ? "Unknown Player " + uuid : player.getName();
|
||||
}
|
||||
|
||||
public static @Nullable UUID offlinePlayerUUIDByName(String name){
|
||||
final OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayerIfCached(name); // getOfflinePlayerIfCached(String) is safer (I have experience with getOfflinePlayer(String) returning wrong UUIDs)
|
||||
if (offlinePlayer != null) {
|
||||
return offlinePlayer.getUniqueId();
|
||||
} else {
|
||||
// try to find uuid by searching through punishments
|
||||
final Punishment punishment = SModerationPaper.container.find(p -> offlinePlayerName(p.to).equalsIgnoreCase(name));
|
||||
if (punishment != null) {
|
||||
return punishment.to;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
return player.getName() == null ? uuid.toString() : player.getName();
|
||||
}
|
||||
|
||||
public static @Nullable CommandSender senderByUUID(@NotNull UUID uid){
|
||||
@@ -49,31 +28,4 @@ public abstract class PlayerUtil {
|
||||
return Bukkit.getPlayer(uid);
|
||||
}
|
||||
}
|
||||
|
||||
public static @Nullable Player findOnlinePlayer(String name){
|
||||
for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
|
||||
if (onlinePlayer.getName().equalsIgnoreCase(name)){
|
||||
return onlinePlayer;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static List<String> listPlayerNames(){
|
||||
return listPlayerNames(pl -> true);
|
||||
}
|
||||
|
||||
public static List<String> listPlayerNames(final Predicate<Player> predicate) {
|
||||
final ArrayList<String> names = new ArrayList<>();
|
||||
for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
|
||||
if (predicate.test(onlinePlayer)){
|
||||
names.add(onlinePlayer.getName());
|
||||
}
|
||||
}
|
||||
return List.copyOf(names);
|
||||
}
|
||||
|
||||
public static List<String> listPlayerNames(String search) {
|
||||
return StringUtil.copyPartialMatches(search, listPlayerNames(), new ArrayList<>());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,19 @@
|
||||
package de.shiewk.smoderation.paper.util;
|
||||
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import org.jetbrains.annotations.Range;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.TimeZone;
|
||||
|
||||
public abstract class TimeUtil {
|
||||
import static net.kyori.adventure.text.Component.*;
|
||||
|
||||
public final class TimeUtil {
|
||||
private TimeUtil(){}
|
||||
|
||||
public static String formatTimeLong(long millis){
|
||||
public static Component formatTimeLong(long millis){
|
||||
long seconds = millis / 1000;
|
||||
millis -= seconds * 1000;
|
||||
|
||||
@@ -31,95 +35,65 @@ public abstract class TimeUtil {
|
||||
long weeks = days / 7;
|
||||
days -= weeks * 7;
|
||||
|
||||
StringBuilder builder = new StringBuilder();
|
||||
TextComponent.Builder builder = empty().toBuilder();
|
||||
|
||||
if (years > 0){
|
||||
if (!builder.isEmpty()){
|
||||
builder.append(" ");
|
||||
if (!builder.children().isEmpty()){
|
||||
builder.appendSpace();
|
||||
}
|
||||
builder.append("%s years".formatted(years));
|
||||
builder.append(translatable("smod.time.years", text(years)));
|
||||
}
|
||||
|
||||
if (months > 0){
|
||||
if (!builder.isEmpty()){
|
||||
builder.append(" ");
|
||||
if (!builder.children().isEmpty()){
|
||||
builder.appendSpace();
|
||||
}
|
||||
builder.append("%s months".formatted(months));
|
||||
builder.append(translatable("smod.time.months", text(months)));
|
||||
}
|
||||
|
||||
if (weeks > 0){
|
||||
if (!builder.isEmpty()){
|
||||
builder.append(" ");
|
||||
if (!builder.children().isEmpty()){
|
||||
builder.appendSpace();
|
||||
}
|
||||
builder.append("%s weeks".formatted(weeks));
|
||||
builder.append(translatable("smod.time.weeks", text(weeks)));
|
||||
}
|
||||
|
||||
if (days > 0){
|
||||
if (!builder.isEmpty()){
|
||||
builder.append(" ");
|
||||
if (!builder.children().isEmpty()){
|
||||
builder.appendSpace();
|
||||
}
|
||||
builder.append("%s days".formatted(days));
|
||||
builder.append(translatable("smod.time.days", text(days)));
|
||||
}
|
||||
|
||||
if (hours > 0){
|
||||
if (!builder.isEmpty()){
|
||||
builder.append(" ");
|
||||
if (!builder.children().isEmpty()){
|
||||
builder.appendSpace();
|
||||
}
|
||||
builder.append("%s hours".formatted(hours));
|
||||
builder.append(translatable("smod.time.hours", text(hours)));
|
||||
}
|
||||
|
||||
if (minutes > 0){
|
||||
if (!builder.isEmpty()){
|
||||
builder.append(" ");
|
||||
if (!builder.children().isEmpty()){
|
||||
builder.appendSpace();
|
||||
}
|
||||
builder.append("%s minutes".formatted(minutes));
|
||||
builder.append(translatable("smod.time.minutes", text(minutes)));
|
||||
}
|
||||
|
||||
if (seconds > 0){
|
||||
if (!builder.isEmpty()){
|
||||
builder.append(" ");
|
||||
if (!builder.children().isEmpty()){
|
||||
builder.appendSpace();
|
||||
}
|
||||
builder.append("%s seconds".formatted(seconds));
|
||||
builder.append(translatable("smod.time.seconds", text(seconds)));
|
||||
}
|
||||
|
||||
if (builder.isEmpty()){
|
||||
builder.append("%s ms".formatted(millis));
|
||||
if (builder.children().isEmpty()){
|
||||
builder.append(translatable("smod.time.milliseconds", text(millis)));
|
||||
}
|
||||
|
||||
return builder.toString();
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
public static long parseDurationMillisSafely(String in){
|
||||
try {
|
||||
return parseDurationMillis(in);
|
||||
} catch (Throwable e){
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
public static long parseDurationMillis(String in){
|
||||
if (in.endsWith("ms")){
|
||||
return Long.parseLong(in.substring(0, in.length()-2));
|
||||
} else if (in.endsWith("s")){
|
||||
return Long.parseLong(in.substring(0, in.length()-1)) * 1000L;
|
||||
} else if (in.endsWith("min")){
|
||||
return Long.parseLong(in.substring(0, in.length()-3)) * 60000L;
|
||||
} else if (in.endsWith("h")){
|
||||
return Long.parseLong(in.substring(0, in.length()-1)) * 3600000L;
|
||||
} else if (in.endsWith("d")){
|
||||
return Long.parseLong(in.substring(0, in.length()-1)) * 86400000L;
|
||||
} else if (in.endsWith("w")){
|
||||
return Long.parseLong(in.substring(0, in.length()-1)) * 604800000L;
|
||||
} else if (in.endsWith("mo")){
|
||||
return Long.parseLong(in.substring(0, in.length()-2)) * 2592000000L;
|
||||
} else if (in.endsWith("y")){
|
||||
return Long.parseLong(in.substring(0, in.length()-1)) * 31536000000L;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
public static String calendarTimestamp(long time){
|
||||
public static Component calendarTimestamp(long time){
|
||||
Date date = new Date(time);
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTime(date);
|
||||
@@ -129,73 +103,22 @@ public abstract class TimeUtil {
|
||||
int hour = calendar.get(Calendar.HOUR_OF_DAY);
|
||||
int year = calendar.get(Calendar.YEAR);
|
||||
|
||||
String day = numberWithSuffix(calendar.get(Calendar.DAY_OF_MONTH));
|
||||
String month = monthName(calendar.get(Calendar.MONTH));
|
||||
return "%s %s %s, %s:%s:%s %s".formatted(
|
||||
int day = calendar.get(Calendar.DAY_OF_MONTH);
|
||||
Component month = monthName(calendar.get(Calendar.MONTH));
|
||||
return translatable(
|
||||
"smod.time.timestamp",
|
||||
text(year),
|
||||
month,
|
||||
day,
|
||||
year,
|
||||
hour < 10 ? "0" + hour : hour,
|
||||
minute < 10 ? "0" + minute : minute,
|
||||
second < 10 ? "0" + second : second,
|
||||
zone.getDisplayName(false, TimeZone.SHORT)
|
||||
text(day),
|
||||
text((hour < 10 ? "0" : "") + hour),
|
||||
text((minute < 10 ? "0" : "") + minute),
|
||||
text((second < 10 ? "0" : "") + second),
|
||||
text(zone.getDisplayName(zone.inDaylightTime(calendar.getTime()), TimeZone.SHORT))
|
||||
);
|
||||
}
|
||||
|
||||
private static String numberWithSuffix(int i){
|
||||
return i + numberSuffix(i);
|
||||
public static Component monthName(@Range(from = 0, to = 11) int m){
|
||||
return translatable("smod.time.month." + m);
|
||||
}
|
||||
|
||||
private static String numberSuffix(int i){
|
||||
if ((i % 10) == 1 && i != 11){
|
||||
return "st";
|
||||
} else if ((i % 10) == 2 && i != 12){
|
||||
return "nd";
|
||||
} else if ((i % 10) == 3 && i != 13){
|
||||
return "rd";
|
||||
}
|
||||
return "th";
|
||||
}
|
||||
|
||||
public static String monthName(@Range(from = 0, to = 11) int m){
|
||||
switch (m){
|
||||
case 0 -> {
|
||||
return "January";
|
||||
}
|
||||
case 1 -> {
|
||||
return "February";
|
||||
}
|
||||
case 2 -> {
|
||||
return "March";
|
||||
}
|
||||
case 3 -> {
|
||||
return "April";
|
||||
}
|
||||
case 4 -> {
|
||||
return "May";
|
||||
}
|
||||
case 5 -> {
|
||||
return "June";
|
||||
}
|
||||
case 6 -> {
|
||||
return "July";
|
||||
}
|
||||
case 7 -> {
|
||||
return "August";
|
||||
}
|
||||
case 8 -> {
|
||||
return "September";
|
||||
}
|
||||
case 9 -> {
|
||||
return "October";
|
||||
}
|
||||
case 10 -> {
|
||||
return "November";
|
||||
}
|
||||
case 11 -> {
|
||||
return "December";
|
||||
}
|
||||
}
|
||||
return "Unknown Month";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user