From bba5a66cfc79bf829c4765ca1dca67e83afa6631 Mon Sep 17 00:00:00 2001 From: Tom Date: Mon, 13 May 2024 21:01:29 +0200 Subject: [PATCH 1/6] Added velocity support --- pom.xml | 1 + velocity/pom.xml | 115 +++++ velocity/src/main/java/VersionInfo.java | 7 + .../advancedban/velocity/VelocityMain.java | 64 +++ .../advancedban/velocity/VelocityMethods.java | 395 ++++++++++++++++++ .../velocity/event/PunishmentEvent.java | 31 ++ .../velocity/event/RevokePunishmentEvent.java | 33 ++ .../listener/ChatListenerVelocity.java | 20 + .../listener/CommandReceiverVelocity.java | 45 ++ .../listener/ConnectionListenerVelocity.java | 23 + .../velocity/utils/LuckPermsOfflineUser.java | 28 ++ 11 files changed, 762 insertions(+) create mode 100644 velocity/pom.xml create mode 100644 velocity/src/main/java/VersionInfo.java create mode 100644 velocity/src/main/java/me/leoko/advancedban/velocity/VelocityMain.java create mode 100644 velocity/src/main/java/me/leoko/advancedban/velocity/VelocityMethods.java create mode 100644 velocity/src/main/java/me/leoko/advancedban/velocity/event/PunishmentEvent.java create mode 100644 velocity/src/main/java/me/leoko/advancedban/velocity/event/RevokePunishmentEvent.java create mode 100644 velocity/src/main/java/me/leoko/advancedban/velocity/listener/ChatListenerVelocity.java create mode 100644 velocity/src/main/java/me/leoko/advancedban/velocity/listener/CommandReceiverVelocity.java create mode 100644 velocity/src/main/java/me/leoko/advancedban/velocity/listener/ConnectionListenerVelocity.java create mode 100644 velocity/src/main/java/me/leoko/advancedban/velocity/utils/LuckPermsOfflineUser.java diff --git a/pom.xml b/pom.xml index a2e99318..3d2d41d9 100644 --- a/pom.xml +++ b/pom.xml @@ -14,6 +14,7 @@ bukkit bungee bundle + velocity diff --git a/velocity/pom.xml b/velocity/pom.xml new file mode 100644 index 00000000..10032abc --- /dev/null +++ b/velocity/pom.xml @@ -0,0 +1,115 @@ + + + 4.0.0 + + + me.leoko.advancedban + AdvancedBan + 2.4.1 + + + AdvancedBan-Velocity + jar + + + + velocity + https://nexus.velocitypowered.com/repository/maven-public/ + + + + + + com.velocitypowered + velocity-api + 1.1.3 + provided + + + me.leoko.advancedban + AdvancedBan-Core + ${project.version} + compile + + + net.luckperms + api + 5.2 + provided + + + org.mariadb.jdbc + mariadb-java-client + 2.7.0 + + + org.slf4j + slf4j-api + + 1.7.30 + provided + + + + + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.3 + + + + org.apache.commons + ${project.groupId}.shaded.org.apache.commons + + + com.zaxxer.hikari + ${project.groupId}.shaded.com.zaxxer.hikari + + + org.hsqldb + ${project.groupId}.shaded.org.hsqldb + + + org.mariadb.jdbc + ${project.groupId}.shaded.org.mariadb.jdbc + + + + + + + + + package + + shade + + + + + + org.codehaus.mojo + templating-maven-plugin + 1.0.0 + + + filter-src + + filter-sources + + + ${basedir}/src/main/java-templates + ${project.build.directory}/generated-sources/java-templates + + + + + + + + \ No newline at end of file diff --git a/velocity/src/main/java/VersionInfo.java b/velocity/src/main/java/VersionInfo.java new file mode 100644 index 00000000..5958b99e --- /dev/null +++ b/velocity/src/main/java/VersionInfo.java @@ -0,0 +1,7 @@ +package me.leoko.advancedban; + +public class VersionInfo { + + public static final String VERSION = "${project.version}"; + +} \ No newline at end of file diff --git a/velocity/src/main/java/me/leoko/advancedban/velocity/VelocityMain.java b/velocity/src/main/java/me/leoko/advancedban/velocity/VelocityMain.java new file mode 100644 index 00000000..be5f1927 --- /dev/null +++ b/velocity/src/main/java/me/leoko/advancedban/velocity/VelocityMain.java @@ -0,0 +1,64 @@ +package me.leoko.advancedban.velocity; + +import com.google.inject.Inject; +import com.velocitypowered.api.event.proxy.ProxyInitializeEvent; +import com.velocitypowered.api.event.Subscribe; +import com.velocitypowered.api.event.proxy.ProxyShutdownEvent; +import com.velocitypowered.api.plugin.Dependency; +import com.velocitypowered.api.plugin.Plugin; +import com.velocitypowered.api.plugin.annotation.DataDirectory; +import com.velocitypowered.api.proxy.ProxyServer; +import me.leoko.advancedban.velocity.listener.ChatListenerVelocity; +import me.leoko.advancedban.velocity.listener.ConnectionListenerVelocity; +import org.slf4j.Logger; +import me.leoko.advancedban.Universal; + +import java.nio.file.Path; + + +@Plugin( + id = "advancedban", + name = "AdvancedBan", + version = "1.0-SNAPSHOT", + dependencies = { + @Dependency(id = "luckperms", optional = true) + } +) +public class VelocityMain { + + @Inject + private final Logger logger; + private final Path dataDirectory; + private final ProxyServer server; + + private static VelocityMain velocityMain; + + @Inject + public VelocityMain(Logger logger, @DataDirectory Path dataDirectory, ProxyServer server) { + velocityMain = this; + this.logger = logger; + this.dataDirectory = dataDirectory; + this.server = server; + } + + + + @Subscribe + public void onProxyInitialization(ProxyInitializeEvent event) { + Universal.get().setup(new VelocityMethods(dataDirectory, server, logger)); + + server.getEventManager().register(this, new ChatListenerVelocity()); + server.getEventManager().register(this, new ConnectionListenerVelocity()); + + } + + @Subscribe + public void onProxyShutdown(ProxyShutdownEvent event) { + Universal.get().shutdown(); + } + + + public static VelocityMain get() { + return velocityMain; + } +} diff --git a/velocity/src/main/java/me/leoko/advancedban/velocity/VelocityMethods.java b/velocity/src/main/java/me/leoko/advancedban/velocity/VelocityMethods.java new file mode 100644 index 00000000..b9385c05 --- /dev/null +++ b/velocity/src/main/java/me/leoko/advancedban/velocity/VelocityMethods.java @@ -0,0 +1,395 @@ +package me.leoko.advancedban.velocity; + +import com.google.common.reflect.TypeToken; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import com.velocitypowered.api.command.CommandMeta; +import com.velocitypowered.api.command.CommandSource; +import com.velocitypowered.api.proxy.Player; +import com.velocitypowered.api.proxy.ProxyServer; +import me.leoko.advancedban.AbstractMethodInterface; +import me.leoko.advancedban.ServerType; +import me.leoko.advancedban.Universal; +import me.leoko.advancedban.VersionInfo; +import me.leoko.advancedban.manager.PunishmentManager; +import me.leoko.advancedban.manager.UUIDManager; +import me.leoko.advancedban.utils.Permissionable; +import me.leoko.advancedban.utils.Punishment; +import me.leoko.advancedban.utils.tabcompletion.TabCompleter; +import me.leoko.advancedban.velocity.listener.CommandReceiverVelocity; +import me.leoko.advancedban.velocity.event.PunishmentEvent; +import me.leoko.advancedban.velocity.event.RevokePunishmentEvent; +import me.leoko.advancedban.velocity.utils.LuckPermsOfflineUser; +import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; +import ninja.leaping.configurate.ConfigurationNode; +import ninja.leaping.configurate.objectmapping.ObjectMappingException; +import ninja.leaping.configurate.yaml.YAMLConfigurationLoader; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.slf4j.Logger; + +import java.io.File; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; +import java.nio.file.Path; +import java.util.List; +import java.util.Optional; +import java.util.UUID; +import java.util.concurrent.TimeUnit; + +public class VelocityMethods extends AbstractMethodInterface { + + private final ProxyServer server; + private final Path dataDirectory; + private final Logger logger; + + private boolean luckPermsSupport; + + public VelocityMethods(Path dataDirectory, + final @NonNull ProxyServer server, + Logger logger) { + super(dataDirectory); + this.dataDirectory = dataDirectory; + this.server = server; + this.logger = logger; + + if (server.getPluginManager().getPlugin("luckperms").isPresent()) { + luckPermsSupport = true; + log("[AdvancedBan] Offline permission support through LuckPerms active"); + } else { + luckPermsSupport = false; + log("[AdvancedBan] No offline permission support through LuckPerms"); + } + + } + + + @Override + protected ConfigurationNode loadConfiguration(Path configPath) throws IOException { + YAMLConfigurationLoader loader = YAMLConfigurationLoader.builder().setPath(configPath).build(); + + //YamlConfigurationLoader loader = YamlConfigurationLoader.builder().path(configPath).build(); + return loader.load(); + } + + @Override + public String getFromUrlJson(String url, String key) { + try { + HttpURLConnection request = (HttpURLConnection) new URL(url).openConnection(); + request.connect(); + + JsonObject json = (JsonObject) JsonParser.parseReader(new InputStreamReader(request.getInputStream())); + + String[] keys = key.split("\\|"); + for (int i = 0; i < keys.length - 1; i++) { + json = json.getAsJsonObject(keys[i]); + } + + return json.get(keys[keys.length - 1]).toString().replaceAll("\"", ""); + + } catch (Exception exc) { + return null; + } + } + + @Override + public String getVersion() { + return VersionInfo.VERSION; + } + + // Object.toString() is considered safe in this scenario (as per Configurate Docs) + @Override + public String[] getKeys(Object file, String path) { + return ((ConfigurationNode)file).getNode(path.split("\\.")).getChildrenMap().keySet().stream().map(Object::toString).toArray(String[]::new); + } + + @Override + public void setupMetrics() { + // no bstats for velocity yet + } + + + @Override @Deprecated + public boolean isBungee() { + return false; + } + + @Override + public boolean isProxy() { + return true; + } + + @Override + public ServerType getServerType() { + return ServerType.VELOCITY; + } + + @Override + public String clearFormatting(String text) { + return text.replaceAll("(&[^\\s])+", ""); + } + + @Override + public Object getPlugin() { + return VelocityMain.get(); + } + + @Override + public File getDataFolder() { + return dataDirectory.toFile(); + } + + @Override + public void setCommandExecutor(String cmd, TabCompleter tabCompleter) { + CommandMeta meta = server.getCommandManager().metaBuilder(cmd).build(); + server.getCommandManager().register(meta, new CommandReceiverVelocity(server, cmd)); + } + + @Override + public void sendMessage(Object player, String msg) { + ((CommandSource) player).sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(msg)); + } + + @Override + public String getName(Object player) { + if (player instanceof Player) { + return ((Player) player).getUsername(); + } else { + return "CONSOLE"; + } + } + + @Override + public String getName(String uuid) { + return server.getPlayer(UUID.fromString(uuid)).get().getUsername(); + } + + @Override + public String getIP(Object player) { + return ((Player)player).getRemoteAddress().getHostName(); + } + + @Override + public String getInternUUID(Object player) { + return player instanceof Player ? ((Player) player).getUniqueId().toString().replace("-", "") : "none"; + } + + @Override + public String getInternUUID(String player) { + Optional optionalPlayer = server.getPlayer(player); + return optionalPlayer.map(value -> value.getUniqueId().toString().replace("-", "")).orElse(null); + } + + @Override + public boolean hasPerms(Object player, String perms) { + return ((CommandSource) player).hasPermission(perms); + } + + @Override + public Permissionable getOfflinePermissionPlayer(String name) { + if(luckPermsSupport) return new LuckPermsOfflineUser(name); + + return permission -> false; + } + + @Override + public boolean isOnline(String name) { + return server.getPlayer(name).isPresent(); + } + + @Override + public Player getPlayer(String name) { + return server.getPlayer(name).orElse(null); + } + + @Override + public void kickPlayer(String player, String reason) { + server.getPlayer(player).get().disconnect(LegacyComponentSerializer.legacyAmpersand().deserialize(reason)); + } + + @Override + public Player[] getOnlinePlayers() { + return server.getAllPlayers().toArray(new Player[]{}); + } + + @Override + public void scheduleAsyncRep(Runnable rn, long l1, long l2) { + server.getScheduler().buildTask(getPlugin(), rn).delay(l1*50, TimeUnit.MILLISECONDS).repeat(l2*50, TimeUnit.MILLISECONDS).schedule(); + } + + @Override + public void scheduleAsync(Runnable rn, long l1) { + server.getScheduler().buildTask(getPlugin(), rn).delay(l1*50, TimeUnit.MILLISECONDS).schedule(); + } + + @Override + public void runAsync(Runnable rn) { + server.getScheduler().buildTask(getPlugin(), rn).schedule(); + } + + @Override + public void runSync(Runnable rn) { + rn.run(); + } + + @Override + public void executeCommand(String cmd) { + server.getCommandManager().executeAsync(server.getConsoleCommandSource(), cmd).exceptionally((ex) -> { + ex.printStackTrace(); + return null; + }); + } + + @Override + public boolean callChat(Object player) { + Punishment pnt = PunishmentManager.get().getMute(UUIDManager.get().getUUID(getName(player))); + if (pnt != null) { + for (String str : pnt.getLayout()) { + sendMessage(player, str); + } + return true; + } + return false; + } + + @Override + public boolean callCMD(Object player, String cmd) { + Punishment pnt; + if (Universal.get().isMuteCommand(cmd.substring(1)) + && (pnt = PunishmentManager.get().getMute(UUIDManager.get().getUUID(getName(player)))) != null) { + for (String str : pnt.getLayout()) { + sendMessage(player, str); + } + return true; + } + return false; + } + + @Override + public String parseJSON(InputStreamReader json, String key) { + JsonObject element; + try { + element = JsonParser.parseReader(json).getAsJsonObject(); + } catch (IllegalStateException e) { + return null; + } + JsonElement obj = element.get(key); + return obj != null ? obj.toString().replaceAll("\"", "") : null; + } + + @Override + public String parseJSON(String json, String key) { + JsonObject element; + try { + element = (JsonObject) JsonParser.parseString(json).getAsJsonObject(); + } catch (IllegalStateException e) { + return null; + } + JsonElement obj = element.get(key); + return obj != null ? obj.toString().replaceAll("\"", "") : null; + } + + @Override + public Boolean getBoolean(Object file, String path) { + return getConfigNode(file, path).getBoolean(); + } + + @Override + public String getString(Object file, String path) { + return getConfigNode(file, path).getString(); + } + + @Override + public Long getLong(Object file, String path) { + return getConfigNode(file, path).getLong(); + } + + @Override + public Integer getInteger(Object file, String path) { + return getConfigNode(file, path).getInt(); + } + + @Override + public List getStringList(Object file, String path) { + try { + return getConfigNode(file, path).getList(TypeToken.of(String.class)); + } catch (ObjectMappingException e) { + throw new RuntimeException(e); + } + } + + @Override + public boolean getBoolean(Object file, String path, boolean def) { + return getConfigNode(file, path).getBoolean(def); + } + + @Override + public String getString(Object file, String path, String def) { + return getConfigNode(file, path).getString(def); + } + + @Override + public long getLong(Object file, String path, long def) { + return getConfigNode(file, path).getLong(def); + } + + @Override + public int getInteger(Object file, String path, int def) { + return getConfigNode(file, path).getInt(def); + } + + @Override + public boolean contains(Object file, String path) { + return !getConfigNode(file, path).isEmpty(); + } + + @Override + public String getFileName(Object file) { + return "[Only Available in the Bukkit Version!]"; + } + + @Override + public void callPunishmentEvent(Punishment punishment) { + callPunishmentEvent(punishment, false); + } + + @Override + public void callPunishmentEvent(Punishment punishment, boolean silent) { + server.getEventManager().fireAndForget(new PunishmentEvent(punishment, silent)); + } + + @Override + public void callRevokePunishmentEvent(Punishment punishment, boolean massClear) { + server.getEventManager().fireAndForget(new RevokePunishmentEvent(punishment, massClear)); + } + + @Override + public boolean isOnlineMode() { + return server.getConfiguration().isOnlineMode(); + } + + @Override + public void notify(String perm, List notification) { + server.getAllPlayers().forEach(player -> { + if (player.hasPermission(perm)) { + notification.forEach(str -> sendMessage(player, str)); + } + }); + } + + @Override + public void log(String msg) { + server.getConsoleCommandSource().sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(msg)); + } + + @Override + public boolean isUnitTesting() { + return false; + } + + private ConfigurationNode getConfigNode(Object file, String path) { + return ((ConfigurationNode) file).getNode((Object[]) path.split("\\.")); + } +} \ No newline at end of file diff --git a/velocity/src/main/java/me/leoko/advancedban/velocity/event/PunishmentEvent.java b/velocity/src/main/java/me/leoko/advancedban/velocity/event/PunishmentEvent.java new file mode 100644 index 00000000..365924c6 --- /dev/null +++ b/velocity/src/main/java/me/leoko/advancedban/velocity/event/PunishmentEvent.java @@ -0,0 +1,31 @@ +package me.leoko.advancedban.velocity.event; + +import me.leoko.advancedban.utils.Punishment; +import org.checkerframework.checker.nullness.qual.NonNull; + +public class PunishmentEvent { + + private final Punishment punishment; + + private final boolean silent; + + public PunishmentEvent(@NonNull Punishment punishment) { + this(punishment, false); + } + + public PunishmentEvent(@NonNull Punishment punishment, boolean silent) { + this.punishment = punishment; + this.silent = silent; + } + + + public Punishment getPunishment() { + return punishment; + } + + public boolean isSilent() { + return silent; + } + + +} diff --git a/velocity/src/main/java/me/leoko/advancedban/velocity/event/RevokePunishmentEvent.java b/velocity/src/main/java/me/leoko/advancedban/velocity/event/RevokePunishmentEvent.java new file mode 100644 index 00000000..f1f8dccd --- /dev/null +++ b/velocity/src/main/java/me/leoko/advancedban/velocity/event/RevokePunishmentEvent.java @@ -0,0 +1,33 @@ +package me.leoko.advancedban.velocity.event; + +import me.leoko.advancedban.utils.Punishment; +import org.checkerframework.checker.nullness.qual.NonNull; + +public class RevokePunishmentEvent { + private final Punishment punishment; + private final boolean massClear; + + public RevokePunishmentEvent(@NonNull Punishment punishment, boolean massClear) { + this.punishment = punishment; + this.massClear = massClear; + } + + /** + * Returns the punishment involved in this event + * + * @return Punishment + */ + public Punishment getPunishment() { + return punishment; + } + + /** + * If this event is part of a mass clearing of punishments + *

Useful to reduce spam/noise

+ * + * @return True if part of a mass clearing + */ + public boolean isMassClear() { + return massClear; + } +} diff --git a/velocity/src/main/java/me/leoko/advancedban/velocity/listener/ChatListenerVelocity.java b/velocity/src/main/java/me/leoko/advancedban/velocity/listener/ChatListenerVelocity.java new file mode 100644 index 00000000..aab8c28d --- /dev/null +++ b/velocity/src/main/java/me/leoko/advancedban/velocity/listener/ChatListenerVelocity.java @@ -0,0 +1,20 @@ +package me.leoko.advancedban.velocity.listener; + +import com.velocitypowered.api.event.PostOrder; +import com.velocitypowered.api.event.Subscribe; +import com.velocitypowered.api.event.player.PlayerChatEvent; +import me.leoko.advancedban.Universal; + +public class ChatListenerVelocity { + + @Subscribe(order = PostOrder.FIRST) + public void onChat(PlayerChatEvent event) { + if (!event.getMessage().startsWith("/")) { + if (Universal.get().getMethods().callChat(event.getPlayer())) { + event.setResult(PlayerChatEvent.ChatResult.denied()); + } + } else if (Universal.get().getMethods().callCMD(event.getPlayer(), event.getMessage())) { + event.setResult(PlayerChatEvent.ChatResult.denied()); + } + } +} diff --git a/velocity/src/main/java/me/leoko/advancedban/velocity/listener/CommandReceiverVelocity.java b/velocity/src/main/java/me/leoko/advancedban/velocity/listener/CommandReceiverVelocity.java new file mode 100644 index 00000000..6ef986ed --- /dev/null +++ b/velocity/src/main/java/me/leoko/advancedban/velocity/listener/CommandReceiverVelocity.java @@ -0,0 +1,45 @@ +package me.leoko.advancedban.velocity.listener; + +import com.velocitypowered.api.command.CommandSource; +import com.velocitypowered.api.command.SimpleCommand; +import com.velocitypowered.api.proxy.ProxyServer; +import me.leoko.advancedban.Universal; +import me.leoko.advancedban.manager.CommandManager; +import me.leoko.advancedban.utils.Command; + +import java.util.Collections; +import java.util.List; + +public class CommandReceiverVelocity implements SimpleCommand { + + private final ProxyServer server; + private final String cmd; + + public CommandReceiverVelocity(ProxyServer server, String cmd) { + this.cmd = cmd; + this.server = server; + } + + @Override + public void execute(Invocation invocation) { + String[] args = invocation.arguments(); + CommandSource commandSource = invocation.source(); + if (invocation.arguments().length > 0) { + args[0] = (this.server.getPlayer(args[0]).isPresent() ? this.server.getPlayer(args[0]).get().getUsername() : args[0]); + } + CommandManager.get().onCommand(commandSource, cmd, args); + } + + @Override + public List suggest(Invocation invocation) { + String[] args = invocation.arguments(); + CommandSource source = invocation.source(); + final Command command = Command.getByName(cmd); + if (command != null) { + if (command.getPermission() == null || Universal.get().getMethods().hasPerms(source, command.getPermission())) { + return command.getTabCompleter().onTabComplete(source, args); + } + } + return Collections.emptyList(); + } +} diff --git a/velocity/src/main/java/me/leoko/advancedban/velocity/listener/ConnectionListenerVelocity.java b/velocity/src/main/java/me/leoko/advancedban/velocity/listener/ConnectionListenerVelocity.java new file mode 100644 index 00000000..be61225c --- /dev/null +++ b/velocity/src/main/java/me/leoko/advancedban/velocity/listener/ConnectionListenerVelocity.java @@ -0,0 +1,23 @@ +package me.leoko.advancedban.velocity.listener; + +import com.velocitypowered.api.event.ResultedEvent; +import com.velocitypowered.api.event.Subscribe; +import com.velocitypowered.api.event.connection.LoginEvent; +import com.velocitypowered.api.proxy.Player; +import me.leoko.advancedban.Universal; +import me.leoko.advancedban.manager.UUIDManager; +import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; + +public class ConnectionListenerVelocity { + + @Subscribe + public void onLogin(LoginEvent event) { + Player player = event.getPlayer(); + UUIDManager.get().supplyInternUUID(player.getUsername(), player.getUniqueId()); + String result = Universal.get().callConnection(player.getUsername(), player.getRemoteAddress().getAddress().getHostAddress()); + if (result != null) { + event.setResult(ResultedEvent.ComponentResult.denied(LegacyComponentSerializer.legacyAmpersand().deserialize(result))); + } + } + +} diff --git a/velocity/src/main/java/me/leoko/advancedban/velocity/utils/LuckPermsOfflineUser.java b/velocity/src/main/java/me/leoko/advancedban/velocity/utils/LuckPermsOfflineUser.java new file mode 100644 index 00000000..ec094844 --- /dev/null +++ b/velocity/src/main/java/me/leoko/advancedban/velocity/utils/LuckPermsOfflineUser.java @@ -0,0 +1,28 @@ +package me.leoko.advancedban.velocity.utils; + + +import me.leoko.advancedban.utils.Permissionable; +import net.luckperms.api.LuckPermsProvider; +import net.luckperms.api.model.user.User; +import net.luckperms.api.model.user.UserManager; + +import java.util.UUID; + +public class LuckPermsOfflineUser implements Permissionable { + + private User permissionUser; + + public LuckPermsOfflineUser(String name) { + final UserManager userManager = LuckPermsProvider.get().getUserManager(); + final UUID uuid = userManager.lookupUniqueId(name).join(); + if(uuid != null) { + this.permissionUser = userManager.loadUser(uuid).join(); + } + } + + + @Override + public boolean hasPermission(String permission) { + return permissionUser != null && permissionUser.getCachedData().getPermissionData().checkPermission(permission).asBoolean(); + } +} From 9c900f8d33751facab08ceac38404635ce8f38dc Mon Sep 17 00:00:00 2001 From: Tom Date: Wed, 19 Jun 2024 22:30:56 +0200 Subject: [PATCH 2/6] Velocity integration --- velocity/pom.xml | 91 ++---- velocity/src/main/java/VersionInfo.java | 7 - .../advancedban/velocity/VelocityMain.java | 28 +- .../advancedban/velocity/VelocityMethods.java | 299 ++++++++++-------- .../velocity/event/PunishmentEvent.java | 11 +- .../velocity/event/RevokePunishmentEvent.java | 2 +- .../listener/ChatListenerVelocity.java | 1 + .../listener/CommandReceiverVelocity.java | 1 - .../velocity/utils/LuckPermsOfflineUser.java | 6 +- 9 files changed, 209 insertions(+), 237 deletions(-) delete mode 100644 velocity/src/main/java/VersionInfo.java diff --git a/velocity/pom.xml b/velocity/pom.xml index 10032abc..4386d4f4 100644 --- a/velocity/pom.xml +++ b/velocity/pom.xml @@ -7,16 +7,28 @@ me.leoko.advancedban AdvancedBan - 2.4.1 + 2.3.0 - AdvancedBan-Velocity + + AdvancedBan-Proxy jar + + 11 + UTF-8 + + + + - velocity - https://nexus.velocitypowered.com/repository/maven-public/ + papermc-repo + https://repo.papermc.io/repository/maven-public/ + + + jitpack.io + https://jitpack.io @@ -24,15 +36,20 @@ com.velocitypowered velocity-api - 1.1.3 + 3.3.0-SNAPSHOT provided me.leoko.advancedban AdvancedBan-Core - ${project.version} + 2.3.0 compile + + com.github.Carleslc.Simple-YAML + Simple-Yaml + 1.8.4 + net.luckperms api @@ -52,64 +69,4 @@ provided - - - - - org.apache.maven.plugins - maven-shade-plugin - 3.2.3 - - - - org.apache.commons - ${project.groupId}.shaded.org.apache.commons - - - com.zaxxer.hikari - ${project.groupId}.shaded.com.zaxxer.hikari - - - org.hsqldb - ${project.groupId}.shaded.org.hsqldb - - - org.mariadb.jdbc - ${project.groupId}.shaded.org.mariadb.jdbc - - - - - - - - - package - - shade - - - - - - org.codehaus.mojo - templating-maven-plugin - 1.0.0 - - - filter-src - - filter-sources - - - ${basedir}/src/main/java-templates - ${project.build.directory}/generated-sources/java-templates - - - - - - - - \ No newline at end of file + diff --git a/velocity/src/main/java/VersionInfo.java b/velocity/src/main/java/VersionInfo.java deleted file mode 100644 index 5958b99e..00000000 --- a/velocity/src/main/java/VersionInfo.java +++ /dev/null @@ -1,7 +0,0 @@ -package me.leoko.advancedban; - -public class VersionInfo { - - public static final String VERSION = "${project.version}"; - -} \ No newline at end of file diff --git a/velocity/src/main/java/me/leoko/advancedban/velocity/VelocityMain.java b/velocity/src/main/java/me/leoko/advancedban/velocity/VelocityMain.java index be5f1927..66f4dab6 100644 --- a/velocity/src/main/java/me/leoko/advancedban/velocity/VelocityMain.java +++ b/velocity/src/main/java/me/leoko/advancedban/velocity/VelocityMain.java @@ -3,19 +3,17 @@ import com.google.inject.Inject; import com.velocitypowered.api.event.proxy.ProxyInitializeEvent; import com.velocitypowered.api.event.Subscribe; -import com.velocitypowered.api.event.proxy.ProxyShutdownEvent; import com.velocitypowered.api.plugin.Dependency; import com.velocitypowered.api.plugin.Plugin; import com.velocitypowered.api.plugin.annotation.DataDirectory; import com.velocitypowered.api.proxy.ProxyServer; +import me.leoko.advancedban.Universal; import me.leoko.advancedban.velocity.listener.ChatListenerVelocity; import me.leoko.advancedban.velocity.listener.ConnectionListenerVelocity; import org.slf4j.Logger; -import me.leoko.advancedban.Universal; import java.nio.file.Path; - @Plugin( id = "advancedban", name = "AdvancedBan", @@ -30,35 +28,29 @@ public class VelocityMain { private final Logger logger; private final Path dataDirectory; private final ProxyServer server; + private static VelocityMain instance; - private static VelocityMain velocityMain; @Inject - public VelocityMain(Logger logger, @DataDirectory Path dataDirectory, ProxyServer server) { - velocityMain = this; - this.logger = logger; - this.dataDirectory = dataDirectory; + public VelocityMain(ProxyServer server, @DataDirectory Path dataDirectory, Logger logger){ this.server = server; + this.dataDirectory = dataDirectory; + this.logger = logger; + instance = this; } - @Subscribe public void onProxyInitialization(ProxyInitializeEvent event) { - Universal.get().setup(new VelocityMethods(dataDirectory, server, logger)); - server.getEventManager().register(this, new ChatListenerVelocity()); - server.getEventManager().register(this, new ConnectionListenerVelocity()); + Universal.get().setup(new VelocityMethods(server, dataDirectory, logger)); - } + server.getEventManager().register(this, new ConnectionListenerVelocity()); + server.getEventManager().register(this, new ChatListenerVelocity()); - @Subscribe - public void onProxyShutdown(ProxyShutdownEvent event) { - Universal.get().shutdown(); } - public static VelocityMain get() { - return velocityMain; + return instance; } } diff --git a/velocity/src/main/java/me/leoko/advancedban/velocity/VelocityMethods.java b/velocity/src/main/java/me/leoko/advancedban/velocity/VelocityMethods.java index b9385c05..730733e7 100644 --- a/velocity/src/main/java/me/leoko/advancedban/velocity/VelocityMethods.java +++ b/velocity/src/main/java/me/leoko/advancedban/velocity/VelocityMethods.java @@ -1,32 +1,20 @@ package me.leoko.advancedban.velocity; -import com.google.common.reflect.TypeToken; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.velocitypowered.api.command.CommandMeta; import com.velocitypowered.api.command.CommandSource; +import com.velocitypowered.api.plugin.annotation.DataDirectory; import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.ProxyServer; -import me.leoko.advancedban.AbstractMethodInterface; -import me.leoko.advancedban.ServerType; +import me.leoko.advancedban.MethodInterface; import me.leoko.advancedban.Universal; -import me.leoko.advancedban.VersionInfo; import me.leoko.advancedban.manager.PunishmentManager; import me.leoko.advancedban.manager.UUIDManager; import me.leoko.advancedban.utils.Permissionable; import me.leoko.advancedban.utils.Punishment; import me.leoko.advancedban.utils.tabcompletion.TabCompleter; -import me.leoko.advancedban.velocity.listener.CommandReceiverVelocity; -import me.leoko.advancedban.velocity.event.PunishmentEvent; -import me.leoko.advancedban.velocity.event.RevokePunishmentEvent; -import me.leoko.advancedban.velocity.utils.LuckPermsOfflineUser; -import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; -import ninja.leaping.configurate.ConfigurationNode; -import ninja.leaping.configurate.objectmapping.ObjectMappingException; -import ninja.leaping.configurate.yaml.YAMLConfigurationLoader; -import org.checkerframework.checker.nullness.qual.NonNull; -import org.slf4j.Logger; import java.io.File; import java.io.IOException; @@ -39,22 +27,43 @@ import java.util.UUID; import java.util.concurrent.TimeUnit; -public class VelocityMethods extends AbstractMethodInterface { +import me.leoko.advancedban.velocity.event.PunishmentEvent; +import me.leoko.advancedban.velocity.listener.CommandReceiverVelocity; +import me.leoko.advancedban.velocity.utils.LuckPermsOfflineUser; +import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; +import org.simpleyaml.configuration.file.YamlFile; +import org.slf4j.Logger; + +public class VelocityMethods implements MethodInterface { + private final Logger logger; private final ProxyServer server; private final Path dataDirectory; - private final Logger logger; private boolean luckPermsSupport; - public VelocityMethods(Path dataDirectory, - final @NonNull ProxyServer server, - Logger logger) { - super(dataDirectory); + private final File configFile; + private final File messageFile; + private final File layoutFile; + private final File mysqlFile; + private YamlFile config; + private YamlFile messages; + private YamlFile layouts; + private YamlFile mysql; + + + + + public VelocityMethods(ProxyServer server, @DataDirectory Path dataDirectory, Logger logger){ this.dataDirectory = dataDirectory; this.server = server; this.logger = logger; + this.configFile = new File(getDataFolder(), "config.yml"); + this.messageFile =new File(getDataFolder(), "Messages.yml"); + this.layoutFile = new File(getDataFolder(), "Layouts.yml"); + this.mysqlFile = new File(getDataFolder(), "MySQL.yml"); + if (server.getPluginManager().getPlugin("luckperms").isPresent()) { luckPermsSupport = true; log("[AdvancedBan] Offline permission support through LuckPerms active"); @@ -62,16 +71,42 @@ public VelocityMethods(Path dataDirectory, luckPermsSupport = false; log("[AdvancedBan] No offline permission support through LuckPerms"); } - } + + @Override - protected ConfigurationNode loadConfiguration(Path configPath) throws IOException { - YAMLConfigurationLoader loader = YAMLConfigurationLoader.builder().setPath(configPath).build(); + public void loadFiles() { + try { + config = new YamlFile(configFile); + messages = new YamlFile(messageFile); + layouts = new YamlFile(layoutFile); + mysql = new YamlFile(mysqlFile); + + if (!configFile.exists()) { + config.createNewFile(); + } + if (!messageFile.exists()) { + messages.createNewFile(); + } + if (!layoutFile.exists()) { + layouts.createNewFile(); + } + if (!mysqlFile.exists()) { + mysql.createNewFile(); + } + config.load(); + messages.load(); + layouts.load(); + mysql.load(); + + + } catch (IOException e) { + logger.error(e.getMessage()); + } + - //YamlConfigurationLoader loader = YamlConfigurationLoader.builder().path(configPath).build(); - return loader.load(); } @Override @@ -96,39 +131,44 @@ public String getFromUrlJson(String url, String key) { @Override public String getVersion() { - return VersionInfo.VERSION; + return "2.3.0"; } - // Object.toString() is considered safe in this scenario (as per Configurate Docs) @Override public String[] getKeys(Object file, String path) { - return ((ConfigurationNode)file).getNode(path.split("\\.")).getChildrenMap().keySet().stream().map(Object::toString).toArray(String[]::new); + return ((YamlFile) file).getConfigurationSection(path).getKeys(true).toArray(new String[0]); + + } @Override - public void setupMetrics() { - // no bstats for velocity yet + public Object getConfig() { + return config; } + @Override + public Object getMessages() { + return messages; + } - @Override @Deprecated - public boolean isBungee() { - return false; + @Override + public Object getLayouts() { + return layouts; } @Override - public boolean isProxy() { - return true; + public void setupMetrics() { + } @Override - public ServerType getServerType() { - return ServerType.VELOCITY; + public boolean isBungee() { + return true; } @Override - public String clearFormatting(String text) { - return text.replaceAll("(&[^\\s])+", ""); + public String clearFormatting(String s) { + return s.replaceAll("(&[^\\s])+", ""); } @Override @@ -142,112 +182,113 @@ public File getDataFolder() { } @Override - public void setCommandExecutor(String cmd, TabCompleter tabCompleter) { + public void setCommandExecutor(String cmd, String permission, TabCompleter tabCompleter) { + + //With permissions CommandMeta meta = server.getCommandManager().metaBuilder(cmd).build(); server.getCommandManager().register(meta, new CommandReceiverVelocity(server, cmd)); } @Override - public void sendMessage(Object player, String msg) { - ((CommandSource) player).sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(msg)); + public void sendMessage(Object o, String s) { + ((CommandSource) o).sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(s)); } @Override - public String getName(Object player) { - if (player instanceof Player) { - return ((Player) player).getUsername(); + public String getName(Object o) { + if (o instanceof Player) { + return ((Player) o).getUsername(); } else { return "CONSOLE"; } } @Override - public String getName(String uuid) { - return server.getPlayer(UUID.fromString(uuid)).get().getUsername(); + public String getName(String s) { + return server.getPlayer(UUID.fromString(s)).get().getUsername(); } @Override - public String getIP(Object player) { - return ((Player)player).getRemoteAddress().getHostName(); + public String getIP(Object o) { + return ((Player) o).getRemoteAddress().getHostName(); } @Override - public String getInternUUID(Object player) { - return player instanceof Player ? ((Player) player).getUniqueId().toString().replace("-", "") : "none"; + public String getInternUUID(Object o) { + return o instanceof Player ? ((Player) o).getUniqueId().toString().replace("-", "") : "none"; } @Override - public String getInternUUID(String player) { - Optional optionalPlayer = server.getPlayer(player); + public String getInternUUID(String s) { + Optional optionalPlayer = server.getPlayer(s); return optionalPlayer.map(value -> value.getUniqueId().toString().replace("-", "")).orElse(null); } @Override - public boolean hasPerms(Object player, String perms) { - return ((CommandSource) player).hasPermission(perms); + public boolean hasPerms(Object o, String s) { + return ((CommandSource) o).hasPermission(s); } @Override - public Permissionable getOfflinePermissionPlayer(String name) { - if(luckPermsSupport) return new LuckPermsOfflineUser(name); - + public Permissionable getOfflinePermissionPlayer(String s) { + if(luckPermsSupport) return new LuckPermsOfflineUser(s); return permission -> false; } @Override - public boolean isOnline(String name) { - return server.getPlayer(name).isPresent(); + public boolean isOnline(String s) { + return server.getPlayer(s).isPresent(); } @Override - public Player getPlayer(String name) { - return server.getPlayer(name).orElse(null); + public Object getPlayer(String s) { + return server.getPlayer(s).orElse(null); } @Override - public void kickPlayer(String player, String reason) { - server.getPlayer(player).get().disconnect(LegacyComponentSerializer.legacyAmpersand().deserialize(reason)); + public void kickPlayer(String s, String s1) { + server.getPlayer(s).get().disconnect(LegacyComponentSerializer.legacyAmpersand().deserialize(s1)); } @Override - public Player[] getOnlinePlayers() { + public Object[] getOnlinePlayers() { return server.getAllPlayers().toArray(new Player[]{}); } @Override - public void scheduleAsyncRep(Runnable rn, long l1, long l2) { - server.getScheduler().buildTask(getPlugin(), rn).delay(l1*50, TimeUnit.MILLISECONDS).repeat(l2*50, TimeUnit.MILLISECONDS).schedule(); + public void scheduleAsyncRep(Runnable runnable, long l, long l1) { + server.getScheduler().buildTask(getPlugin(), runnable).delay(l*50, TimeUnit.MILLISECONDS).repeat(l1*50, TimeUnit.MILLISECONDS).schedule(); } @Override - public void scheduleAsync(Runnable rn, long l1) { - server.getScheduler().buildTask(getPlugin(), rn).delay(l1*50, TimeUnit.MILLISECONDS).schedule(); + public void scheduleAsync(Runnable runnable, long l) { + server.getScheduler().buildTask(getPlugin(), runnable).delay(l*50, TimeUnit.MILLISECONDS).schedule(); } @Override - public void runAsync(Runnable rn) { - server.getScheduler().buildTask(getPlugin(), rn).schedule(); + public void runAsync(Runnable runnable) { + server.getScheduler().buildTask(getPlugin(), runnable).schedule(); } @Override - public void runSync(Runnable rn) { - rn.run(); + public void runSync(Runnable runnable) { + runnable.run(); } @Override - public void executeCommand(String cmd) { - server.getCommandManager().executeAsync(server.getConsoleCommandSource(), cmd).exceptionally((ex) -> { - ex.printStackTrace(); + public void executeCommand(String s) { + server.getCommandManager().executeAsync(server.getConsoleCommandSource(), s).exceptionally((ex) -> { + logger.error(ex.getMessage()); return null; }); } @Override - public boolean callChat(Object player) { - Punishment pnt = PunishmentManager.get().getMute(UUIDManager.get().getUUID(getName(player))); + public boolean callChat(Object o) { + Punishment pnt = PunishmentManager.get().getMute(UUIDManager.get().getUUID(getName(o))); if (pnt != null) { for (String str : pnt.getLayout()) { - sendMessage(player, str); + sendMessage(o, str); } return true; } @@ -255,12 +296,12 @@ public boolean callChat(Object player) { } @Override - public boolean callCMD(Object player, String cmd) { + public boolean callCMD(Object o, String s) { Punishment pnt; - if (Universal.get().isMuteCommand(cmd.substring(1)) - && (pnt = PunishmentManager.get().getMute(UUIDManager.get().getUUID(getName(player)))) != null) { + if (Universal.get().isMuteCommand(s.substring(1)) + && (pnt = PunishmentManager.get().getMute(UUIDManager.get().getUUID(getName(o)))) != null) { for (String str : pnt.getLayout()) { - sendMessage(player, str); + sendMessage(o, str); } return true; } @@ -268,101 +309,97 @@ public boolean callCMD(Object player, String cmd) { } @Override - public String parseJSON(InputStreamReader json, String key) { + public Object getMySQLFile() { + return null; + } + + @Override + public String parseJSON(InputStreamReader inputStreamReader, String s) { JsonObject element; try { - element = JsonParser.parseReader(json).getAsJsonObject(); + element = JsonParser.parseReader(inputStreamReader).getAsJsonObject(); } catch (IllegalStateException e) { return null; } - JsonElement obj = element.get(key); + JsonElement obj = element.get(s); return obj != null ? obj.toString().replaceAll("\"", "") : null; } @Override - public String parseJSON(String json, String key) { + public String parseJSON(String s, String s1) { JsonObject element; try { - element = (JsonObject) JsonParser.parseString(json).getAsJsonObject(); + element = (JsonObject) JsonParser.parseString(s).getAsJsonObject(); } catch (IllegalStateException e) { return null; } - JsonElement obj = element.get(key); + JsonElement obj = element.get(s1); return obj != null ? obj.toString().replaceAll("\"", "") : null; } @Override - public Boolean getBoolean(Object file, String path) { - return getConfigNode(file, path).getBoolean(); + public Boolean getBoolean(Object o, String s) { + return ((YamlFile) o).getBoolean(s); } @Override - public String getString(Object file, String path) { - return getConfigNode(file, path).getString(); + public String getString(Object o, String s) { + return ((YamlFile) o).getString(s); } @Override - public Long getLong(Object file, String path) { - return getConfigNode(file, path).getLong(); + public Long getLong(Object o, String s) { + return ((YamlFile) o).getLong(s); } @Override - public Integer getInteger(Object file, String path) { - return getConfigNode(file, path).getInt(); + public Integer getInteger(Object o, String s) { + return ((YamlFile) o).getInt(s); } @Override - public List getStringList(Object file, String path) { - try { - return getConfigNode(file, path).getList(TypeToken.of(String.class)); - } catch (ObjectMappingException e) { - throw new RuntimeException(e); - } + public List getStringList(Object o, String s) { + return ((YamlFile) o).getStringList(s); } @Override - public boolean getBoolean(Object file, String path, boolean def) { - return getConfigNode(file, path).getBoolean(def); + public boolean getBoolean(Object o, String s, boolean b) { + return ((YamlFile) o).getBoolean(s, b); } @Override - public String getString(Object file, String path, String def) { - return getConfigNode(file, path).getString(def); + public String getString(Object o, String s, String s1) { + return ((YamlFile) o).getString(s, s1); } @Override - public long getLong(Object file, String path, long def) { - return getConfigNode(file, path).getLong(def); + public long getLong(Object o, String s, long l) { + return ((YamlFile) o).getLong(s, l); } @Override - public int getInteger(Object file, String path, int def) { - return getConfigNode(file, path).getInt(def); + public int getInteger(Object o, String s, int i) { + return ((YamlFile) o).getInt(s, i); } @Override - public boolean contains(Object file, String path) { - return !getConfigNode(file, path).isEmpty(); + public boolean contains(Object o, String s) { + return ((YamlFile) o).contains(s); } @Override - public String getFileName(Object file) { - return "[Only Available in the Bukkit Version!]"; + public String getFileName(Object o) { + return "[Only available in the Bukkit Version!]"; } @Override public void callPunishmentEvent(Punishment punishment) { - callPunishmentEvent(punishment, false); + server.getEventManager().fireAndForget(new PunishmentEvent(punishment)); } @Override - public void callPunishmentEvent(Punishment punishment, boolean silent) { - server.getEventManager().fireAndForget(new PunishmentEvent(punishment, silent)); - } - - @Override - public void callRevokePunishmentEvent(Punishment punishment, boolean massClear) { - server.getEventManager().fireAndForget(new RevokePunishmentEvent(punishment, massClear)); + public void callRevokePunishmentEvent(Punishment punishment, boolean b) { + server.getEventManager().fireAndForget(new PunishmentEvent(punishment, b)); } @Override @@ -371,25 +408,21 @@ public boolean isOnlineMode() { } @Override - public void notify(String perm, List notification) { + public void notify(String s, List list) { server.getAllPlayers().forEach(player -> { - if (player.hasPermission(perm)) { - notification.forEach(str -> sendMessage(player, str)); + if (player.hasPermission(s)) { + list.forEach(str -> sendMessage(player, str)); } }); } @Override - public void log(String msg) { - server.getConsoleCommandSource().sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(msg)); + public void log(String s) { + server.getConsoleCommandSource().sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(s)); } @Override public boolean isUnitTesting() { return false; } - - private ConfigurationNode getConfigNode(Object file, String path) { - return ((ConfigurationNode) file).getNode((Object[]) path.split("\\.")); - } -} \ No newline at end of file +} diff --git a/velocity/src/main/java/me/leoko/advancedban/velocity/event/PunishmentEvent.java b/velocity/src/main/java/me/leoko/advancedban/velocity/event/PunishmentEvent.java index 365924c6..174f1244 100644 --- a/velocity/src/main/java/me/leoko/advancedban/velocity/event/PunishmentEvent.java +++ b/velocity/src/main/java/me/leoko/advancedban/velocity/event/PunishmentEvent.java @@ -3,10 +3,12 @@ import me.leoko.advancedban.utils.Punishment; import org.checkerframework.checker.nullness.qual.NonNull; +/** + * Event fired when a punishment is created + */ public class PunishmentEvent { private final Punishment punishment; - private final boolean silent; public PunishmentEvent(@NonNull Punishment punishment) { @@ -18,14 +20,11 @@ public PunishmentEvent(@NonNull Punishment punishment, boolean silent) { this.silent = silent; } - public Punishment getPunishment() { - return punishment; + return this.punishment; } public boolean isSilent() { return silent; } - - -} +} \ No newline at end of file diff --git a/velocity/src/main/java/me/leoko/advancedban/velocity/event/RevokePunishmentEvent.java b/velocity/src/main/java/me/leoko/advancedban/velocity/event/RevokePunishmentEvent.java index f1f8dccd..3ec8cbbe 100644 --- a/velocity/src/main/java/me/leoko/advancedban/velocity/event/RevokePunishmentEvent.java +++ b/velocity/src/main/java/me/leoko/advancedban/velocity/event/RevokePunishmentEvent.java @@ -30,4 +30,4 @@ public Punishment getPunishment() { public boolean isMassClear() { return massClear; } -} +} \ No newline at end of file diff --git a/velocity/src/main/java/me/leoko/advancedban/velocity/listener/ChatListenerVelocity.java b/velocity/src/main/java/me/leoko/advancedban/velocity/listener/ChatListenerVelocity.java index aab8c28d..c8312670 100644 --- a/velocity/src/main/java/me/leoko/advancedban/velocity/listener/ChatListenerVelocity.java +++ b/velocity/src/main/java/me/leoko/advancedban/velocity/listener/ChatListenerVelocity.java @@ -17,4 +17,5 @@ public void onChat(PlayerChatEvent event) { event.setResult(PlayerChatEvent.ChatResult.denied()); } } + } diff --git a/velocity/src/main/java/me/leoko/advancedban/velocity/listener/CommandReceiverVelocity.java b/velocity/src/main/java/me/leoko/advancedban/velocity/listener/CommandReceiverVelocity.java index 6ef986ed..0af67a0d 100644 --- a/velocity/src/main/java/me/leoko/advancedban/velocity/listener/CommandReceiverVelocity.java +++ b/velocity/src/main/java/me/leoko/advancedban/velocity/listener/CommandReceiverVelocity.java @@ -1,5 +1,4 @@ package me.leoko.advancedban.velocity.listener; - import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.SimpleCommand; import com.velocitypowered.api.proxy.ProxyServer; diff --git a/velocity/src/main/java/me/leoko/advancedban/velocity/utils/LuckPermsOfflineUser.java b/velocity/src/main/java/me/leoko/advancedban/velocity/utils/LuckPermsOfflineUser.java index ec094844..f6577b73 100644 --- a/velocity/src/main/java/me/leoko/advancedban/velocity/utils/LuckPermsOfflineUser.java +++ b/velocity/src/main/java/me/leoko/advancedban/velocity/utils/LuckPermsOfflineUser.java @@ -1,6 +1,5 @@ package me.leoko.advancedban.velocity.utils; - import me.leoko.advancedban.utils.Permissionable; import net.luckperms.api.LuckPermsProvider; import net.luckperms.api.model.user.User; @@ -15,14 +14,13 @@ public class LuckPermsOfflineUser implements Permissionable { public LuckPermsOfflineUser(String name) { final UserManager userManager = LuckPermsProvider.get().getUserManager(); final UUID uuid = userManager.lookupUniqueId(name).join(); - if(uuid != null) { + if (uuid != null) { this.permissionUser = userManager.loadUser(uuid).join(); } } - @Override public boolean hasPermission(String permission) { return permissionUser != null && permissionUser.getCachedData().getPermissionData().checkPermission(permission).asBoolean(); } -} +} \ No newline at end of file From 6d8de6ffd22b99b46feec1b9e3d7627515c0c70d Mon Sep 17 00:00:00 2001 From: Tom Date: Wed, 19 Jun 2024 22:31:37 +0200 Subject: [PATCH 3/6] Fix an error on build + remove logger error for velocity --- bundle/pom.xml | 79 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 60 insertions(+), 19 deletions(-) diff --git a/bundle/pom.xml b/bundle/pom.xml index a9155cf9..530b7ea7 100644 --- a/bundle/pom.xml +++ b/bundle/pom.xml @@ -3,7 +3,7 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - + me.leoko.advancedban AdvancedBan @@ -26,32 +26,38 @@ ${project.version} runtime + + me.leoko.advancedban + AdvancedBan-Proxy + ${project.version} + runtime + - + - + org.apache.maven.plugins maven-shade-plugin 3.2.3 - - org.apache.commons - ${project.groupId}.shaded.org.apache.commons - - - org.slf4j - ${project.groupId}.shaded.org.slf4j - - - com.zaxxer.hikari - ${project.groupId}.shaded.com.zaxxer.hikari - - - org.hsqldb - ${project.groupId}.shaded.org.hsqldb - + + org.apache.commons + ${project.groupId}.shaded.org.apache.commons + + + + com.zaxxer.hikari + ${project.groupId}.shaded.com.zaxxer.hikari + + + org.hsqldb + ${project.groupId}.shaded.org.hsqldb + org.bstats ${project.groupId}.shaded.org.bstats @@ -70,6 +76,41 @@ + + + + org.apache.maven.plugins + maven-source-plugin + 3.2.1 + + + attach-sources + + jar-no-fork + + + sources + + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 3.3.0 + + + attach-javadocs + + jar + + + javadoc + + + + \ No newline at end of file From de7ed996196006ff253456addeb83c20e8798de9 Mon Sep 17 00:00:00 2001 From: Tom Date: Wed, 19 Jun 2024 22:31:55 +0200 Subject: [PATCH 4/6] Fix an error on user not banned (cache) --- core/src/main/java/me/leoko/advancedban/utils/Punishment.java | 1 + 1 file changed, 1 insertion(+) diff --git a/core/src/main/java/me/leoko/advancedban/utils/Punishment.java b/core/src/main/java/me/leoko/advancedban/utils/Punishment.java index 2bbc4230..4284f50d 100644 --- a/core/src/main/java/me/leoko/advancedban/utils/Punishment.java +++ b/core/src/main/java/me/leoko/advancedban/utils/Punishment.java @@ -105,6 +105,7 @@ public void create(boolean silent) { if (getType().getBasic() == PunishmentType.BAN || getType() == PunishmentType.KICK) { mi.runSync(() -> mi.kickPlayer(getName(), getLayoutBSN())); + PunishmentManager.get().getLoadedPunishments(false).add(this); } else { if (getType().getBasic() != PunishmentType.NOTE) for (String str : getLayout()) { From d9f9305539f145ec819ef61a5f857f10751c6252 Mon Sep 17 00:00:00 2001 From: Tom Date: Wed, 19 Jun 2024 22:36:04 +0200 Subject: [PATCH 5/6] Added author --- .../main/java/me/leoko/advancedban/velocity/VelocityMain.java | 1 + 1 file changed, 1 insertion(+) diff --git a/velocity/src/main/java/me/leoko/advancedban/velocity/VelocityMain.java b/velocity/src/main/java/me/leoko/advancedban/velocity/VelocityMain.java index 66f4dab6..ac670aa2 100644 --- a/velocity/src/main/java/me/leoko/advancedban/velocity/VelocityMain.java +++ b/velocity/src/main/java/me/leoko/advancedban/velocity/VelocityMain.java @@ -18,6 +18,7 @@ id = "advancedban", name = "AdvancedBan", version = "1.0-SNAPSHOT", + authors = {"Leoko"}, dependencies = { @Dependency(id = "luckperms", optional = true) } From 36d3cecbf1153978e3d81a90a4b4d8e2fb0b96aa Mon Sep 17 00:00:00 2001 From: Tom Date: Sun, 1 Sep 2024 11:05:46 +0200 Subject: [PATCH 6/6] Added what I do on other repo --- bundle/pom.xml | 73 +++-------- bungee/pom.xml | 2 +- .../advancedban/utils/DynamicDataSource.java | 12 +- .../leoko/advancedban/utils/Punishment.java | 4 +- velocity/pom.xml | 14 ++- .../advancedban/velocity/VelocityMain.java | 4 +- .../advancedban/velocity/VelocityMethods.java | 118 +++++++++++++++--- 7 files changed, 138 insertions(+), 89 deletions(-) diff --git a/bundle/pom.xml b/bundle/pom.xml index 530b7ea7..afbcd139 100644 --- a/bundle/pom.xml +++ b/bundle/pom.xml @@ -3,7 +3,7 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - + me.leoko.advancedban AdvancedBan @@ -26,38 +26,36 @@ ${project.version} runtime + me.leoko.advancedban - AdvancedBan-Proxy + AdvancedBan-Velocity ${project.version} runtime - + - + org.apache.maven.plugins maven-shade-plugin 3.2.3 - - org.apache.commons - ${project.groupId}.shaded.org.apache.commons - - - - com.zaxxer.hikari - ${project.groupId}.shaded.com.zaxxer.hikari - - - org.hsqldb - ${project.groupId}.shaded.org.hsqldb - + + org.apache.commons + ${project.groupId}.shaded.org.apache.commons + + + + com.zaxxer.hikari + ${project.groupId}.shaded.com.zaxxer.hikari + + + org.hsqldb + ${project.groupId}.shaded.org.hsqldb + org.bstats ${project.groupId}.shaded.org.bstats @@ -76,41 +74,6 @@ - - - - org.apache.maven.plugins - maven-source-plugin - 3.2.1 - - - attach-sources - - jar-no-fork - - - sources - - - - - - - org.apache.maven.plugins - maven-javadoc-plugin - 3.3.0 - - - attach-javadocs - - jar - - - javadoc - - - - \ No newline at end of file diff --git a/bungee/pom.xml b/bungee/pom.xml index 4a03fc80..f7222131 100644 --- a/bungee/pom.xml +++ b/bungee/pom.xml @@ -22,7 +22,7 @@ md_5-snapshots - http://repo.md-5.net/content/repositories/snapshots/ + https://repo.md-5.net/content/repositories/snapshots/ diff --git a/core/src/main/java/me/leoko/advancedban/utils/DynamicDataSource.java b/core/src/main/java/me/leoko/advancedban/utils/DynamicDataSource.java index 3b65c016..3d9ff380 100644 --- a/core/src/main/java/me/leoko/advancedban/utils/DynamicDataSource.java +++ b/core/src/main/java/me/leoko/advancedban/utils/DynamicDataSource.java @@ -11,12 +11,12 @@ public class DynamicDataSource { public DynamicDataSource(boolean preferMySQL) throws ClassNotFoundException { MethodInterface mi = Universal.get().getMethods(); if (preferMySQL) { - String ip = mi.getString(mi.getMySQLFile(), "MySQL.IP", "Unknown"); - String dbName = mi.getString(mi.getMySQLFile(), "MySQL.DB-Name", "Unknown"); - String usrName = mi.getString(mi.getMySQLFile(), "MySQL.Username", "Unknown"); - String password = mi.getString(mi.getMySQLFile(), "MySQL.Password", "Unknown"); - String properties = mi.getString(mi.getMySQLFile(), "MySQL.Properties", "verifyServerCertificate=false&useSSL=false&useUnicode=true&characterEncoding=utf8"); - int port = mi.getInteger(mi.getMySQLFile(), "MySQL.Port", 3306); + String ip = mi.getString(mi.getConfig(), "MySQL.IP", "Unknown"); + String dbName = mi.getString(mi.getConfig(), "MySQL.DB-Name", "Unknown"); + String usrName = mi.getString(mi.getConfig(), "MySQL.Username", "Unknown"); + String password = mi.getString(mi.getConfig(), "MySQL.Password", "Unknown"); + String properties = mi.getString(mi.getConfig(), "MySQL.Properties", "verifyServerCertificate=false&useSSL=false&useUnicode=true&characterEncoding=utf8"); + int port = mi.getInteger(mi.getConfig(), "MySQL.Port", 3306); Class.forName("com.mysql.jdbc.Driver"); config.setJdbcUrl("jdbc:mysql://" + ip + ":" + port + "/" + dbName + "?"+properties); diff --git a/core/src/main/java/me/leoko/advancedban/utils/Punishment.java b/core/src/main/java/me/leoko/advancedban/utils/Punishment.java index 4284f50d..28f351a6 100644 --- a/core/src/main/java/me/leoko/advancedban/utils/Punishment.java +++ b/core/src/main/java/me/leoko/advancedban/utils/Punishment.java @@ -103,15 +103,15 @@ public void create(boolean silent) { if (mi.isOnline(getName())) { final Object p = mi.getPlayer(getName()); + PunishmentManager.get().getLoadedPunishments(false).add(this); + if (getType().getBasic() == PunishmentType.BAN || getType() == PunishmentType.KICK) { mi.runSync(() -> mi.kickPlayer(getName(), getLayoutBSN())); - PunishmentManager.get().getLoadedPunishments(false).add(this); } else { if (getType().getBasic() != PunishmentType.NOTE) for (String str : getLayout()) { mi.sendMessage(p, str); } - PunishmentManager.get().getLoadedPunishments(false).add(this); } } diff --git a/velocity/pom.xml b/velocity/pom.xml index 4386d4f4..8a1e3116 100644 --- a/velocity/pom.xml +++ b/velocity/pom.xml @@ -3,15 +3,13 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - me.leoko.advancedban AdvancedBan 2.3.0 - - AdvancedBan-Proxy + AdvancedBan-Velocity jar @@ -19,8 +17,6 @@ UTF-8 - - papermc-repo @@ -68,5 +64,11 @@ 1.7.30 provided + + mysql + mysql-connector-java + 8.0.26 + - + + \ No newline at end of file diff --git a/velocity/src/main/java/me/leoko/advancedban/velocity/VelocityMain.java b/velocity/src/main/java/me/leoko/advancedban/velocity/VelocityMain.java index ac670aa2..b439b18e 100644 --- a/velocity/src/main/java/me/leoko/advancedban/velocity/VelocityMain.java +++ b/velocity/src/main/java/me/leoko/advancedban/velocity/VelocityMain.java @@ -44,11 +44,11 @@ public VelocityMain(ProxyServer server, @DataDirectory Path dataDirectory, Logge @Subscribe public void onProxyInitialization(ProxyInitializeEvent event) { - Universal.get().setup(new VelocityMethods(server, dataDirectory, logger)); - server.getEventManager().register(this, new ConnectionListenerVelocity()); server.getEventManager().register(this, new ChatListenerVelocity()); + Universal.get().setup(new VelocityMethods(server, dataDirectory, logger)); + } public static VelocityMain get() { diff --git a/velocity/src/main/java/me/leoko/advancedban/velocity/VelocityMethods.java b/velocity/src/main/java/me/leoko/advancedban/velocity/VelocityMethods.java index 730733e7..764f878c 100644 --- a/velocity/src/main/java/me/leoko/advancedban/velocity/VelocityMethods.java +++ b/velocity/src/main/java/me/leoko/advancedban/velocity/VelocityMethods.java @@ -16,11 +16,10 @@ import me.leoko.advancedban.utils.Punishment; import me.leoko.advancedban.utils.tabcompletion.TabCompleter; -import java.io.File; -import java.io.IOException; -import java.io.InputStreamReader; +import java.io.*; import java.net.HttpURLConnection; import java.net.URL; +import java.net.URLConnection; import java.nio.file.Path; import java.util.List; import java.util.Optional; @@ -45,11 +44,9 @@ public class VelocityMethods implements MethodInterface { private final File configFile; private final File messageFile; private final File layoutFile; - private final File mysqlFile; private YamlFile config; private YamlFile messages; private YamlFile layouts; - private YamlFile mysql; @@ -62,7 +59,6 @@ public VelocityMethods(ProxyServer server, @DataDirectory Path dataDirectory, Lo this.configFile = new File(getDataFolder(), "config.yml"); this.messageFile =new File(getDataFolder(), "Messages.yml"); this.layoutFile = new File(getDataFolder(), "Layouts.yml"); - this.mysqlFile = new File(getDataFolder(), "MySQL.yml"); if (server.getPluginManager().getPlugin("luckperms").isPresent()) { luckPermsSupport = true; @@ -79,27 +75,25 @@ public VelocityMethods(ProxyServer server, @DataDirectory Path dataDirectory, Lo @Override public void loadFiles() { try { + + config = new YamlFile(configFile); messages = new YamlFile(messageFile); layouts = new YamlFile(layoutFile); - mysql = new YamlFile(mysqlFile); if (!configFile.exists()) { - config.createNewFile(); + saveResource("config.yml", true); } if (!messageFile.exists()) { - messages.createNewFile(); + saveResource("Messages.yml", true); } if (!layoutFile.exists()) { - layouts.createNewFile(); - } - if (!mysqlFile.exists()) { - mysql.createNewFile(); + saveResource("Layouts.yml", true); } + config.load(); messages.load(); layouts.load(); - mysql.load(); } catch (IOException e) { @@ -143,17 +137,44 @@ public String[] getKeys(Object file, String path) { @Override public Object getConfig() { - return config; + try{ + if(config == null){ + config = new YamlFile(configFile); + config.load(); + } + return config; + }catch (Exception e){ + logger.error(e.getMessage()); + return config; + } } @Override public Object getMessages() { - return messages; + try{ + if(messages == null){ + messages = new YamlFile(messageFile); + messages.load(); + } + return messages; + }catch (Exception e){ + logger.error(e.getMessage()); + return messages; + } } @Override public Object getLayouts() { - return layouts; + try{ + if(layouts == null){ + layouts = new YamlFile(layoutFile); + layouts.load(); + } + return layouts; + }catch (Exception e){ + logger.error(e.getMessage()); + return layouts; + } } @Override @@ -425,4 +446,67 @@ public void log(String s) { public boolean isUnitTesting() { return false; } + + //Extracted from https://github.com/Bukkit/Bukkit/blob/master/src/main/java/org/bukkit/plugin/java/JavaPlugin.java + + public void saveResource(String resourcePath, boolean replace) { + if (resourcePath == null || resourcePath.equals("")) { + throw new IllegalArgumentException("ResourcePath cannot be null or empty"); + } + + resourcePath = resourcePath.replace('\\', '/'); + InputStream in = getResource(resourcePath); + if (in == null) { + throw new IllegalArgumentException("The embedded resource '" + resourcePath + "' cannot be found"); + } + + File outFile = new File(getDataFolder(), resourcePath); + int lastIndex = resourcePath.lastIndexOf('/'); + File outDir = new File(getDataFolder(), resourcePath.substring(0, lastIndex >= 0 ? lastIndex : 0)); + + if (!outDir.exists()) { + outDir.mkdirs(); + } + + try { + if (!outFile.exists() || replace) { + OutputStream out = new FileOutputStream(outFile); + byte[] buf = new byte[1024]; + int len; + while ((len = in.read(buf)) > 0) { + out.write(buf, 0, len); + } + out.close(); + in.close(); + } else { + logger.warn("Could not save " + outFile.getName() + " to " + outFile + " because " + outFile.getName() + " already exists."); + } + } catch (IOException ex) { + logger.error("Could not save " + outFile.getName() + " to " + outFile, ex); + } + } + + + public InputStream getResource(String filename) { + if (filename == null) { + throw new IllegalArgumentException("Filename cannot be null"); + } + + try { + URL url = getClass().getClassLoader().getResource(filename); + + if (url == null) { + return null; + } + + URLConnection connection = url.openConnection(); + connection.setUseCaches(false); + return connection.getInputStream(); + } catch (IOException ex) { + return null; + } + } + } + +