Skip to content

Commit

Permalink
generic game event
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Feb 12, 2022
1 parent a09a119 commit 58c3318
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
@@ -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:<game_event_name> 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
// <context.location> returns the location of the event.
// <context.entity> returns the entity that triggered the event, if any.
// <context.game_event> 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>.
// <context.radius> 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);
}
}

0 comments on commit 58c3318

Please sign in to comment.