Skip to content

Commit

Permalink
World WebHook (#32)
Browse files Browse the repository at this point in the history
* Added `world_loader` WebHook

* Added `world_save` WebHook

* Added `world_unload` WebHook

* meet code standards
  • Loading branch information
Shweit authored Aug 31, 2024
1 parent 20c9303 commit 18f3966
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/main/java/com/shweit/serverapi/webhooks/RegisterWebHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/shweit/serverapi/webhooks/WebHookEnum.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/com/shweit/serverapi/webhooks/world/WorldLoad.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
32 changes: 32 additions & 0 deletions src/main/java/com/shweit/serverapi/webhooks/world/WorldSave.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
32 changes: 32 additions & 0 deletions src/main/java/com/shweit/serverapi/webhooks/world/WorldUnload.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
3 changes: 3 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down

0 comments on commit 18f3966

Please sign in to comment.