From 463cdaef975d76640c12cc40e566de9bd90047b3 Mon Sep 17 00:00:00 2001 From: SageSphinx63920 Date: Tue, 2 Apr 2024 21:20:20 +0200 Subject: [PATCH] Better hook into geyser, resource filtering and general fixes --- pom.xml | 20 +++- src/main/java/de/hdg/keklist/Keklist.java | 21 ++++- .../keklist/events/GeyserConnectionEvent.java | 47 ++++++++++ .../hdg/keklist/events/PreLoginKickEvent.java | 2 +- .../extentions/GeyserEventRegistrar.java | 26 ++++++ .../hdg/keklist/util/KeklistConfigUtil.java | 16 ++-- src/main/resources/assets/lang/en-us.json | 4 + src/main/resources/config.yml | 93 ------------------- src/main/resources/paper-plugin.yml | 9 +- start.sh | 2 +- 10 files changed, 132 insertions(+), 108 deletions(-) create mode 100644 src/main/java/de/hdg/keklist/events/GeyserConnectionEvent.java create mode 100644 src/main/java/de/hdg/keklist/extentions/GeyserEventRegistrar.java delete mode 100644 src/main/resources/config.yml diff --git a/pom.xml b/pom.xml index 4b24aa1..5f663ce 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ de.sage.minecraft keklist - 1.0-SNAPSHOT + 1.0.0-SNAPSHOT jar Keklist @@ -148,8 +148,18 @@ - src/main/resources + ${project.basedir}/src/main/resources true + + *.yml + + + + ${project.basedir}/src/main/resources + false + + *.yml + @@ -250,6 +260,12 @@ 5.4 provided + + org.geysermc.geyser + api + 2.2.3-SNAPSHOT + provided + diff --git a/src/main/java/de/hdg/keklist/Keklist.java b/src/main/java/de/hdg/keklist/Keklist.java index 497ca4b..6356ea5 100644 --- a/src/main/java/de/hdg/keklist/Keklist.java +++ b/src/main/java/de/hdg/keklist/Keklist.java @@ -15,6 +15,7 @@ import de.hdg.keklist.events.ListPingEvent; import de.hdg.keklist.events.PreLoginKickEvent; import de.hdg.keklist.events.ServerWhitelistChangeEvent; +import de.hdg.keklist.extentions.GeyserEventRegistrar; import de.hdg.keklist.extentions.PlaceholderAPIExtension; import de.hdg.keklist.extentions.context.BlacklistedCalculator; import de.hdg.keklist.extentions.context.WhitelistedCalculator; @@ -44,6 +45,7 @@ import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.java.JavaPlugin; import org.geysermc.floodgate.api.FloodgateApi; +import org.geysermc.geyser.api.GeyserApi; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -65,6 +67,7 @@ public final class Keklist extends JavaPlugin { private PlaceholderAPIExtension placeholders; private ContextManager contextManager; private final List> registeredCalculators = new ArrayList<>(); + private GeyserApi geyserApi; /* Global */ private static @Getter DB database; @@ -116,7 +119,7 @@ public void onLoad() { //set debug mode debug = getConfig().getBoolean("debug"); - updateChecker = new UpdateChecker("simpig-city", "Keklist", getPluginMeta().getVersion(), true, getLogger()); + //updateChecker = new UpdateChecker("simpig-city", "Keklist", getPluginMeta().getVersion(), true, getLogger()); //SQL if (getConfig().getBoolean("mariadb.enabled")) { @@ -192,6 +195,20 @@ public void onEnable() { } } + // Geyser hook + if (Bukkit.getPluginManager().getPlugin("Geyser-Spigot") != null) { + geyserApi = GeyserApi.api(); + + // We will not set the prefix if geyser is not enabled as the proxy could provide a different prefix than the floodgate api on sub server does + if(geyserApi.usernamePrefix() != null) { + getConfig().set("floodgate.prefix", geyserApi.usernamePrefix()); + getLogger().info(translations.get("geyser.prefix", geyserApi.usernamePrefix())); + } + + GeyserEventRegistrar eventRegistrar = new GeyserEventRegistrar(geyserApi, this); + eventRegistrar.registerEvents(); + } + // Update checker // TODO : Uncomment this when the plugin is *publicly* released /*if (getConfig().getBoolean("update.check")) @@ -204,7 +221,7 @@ public void onEnable() { } }, 0, getConfig().getInt("update.interval"), java.util.concurrent.TimeUnit.HOURS);*/ - updateChecker.setUpdateMessage(translations.get("update.message", getPluginMeta().getVersion())); + //updateChecker.setUpdateMessage(translations.get("update.message", getPluginMeta().getVersion())); } diff --git a/src/main/java/de/hdg/keklist/events/GeyserConnectionEvent.java b/src/main/java/de/hdg/keklist/events/GeyserConnectionEvent.java new file mode 100644 index 0000000..5943042 --- /dev/null +++ b/src/main/java/de/hdg/keklist/events/GeyserConnectionEvent.java @@ -0,0 +1,47 @@ +package de.hdg.keklist.events; + +import de.hdg.keklist.Keklist; +import de.hdg.keklist.extentions.WebhookManager; +import org.bukkit.Bukkit; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.entity.Player; +import org.geysermc.geyser.api.event.connection.ConnectionRequestEvent; + +import java.sql.ResultSet; +import java.sql.SQLException; + +public class GeyserConnectionEvent { + + private static final FileConfiguration config = Keklist.getInstance().getConfig(); + + public static void onConnectionRequestEvent(ConnectionRequestEvent event) { + String ip = event.getInetSocketAddress().getAddress().getHostAddress(); + + try (ResultSet rsIp = Keklist.getDatabase().onQuery("SELECT * FROM blacklistIp WHERE ip = ?", ip)) { + if (rsIp.next()) { + if (config.getBoolean("blacklist.allow-join-with-admin")) { + for (Player player : Keklist.getInstance().getServer().getOnlinePlayers()) { + if (player.hasPermission(config.getString("blacklist.admin-permission"))) { + return; + } + } + } + + if (Keklist.getInstance().getConfig().getBoolean("chat-notify")) + Bukkit.broadcast(Keklist.getInstance().getMiniMessage().deserialize(Keklist.getTranslations().get("notify.kick", ip)), "keklist.notify.kicked"); + + if (Keklist.getWebhookManager() != null) + Keklist.getWebhookManager().fireBlacklistEvent(WebhookManager.EVENT_TYPE.BLACKLIST_KICK, ip, rsIp.getString("byPlayer"), null, System.currentTimeMillis()); + + Bukkit.getConsoleSender().sendMessage(Keklist.getInstance().getMiniMessage().deserialize(Keklist.getTranslations().get("notify.kick", ip))); + + event.setCancelled(true); + } + } catch (SQLException e) { + Keklist.getInstance().getLogger().severe(Keklist.getTranslations().get("geyser.events.error", e.getMessage())); + Keklist.getInstance().getLogger().severe(Keklist.getTranslations().get("geyser.events.fallback")); + event.setCancelled(true); + } + + } +} diff --git a/src/main/java/de/hdg/keklist/events/PreLoginKickEvent.java b/src/main/java/de/hdg/keklist/events/PreLoginKickEvent.java index 80ac63e..eb3b340 100644 --- a/src/main/java/de/hdg/keklist/events/PreLoginKickEvent.java +++ b/src/main/java/de/hdg/keklist/events/PreLoginKickEvent.java @@ -107,7 +107,7 @@ public void onPreLogin(AsyncPlayerPreLoginEvent event) { client.newCall(request).enqueue(new PreLoginKickEvent.WebhooknameCallback(WebhookManager.EVENT_TYPE.WHITELIST_KICK, event.getAddress().getHostAddress(), null, System.currentTimeMillis())); } else { if (Keklist.getWebhookManager() != null) - Keklist.getWebhookManager().fireWhitelistEvent(WebhookManager.EVENT_TYPE.WHITELIST_KICK, event.getName()+ " (" + event.getAddress().getHostAddress() + ")", null, System.currentTimeMillis()); + Keklist.getWebhookManager().fireWhitelistEvent(WebhookManager.EVENT_TYPE.WHITELIST_KICK, event.getName() + " (" + event.getAddress().getHostAddress() + ")", null, System.currentTimeMillis()); if (Keklist.getInstance().getConfig().getBoolean("chat-notify")) Bukkit.broadcast(Keklist.getInstance().getMiniMessage().deserialize(Keklist.getTranslations().get("notify.kick", event.getName())), "keklist.notify.kicked"); diff --git a/src/main/java/de/hdg/keklist/extentions/GeyserEventRegistrar.java b/src/main/java/de/hdg/keklist/extentions/GeyserEventRegistrar.java new file mode 100644 index 0000000..83b2ec7 --- /dev/null +++ b/src/main/java/de/hdg/keklist/extentions/GeyserEventRegistrar.java @@ -0,0 +1,26 @@ +package de.hdg.keklist.extentions; + +import de.hdg.keklist.Keklist; +import de.hdg.keklist.events.GeyserConnectionEvent; +import lombok.SneakyThrows; +import org.geysermc.event.PostOrder; +import org.geysermc.geyser.api.GeyserApi; +import org.geysermc.geyser.api.event.EventRegistrar; +import org.geysermc.geyser.api.event.connection.ConnectionRequestEvent; + +public class GeyserEventRegistrar implements EventRegistrar { + + private final GeyserApi geyserApi; + private final Keklist keklist; + + public GeyserEventRegistrar(GeyserApi geyserApi, Keklist keklist) { + this.geyserApi = geyserApi; + this.keklist = keklist; + } + + @SneakyThrows + public void registerEvents() { + geyserApi.eventBus().subscribe(this, ConnectionRequestEvent.class, GeyserConnectionEvent::onConnectionRequestEvent, PostOrder.FIRST); + keklist.getLogger().info(Keklist.getTranslations().get("geyser.events.registered")); + } +} diff --git a/src/main/java/de/hdg/keklist/util/KeklistConfigUtil.java b/src/main/java/de/hdg/keklist/util/KeklistConfigUtil.java index 98bca9a..73a39c1 100644 --- a/src/main/java/de/hdg/keklist/util/KeklistConfigUtil.java +++ b/src/main/java/de/hdg/keklist/util/KeklistConfigUtil.java @@ -30,14 +30,17 @@ public void updateConfig() { newConfig.load(reader); FileConfigurationOptions newOptions = newConfig.options(); + boolean update = false; + for (String key : newConfig.getKeys(true)) { if (!oldConfig.getKeys(true).contains(key)) { oldConfig.set(key, newConfig.get(key)); - - if(Keklist.isDebug()) - plugin.getLogger().info("Added new config value: " + key); + update = true; } + if (Keklist.isDebug()) + plugin.getLogger().info("Added new config value: " + key); + oldConfig.setComments(key, newConfig.getComments(key)); oldConfig.setInlineComments(key, newConfig.getInlineComments(key)); } @@ -45,12 +48,13 @@ public void updateConfig() { oldConfig.save(new File(plugin.getDataFolder(), "config.yml")); plugin.reloadConfig(); - plugin.getLogger().info("Config updated to version: " + newConfig.get("config_version") + "!"); + if (update) + plugin.getLogger().info("Config updated to version: " + newConfig.get("config_version") + "!"); reader.close(); } catch (IOException | InvalidConfigurationException e) { - plugin.getLogger().severe("Error while updating config! Please report this to the developer!"); - plugin.getLogger().severe(e.getMessage()); + plugin.getLogger().severe("Error while updating config! Please report this to the developer!"); + plugin.getLogger().severe(e.getMessage()); } } } diff --git a/src/main/resources/assets/lang/en-us.json b/src/main/resources/assets/lang/en-us.json index 806ab1b..716cfd7 100644 --- a/src/main/resources/assets/lang/en-us.json +++ b/src/main/resources/assets/lang/en-us.json @@ -228,6 +228,10 @@ "keklist.manage.disabled": "This command is disabled due to security reasons! This can be enabled in the config.", "floodgate.api-key-not-set": "You did not set the Floodgate API key in the config! Either get the floodgate uuid from https://mcprofile.io using the UUID directly or the floodgate user must be in the server's cache otherwise the request will fail!", + "geyser.prefix": "Geyser detected, using prefix: %s", + "geyser.events.registered": "Geyser events registered!", + "geyser.events.error": "An error occurred while checking the IP blacklist on geyser: %s", + "geyser.events.fallback": "Fallback to deny connection.", "http.error": "Could not connect to the Mojang-API!", "http.not-found": "Could not find the player! More information: %s", diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml deleted file mode 100644 index 32359fe..0000000 --- a/src/main/resources/config.yml +++ /dev/null @@ -1,93 +0,0 @@ -# Settings for every feature of the Keklist Paper plugin - -language: "en-us" # Language for the plugin -enable-manage-command: false # Enable the /keklist command to enable/disable the blacklist/whitelist -date-format: "dd-MM-yyyy HH:mm" # Date format for the blacklist/whitelist GUI -chat-notify: false # Notify the player if someone joins the server and is blacklisted/whitelisted - -update: - check: true # Check for updates - interval: 12 # Check for updates every x hours - -# Settings for the blacklist feature -blacklist: - enabled: true # Enable the blacklist feature - allow-join-with-admin: false # Allows blacklisted player to join if a player with the admin-permission is online - admin-permission: "blacklist.admin" - limbo: false # May let the player join if nobody is online but fallback kicked; Needs velocity plugin - icon-file: "default" # The icon for the server if player is blacklisted. Put the file in the same folder as the config - -# Settings for the whitelist feature -# Note: THIS WILL OVERRIDE THE DEFAULT WHITELIST; It works the same way as the default whitelist but does not import the players automatically -whitelist: - enabled: false # Enable the whitelist feature - change-motd: true # Change the motd if the server is in whitelist mode - hide-online-players: false # This sends fake player's AND fake player counts to the client - fake-players: # List of fake players that are shown to the client NOTE: Can be empty - - SageSphinx63920 - - hdgaymer1404Jonas - - LPTP1 - fake-max-range: 20-40 # Please use the format: INTEGER-INTEGER - fake-online-range: 0-10 # Please use the format: INTEGER-INTEGER - -# Extentions for the plugin -placeholderapi: false # Enable PlaceholderAPI support -plan-support: false # Enable support for the Plan plugin - -# Note: If you use a proxy you need to enable 'send-floodgate-data' on the proxy's config -# More: https://wiki.geysermc.org/floodgate/setup/ -floodgate: - prefix: "." # Prefix for the floodgate player - api-key: "your-api" # https://mcprofile.io api key for getting the floodgate uuid for a bedrock player not being in the server's cache. Please read the wiki for more information - -discord: - enabled: false - webhook-url: https://discord.com # Webhook url for the discord webhook - username: "Keklist" # Username for the webhook - avatar-url: https://cdn.discordapp.com/attachments/1056727727991959673/1102655035290157176/keklist.png # This is the default avatar from keklist - events: ["blacklist_add", "blacklist_remove", "blacklist_kick", "whitelist_add", "whitelist_remove", "whitelist_kick", "limbo"] # List of events which trigger a message - ping-roles: ["214809157574983681"] # Pings this role on any event. NOTE: Can be empty - -# Settings for the database -# This is useful to share the blacklist and whitelist between multiple servers -mariadb: - enabled: false - host: localhost - port: 3306 - database: keklist - username: root - password: root - options: "?useSSL=false&serverTimezone=MEZ" - -# Messages for the plugin -# This fully supports minimessage -messages: - motd: - blacklisted: - - "Your IP is blacklisted on this server!" - - "You are not allowed to join this server!" - whitelisted: - - "The server is in whitelist mode but you can join." - - "Glad you can join us!" - default: - - "This is a normal motd" - - "By default these are visible for everyone" - - kick: - blacklisted: - - "You're kicked because you're on the blacklist!" - - "Sorry, but you're not allowed to join this server!" - whitelisted: - - "You're kicked because the server is in whitelist mode!" - - "Sorry, but you're not on the whitelist!" - -# Enable bstats -# This is used to collect anonymous data about the plugin, which helps me to improve the plugin -bstats: true - -# Debug mode -# This will enable debug messages in the console -debug: false - -# Don't touch it! -config_version: 1 \ No newline at end of file diff --git a/src/main/resources/paper-plugin.yml b/src/main/resources/paper-plugin.yml index 98b706e..99a0c2e 100644 --- a/src/main/resources/paper-plugin.yml +++ b/src/main/resources/paper-plugin.yml @@ -1,8 +1,8 @@ name: Keklist -version: '1.0.0' +version: ${project.version} main: de.hdg.keklist.Keklist api-version: '1.20' -description: Custom made white and blacklist with many features! +description: ${project.description} authors: [ "hdgamer1404Jonas", "SageSphinx63920" ] website: https://keklist.pages.dev loader: de.hdg.keklist.KeklistLoader @@ -26,7 +26,10 @@ dependencies: PlaceholderAPI: required: false - + + Geyser-Spigot: + load: BEFORE + required: false permissions: keklist.gui.use: diff --git a/start.sh b/start.sh index 675a48b..09f3ce0 100644 --- a/start.sh +++ b/start.sh @@ -2,7 +2,7 @@ mvn package -f pom.xml mkdir -p ./testserver/plugins -cp ./target/keklist-1.0-SNAPSHOT.jar ./testserver/plugins +cp ./target/keklist-*.*.*-SNAPSHOT.jar ./testserver/plugins cd ./testserver || exit