diff --git a/plugin/src/main/java/com/denizenscript/denizen/events/ScriptEventRegistry.java b/plugin/src/main/java/com/denizenscript/denizen/events/ScriptEventRegistry.java index f84b257f7a..a17fb59276 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/events/ScriptEventRegistry.java +++ b/plugin/src/main/java/com/denizenscript/denizen/events/ScriptEventRegistry.java @@ -12,6 +12,8 @@ import com.denizenscript.denizen.events.server.*; import com.denizenscript.denizen.events.vehicle.*; import com.denizenscript.denizen.events.world.*; +import com.denizenscript.denizen.nms.NMSHandler; +import com.denizenscript.denizen.nms.NMSVersion; import com.denizenscript.denizen.utilities.depends.Depends; import com.denizenscript.denizencore.events.ScriptEvent; import com.denizenscript.denizencore.events.ScriptEventCouldMatcher; @@ -234,6 +236,9 @@ public static void registerMainEvents() { // World events ScriptEvent.registerScriptEvent(ChunkLoadScriptEvent.class); ScriptEvent.registerScriptEvent(ChunkUnloadScriptEvent.class); + if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_17)) { + ScriptEvent.registerScriptEvent(GenericGameEventScriptEvent.class); + } ScriptEvent.registerScriptEvent(LightningStrikesScriptEvent.class); ScriptEvent.registerScriptEvent(LingeringPotionSplashScriptEvent.class); ScriptEvent.registerScriptEvent(LootGenerateScriptEvent.class); diff --git a/plugin/src/main/java/com/denizenscript/denizen/events/world/GenericGameEventScriptEvent.java b/plugin/src/main/java/com/denizenscript/denizen/events/world/GenericGameEventScriptEvent.java new file mode 100644 index 0000000000..f797b7730e --- /dev/null +++ b/plugin/src/main/java/com/denizenscript/denizen/events/world/GenericGameEventScriptEvent.java @@ -0,0 +1,93 @@ +package com.denizenscript.denizen.events.world; + +import com.denizenscript.denizen.events.BukkitScriptEvent; +import com.denizenscript.denizen.objects.EntityTag; +import com.denizenscript.denizen.objects.LocationTag; +import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData; +import com.denizenscript.denizencore.objects.ObjectTag; +import com.denizenscript.denizencore.objects.core.ElementTag; +import com.denizenscript.denizencore.scripts.ScriptEntryData; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.world.GenericGameEvent; + +public class GenericGameEventScriptEvent extends BukkitScriptEvent implements Listener { + + // <--[event] + // @Events + // generic game event + // + // @Group World + // + // @Location true + // + // @Switch type: to only process the event when a specific game event is fired. + // + // @Cancellable true + // + // @Triggers when the minecraft world experiences a generic minecraft game event. This is normally used for sculk sensors. + // + // @Context + // returns the location of the event. + // returns the entity that triggered the event, if any. + // returns the name of the Minecraft game event, for example "minecraft:block_change". See <@link url https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/GameEvent.html>. + // returns the radius, in blocks, that the event is broadcast to. + // + // --> + + public GenericGameEventScriptEvent() { + instance = this; + registerCouldMatcher("generic game event"); + registerSwitches("type"); + } + + public static GenericGameEventScriptEvent instance; + public LocationTag location; + public GenericGameEvent event; + + @Override + public boolean matches(ScriptPath path) { + if (!runInCheck(path, location)) { + return false; + } + String typeSwitch = path.switches.get("type"); + if (typeSwitch != null) { + if (!runGenericCheck(typeSwitch, event.getEvent().getKey().toString()) && !runGenericCheck(typeSwitch, event.getEvent().getKey().getKey())) { + return false; + } + } + return super.matches(path); + } + + @Override + public String getName() { + return "WorldSaves"; + } + + @Override + public ScriptEntryData getScriptEntryData() { + return new BukkitScriptEntryData(event.getEntity()); + } + + @Override + public ObjectTag getContext(String name) { + switch (name) { + case "location": return location; + case "entity": + if (event.getEntity() != null) { + return new EntityTag(event.getEntity()).getDenizenObject(); + } + break; + case "game_event": return new ElementTag(event.getEvent().getKey().toString()); + case "radius": return new ElementTag(event.getRadius()); + } + return super.getContext(name); + } + + @EventHandler + public void onGenericGameEvent(GenericGameEvent event) { + location = new LocationTag(event.getLocation()); + this.event = event; + fire(event); + } +}