From 18f39664652c13005e95474f6ee7ee76941789b2 Mon Sep 17 00:00:00 2001 From: Dennis van den Brock <72552171+Shweit@users.noreply.github.com> Date: Sat, 31 Aug 2024 21:44:31 +0200 Subject: [PATCH] World WebHook (#32) * Added `world_loader` WebHook * Added `world_save` WebHook * Added `world_unload` WebHook * meet code standards --- .../serverapi/webhooks/RegisterWebHooks.java | 12 +++++++ .../serverapi/webhooks/WebHookEnum.java | 5 ++- .../serverapi/webhooks/world/WorldLoad.java | 32 +++++++++++++++++++ .../serverapi/webhooks/world/WorldSave.java | 32 +++++++++++++++++++ .../serverapi/webhooks/world/WorldUnload.java | 32 +++++++++++++++++++ src/main/resources/config.yml | 3 ++ 6 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/shweit/serverapi/webhooks/world/WorldLoad.java create mode 100644 src/main/java/com/shweit/serverapi/webhooks/world/WorldSave.java create mode 100644 src/main/java/com/shweit/serverapi/webhooks/world/WorldUnload.java diff --git a/src/main/java/com/shweit/serverapi/webhooks/RegisterWebHooks.java b/src/main/java/com/shweit/serverapi/webhooks/RegisterWebHooks.java index a7b16a9..96c76a2 100644 --- a/src/main/java/com/shweit/serverapi/webhooks/RegisterWebHooks.java +++ b/src/main/java/com/shweit/serverapi/webhooks/RegisterWebHooks.java @@ -15,6 +15,9 @@ import com.shweit.serverapi.webhooks.weather.LightningStrike; import com.shweit.serverapi.webhooks.weather.ThunderChange; import com.shweit.serverapi.webhooks.weather.WeatherChange; +import com.shweit.serverapi.webhooks.world.WorldLoad; +import com.shweit.serverapi.webhooks.world.WorldSave; +import com.shweit.serverapi.webhooks.world.WorldUnload; import org.bukkit.configuration.file.FileConfiguration; import org.json.JSONObject; @@ -141,6 +144,15 @@ public void registerWebHooks(final FileConfiguration config) { new ThunderChange().register(); Logger.debug("Registered thunder_change WebHook"); + + new WorldLoad().register(); + Logger.debug("Registered world_load WebHook"); + + new WorldSave().register(); + Logger.debug("Registered world_save WebHook"); + + new WorldUnload().register(); + Logger.debug("Registered world_unload WebHook"); } public static void sendToAllUrls(final JSONObject jsonObject) { diff --git a/src/main/java/com/shweit/serverapi/webhooks/WebHookEnum.java b/src/main/java/com/shweit/serverapi/webhooks/WebHookEnum.java index 57b9fcb..289cb5e 100644 --- a/src/main/java/com/shweit/serverapi/webhooks/WebHookEnum.java +++ b/src/main/java/com/shweit/serverapi/webhooks/WebHookEnum.java @@ -38,7 +38,10 @@ public enum WebHookEnum { PLAYER_RESPAWN("player_respawn", "Triggered when a player respawns"), LIGHTNING_STRIKE("lightning_strike", "Triggered when lightning strikes"), WEATHER_CHANGE("weather_change", "Triggered when the weather changes"), - THUNDER_CHANGE("thunder_change", "Triggered when the thunder changes"),; + THUNDER_CHANGE("thunder_change", "Triggered when the thunder changes"), + WORLD_LOAD("world_load", "Triggered when a world is loaded"), + WORLD_SAVE("world_save", "Triggered when a world is saved"), + WORLD_UNLOAD("world_unload", "Triggered when a world is unloaded"); public final String label; public final String description; diff --git a/src/main/java/com/shweit/serverapi/webhooks/world/WorldLoad.java b/src/main/java/com/shweit/serverapi/webhooks/world/WorldLoad.java new file mode 100644 index 0000000..fa189b1 --- /dev/null +++ b/src/main/java/com/shweit/serverapi/webhooks/world/WorldLoad.java @@ -0,0 +1,32 @@ +package com.shweit.serverapi.webhooks.world; + +import com.shweit.serverapi.MinecraftServerAPI; +import com.shweit.serverapi.webhooks.RegisterWebHooks; +import com.shweit.serverapi.webhooks.WebHook; +import com.shweit.serverapi.webhooks.WebHookEnum; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.world.WorldLoadEvent; +import org.json.JSONObject; + +public final class WorldLoad implements WebHook, Listener { + + private final String eventName = WebHookEnum.WORLD_LOAD.label; + + @Override + public void register() { + if (RegisterWebHooks.doActivateWebhook(eventName)) { + MinecraftServerAPI plugin = MinecraftServerAPI.getInstance(); + plugin.getServer().getPluginManager().registerEvents(this, plugin); + } + } + + @EventHandler + public void onWorldLoad(final WorldLoadEvent event) { + JSONObject jsonObject = new JSONObject(); + jsonObject.put("event", eventName); + jsonObject.put("world", event.getWorld().getName()); + + RegisterWebHooks.sendToAllUrls(jsonObject); + } +} diff --git a/src/main/java/com/shweit/serverapi/webhooks/world/WorldSave.java b/src/main/java/com/shweit/serverapi/webhooks/world/WorldSave.java new file mode 100644 index 0000000..81bb948 --- /dev/null +++ b/src/main/java/com/shweit/serverapi/webhooks/world/WorldSave.java @@ -0,0 +1,32 @@ +package com.shweit.serverapi.webhooks.world; + +import com.shweit.serverapi.MinecraftServerAPI; +import com.shweit.serverapi.webhooks.RegisterWebHooks; +import com.shweit.serverapi.webhooks.WebHook; +import com.shweit.serverapi.webhooks.WebHookEnum; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.world.WorldSaveEvent; +import org.json.JSONObject; + +public final class WorldSave implements WebHook, Listener { + + private final String eventName = WebHookEnum.WORLD_SAVE.label; + + @Override + public void register() { + if (RegisterWebHooks.doActivateWebhook(eventName)) { + MinecraftServerAPI plugin = MinecraftServerAPI.getInstance(); + plugin.getServer().getPluginManager().registerEvents(this, plugin); + } + } + + @EventHandler + public void onWorldSave(final WorldSaveEvent event) { + JSONObject jsonObject = new JSONObject(); + jsonObject.put("event", eventName); + jsonObject.put("world", event.getWorld().getName()); + + RegisterWebHooks.sendToAllUrls(jsonObject); + } +} diff --git a/src/main/java/com/shweit/serverapi/webhooks/world/WorldUnload.java b/src/main/java/com/shweit/serverapi/webhooks/world/WorldUnload.java new file mode 100644 index 0000000..60f14d9 --- /dev/null +++ b/src/main/java/com/shweit/serverapi/webhooks/world/WorldUnload.java @@ -0,0 +1,32 @@ +package com.shweit.serverapi.webhooks.world; + +import com.shweit.serverapi.MinecraftServerAPI; +import com.shweit.serverapi.webhooks.RegisterWebHooks; +import com.shweit.serverapi.webhooks.WebHook; +import com.shweit.serverapi.webhooks.WebHookEnum; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.world.WorldUnloadEvent; +import org.json.JSONObject; + +public final class WorldUnload implements WebHook, Listener { + + private final String eventName = WebHookEnum.WORLD_UNLOAD.label; + + @Override + public void register() { + if (RegisterWebHooks.doActivateWebhook(eventName)) { + MinecraftServerAPI plugin = MinecraftServerAPI.getInstance(); + plugin.getServer().getPluginManager().registerEvents(this, plugin); + } + } + + @EventHandler + public void onWorldUnload(final WorldUnloadEvent event) { + JSONObject jsonObject = new JSONObject(); + jsonObject.put("event", eventName); + jsonObject.put("world", event.getWorld().getName()); + + RegisterWebHooks.sendToAllUrls(jsonObject); + } +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 3cf176a..eda2c98 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -61,6 +61,9 @@ webhooks: lightning_strike: true weather_change: true thunder_change: true + world_load: true + world_save: true + world_unload: true # Here you can list an extra set of Files or Directories which should be saved by the Backup-System. # Following directories and files are already saved by default: ["world", "world_nether", "world_the_end", "plugins", "config", "server.properties", "banned-ips.json", "banned-players.json", "ops.json", "whitelist.json"]