This repository has been archived by the owner on Apr 12, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
experimental 'internal event' scumhackery
- Loading branch information
1 parent
e36a852
commit 1ae0a61
Showing
2 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
src/main/java/com/denizenscript/denizen2sponge/events/server/InternalScriptEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package com.denizenscript.denizen2sponge.events.server; | ||
|
||
import com.denizenscript.denizen2core.events.ScriptEvent; | ||
import com.denizenscript.denizen2core.tags.AbstractTagObject; | ||
import com.denizenscript.denizen2core.tags.objects.MapTag; | ||
import com.denizenscript.denizen2core.tags.objects.TextTag; | ||
import com.denizenscript.denizen2sponge.Denizen2Sponge; | ||
import org.spongepowered.api.Sponge; | ||
import org.spongepowered.api.event.Cancellable; | ||
import org.spongepowered.api.event.Event; | ||
import org.spongepowered.api.event.Listener; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class InternalScriptEvent extends ScriptEvent { | ||
|
||
// <--[event] | ||
// @Events | ||
// internal event | ||
// | ||
// @Updated 2016/12/11 | ||
// | ||
// @Cancellable true | ||
// | ||
// @Note Cancellability depends on internal event. | ||
// | ||
// @Triggers when any internal event occurs. | ||
// | ||
// @Switch event_type (TextTag) checks the event type. | ||
// | ||
// @Context | ||
// event_type (TextTag) returns the internal name of the event type. | ||
// cause (MapTag) returns a simple Text:TextTag map of all named causes in the event. | ||
// | ||
// @Determinations | ||
// None | ||
// --> | ||
|
||
@Override | ||
public String getName() { | ||
return "InternalEvent"; | ||
} | ||
|
||
@Override | ||
public boolean couldMatch(ScriptEventData data) { | ||
return data.eventPath.startsWith("internal event"); | ||
} | ||
|
||
@Override | ||
public boolean matches(ScriptEventData data) { | ||
if (data.switches.containsKey("type")) { | ||
if (!data.switches.get("event_type").equalsIgnoreCase(event_type.getInternal())) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
public TextTag event_type; | ||
|
||
public MapTag cause; | ||
|
||
public Event internal; | ||
|
||
@Override | ||
public HashMap<String, AbstractTagObject> getDefinitions(ScriptEventData data) { | ||
HashMap<String, AbstractTagObject> defs = super.getDefinitions(data); | ||
defs.put("event_type", event_type); | ||
defs.put("cause", cause); | ||
return defs; | ||
} | ||
|
||
@Override | ||
public void enable() { | ||
Sponge.getEventManager().registerListeners(Denizen2Sponge.instance, this); | ||
} | ||
|
||
@Override | ||
public void disable() { | ||
Sponge.getEventManager().unregisterListeners(this); | ||
} | ||
|
||
@Listener | ||
public void onInternalEvent(Event evt) { | ||
InternalScriptEvent event = (InternalScriptEvent) clone(); | ||
event.internal = evt; | ||
event.event_type = new TextTag(evt.getClass().getTypeName()); | ||
if (evt instanceof Cancellable) { | ||
event.cancelled = ((Cancellable) evt).isCancelled(); | ||
} | ||
else { | ||
event.cancelled = false; | ||
} | ||
Map<String, Object> causes = evt.getCause().getNamedCauses(); | ||
event.cause = new MapTag(); | ||
for (Map.Entry<String, Object> tc : causes.entrySet()) { | ||
event.cause.getInternal().put(tc.getKey(), new TextTag(tc.getValue().toString())); | ||
} | ||
event.run(); | ||
if (evt instanceof Cancellable) { | ||
((Cancellable) evt).setCancelled(event.cancelled); | ||
} | ||
} | ||
|
||
@Override | ||
public void applyDetermination(boolean errors, String determination, AbstractTagObject value) { | ||
super.applyDetermination(errors, determination, value); | ||
} | ||
} |