Skip to content

Commit

Permalink
Weather WebHook (#31)
Browse files Browse the repository at this point in the history
* Added `lightning_strike` WebHook

* Added `weather_change`, `thunder_change` WebHook

* meet Code standards

* made json parameter in WeatherChange more clear
  • Loading branch information
Shweit authored Aug 31, 2024
1 parent 1eff9b1 commit 20c9303
Show file tree
Hide file tree
Showing 6 changed files with 118 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 @@ -12,6 +12,9 @@
import com.shweit.serverapi.webhooks.server.PluginDisable;
import com.shweit.serverapi.webhooks.server.PluginEnable;
import com.shweit.serverapi.webhooks.server.ServerStart;
import com.shweit.serverapi.webhooks.weather.LightningStrike;
import com.shweit.serverapi.webhooks.weather.ThunderChange;
import com.shweit.serverapi.webhooks.weather.WeatherChange;
import org.bukkit.configuration.file.FileConfiguration;
import org.json.JSONObject;

Expand Down Expand Up @@ -129,6 +132,15 @@ public void registerWebHooks(final FileConfiguration config) {

new PlayerRespawn().register();
Logger.debug("Registered player_respawn WebHook");

new LightningStrike().register();
Logger.debug("Registered lightning_strike WebHook");

new WeatherChange().register();
Logger.debug("Registered weather_change WebHook");

new ThunderChange().register();
Logger.debug("Registered thunder_change 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 @@ -35,7 +35,10 @@ public enum WebHookEnum {
PLAYER_JOIN("player_join", "Triggered when a player joins the server"),
PLAYER_KICK("player_kick", "Triggered when a player is kicked from the server"),
PLAYER_QUIT("player_quit", "Triggered when a player quits the server"),
PLAYER_RESPAWN("player_respawn", "Triggered when a player respawns"),;
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"),;

public final String label;
public final String description;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.shweit.serverapi.webhooks.weather;

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.weather.LightningStrikeEvent;
import org.json.JSONObject;

public final class LightningStrike implements WebHook, Listener {

private final String eventName = WebHookEnum.LIGHTNING_STRIKE.label;

@Override
public void register() {
if (RegisterWebHooks.doActivateWebhook(eventName)) {
MinecraftServerAPI plugin = MinecraftServerAPI.getInstance();
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}
}

@EventHandler
public void onLightningStrike(final LightningStrikeEvent event) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("event", eventName);
jsonObject.put("location", event.getLightning().getLocation().toString());
jsonObject.put("cause", event.getCause().name());

RegisterWebHooks.sendToAllUrls(jsonObject);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.shweit.serverapi.webhooks.weather;

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.weather.ThunderChangeEvent;
import org.json.JSONObject;

public final class ThunderChange implements WebHook, Listener {

private final String eventName = WebHookEnum.THUNDER_CHANGE.label;

@Override
public void register() {
if (RegisterWebHooks.doActivateWebhook(eventName)) {
MinecraftServerAPI plugin = MinecraftServerAPI.getInstance();
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}
}

@EventHandler
public void onThunderChange(final ThunderChangeEvent event) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("event", eventName);
jsonObject.put("world", event.getWorld().getName());
jsonObject.put("thunder", event.toThunderState());

RegisterWebHooks.sendToAllUrls(jsonObject);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.shweit.serverapi.webhooks.weather;

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.weather.WeatherChangeEvent;
import org.json.JSONObject;

public final class WeatherChange implements WebHook, Listener {

private final String eventName = WebHookEnum.WEATHER_CHANGE.label;

@Override
public void register() {
if (RegisterWebHooks.doActivateWebhook(eventName)) {
MinecraftServerAPI plugin = MinecraftServerAPI.getInstance();
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}
}

@EventHandler
public void onWeatherChange(final WeatherChangeEvent event) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("event", eventName);
jsonObject.put("world", event.getWorld().getName());
jsonObject.put("rain", event.toWeatherState());

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 @@ -58,6 +58,9 @@ webhooks:
player_kick: true
player_quit: true
player_respawn: true
lightning_strike: true
weather_change: true
thunder_change: 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 20c9303

Please sign in to comment.