diff --git a/src/main/java/de/jvstvshd/velocitypunishment/VelocityPunishmentPlugin.java b/src/main/java/de/jvstvshd/velocitypunishment/VelocityPunishmentPlugin.java index 36a2edf2..a36ef02c 100644 --- a/src/main/java/de/jvstvshd/velocitypunishment/VelocityPunishmentPlugin.java +++ b/src/main/java/de/jvstvshd/velocitypunishment/VelocityPunishmentPlugin.java @@ -14,6 +14,8 @@ import de.jvstvshd.velocitypunishment.config.ConfigurationManager; import de.jvstvshd.velocitypunishment.listener.ChatListener; import de.jvstvshd.velocitypunishment.listener.ConnectListener; +import de.jvstvshd.velocitypunishment.message.MessageProvider; +import de.jvstvshd.velocitypunishment.message.ResourceBundleMessageProvider; import de.jvstvshd.velocitypunishment.punishment.PunishmentManager; import de.jvstvshd.velocitypunishment.punishment.impl.DefaultPlayerResolver; import de.jvstvshd.velocitypunishment.punishment.impl.DefaultPunishmentManager; @@ -36,9 +38,11 @@ public class VelocityPunishmentPlugin { private final ProxyServer server; private final Logger logger; private final ConfigurationManager configurationManager; + private final ExecutorService service = Executors.newCachedThreadPool(); private PunishmentManager punishmentManager; private HikariDataSource dataSource; - private final PlayerResolver playerResolver; + private PlayerResolver playerResolver; + private MessageProvider messageProvider; @Inject public VelocityPunishmentPlugin(ProxyServer server, Logger logger, @DataDirectory Path dataDirectory) { @@ -52,6 +56,7 @@ public VelocityPunishmentPlugin(ProxyServer server, Logger logger, @DataDirector public void onProxyInitialization(ProxyInitializeEvent event) { try { configurationManager.load(); + this.messageProvider = new ResourceBundleMessageProvider(configurationManager.getConfiguration()); } catch (IOException e) { logger.error("Could not load configuration", e); } @@ -66,19 +71,18 @@ public void onProxyInitialization(ProxyInitializeEvent event) { } private void setup(CommandManager commandManager, EventManager eventManager) { - ExecutorService service = Executors.newCachedThreadPool(); - ChatListener chatListener = new ChatListener(punishmentManager, service); + ChatListener chatListener = new ChatListener(this); eventManager.register(this, new ConnectListener(punishmentManager, Executors.newCachedThreadPool(), server, chatListener)); eventManager.register(this, chatListener); - commandManager.register(commandManager.metaBuilder("ban").build(), new BanCommand(server, punishmentManager, playerResolver)); - commandManager.register(commandManager.metaBuilder("tempban").build(), new TempbanCommand(punishmentManager, server, service, playerResolver)); - commandManager.register(commandManager.metaBuilder("unban").build(), new UnbanCommand(punishmentManager, dataSource, service, playerResolver)); - commandManager.register(commandManager.metaBuilder("punishment").build(), new PunishmentCommand(service, punishmentManager, dataSource, server, chatListener, playerResolver)); - commandManager.register(commandManager.metaBuilder("mute").build(), new MuteCommand(punishmentManager, server, chatListener, service, playerResolver)); - commandManager.register(commandManager.metaBuilder("tempmute").build(), new TempmuteCommand(punishmentManager, server, service, playerResolver)); - commandManager.register(commandManager.metaBuilder("unmute").build(), new UnmuteCommand(punishmentManager, dataSource, service, chatListener, playerResolver)); + commandManager.register(commandManager.metaBuilder("ban").build(), new BanCommand(this)); + commandManager.register(commandManager.metaBuilder("tempban").build(), new TempbanCommand(this)); + commandManager.register(commandManager.metaBuilder("unban").build(), new UnbanCommand(this)); + commandManager.register(commandManager.metaBuilder("punishment").build(), new PunishmentCommand(this, chatListener)); + commandManager.register(commandManager.metaBuilder("mute").build(), new MuteCommand(this, chatListener)); + commandManager.register(commandManager.metaBuilder("tempmute").build(), new TempmuteCommand(this)); + commandManager.register(commandManager.metaBuilder("unmute").build(), new UnmuteCommand(this, chatListener)); } public PunishmentManager getPunishmentManager() { @@ -106,4 +110,36 @@ private void initDataSource() throws SQLException { statement.execute(); } } + + public void setPunishmentManager(PunishmentManager punishmentManager) { + this.punishmentManager = punishmentManager; + } + + public void setPlayerResolver(PlayerResolver playerResolver) { + this.playerResolver = playerResolver; + } + + public void setMessageProvider(MessageProvider messageProvider) { + this.messageProvider = messageProvider; + } + + public PlayerResolver getPlayerResolver() { + return playerResolver; + } + + public ProxyServer getServer() { + return server; + } + + public ExecutorService getService() { + return service; + } + + public HikariDataSource getDataSource() { + return dataSource; + } + + public MessageProvider getMessageProvider() { + return messageProvider; + } } diff --git a/src/main/java/de/jvstvshd/velocitypunishment/commands/BanCommand.java b/src/main/java/de/jvstvshd/velocitypunishment/commands/BanCommand.java index 59dd364f..b7d8f81b 100644 --- a/src/main/java/de/jvstvshd/velocitypunishment/commands/BanCommand.java +++ b/src/main/java/de/jvstvshd/velocitypunishment/commands/BanCommand.java @@ -3,10 +3,8 @@ import com.google.common.collect.ImmutableList; import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.SimpleCommand; -import com.velocitypowered.api.proxy.ProxyServer; +import de.jvstvshd.velocitypunishment.VelocityPunishmentPlugin; import de.jvstvshd.velocitypunishment.punishment.PunishmentHelper; -import de.jvstvshd.velocitypunishment.punishment.PunishmentManager; -import de.jvstvshd.velocitypunishment.util.PlayerResolver; import de.jvstvshd.velocitypunishment.util.Util; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TextComponent; @@ -14,60 +12,62 @@ import net.kyori.adventure.text.format.TextDecoration; import java.util.List; -import java.util.Optional; -import java.util.UUID; import java.util.stream.Collectors; +import static de.jvstvshd.velocitypunishment.util.Util.INTERNAL_ERROR; import static de.jvstvshd.velocitypunishment.util.Util.copyComponent; public class BanCommand implements SimpleCommand { - private final ProxyServer proxyServer; - private final PunishmentManager punishmentManager; - private final PlayerResolver playerResolver; + private final VelocityPunishmentPlugin plugin; - public BanCommand(ProxyServer proxyServer, PunishmentManager punishmentManager, PlayerResolver playerResolver) { - this.proxyServer = proxyServer; - this.punishmentManager = punishmentManager; - this.playerResolver = playerResolver; + public BanCommand(VelocityPunishmentPlugin plugin) { + this.plugin = plugin; } @Override public void execute(Invocation invocation) { CommandSource source = invocation.source(); if (invocation.arguments().length < 1) { - source.sendMessage(Component.text("Please use /ban [reason]").color(NamedTextColor.DARK_RED)); + //source.sendMessage(Component.text().append(plugin.getMessageProvider().prefix(), plugin.getMessageProvider().provide("command.ban.usage").color(NamedTextColor.RED))); + source.sendMessage(Component.text("Please use /ban [reason]").color(NamedTextColor.DARK_RED)); return; } - - PunishmentHelper parser = new PunishmentHelper(); - Optional optionalUUID = parser.parseUuid(playerResolver, invocation); - UUID uuid; - if (optionalUUID.isEmpty()) { - source.sendMessage(Component.text(invocation.arguments()[0] + " could not be found.")); - return; - } - uuid = optionalUUID.get(); - TextComponent component = parser.parseComponent(1, invocation); - punishmentManager.createPermanentBan(uuid, component).punish().whenCompleteAsync((unused, throwable) -> { + var playerResolver = plugin.getPlayerResolver(); + var punishmentManager = plugin.getPunishmentManager(); + var parser = new PunishmentHelper(); + playerResolver.getOrQueryPlayerUuid(invocation.arguments()[0], plugin.getService()).whenComplete((uuid, throwable) -> { if (throwable != null) { + source.sendMessage(INTERNAL_ERROR); throwable.printStackTrace(); - source.sendMessage(Util.INTERNAL_ERROR); - } else { - String uuidString = uuid.toString().toLowerCase(); - source.sendMessage(Component.text("You have banned the player ").color(NamedTextColor.RED) - .append(Component.text().append(copyComponent(invocation.arguments()[0]).color(NamedTextColor.YELLOW).decorate(TextDecoration.BOLD), - Component.text("/").color(NamedTextColor.WHITE), - copyComponent(uuidString).color(NamedTextColor.RED).decorate(TextDecoration.BOLD), - Component.text(" for ").color(NamedTextColor.RED), - component, - Component.text(".").color(NamedTextColor.RED)))); + return; + } + if (uuid == null) { + source.sendMessage(Component.translatable().args(Component.text(invocation.arguments()[0])).key(invocation.arguments()[0] + " could not be found.").build()); + return; } + TextComponent component = parser.parseComponent(1, invocation); + punishmentManager.createPermanentBan(uuid, component).punish().whenCompleteAsync((unused, t) -> { + if (t != null) { + t.printStackTrace(); + source.sendMessage(Util.INTERNAL_ERROR); + } else { + String uuidString = uuid.toString().toLowerCase(); + source.sendMessage(Component.text("You have banned the player ").color(NamedTextColor.RED) + .append(Component.text().append(copyComponent(invocation.arguments()[0]).color(NamedTextColor.YELLOW).decorate(TextDecoration.BOLD), + Component.text("/").color(NamedTextColor.WHITE), + copyComponent(uuidString).color(NamedTextColor.RED).decorate(TextDecoration.BOLD), + Component.text(" for ").color(NamedTextColor.RED), + component, + Component.text(".").color(NamedTextColor.RED)))); + } + }); }); } @Override public List suggest(Invocation invocation) { + var proxyServer = plugin.getServer(); String[] args = invocation.arguments(); if (args.length == 0) { return Util.getPlayerNames(proxyServer.getAllPlayers()); diff --git a/src/main/java/de/jvstvshd/velocitypunishment/commands/MuteCommand.java b/src/main/java/de/jvstvshd/velocitypunishment/commands/MuteCommand.java index e63aa923..372f8cbc 100644 --- a/src/main/java/de/jvstvshd/velocitypunishment/commands/MuteCommand.java +++ b/src/main/java/de/jvstvshd/velocitypunishment/commands/MuteCommand.java @@ -3,12 +3,10 @@ import com.google.common.collect.ImmutableList; import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.SimpleCommand; -import com.velocitypowered.api.proxy.ProxyServer; +import de.jvstvshd.velocitypunishment.VelocityPunishmentPlugin; import de.jvstvshd.velocitypunishment.listener.ChatListener; import de.jvstvshd.velocitypunishment.punishment.Mute; import de.jvstvshd.velocitypunishment.punishment.PunishmentHelper; -import de.jvstvshd.velocitypunishment.punishment.PunishmentManager; -import de.jvstvshd.velocitypunishment.util.PlayerResolver; import de.jvstvshd.velocitypunishment.util.Util; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TextComponent; @@ -16,30 +14,22 @@ import net.kyori.adventure.text.format.TextDecoration; import java.util.List; -import java.util.Optional; -import java.util.UUID; import java.util.concurrent.ExecutionException; -import java.util.concurrent.ExecutorService; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.stream.Collectors; +import static de.jvstvshd.velocitypunishment.util.Util.INTERNAL_ERROR; import static de.jvstvshd.velocitypunishment.util.Util.copyComponent; public class MuteCommand implements SimpleCommand { - private final PunishmentManager punishmentManager; - private final ProxyServer server; + private final VelocityPunishmentPlugin plugin; private final ChatListener chatListener; - private final ExecutorService service; - private final PlayerResolver playerResolver; - public MuteCommand(PunishmentManager punishmentManager, ProxyServer server, ChatListener chatListener, ExecutorService service, PlayerResolver playerResolver) { - this.punishmentManager = punishmentManager; - this.server = server; + public MuteCommand(VelocityPunishmentPlugin plugin, ChatListener chatListener) { + this.plugin = plugin; this.chatListener = chatListener; - this.service = service; - this.playerResolver = playerResolver; } @Override @@ -49,15 +39,19 @@ public void execute(Invocation invocation) { source.sendMessage(Component.text("Please user /mute [reason]").color(NamedTextColor.DARK_RED)); return; } - service.execute(() -> { - PunishmentHelper parser = new PunishmentHelper(); - Optional optionalUUID = parser.parseUuid(playerResolver, invocation); - UUID uuid; - if (optionalUUID.isEmpty()) { + var playerResolver = plugin.getPlayerResolver(); + var punishmentManager = plugin.getPunishmentManager(); + PunishmentHelper parser = new PunishmentHelper(); + playerResolver.getOrQueryPlayerUuid(invocation.arguments()[0], plugin.getService()).whenCompleteAsync((uuid, throwable) -> { + if (throwable != null) { + source.sendMessage(INTERNAL_ERROR); + throwable.printStackTrace(); + return; + } + if (uuid == null) { source.sendMessage(Component.text(invocation.arguments()[0] + " could not be found.")); return; } - uuid = optionalUUID.get(); TextComponent reason = parser.parseComponent(1, invocation); Mute mute; @@ -81,15 +75,16 @@ public void execute(Invocation invocation) { ).build(); source.sendMessage(component); source.sendMessage(Component.text("Punishment id: " + mute.getPunishmentUuid().toString().toLowerCase()).color(NamedTextColor.YELLOW)); - if (server.getPlayer(uuid).isPresent()) { + if (plugin.getServer().getPlayer(uuid).isPresent()) { chatListener.getMutes().put(uuid, new ChatListener.MuteContainer().setMute(mute)); } - }); + }, plugin.getService()); } @Override public List suggest(Invocation invocation) { String[] args = invocation.arguments(); + var server = plugin.getServer(); if (args.length == 0) { return Util.getPlayerNames(server.getAllPlayers()); } diff --git a/src/main/java/de/jvstvshd/velocitypunishment/commands/PunishmentCommand.java b/src/main/java/de/jvstvshd/velocitypunishment/commands/PunishmentCommand.java index 7e9c8ee2..de22e02b 100644 --- a/src/main/java/de/jvstvshd/velocitypunishment/commands/PunishmentCommand.java +++ b/src/main/java/de/jvstvshd/velocitypunishment/commands/PunishmentCommand.java @@ -4,11 +4,10 @@ import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.SimpleCommand; import com.velocitypowered.api.proxy.ProxyServer; +import de.jvstvshd.velocitypunishment.VelocityPunishmentPlugin; import de.jvstvshd.velocitypunishment.listener.ChatListener; import de.jvstvshd.velocitypunishment.punishment.Punishment; import de.jvstvshd.velocitypunishment.punishment.PunishmentHelper; -import de.jvstvshd.velocitypunishment.punishment.PunishmentManager; -import de.jvstvshd.velocitypunishment.util.PlayerResolver; import de.jvstvshd.velocitypunishment.util.Util; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.event.ClickEvent; @@ -25,22 +24,22 @@ import java.util.concurrent.*; import java.util.stream.Collectors; +import static de.jvstvshd.velocitypunishment.util.Util.INTERNAL_ERROR; + public class PunishmentCommand implements SimpleCommand { private final ExecutorService service; - private final PunishmentManager punishmentManager; private final DataSource dataSource; private final ProxyServer server; private final ChatListener chatListener; - private final PlayerResolver playerResolver; + private final VelocityPunishmentPlugin plugin; - public PunishmentCommand(ExecutorService service, PunishmentManager punishmentManager, DataSource dataSource, ProxyServer server, ChatListener chatListener, PlayerResolver playerResolver) { - this.service = service; - this.punishmentManager = punishmentManager; - this.dataSource = dataSource; - this.server = server; + public PunishmentCommand(VelocityPunishmentPlugin plugin, ChatListener chatListener) { + this.service = plugin.getService(); + this.dataSource = plugin.getDataSource(); + this.server = plugin.getServer(); this.chatListener = chatListener; - this.playerResolver = playerResolver; + this.plugin = plugin; } private final static List options = ImmutableList.of("annul", "remove", "info", "change"); @@ -53,18 +52,23 @@ public void execute(Invocation invocation) { source.sendMessage(Component.text("Please use /punishment or ").color(NamedTextColor.DARK_RED)); return; } + var punishmentManager = plugin.getPunishmentManager(); if (arguments[0].equalsIgnoreCase("playerinfo")) { - service.execute(() -> { - PunishmentHelper helper = new PunishmentHelper(); - UUID playerUuid = helper.getPlayerUuid(1, service, playerResolver, invocation); - if (playerUuid == null) { + var playerResolver = plugin.getPlayerResolver(); + PunishmentHelper helper = new PunishmentHelper(); + helper.getPlayerUuid(1, service, playerResolver, invocation).whenCompleteAsync((uuid, throwable) -> { + if (throwable != null) { + source.sendMessage(INTERNAL_ERROR); + throwable.printStackTrace(); + return; + } + if (uuid == null) { invocation.source().sendMessage(Component.text("This player is not banned at the moment.").color(NamedTextColor.RED)); return; } - List punishments; try { - punishments = punishmentManager.getPunishments(playerUuid, service).get(10, TimeUnit.SECONDS); + punishments = punishmentManager.getPunishments(uuid, service).get(10, TimeUnit.SECONDS); } catch (InterruptedException | TimeoutException | ExecutionException e) { e.printStackTrace(); source.sendMessage(Util.INTERNAL_ERROR); @@ -79,7 +83,7 @@ public void execute(Invocation invocation) { .color(NamedTextColor.GREEN))); source.sendMessage(component); } - }); + }, service); return; } UUID uuid; @@ -120,7 +124,12 @@ public void execute(Invocation invocation) { return; } source.sendMessage(Component.text("The punishment was successfully annulled.").color(NamedTextColor.GREEN)); - chatListener.update(uuid); + try { + chatListener.update(uuid); + } catch (ExecutionException | InterruptedException | TimeoutException e) { + source.sendMessage(INTERNAL_ERROR); + e.printStackTrace(); + } }); case "info" -> source.sendMessage(new PunishmentHelper().buildPunishmentData(punishment)); case "change" -> source.sendMessage(Component.text("Soon™️")); @@ -145,7 +154,7 @@ public CompletableFuture> suggestAsync(Invocation invocation) { } catch (SQLException e) { e.printStackTrace(); } - list.addAll(Util.getPlayerNames(server.getAllPlayers()).stream().map(String::toLowerCase).collect(Collectors.toList())); + list.addAll(Util.getPlayerNames(server.getAllPlayers()).stream().map(String::toLowerCase).toList()); return ImmutableList.copyOf(list); }, service); } diff --git a/src/main/java/de/jvstvshd/velocitypunishment/commands/TempbanCommand.java b/src/main/java/de/jvstvshd/velocitypunishment/commands/TempbanCommand.java index 16568d24..33a4fd9c 100644 --- a/src/main/java/de/jvstvshd/velocitypunishment/commands/TempbanCommand.java +++ b/src/main/java/de/jvstvshd/velocitypunishment/commands/TempbanCommand.java @@ -4,10 +4,9 @@ import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.SimpleCommand; import com.velocitypowered.api.proxy.ProxyServer; +import de.jvstvshd.velocitypunishment.VelocityPunishmentPlugin; import de.jvstvshd.velocitypunishment.punishment.PunishmentDuration; import de.jvstvshd.velocitypunishment.punishment.PunishmentHelper; -import de.jvstvshd.velocitypunishment.punishment.PunishmentManager; -import de.jvstvshd.velocitypunishment.util.PlayerResolver; import de.jvstvshd.velocitypunishment.util.Util; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TextComponent; @@ -17,25 +16,23 @@ import java.time.format.DateTimeFormatter; import java.util.List; import java.util.Optional; -import java.util.UUID; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.stream.Collectors; +import static de.jvstvshd.velocitypunishment.util.Util.INTERNAL_ERROR; import static de.jvstvshd.velocitypunishment.util.Util.copyComponent; public class TempbanCommand implements SimpleCommand { - private final PunishmentManager punishmentManager; private final ProxyServer proxyServer; private final ExecutorService service; - private final PlayerResolver playerResolver; + private final VelocityPunishmentPlugin plugin; - public TempbanCommand(PunishmentManager punishmentManager, ProxyServer proxyServer, ExecutorService service, PlayerResolver playerResolver) { - this.punishmentManager = punishmentManager; - this.proxyServer = proxyServer; - this.service = service; - this.playerResolver = playerResolver; + public TempbanCommand(VelocityPunishmentPlugin plugin) { + this.proxyServer = plugin.getServer(); + this.service = plugin.getService(); + this.plugin = plugin; } @Override @@ -45,15 +42,17 @@ public void execute(Invocation invocation) { source.sendMessage(Component.text("Please use /tempban [reason]").color(NamedTextColor.DARK_RED)); return; } - service.execute(() -> { - PunishmentHelper parser = new PunishmentHelper(); - Optional optionalUUID = parser.parseUuid(playerResolver, invocation); - UUID uuid; - if (optionalUUID.isEmpty()) { + PunishmentHelper parser = new PunishmentHelper(); + plugin.getPlayerResolver().getOrQueryPlayerUuid(invocation.arguments()[0], service).whenCompleteAsync((uuid, throwable) -> { + if (throwable != null) { + source.sendMessage(INTERNAL_ERROR); + throwable.printStackTrace(); + return; + } + if (uuid == null) { source.sendMessage(Component.text(invocation.arguments()[0] + " could not be found.")); return; } - uuid = optionalUUID.get(); Optional optDuration = parser.parseDuration(1, invocation); if (optDuration.isEmpty()) { return; @@ -61,14 +60,14 @@ public void execute(Invocation invocation) { PunishmentDuration duration = optDuration.get(); TextComponent component = parser.parseComponent(2, invocation); try { - punishmentManager.createBan(uuid, component, duration).punish().get(); + plugin.getPunishmentManager().createBan(uuid, component, duration).punish().get(); } catch (InterruptedException | ExecutionException e) { invocation.source().sendMessage(Util.INTERNAL_ERROR); e.printStackTrace(); return; } String until = duration.expiration().format(DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm:ss")); - String uuidString = uuid.toString().toLowerCase();//.replace('-', Character.MIN_VALUE); + String uuidString = uuid.toString().toLowerCase(); source.sendMessage(Component.text("You have banned the player ").color(NamedTextColor.RED) .append(Component.text().append(copyComponent(invocation.arguments()[0]).color(NamedTextColor.YELLOW).decorate(TextDecoration.BOLD), Component.text("/").color(NamedTextColor.WHITE), @@ -78,7 +77,8 @@ public void execute(Invocation invocation) { Component.text(" until ").color(NamedTextColor.RED), Component.text(until).color(NamedTextColor.GREEN), Component.text(".").color(NamedTextColor.RED)))); - }); + }, service); + } @Override diff --git a/src/main/java/de/jvstvshd/velocitypunishment/commands/TempmuteCommand.java b/src/main/java/de/jvstvshd/velocitypunishment/commands/TempmuteCommand.java index bcc3b2be..4e63b86d 100644 --- a/src/main/java/de/jvstvshd/velocitypunishment/commands/TempmuteCommand.java +++ b/src/main/java/de/jvstvshd/velocitypunishment/commands/TempmuteCommand.java @@ -4,11 +4,10 @@ import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.SimpleCommand; import com.velocitypowered.api.proxy.ProxyServer; +import de.jvstvshd.velocitypunishment.VelocityPunishmentPlugin; import de.jvstvshd.velocitypunishment.punishment.Mute; import de.jvstvshd.velocitypunishment.punishment.PunishmentDuration; import de.jvstvshd.velocitypunishment.punishment.PunishmentHelper; -import de.jvstvshd.velocitypunishment.punishment.PunishmentManager; -import de.jvstvshd.velocitypunishment.util.PlayerResolver; import de.jvstvshd.velocitypunishment.util.Util; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TextComponent; @@ -18,25 +17,23 @@ import java.time.format.DateTimeFormatter; import java.util.List; import java.util.Optional; -import java.util.UUID; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.stream.Collectors; +import static de.jvstvshd.velocitypunishment.util.Util.INTERNAL_ERROR; import static de.jvstvshd.velocitypunishment.util.Util.copyComponent; public class TempmuteCommand implements SimpleCommand { - private final PunishmentManager punishmentManager; private final ProxyServer proxyServer; private final ExecutorService service; - private final PlayerResolver playerResolver; + private final VelocityPunishmentPlugin plugin; - public TempmuteCommand(PunishmentManager punishmentManager, ProxyServer proxyServer, ExecutorService service, PlayerResolver playerResolver) { - this.punishmentManager = punishmentManager; - this.proxyServer = proxyServer; - this.service = service; - this.playerResolver = playerResolver; + public TempmuteCommand(VelocityPunishmentPlugin plugin) { + this.proxyServer = plugin.getServer(); + this.service = plugin.getService(); + this.plugin = plugin; } @Override @@ -46,15 +43,17 @@ public void execute(Invocation invocation) { source.sendMessage(Component.text("Please use /tempmute [reason]").color(NamedTextColor.DARK_RED)); return; } - service.execute(() -> { - PunishmentHelper parser = new PunishmentHelper(); - Optional optionalUUID = parser.parseUuid(playerResolver, invocation); - UUID uuid; - if (optionalUUID.isEmpty()) { + PunishmentHelper parser = new PunishmentHelper(); + plugin.getPlayerResolver().getOrQueryPlayerUuid(invocation.arguments()[0], service).whenCompleteAsync((uuid, throwable) -> { + if (throwable != null) { + source.sendMessage(INTERNAL_ERROR); + throwable.printStackTrace(); + return; + } + if (uuid == null) { source.sendMessage(Component.text(invocation.arguments()[0] + " could not be found.")); return; } - uuid = optionalUUID.get(); Optional optDuration = parser.parseDuration(1, invocation); if (optDuration.isEmpty()) { return; @@ -63,7 +62,7 @@ public void execute(Invocation invocation) { TextComponent component = parser.parseComponent(2, invocation); Mute mute; try { - mute = punishmentManager.createMute(uuid, component, duration); + mute = plugin.getPunishmentManager().createMute(uuid, component, duration); mute.punish().get(); } catch (InterruptedException | ExecutionException e) { invocation.source().sendMessage(Util.INTERNAL_ERROR); @@ -82,7 +81,7 @@ public void execute(Invocation invocation) { Component.text(until).color(NamedTextColor.GREEN), Component.text(".").color(NamedTextColor.RED)))); source.sendMessage(Component.text("Punishment id: " + mute.getPunishmentUuid().toString().toLowerCase()).color(NamedTextColor.YELLOW)); - }); + }, service); } @Override diff --git a/src/main/java/de/jvstvshd/velocitypunishment/commands/UnbanCommand.java b/src/main/java/de/jvstvshd/velocitypunishment/commands/UnbanCommand.java index 9177860c..f430dee4 100644 --- a/src/main/java/de/jvstvshd/velocitypunishment/commands/UnbanCommand.java +++ b/src/main/java/de/jvstvshd/velocitypunishment/commands/UnbanCommand.java @@ -2,12 +2,10 @@ import com.google.common.collect.ImmutableList; import com.velocitypowered.api.command.SimpleCommand; -import com.zaxxer.hikari.HikariDataSource; +import de.jvstvshd.velocitypunishment.VelocityPunishmentPlugin; import de.jvstvshd.velocitypunishment.punishment.Punishment; import de.jvstvshd.velocitypunishment.punishment.PunishmentHelper; -import de.jvstvshd.velocitypunishment.punishment.PunishmentManager; import de.jvstvshd.velocitypunishment.punishment.StandardPunishmentType; -import de.jvstvshd.velocitypunishment.util.PlayerResolver; import de.jvstvshd.velocitypunishment.util.Util; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.event.ClickEvent; @@ -23,21 +21,20 @@ import java.util.ArrayList; import java.util.List; import java.util.Locale; -import java.util.UUID; import java.util.concurrent.*; +import static de.jvstvshd.velocitypunishment.util.Util.INTERNAL_ERROR; + public class UnbanCommand implements SimpleCommand { private final ExecutorService service; - private final PunishmentManager manager; private final DataSource dataSource; - private final PlayerResolver playerResolver; + private final VelocityPunishmentPlugin plugin; - public UnbanCommand(PunishmentManager punishmentManager, HikariDataSource dataSource, ExecutorService service, PlayerResolver playerResolver) { - this.manager = punishmentManager; - this.dataSource = dataSource; - this.service = service; - this.playerResolver = playerResolver; + public UnbanCommand(VelocityPunishmentPlugin plugin) { + this.dataSource = plugin.getDataSource(); + this.service = plugin.getService(); + this.plugin = plugin; } @Override @@ -46,16 +43,21 @@ public void execute(Invocation invocation) { invocation.source().sendMessage(Component.text("Please use /unban ").color(NamedTextColor.DARK_RED)); return; } - service.execute(() -> { - PunishmentHelper helper = new PunishmentHelper(); - UUID playerUuid = helper.getPlayerUuid(0, service, playerResolver, invocation); - if (playerUuid == null) { - invocation.source().sendMessage(Component.text("This player is not banned at the moment.").color(NamedTextColor.RED)); + var source = invocation.source(); + PunishmentHelper helper = new PunishmentHelper(); + helper.getPlayerUuid(0, service, plugin.getPlayerResolver(), invocation).whenCompleteAsync((uuid, throwable) -> { + if (throwable != null) { + source.sendMessage(INTERNAL_ERROR); + throwable.printStackTrace(); + return; + } + if (uuid == null) { + source.sendMessage(Component.text(invocation.arguments()[0] + " could not be found.")); return; } List punishments; try { - punishments = manager.getPunishments(playerUuid, service, StandardPunishmentType.BAN, StandardPunishmentType.PERMANENT_BAN).get(5, TimeUnit.SECONDS); + punishments = plugin.getPunishmentManager().getPunishments(uuid, service, StandardPunishmentType.BAN, StandardPunishmentType.PERMANENT_BAN).get(5, TimeUnit.SECONDS); } catch (InterruptedException | ExecutionException | TimeoutException e) { e.printStackTrace(); invocation.source().sendMessage(Util.INTERNAL_ERROR); @@ -72,16 +74,16 @@ public void execute(Invocation invocation) { } } else { Punishment punishment = punishments.get(0); - punishment.cancel().whenCompleteAsync((unused, throwable) -> { - if (throwable != null) { - throwable.printStackTrace(); + punishment.cancel().whenCompleteAsync((unused, t) -> { + if (t != null) { + t.printStackTrace(); invocation.source().sendMessage(Util.INTERNAL_ERROR); return; } invocation.source().sendMessage(Component.text("The ban was annulled.").color(NamedTextColor.GREEN)); }); } - }); + }, service); } @Override diff --git a/src/main/java/de/jvstvshd/velocitypunishment/commands/UnmuteCommand.java b/src/main/java/de/jvstvshd/velocitypunishment/commands/UnmuteCommand.java index 05c721ca..75a84e7b 100644 --- a/src/main/java/de/jvstvshd/velocitypunishment/commands/UnmuteCommand.java +++ b/src/main/java/de/jvstvshd/velocitypunishment/commands/UnmuteCommand.java @@ -2,13 +2,11 @@ import com.google.common.collect.ImmutableList; import com.velocitypowered.api.command.SimpleCommand; -import com.zaxxer.hikari.HikariDataSource; +import de.jvstvshd.velocitypunishment.VelocityPunishmentPlugin; import de.jvstvshd.velocitypunishment.listener.ChatListener; import de.jvstvshd.velocitypunishment.punishment.Punishment; import de.jvstvshd.velocitypunishment.punishment.PunishmentHelper; -import de.jvstvshd.velocitypunishment.punishment.PunishmentManager; import de.jvstvshd.velocitypunishment.punishment.StandardPunishmentType; -import de.jvstvshd.velocitypunishment.util.PlayerResolver; import de.jvstvshd.velocitypunishment.util.Util; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.event.ClickEvent; @@ -24,23 +22,22 @@ import java.util.ArrayList; import java.util.List; import java.util.Locale; -import java.util.UUID; import java.util.concurrent.*; +import static de.jvstvshd.velocitypunishment.util.Util.INTERNAL_ERROR; + public class UnmuteCommand implements SimpleCommand { private final ExecutorService service; - private final PunishmentManager manager; private final DataSource dataSource; + private final VelocityPunishmentPlugin plugin; private final ChatListener chatListener; - private final PlayerResolver playerResolver; - public UnmuteCommand(PunishmentManager punishmentManager, HikariDataSource dataSource, ExecutorService service, ChatListener chatListener, PlayerResolver playerResolver) { - this.manager = punishmentManager; - this.dataSource = dataSource; - this.service = service; + public UnmuteCommand(VelocityPunishmentPlugin plugin, ChatListener chatListener) { + this.dataSource = plugin.getDataSource(); + this.service = plugin.getService(); + this.plugin = plugin; this.chatListener = chatListener; - this.playerResolver = playerResolver; } @Override @@ -49,16 +46,21 @@ public void execute(Invocation invocation) { invocation.source().sendMessage(Component.text("Please use /unmute ").color(NamedTextColor.DARK_RED)); return; } - service.execute(() -> { - PunishmentHelper helper = new PunishmentHelper(); - UUID playerUuid = helper.getPlayerUuid(0, service, playerResolver, invocation); - if (playerUuid == null) { - invocation.source().sendMessage(Component.text("This player is not muted at the moment.").color(NamedTextColor.RED)); + var source = invocation.source(); + PunishmentHelper helper = new PunishmentHelper(); + helper.getPlayerUuid(0, service, plugin.getPlayerResolver(), invocation).whenCompleteAsync((uuid, throwable) -> { + if (throwable != null) { + source.sendMessage(INTERNAL_ERROR); + throwable.printStackTrace(); + return; + } + if (uuid == null) { + source.sendMessage(Component.text(invocation.arguments()[0] + " could not be found.")); return; } List punishments; try { - punishments = manager.getPunishments(playerUuid, service, StandardPunishmentType.MUTE, StandardPunishmentType.PERMANENT_MUTE).get(5, TimeUnit.SECONDS); + punishments = plugin.getPunishmentManager().getPunishments(uuid, service, StandardPunishmentType.MUTE, StandardPunishmentType.PERMANENT_MUTE).get(5, TimeUnit.SECONDS); } catch (InterruptedException | ExecutionException | TimeoutException e) { e.printStackTrace(); invocation.source().sendMessage(Util.INTERNAL_ERROR); @@ -75,17 +77,22 @@ public void execute(Invocation invocation) { } } else { Punishment punishment = punishments.get(0); - punishment.cancel().whenCompleteAsync((unused, throwable) -> { - if (throwable != null) { - throwable.printStackTrace(); + punishment.cancel().whenCompleteAsync((unused, t) -> { + if (t != null) { + t.printStackTrace(); invocation.source().sendMessage(Util.INTERNAL_ERROR); return; } invocation.source().sendMessage(Component.text("The ban was annulled.").color(NamedTextColor.GREEN)); - chatListener.update(playerUuid); + try { + chatListener.update(uuid); + } catch (ExecutionException | InterruptedException | TimeoutException e) { + source.sendMessage(INTERNAL_ERROR); + e.printStackTrace(); + } }); } - }); + }, service); } @Override diff --git a/src/main/java/de/jvstvshd/velocitypunishment/config/ConfigData.java b/src/main/java/de/jvstvshd/velocitypunishment/config/ConfigData.java new file mode 100644 index 00000000..2d997f77 --- /dev/null +++ b/src/main/java/de/jvstvshd/velocitypunishment/config/ConfigData.java @@ -0,0 +1,23 @@ +package de.jvstvshd.velocitypunishment.config; + +import java.util.Locale; + +@SuppressWarnings({"FieldMayBeFinal", "FieldCanBeLocal"}) +public class ConfigData { + + private DataBaseData dataBaseData = new DataBaseData(); + private Locale defaultLanguage = Locale.ENGLISH; + private boolean forceUsingDefaultLanguage = false; + + public DataBaseData getDataBaseData() { + return dataBaseData; + } + + public Locale getDefaultLanguage() { + return defaultLanguage; + } + + public boolean isForceUsingDefaultLanguage() { + return forceUsingDefaultLanguage; + } +} diff --git a/src/main/java/de/jvstvshd/velocitypunishment/config/Configuration.java b/src/main/java/de/jvstvshd/velocitypunishment/config/Configuration.java deleted file mode 100644 index f037f0cc..00000000 --- a/src/main/java/de/jvstvshd/velocitypunishment/config/Configuration.java +++ /dev/null @@ -1,11 +0,0 @@ -package de.jvstvshd.velocitypunishment.config; - -@SuppressWarnings("FieldMayBeFinal") -public class Configuration { - - private DataBaseData dataBaseData = new DataBaseData(); - - public DataBaseData getDataBaseData() { - return dataBaseData; - } -} diff --git a/src/main/java/de/jvstvshd/velocitypunishment/config/ConfigurationManager.java b/src/main/java/de/jvstvshd/velocitypunishment/config/ConfigurationManager.java index dc9407db..dfc21b1c 100644 --- a/src/main/java/de/jvstvshd/velocitypunishment/config/ConfigurationManager.java +++ b/src/main/java/de/jvstvshd/velocitypunishment/config/ConfigurationManager.java @@ -11,7 +11,7 @@ public class ConfigurationManager { private final Path path; private final ObjectMapper objectMapper; - private Configuration configuration; + private ConfigData configData; public ConfigurationManager(Path path) { this.path = path; @@ -29,7 +29,7 @@ private void create() throws IOException { } try (FileChannel channel = FileChannel.open(path)) { if (channel.size() <= 0 || write) { - objectMapper.writerWithDefaultPrettyPrinter().writeValues(path.toFile()).write(new Configuration()); + objectMapper.writerWithDefaultPrettyPrinter().writeValues(path.toFile()).write(new ConfigData()); } } @@ -37,14 +37,14 @@ private void create() throws IOException { public void load() throws IOException { create(); - configuration = objectMapper.readValue(path.toFile(), Configuration.class); + configData = objectMapper.readValue(path.toFile(), ConfigData.class); } public void save() throws IOException { - objectMapper.writerWithDefaultPrettyPrinter().writeValues(path.toFile()).write(configuration); + objectMapper.writerWithDefaultPrettyPrinter().writeValues(path.toFile()).write(configData); } - public Configuration getConfiguration() { - return configuration; + public ConfigData getConfiguration() { + return configData; } -} +} \ No newline at end of file diff --git a/src/main/java/de/jvstvshd/velocitypunishment/config/DataBaseData.java b/src/main/java/de/jvstvshd/velocitypunishment/config/DataBaseData.java index bb591d84..0f9ba172 100644 --- a/src/main/java/de/jvstvshd/velocitypunishment/config/DataBaseData.java +++ b/src/main/java/de/jvstvshd/velocitypunishment/config/DataBaseData.java @@ -27,4 +27,6 @@ public String getDatabase() { public String getPort() { return port; } + + } diff --git a/src/main/java/de/jvstvshd/velocitypunishment/listener/ChatListener.java b/src/main/java/de/jvstvshd/velocitypunishment/listener/ChatListener.java index 5c5e07e9..f29b6de4 100644 --- a/src/main/java/de/jvstvshd/velocitypunishment/listener/ChatListener.java +++ b/src/main/java/de/jvstvshd/velocitypunishment/listener/ChatListener.java @@ -4,12 +4,14 @@ import com.velocitypowered.api.event.connection.DisconnectEvent; import com.velocitypowered.api.event.player.PlayerChatEvent; import com.velocitypowered.api.proxy.Player; +import de.jvstvshd.velocitypunishment.VelocityPunishmentPlugin; import de.jvstvshd.velocitypunishment.punishment.Mute; import de.jvstvshd.velocitypunishment.punishment.Punishment; -import de.jvstvshd.velocitypunishment.punishment.PunishmentManager; import de.jvstvshd.velocitypunishment.punishment.StandardPunishmentType; import de.jvstvshd.velocitypunishment.punishment.impl.DefaultMute; import de.jvstvshd.velocitypunishment.util.Util; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; import java.util.HashMap; import java.util.List; @@ -23,12 +25,12 @@ public class ChatListener { private final Map mutes; - private final PunishmentManager punishmentManager; private final ExecutorService service; + private final VelocityPunishmentPlugin plugin; - public ChatListener(PunishmentManager punishmentManager, ExecutorService service) { - this.punishmentManager = punishmentManager; - this.service = service; + public ChatListener(VelocityPunishmentPlugin plugin) { + this.plugin = plugin; + this.service = plugin.getService(); mutes = new HashMap<>(); } @@ -41,9 +43,14 @@ public void onChat(PlayerChatEvent event) { return; } event.setResult(PlayerChatEvent.ChatResult.denied()); - event.getPlayer().sendMessage(container.getMute().createFullReason()); + event.getPlayer().sendMessage(Component.text("Please wait a moment").color(NamedTextColor.RED)); } else { - update(player.getUniqueId()); + try { + update(player.getUniqueId()); + } catch (ExecutionException | InterruptedException | TimeoutException e) { + e.printStackTrace(); + event.getPlayer().sendMessage(Util.INTERNAL_ERROR); + } onChat(event); } } @@ -53,18 +60,14 @@ public void onDisconnect(DisconnectEvent event) { mutes.remove(event.getPlayer().getUniqueId()); } - public void update(UUID uuid) { - try { - List punishments = punishmentManager.getPunishments(uuid, - service, StandardPunishmentType.MUTE, StandardPunishmentType.PERMANENT_MUTE).get(5, TimeUnit.SECONDS); - if (!punishments.isEmpty()) { - Mute mute = Util.getLongestPunishment(Util.convert(punishments)); - mutes.put(uuid, new MuteContainer().setMute(mute)); - } else { - mutes.put(uuid, new MuteContainer()); - } - } catch (InterruptedException | TimeoutException | ExecutionException e) { - e.printStackTrace(); + public void update(UUID uuid) throws ExecutionException, InterruptedException, TimeoutException { + List punishments = plugin.getPunishmentManager().getPunishments(uuid, + service, StandardPunishmentType.MUTE, StandardPunishmentType.PERMANENT_MUTE).get(5, TimeUnit.SECONDS); + if (!punishments.isEmpty()) { + Mute mute = Util.getLongestPunishment(Util.convert(punishments)); + mutes.put(uuid, new MuteContainer().setMute(mute)); + } else { + mutes.put(uuid, new MuteContainer()); } } diff --git a/src/main/java/de/jvstvshd/velocitypunishment/listener/ConnectListener.java b/src/main/java/de/jvstvshd/velocitypunishment/listener/ConnectListener.java index b542a21f..301d2125 100644 --- a/src/main/java/de/jvstvshd/velocitypunishment/listener/ConnectListener.java +++ b/src/main/java/de/jvstvshd/velocitypunishment/listener/ConnectListener.java @@ -50,7 +50,7 @@ public void onConnect(LoginEvent event) { Component deny = ban.createFullReason(); event.setResult(ResultedEvent.ComponentResult.denied(deny)); } else { - service.execute(() -> ban.cancel().whenCompleteAsync((unused, throwable) -> { + ban.cancel().whenCompleteAsync((unused, throwable) -> { if (throwable != null) { throwable.printStackTrace(); return; @@ -60,7 +60,7 @@ public void onConnect(LoginEvent event) { Component.text("'" + ban.getPunishmentUuid().toString().toLowerCase() + "'") .color(NamedTextColor.YELLOW), Component.text("was annulled.").color(NamedTextColor.GREEN))); - })); + }, service); } } } diff --git a/src/main/java/de/jvstvshd/velocitypunishment/message/MessageProvider.java b/src/main/java/de/jvstvshd/velocitypunishment/message/MessageProvider.java new file mode 100644 index 00000000..978feaab --- /dev/null +++ b/src/main/java/de/jvstvshd/velocitypunishment/message/MessageProvider.java @@ -0,0 +1,26 @@ +package de.jvstvshd.velocitypunishment.message; + +import com.velocitypowered.api.proxy.Player; +import net.kyori.adventure.text.Component; +import org.jetbrains.annotations.NotNull; + +public interface MessageProvider { + + @NotNull + Component internalError(); + + @NotNull + Component prefix(); + + @NotNull + Component provide(String key, Player player, Component... args); + + @NotNull + Component provide(String key, Component... args); + + @NotNull + Component internalError(boolean withPrefix); + + @NotNull + Component provide(String key, Player player, boolean withPrefix, Component... args); +} diff --git a/src/main/java/de/jvstvshd/velocitypunishment/message/ResourceBundleMessageProvider.java b/src/main/java/de/jvstvshd/velocitypunishment/message/ResourceBundleMessageProvider.java new file mode 100644 index 00000000..c41939b2 --- /dev/null +++ b/src/main/java/de/jvstvshd/velocitypunishment/message/ResourceBundleMessageProvider.java @@ -0,0 +1,138 @@ +package de.jvstvshd.velocitypunishment.message; + +import com.velocitypowered.api.proxy.Player; +import de.jvstvshd.velocitypunishment.config.ConfigData; +import net.kyori.adventure.key.Key; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.TextComponent; +import net.kyori.adventure.text.format.NamedTextColor; +import net.kyori.adventure.translation.GlobalTranslator; +import net.kyori.adventure.translation.TranslationRegistry; +import net.kyori.adventure.translation.Translator; +import org.jetbrains.annotations.NotNull; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Locale; +import java.util.Objects; +import java.util.PropertyResourceBundle; +import java.util.stream.Stream; + +@SuppressWarnings("ClassCanBeRecord") +public class ResourceBundleMessageProvider implements MessageProvider { + + private final ConfigData configData; + private LocaleProvider localeProvider; + + public ResourceBundleMessageProvider(@NotNull ConfigData configData) { + this.configData = configData; + this.localeProvider = new LocaleProvider(configData); + load(); + } + + private void load() { + try { + var registry = TranslationRegistry.create(Key.key("velocity-punishment")); + registry.defaultLocale(configData.getDefaultLanguage()); + try (Stream paths = Files.list(Path.of("plugins", "velocity-punishment", "translations"))) { + paths.filter(path -> path.getFileName().toString().endsWith(".properties")).forEach(path -> { + PropertyResourceBundle resource; + try { + resource = new PropertyResourceBundle(Files.newInputStream(path)); + var locale = Objects.requireNonNull(Translator.parseLocale(path.getFileName().toString().substring(0, path.getFileName().toString().length() - ".properties".length()))); + registry.registerAll(locale, resource, false); + } catch (IOException e) { + e.printStackTrace(); + } + }); + } + GlobalTranslator.get().addSource(registry); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private Component standard(String key, Component def) { + var rendered = GlobalTranslator.render(Component.translatable(key), configData.getDefaultLanguage()); + if (rendered instanceof TextComponent text && text.content().equalsIgnoreCase(key)) { + return def; + } + return rendered; + } + + @Override + public @NotNull + Component internalError() { + return standard("error.internal", Component.text("An internal error occurred. Please contact the network administrator.") + .color(NamedTextColor.DARK_RED)); + } + + @Override + public @NotNull + Component prefix() { + return standard("prefix", Component.text() + .append(Component.text("[").color(NamedTextColor.GRAY)) + .append(Component.text("Punishment").color(NamedTextColor.YELLOW)) + .append(Component.text("] ").color(NamedTextColor.GRAY)).build()); + } + + + @Override + public @NotNull + Component provide(String key, Player player, Component... args) { + return GlobalTranslator.render(Component.translatable(key, args), localeProvider.provideLocale(player)); + } + + @Override + public @NotNull + Component provide(String key, Component... args) { + Objects.requireNonNull(key, "key may not be null"); + var translatable = Component.translatable(key, args); + if (configData.isForceUsingDefaultLanguage()) { + return GlobalTranslator.render(translatable, configData.getDefaultLanguage()); + } + return translatable; + } + + + private Component withPrefix(Component message) { + Objects.requireNonNull(message); + return Component.text().append(prefix(), message).build(); + } + + @Override + public @NotNull + Component internalError(boolean withPrefix) { + return withPrefix(internalError()); + } + + @Override + public @NotNull + Component provide(String key, Player player, boolean withPrefix, Component... args) { + return withPrefix(provide(key, player, args)); + } + + public LocaleProvider getLocaleProvider() { + return localeProvider; + } + + public void setLocaleProvider(LocaleProvider localeProvider) { + this.localeProvider = localeProvider; + } + + public static class LocaleProvider { + private final ConfigData configData; + + public LocaleProvider(ConfigData configData) { + this.configData = configData; + } + + public Locale provideLocale(Player player) { + if (configData.isForceUsingDefaultLanguage()) { + return configData.getDefaultLanguage(); + } + return player.getEffectiveLocale(); + } + } +} diff --git a/src/main/java/de/jvstvshd/velocitypunishment/punishment/PunishmentHelper.java b/src/main/java/de/jvstvshd/velocitypunishment/punishment/PunishmentHelper.java index 3d7c10d7..6801add3 100644 --- a/src/main/java/de/jvstvshd/velocitypunishment/punishment/PunishmentHelper.java +++ b/src/main/java/de/jvstvshd/velocitypunishment/punishment/PunishmentHelper.java @@ -10,7 +10,8 @@ import java.util.Optional; import java.util.UUID; -import java.util.concurrent.*; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutorService; public class PunishmentHelper { @@ -55,16 +56,6 @@ public Optional parseDuration(int argumentIndex, SimpleComma } } - public Optional parseUuid(PlayerResolver playerResolver, SimpleCommand.Invocation invocation) { - try { - return Optional.ofNullable(playerResolver.getOrQueryPlayerUuid(invocation.arguments()[0], Executors.newSingleThreadExecutor()).get(10, TimeUnit.SECONDS)); - } catch (InterruptedException | ExecutionException | TimeoutException e) { - invocation.source().sendMessage(Component.text().append(Component.text("Cannot parse duration: ").color(NamedTextColor.RED), - Component.text(e.getMessage()).color(NamedTextColor.YELLOW))); - throw new RuntimeException(e); - } - } - public TextComponent parseComponent(int startIndex, SimpleCommand.Invocation invocation) { if (invocation.arguments().length == startIndex) { return Component.text("Ban!").color(NamedTextColor.DARK_RED); @@ -76,18 +67,13 @@ public TextComponent parseComponent(int startIndex, SimpleCommand.Invocation inv return LegacyComponentSerializer.legacyAmpersand().deserialize(builder.toString()); } - public UUID getPlayerUuid(int argumentIndex, ExecutorService service, PlayerResolver playerResolver, SimpleCommand.Invocation invocation) { + public CompletableFuture getPlayerUuid(int argumentIndex, ExecutorService service, PlayerResolver playerResolver, SimpleCommand.Invocation invocation) { String argument = invocation.arguments()[argumentIndex]; if (argument.length() <= 16) { - try { - return playerResolver.getOrQueryPlayerUuid(argument, service).get(5, TimeUnit.SECONDS); - } catch (InterruptedException | TimeoutException | ExecutionException e) { - e.printStackTrace(); - return null; - } + return playerResolver.getOrQueryPlayerUuid(argument, service); } else if (argument.length() <= 36) { try { - return Util.parseUuid(argument); + return CompletableFuture.completedFuture(Util.parseUuid(argument)); } catch (Exception e) { return null; } diff --git a/src/main/java/de/jvstvshd/velocitypunishment/punishment/impl/DefaultBan.java b/src/main/java/de/jvstvshd/velocitypunishment/punishment/impl/DefaultBan.java index b44de7ae..1f11ad48 100644 --- a/src/main/java/de/jvstvshd/velocitypunishment/punishment/impl/DefaultBan.java +++ b/src/main/java/de/jvstvshd/velocitypunishment/punishment/impl/DefaultBan.java @@ -50,6 +50,7 @@ public CompletableFuture punish() { statement.setTimestamp(4, getDuration().timestampExpiration()); statement.setString(5, convertReason(getReason())); statement.setString(6, Util.trimUuid(getPunishmentUuid())); + statement.executeUpdate(); return null; } }, getService()); @@ -61,6 +62,7 @@ public CompletableFuture cancel() { try (Connection connection = getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement(APPLY_ANNUL)) { statement.setString(1, Util.trimUuid(getPunishmentUuid())); + statement.executeUpdate(); return null; } }, getService()); @@ -76,6 +78,7 @@ public CompletableFuture change(PunishmentDuration newDuration, Comp statement.setTimestamp(2, Timestamp.valueOf(newDuration.expiration())); statement.setBoolean(3, newDuration.isPermanent()); statement.setString(4, Util.trimUuid(getPunishmentUuid())); + statement.executeUpdate(); } return new DefaultBan(getPlayerUuid(), newReason, getDataSource(), getPlayerResolver(), getPunishmentManager(), getService(), newDuration); }, getService()); diff --git a/src/main/java/de/jvstvshd/velocitypunishment/punishment/impl/DefaultMute.java b/src/main/java/de/jvstvshd/velocitypunishment/punishment/impl/DefaultMute.java index ff26c193..b5027de9 100644 --- a/src/main/java/de/jvstvshd/velocitypunishment/punishment/impl/DefaultMute.java +++ b/src/main/java/de/jvstvshd/velocitypunishment/punishment/impl/DefaultMute.java @@ -47,6 +47,7 @@ public CompletableFuture punish() { statement.setTimestamp(4, getDuration().timestampExpiration()); statement.setString(5, convertReason(getReason())); statement.setString(6, Util.trimUuid(getPunishmentUuid())); + statement.executeUpdate(); return null; } }, getService()); @@ -59,6 +60,7 @@ public CompletableFuture cancel() { try (Connection connection = getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement(APPLY_ANNUL)) { statement.setString(1, Util.trimUuid(getPunishmentUuid())); + statement.executeUpdate(); return null; } }, getService()); @@ -74,6 +76,7 @@ public CompletableFuture change(PunishmentDuration newDuration, Comp statement.setTimestamp(2, Timestamp.valueOf(newDuration.expiration())); statement.setBoolean(3, newDuration.isPermanent()); statement.setString(4, Util.trimUuid(getPunishmentUuid())); + statement.executeUpdate(); } return new DefaultMute(getPlayerUuid(), newReason, getDataSource(), getPlayerResolver(), getPunishmentManager(), getService(), newDuration); }, getService()); diff --git a/src/main/java/de/jvstvshd/velocitypunishment/punishment/impl/DefaultPunishmentManager.java b/src/main/java/de/jvstvshd/velocitypunishment/punishment/impl/DefaultPunishmentManager.java index 62eef2b3..0658c2fc 100644 --- a/src/main/java/de/jvstvshd/velocitypunishment/punishment/impl/DefaultPunishmentManager.java +++ b/src/main/java/de/jvstvshd/velocitypunishment/punishment/impl/DefaultPunishmentManager.java @@ -14,7 +14,6 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -import java.util.stream.Collectors; import static de.jvstvshd.velocitypunishment.util.Util.executeAsync; @@ -48,7 +47,7 @@ public Mute createMute(UUID player, Component reason, PunishmentDuration duratio @Override public CompletableFuture> getPunishments(UUID player, ExecutorService service, PunishmentType... types) { return executeAsync(() -> { - List typeList = types.length == 0 ? Arrays.stream(StandardPunishmentType.values()).collect(Collectors.toList()) : getTypes(types); + List typeList = types.length == 0 ? Arrays.stream(StandardPunishmentType.values()).toList() : getTypes(types); List punishments = new ArrayList<>(); for (StandardPunishmentType standardPunishmentType : typeList) { try (Connection connection = dataSource.getConnection(); diff --git a/src/main/resources/en.properties b/src/main/resources/en.properties new file mode 100644 index 00000000..7987e758 --- /dev/null +++ b/src/main/resources/en.properties @@ -0,0 +1,2 @@ +commands.general.notfound=The player {0} could not be found. +command.ban.usage=Please use /ban [reason]