Skip to content

Commit

Permalink
improvements & customization
Browse files Browse the repository at this point in the history
Executor#execute has been replaced with CompletableFuture#asyncX calls or there sync equivalents.
To enforce the use of custom punishment managers, player resolvers and message providers, these instances are no longer passed as parameters, but called via getX. Custom instances are hence used when they get set via their associated setters.
A new possibility to provide messages localized and/or customized for each user was introduced with the interface MessageProvider. The default implementation of this interface is ResourceBundleMessageProvider which registers translation sources via the adventure api. In the configuration, the default language can be set which is always used as fallback option and can also be forced to use. A LocaleProvider class is used to provide the player's locale used in provide methods with a player parameter. This instance can be changed via setLocaleProvider to change the way the player's locale is provided.
Default localization resources:
At the moment only a file for english exists with only two translations. Planned is a second file for german and, of course, the replacement of the hard coded messages a variable.
  • Loading branch information
JvstvsHD committed Dec 29, 2021
1 parent fe283fb commit 173734d
Show file tree
Hide file tree
Showing 21 changed files with 448 additions and 226 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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);
}
Expand All @@ -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() {
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,71 +3,71 @@
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;
import net.kyori.adventure.text.format.NamedTextColor;
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 <Player> [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 <player> [reason]").color(NamedTextColor.DARK_RED));
return;
}

PunishmentHelper parser = new PunishmentHelper();
Optional<UUID> 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<String> suggest(Invocation invocation) {
var proxyServer = plugin.getServer();
String[] args = invocation.arguments();
if (args.length == 0) {
return Util.getPlayerNames(proxyServer.getAllPlayers());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,33 @@
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;
import net.kyori.adventure.text.format.NamedTextColor;
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
Expand All @@ -49,15 +39,19 @@ public void execute(Invocation invocation) {
source.sendMessage(Component.text("Please user /mute <player> [reason]").color(NamedTextColor.DARK_RED));
return;
}
service.execute(() -> {
PunishmentHelper parser = new PunishmentHelper();
Optional<UUID> 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;
Expand All @@ -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<String> suggest(Invocation invocation) {
String[] args = invocation.arguments();
var server = plugin.getServer();
if (args.length == 0) {
return Util.getPlayerNames(server.getAllPlayers());
}
Expand Down
Loading

0 comments on commit 173734d

Please sign in to comment.