Skip to content

Commit

Permalink
All return types with CompletableFuture<Boolean> where changed to Com…
Browse files Browse the repository at this point in the history
…pletableFuture<Void>, CompletableFuture#get is not anymore called in commands, instead CompletableFuture#whenCompleteAsync is called.

Methods to retrieve a player's name or uuid were moved to PlayerResolver (default impl: DefaultPlayerResolver).
The version was bumped to 1.0.0-beta.
  • Loading branch information
JvstvsHD committed Nov 14, 2021
1 parent e773b74 commit efef22d
Show file tree
Hide file tree
Showing 24 changed files with 332 additions and 329 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group = "de.jvstvshd.punishment"
version = "1.0.0-SNAPSHOT"
version = "1.0.0-beta"

repositories {
maven(url = "https://nexus.velocitypowered.com/repository/maven-public/")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package de.jvstvshd.velocitypunishment;

import com.google.common.collect.ImmutableMap;
import com.google.inject.Inject;
import com.velocitypowered.api.command.CommandManager;
import com.velocitypowered.api.command.CommandMeta;
import com.velocitypowered.api.command.SimpleCommand;
import com.velocitypowered.api.event.EventManager;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
Expand All @@ -18,7 +15,9 @@
import de.jvstvshd.velocitypunishment.listener.ChatListener;
import de.jvstvshd.velocitypunishment.listener.ConnectListener;
import de.jvstvshd.velocitypunishment.punishment.PunishmentManager;
import de.jvstvshd.velocitypunishment.punishment.impl.StandardPunishmentManager;
import de.jvstvshd.velocitypunishment.punishment.impl.DefaultPlayerResolver;
import de.jvstvshd.velocitypunishment.punishment.impl.DefaultPunishmentManager;
import de.jvstvshd.velocitypunishment.util.PlayerResolver;
import org.slf4j.Logger;

import java.io.IOException;
Expand All @@ -27,8 +26,6 @@
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand All @@ -39,28 +36,27 @@ public class VelocityPunishmentPlugin {
private final ProxyServer server;
private final Logger logger;
private final ConfigurationManager configurationManager;
private final Path dataDirectory;
private PunishmentManager punishmentManager;
private HikariDataSource dataSource;
private final Map<String, SimpleCommand> commandMap = new HashMap<>();
private final PlayerResolver playerResolver;

@Inject
public VelocityPunishmentPlugin(ProxyServer server, Logger logger, @DataDirectory Path dataDirectory) {
this.server = server;
this.logger = logger;
this.dataDirectory = dataDirectory;
this.configurationManager = new ConfigurationManager(Paths.get(dataDirectory.toAbsolutePath().toString(), "config.json"));
this.playerResolver = new DefaultPlayerResolver(server);
}

@Subscribe
public void onProxyInitialization(ProxyInitializeEvent event) throws ReflectiveOperationException {
public void onProxyInitialization(ProxyInitializeEvent event) {
try {
configurationManager.load();
} catch (IOException e) {
logger.error("Could not load configuration", e);
}
dataSource = createDataSource();
punishmentManager = new StandardPunishmentManager(server, dataSource);
punishmentManager = new DefaultPunishmentManager(server, dataSource, playerResolver);
try {
initDataSource();
} catch (SQLException e) {
Expand All @@ -76,17 +72,13 @@ private void setup(CommandManager commandManager, EventManager eventManager) {
eventManager.register(this, new ConnectListener(punishmentManager, Executors.newCachedThreadPool(), server, chatListener));
eventManager.register(this, chatListener);

commandManager.register(commandManager.metaBuilder("ban").build(), new BanCommand(server, punishmentManager, service));
commandManager.register(commandManager.metaBuilder("tempban").build(), new TempbanCommand(punishmentManager, server, service));
commandManager.register(commandManager.metaBuilder("unban").build(), new UnbanCommand(punishmentManager, dataSource, service));
commandManager.register(commandManager.metaBuilder("punishment").build(), new PunishmentCommand(service, punishmentManager, dataSource, server, chatListener));
commandManager.register(commandManager.metaBuilder("mute").build(), new MuteCommand(punishmentManager, server, chatListener, service));
commandManager.register(commandManager.metaBuilder("tempmute").build(), new TempmuteCommand(punishmentManager, server, service));
commandManager.register(commandManager.metaBuilder("unmute").build(), new UnmuteCommand(punishmentManager, dataSource, service, chatListener));
}

public ProxyServer getServer() {
return server;
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));
}

public PunishmentManager getPunishmentManager() {
Expand Down Expand Up @@ -114,13 +106,4 @@ private void initDataSource() throws SQLException {
statement.execute();
}
}

@Deprecated(forRemoval = true)
private void registerCommand(CommandMeta meta, SimpleCommand command) {
server.getCommandManager().register(meta, command);
}

public Map<String, SimpleCommand> getCommandMap() {
return ImmutableMap.copyOf(commandMap);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.velocitypowered.api.proxy.ProxyServer;
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;
Expand All @@ -15,8 +16,6 @@
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.copyComponent;
Expand All @@ -25,12 +24,12 @@ public class BanCommand implements SimpleCommand {

private final ProxyServer proxyServer;
private final PunishmentManager punishmentManager;
private final ExecutorService service;
private final PlayerResolver playerResolver;

public BanCommand(ProxyServer proxyServer, PunishmentManager punishmentManager, ExecutorService service) {
public BanCommand(ProxyServer proxyServer, PunishmentManager punishmentManager, PlayerResolver playerResolver) {
this.proxyServer = proxyServer;
this.punishmentManager = punishmentManager;
this.service = service;
this.playerResolver = playerResolver;
}

@Override
Expand All @@ -41,32 +40,30 @@ public void execute(Invocation invocation) {
return;
}

service.execute(() -> {
PunishmentHelper parser = new PunishmentHelper();
Optional<UUID> optionalUUID = parser.parseUuid(punishmentManager, 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);
try {
punishmentManager.createPermanentBan(uuid, component).punish().get();
} catch (InterruptedException | ExecutionException e) {
invocation.source().sendMessage(Util.INTERNAL_ERROR);
e.printStackTrace();
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) -> {
if (throwable != null) {
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))));
}
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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;
Expand All @@ -31,12 +32,14 @@ public class MuteCommand implements SimpleCommand {
private final ProxyServer server;
private final ChatListener chatListener;
private final ExecutorService service;
private final PlayerResolver playerResolver;

public MuteCommand(PunishmentManager punishmentManager, ProxyServer server, ChatListener chatListener, ExecutorService service) {
public MuteCommand(PunishmentManager punishmentManager, ProxyServer server, ChatListener chatListener, ExecutorService service, PlayerResolver playerResolver) {
this.punishmentManager = punishmentManager;
this.server = server;
this.chatListener = chatListener;
this.service = service;
this.playerResolver = playerResolver;
}

@Override
Expand All @@ -48,7 +51,7 @@ public void execute(Invocation invocation) {
}
service.execute(() -> {
PunishmentHelper parser = new PunishmentHelper();
Optional<UUID> optionalUUID = parser.parseUuid(punishmentManager, invocation);
Optional<UUID> optionalUUID = parser.parseUuid(playerResolver, invocation);
UUID uuid;
if (optionalUUID.isEmpty()) {
source.sendMessage(Component.text(invocation.arguments()[0] + " could not be found."));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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;
Expand All @@ -31,13 +32,15 @@ public class PunishmentCommand implements SimpleCommand {
private final DataSource dataSource;
private final ProxyServer server;
private final ChatListener chatListener;
private final PlayerResolver playerResolver;

public PunishmentCommand(ExecutorService service, PunishmentManager punishmentManager, DataSource dataSource, ProxyServer server, ChatListener chatListener) {
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;
this.chatListener = chatListener;
this.playerResolver = playerResolver;
}

private final static List<String> options = ImmutableList.of("annul", "remove", "info", "change");
Expand All @@ -53,7 +56,7 @@ public void execute(Invocation invocation) {
if (arguments[0].equalsIgnoreCase("playerinfo")) {
service.execute(() -> {
PunishmentHelper helper = new PunishmentHelper();
UUID playerUuid = helper.getPlayerUuid(1, service, punishmentManager, invocation);
UUID playerUuid = helper.getPlayerUuid(1, service, playerResolver, invocation);
if (playerUuid == null) {
invocation.source().sendMessage(Component.text("This player is not banned at the moment.").color(NamedTextColor.RED));
return;
Expand Down Expand Up @@ -81,7 +84,7 @@ public void execute(Invocation invocation) {
}
UUID uuid;
try {
uuid = Util.parse(arguments[0]);
uuid = Util.parseUuid(arguments[0]);
} catch (IllegalArgumentException e) {
source.sendMessage(Component.text().append(Component.text("Could not parse string '").color(NamedTextColor.RED),
Component.text(arguments[0]).color(NamedTextColor.YELLOW),
Expand All @@ -95,47 +98,32 @@ public void execute(Invocation invocation) {
Component.text(option).color(NamedTextColor.YELLOW)));
return;
}
service.execute(() -> {
Punishment punishment;
try {
Optional<? extends Punishment> optionalPunishment = punishmentManager.getPunishment(uuid, service).get(5, TimeUnit.SECONDS);
if (optionalPunishment.isEmpty()) {
source.sendMessage(Component.text().append(Component.text("Could not find a punishment for id '").color(NamedTextColor.RED),
Component.text(uuid.toString().toLowerCase(Locale.ROOT)).color(NamedTextColor.YELLOW),
Component.text("'.").color(NamedTextColor.RED)));
return;
}
punishment = optionalPunishment.get();
} catch (InterruptedException | TimeoutException | ExecutionException e) {
e.printStackTrace();
punishmentManager.getPunishment(uuid, service).whenCompleteAsync((optional, throwable) -> {
if (throwable != null) {
throwable.printStackTrace();
source.sendMessage(Util.INTERNAL_ERROR);
return;
}
Punishment punishment;
if (optional.isEmpty()) {
source.sendMessage(Component.text().append(Component.text("Could not find a punishment for id '").color(NamedTextColor.RED),
Component.text(uuid.toString().toLowerCase(Locale.ROOT)).color(NamedTextColor.YELLOW),
Component.text("'.").color(NamedTextColor.RED)));
return;
}
punishment = optional.get();
switch (option) {
case "annul":
case "remove":
try {
if (punishment.annul().get(5, TimeUnit.SECONDS)) {
source.sendMessage(Component.text("The punishment was successfully annulled.").color(NamedTextColor.GREEN));
chatListener.update(uuid);
} else {
source.sendMessage(Component.text().append(Component.text("Could not annul punishment for id '").color(NamedTextColor.RED),
Component.text(uuid.toString().toLowerCase()).color(NamedTextColor.YELLOW),
Component.text("'.").color(NamedTextColor.RED)));
return;
}
} catch (Exception e) {
e.printStackTrace();
case "annul", "remove" -> punishment.cancel().whenCompleteAsync((unused, t) -> {
if (t != null) {
t.printStackTrace();
source.sendMessage(Util.INTERNAL_ERROR);
return;
}
break;
case "info":
source.sendMessage(new PunishmentHelper().buildPunishmentData(punishment));
break;
case "change":
source.sendMessage(Component.text("Soon™️"));
break;
source.sendMessage(Component.text("The punishment was successfully annulled.").color(NamedTextColor.GREEN));
chatListener.update(uuid);
});
case "info" -> source.sendMessage(new PunishmentHelper().buildPunishmentData(punishment));
case "change" -> source.sendMessage(Component.text("Soon™️"));
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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;
Expand All @@ -28,11 +29,13 @@ public class TempbanCommand implements SimpleCommand {
private final PunishmentManager punishmentManager;
private final ProxyServer proxyServer;
private final ExecutorService service;
private final PlayerResolver playerResolver;

public TempbanCommand(PunishmentManager punishmentManager, ProxyServer proxyServer, ExecutorService service) {
public TempbanCommand(PunishmentManager punishmentManager, ProxyServer proxyServer, ExecutorService service, PlayerResolver playerResolver) {
this.punishmentManager = punishmentManager;
this.proxyServer = proxyServer;
this.service = service;
this.playerResolver = playerResolver;
}

@Override
Expand All @@ -44,7 +47,7 @@ public void execute(Invocation invocation) {
}
service.execute(() -> {
PunishmentHelper parser = new PunishmentHelper();
Optional<UUID> optionalUUID = parser.parseUuid(punishmentManager, invocation);
Optional<UUID> optionalUUID = parser.parseUuid(playerResolver, invocation);
UUID uuid;
if (optionalUUID.isEmpty()) {
source.sendMessage(Component.text(invocation.arguments()[0] + " could not be found."));
Expand Down
Loading

0 comments on commit efef22d

Please sign in to comment.