Skip to content

Commit

Permalink
Add npc opens <block> event (#2293)
Browse files Browse the repository at this point in the history
* Add `npc opens <block>`

* No point in a location matcher

* Add `Cancellable` doc
  • Loading branch information
tal5 committed Jan 17, 2022
1 parent 68872f3 commit a88baba
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
Expand Up @@ -5,6 +5,7 @@
import com.denizenscript.denizen.events.entity.*;
import com.denizenscript.denizen.events.item.*;
import com.denizenscript.denizen.events.npc.NPCNavigationScriptEvent;
import com.denizenscript.denizen.events.npc.NPCOpensScriptEvent;
import com.denizenscript.denizen.events.npc.NPCSpawnScriptEvent;
import com.denizenscript.denizen.events.npc.NPCStuckScriptEvent;
import com.denizenscript.denizen.events.player.*;
Expand All @@ -23,6 +24,7 @@ public class ScriptEventRegistry {

public static void registerCitizensEvents() {
ScriptEvent.registerScriptEvent(NPCNavigationScriptEvent.class);
ScriptEvent.registerScriptEvent(NPCOpensScriptEvent.class);
ScriptEvent.registerScriptEvent(NPCSpawnScriptEvent.class);
ScriptEvent.registerScriptEvent(NPCStuckScriptEvent.class);
}
Expand Down
@@ -0,0 +1,87 @@
package com.denizenscript.denizen.events.npc;

import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizen.objects.LocationTag;
import com.denizenscript.denizen.objects.MaterialTag;
import com.denizenscript.denizen.objects.NPCTag;
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.scripts.ScriptEntryData;
import net.citizensnpcs.api.event.NPCOpenDoorEvent;
import net.citizensnpcs.api.event.NPCOpenGateEvent;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

public class NPCOpensScriptEvent extends BukkitScriptEvent implements Listener {

// <--[event]
// @Events
// npc opens <block>
//
// @Group NPC
//
// @Location true
//
// @Cancellable true
//
// @Triggers when an NPC opens a door or gate.
//
// @Context
// <context.location> returns the location of the door or gate opened.
//
// @NPC Always.
//
// -->

public NPCOpensScriptEvent() {
instance = this;
registerCouldMatcher("npc opens <block>");
}

public static NPCOpensScriptEvent instance;
public NPCTag npc;
public LocationTag location;

@Override
public boolean matches(ScriptPath path) {
if (!runInCheck(path, location)) {
return false;
}
if (!tryMaterial(new MaterialTag(location.getBlock()), path.eventArgLowerAt(2))) {
return false;
}
return super.matches(path);
}

@Override
public String getName() {
return "NPCDoor";
}

@Override
public ScriptEntryData getScriptEntryData() {
return new BukkitScriptEntryData(null, npc);
}

@Override
public ObjectTag getContext(String name) {
if (name.equals("location")) {
return location;
}
return super.getContext(name);
}

@EventHandler
public void NPCOpenDoor(NPCOpenDoorEvent event) {
npc = new NPCTag(event.getNPC());
location = new LocationTag(event.getDoor().getLocation());
fire(event);
}

@EventHandler
public void NPCOpenGate(NPCOpenGateEvent event) {
npc = new NPCTag(event.getNPC());
location = new LocationTag(event.getGate().getLocation());
fire(event);
}
}

0 comments on commit a88baba

Please sign in to comment.