Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inventory WebHook #29

Merged
merged 4 commits into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/main/java/com/shweit/serverapi/webhooks/RegisterWebHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
import com.shweit.serverapi.webhooks.block.*;
import com.shweit.serverapi.webhooks.enchantment.EnchantItem;
import com.shweit.serverapi.webhooks.entity.*;
import com.shweit.serverapi.webhooks.inventory.Brew;
import com.shweit.serverapi.webhooks.inventory.CraftItem;
import com.shweit.serverapi.webhooks.inventory.FurnaceBurn;
import com.shweit.serverapi.webhooks.inventory.FurnaceSmelt;
import com.shweit.serverapi.webhooks.server.PluginDisable;
import com.shweit.serverapi.webhooks.server.PluginEnable;
import com.shweit.serverapi.webhooks.server.ServerStart;
Expand Down Expand Up @@ -82,6 +86,21 @@ public void registerWebHooks(final FileConfiguration config) {

new ExplosionPrime().register();
Logger.debug("Registered explosion_prime WebHook");

new PlayerDeath().register();
Logger.debug("Registered player_death WebHook");

new Brew().register();
Logger.debug("Registered brew WebHook");

new CraftItem().register();
Logger.debug("Registered craft_item WebHook");

new FurnaceBurn().register();
Logger.debug("Registered furnace_burn WebHook");

new FurnaceSmelt().register();
Logger.debug("Registered furnace_smelt WebHook");
}

public static void sendToAllUrls(final JSONObject jsonObject) {
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/com/shweit/serverapi/webhooks/WebHookEnum.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ public enum WebHookEnum {
ENTITY_SHOOT_BOW("entity_shoot_bow", "Triggered when an entity shoots a bow"),
ENTITY_TAME("entity_tame", "Triggered when an entity is tamed"),
EXPLOSION_PRIME("explosion_prime", "Triggered when an explosion is primed"),
PLAYER_DEATH("player_death", "Triggered when a player dies"),;
PLAYER_DEATH("player_death", "Triggered when a player dies"),
BREW("brew", "Triggered when a potion is brewed"),
CRAFT_ITEM("craft_item", "Triggered when an item is crafted"),
FURNACE_BURN("furnace_burn", "Triggered when a furnace burns an item as fuel"),
FURNACE_SMELT("furnace_smelt", "Triggered when a furnace smelts an item"),;

public final String label;
public final String description;
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/com/shweit/serverapi/webhooks/inventory/Brew.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.shweit.serverapi.webhooks.inventory;

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.inventory.BrewEvent;
import org.json.JSONObject;

public final class Brew implements WebHook, Listener {

private final String eventName = WebHookEnum.BREW.label;

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

@EventHandler
public void onBrew(final BrewEvent event) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("event", eventName);
jsonObject.put("fuelLevel", event.getFuelLevel());
jsonObject.put("location", event.getBlock().getLocation().toString());

if (event.getContents().getIngredient() != null) {
jsonObject.put("ingredient", event.getContents().getIngredient().getType().name());
}
RegisterWebHooks.sendToAllUrls(jsonObject);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.shweit.serverapi.webhooks.inventory;

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.inventory.CraftItemEvent;
import org.json.JSONObject;

public final class CraftItem implements WebHook, Listener {

private final String eventName = WebHookEnum.CRAFT_ITEM.label;

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

@EventHandler
public void onCraftItem(final CraftItemEvent event) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("event", eventName);
jsonObject.put("item", event.getRecipe().getResult().getType().name());
jsonObject.put("location", event.getWhoClicked().getLocation().toString());
jsonObject.put("recipe", event.getRecipe().toString());

if (event.getInventory().getResult() != null) {
jsonObject.put("result", event.getInventory().getResult().getType().name());
}

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

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.inventory.FurnaceBurnEvent;
import org.json.JSONObject;

public final class FurnaceBurn implements WebHook, Listener {

private final String eventName = WebHookEnum.FURNACE_BURN.label;

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

@EventHandler
public void onFurnaceBurn(final FurnaceBurnEvent event) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("event", eventName);
jsonObject.put("location", event.getBlock().getLocation().toString());
jsonObject.put("burnTime", event.getBurnTime());
jsonObject.put("fuel", event.getFuel().getType().name());

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

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.inventory.FurnaceSmeltEvent;
import org.json.JSONObject;

public final class FurnaceSmelt implements WebHook, Listener {

private final String eventName = WebHookEnum.FURNACE_SMELT.label;

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

@EventHandler
public void onFurnaceSmelt(final FurnaceSmeltEvent event) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("event", eventName);
jsonObject.put("location", event.getBlock().getLocation().toString());
jsonObject.put("result", event.getResult().getType().name());
jsonObject.put("source", event.getSource().getType().name());

RegisterWebHooks.sendToAllUrls(jsonObject);
}
}
4 changes: 4 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ webhooks:
entity_tame: true
explosion_prime: true
player_death: true
brew: true
craft_item: false # This is disabled by default, because it can spam your Webhook-URLs.
furnace_burn: true
furnace_smelt: 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
Loading