Skip to content

Commit

Permalink
event debugging contexts don't need to exist
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Nov 14, 2021
1 parent 1421c9c commit b2159ad
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 39 deletions.
43 changes: 18 additions & 25 deletions src/main/java/com/denizenscript/denizencore/events/ScriptEvent.java
Expand Up @@ -23,7 +23,6 @@

import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.regex.Pattern;

/**
Expand Down Expand Up @@ -82,19 +81,25 @@ public static void registerScriptEvent(ScriptEvent event) {
*/
public static HashMap<String, ScriptEvent> eventLookup = new HashMap<>();

public static class InternalEventData {

/**
* Statistics about an event firing.
*/
public long stats_fires = 0, stats_scriptFires = 0, stats_nanoTimes = 0;
}

/**
* Statistics about an event firing.
* This ScriptEvent object's base data (separate from the firing-related data of an event happening). Stored in a separate instance to avoid duplication issues.
*/
public static class StatData {
public long fires = 0;
public long scriptFires = 0;
public long nanoTimes = 0;
}
public InternalEventData eventData = new InternalEventData();

public ArrayList<ScriptPath> eventPaths = new ArrayList<>();

/**
* This event's statistics. Stored in a separate instance to avoid duplication issues.
* Whether this event has been cancelled.
*/
public StatData stats = new StatData();
public boolean cancelled = false;

/**
* Represents a single path for an event within a world container, based on raw text of a script.
Expand Down Expand Up @@ -424,10 +429,6 @@ else if (path.switch_ignoreCancelled != null) {
*/
public static List<BiFunction<ScriptEvent, ScriptPath, Boolean>> extraMatchers = new ArrayList<>();

public ArrayList<ScriptPath> eventPaths = new ArrayList<>();

public boolean cancelled = false;

// <--[language]
// @name Script Event Priority
// @group Script Events
Expand Down Expand Up @@ -518,7 +519,7 @@ public boolean matches(ScriptPath path) {
*/
public ScriptEvent fire() {
ScriptEvent copy = clone();
stats.fires++;
eventData.stats_fires++;
for (ScriptPath path : eventPaths) {
try {
if (matchesScript(copy, path)) {
Expand All @@ -539,11 +540,9 @@ public ScriptEvent fire() {
return copy;
}

private String currentEvent;

public void run(ScriptPath path) {
try {
stats.scriptFires++;
eventData.stats_scriptFires++;
if (path.container.shouldDebug()) {
Debug.echoDebug(path.container, "<Y>Running script event '<A>" + getName() + "<Y>', event='<A>" + (path.fireAfter ? "after " : "on ") + path.event + "<Y>'"
+ " for script '<A>" + path.container.getName() + "<Y>'");
Expand All @@ -554,13 +553,12 @@ public void run(ScriptPath path) {
List<ScriptEntry> entries = ScriptContainer.cleanDup(getScriptEntryData(), path.set);
ScriptQueue queue = new InstantQueue(path.container.getName());
queue.addEntries(entries);
currentEvent = path.event;
queue.setContextSource(this);
if (!path.fireAfter) {
queue.determinationTarget = (o) -> handleBaseDetermination(path, o);
}
queue.start();
stats.nanoTimes += System.nanoTime() - queue.startTime;
eventData.stats_nanoTimes += System.nanoTime() - queue.startTime;
}
catch (Exception e) {
Debug.echoError("Handling script " + path.container.getName() + " path:" + path.event + ":::");
Expand All @@ -574,20 +572,15 @@ public void run(ScriptPath path) {
// @description
// Every modern ScriptEvent has some special context tags available.
// The most noteworthy is "context.cancelled", which tracks whether the script event has been cancelled.
// You can also use "context.event_header", which returns the exact event header text that fired (which may be useful for some types of dynamic script).
// That returns, for example, "on player breaks stone".
// You can also use "context.event_name", which returns the internal name of the script event that fired (which may be useful for some debugging techniques).
// That returns, for example, "PlayerBreaksBlock".
// -->

@Override
public ObjectTag getContext(String name) {
switch (name) {
case "cancelled":
return new ElementTag(cancelled);
case "event_header":
return new ElementTag(currentEvent);
case "event_name":
case "event_name": // Intentionally undocumented, can be removed without harm
return new ElementTag(getName());
}
return null;
Expand Down
Expand Up @@ -28,14 +28,14 @@ public static String getStats() {
StringBuilder stats = new StringBuilder();
TreeSet<Map.Entry<Long, String>> statsSet = new TreeSet<>(Comparator.comparingLong(Map.Entry::getKey));
for (ScriptEvent event : ScriptEvent.events) {
if (event.stats.fires > 0) {
if (event.eventData.stats_fires > 0) {
stats.setLength(0);
stats.append(c1).append("Event '").append(event.getName()).append(c1).append("' ran ").append(c2).append(event.stats.fires)
.append(c1).append(" times (").append(c2).append(event.stats.scriptFires).append(c1).append(" script fires)")
.append(c1).append(", totalling ").append(c2).append((float) event.stats.nanoTimes / 1000000f)
.append(c1).append("ms, averaging ").append(c2).append((float) event.stats.nanoTimes / 1000000f / (float) event.stats.fires)
.append(c1).append("ms per event or ").append(c2).append(+((float) event.stats.nanoTimes / 1000000f / (float) event.stats.scriptFires)).append(c1).append("ms per script.\n");
statsSet.add(new HashMap.SimpleEntry<>(event.stats.nanoTimes, stats.toString()));
stats.append(c1).append("Event '").append(event.getName()).append(c1).append("' ran ").append(c2).append(event.eventData.stats_fires)
.append(c1).append(" times (").append(c2).append(event.eventData.stats_scriptFires).append(c1).append(" script fires)")
.append(c1).append(", totalling ").append(c2).append((float) event.eventData.stats_nanoTimes / 1000000f)
.append(c1).append("ms, averaging ").append(c2).append((float) event.eventData.stats_nanoTimes / 1000000f / (float) event.eventData.stats_fires)
.append(c1).append("ms per event or ").append(c2).append(+((float) event.eventData.stats_nanoTimes / 1000000f / (float) event.eventData.stats_scriptFires)).append(c1).append("ms per script.\n");
statsSet.add(new HashMap.SimpleEntry<>(event.eventData.stats_nanoTimes, stats.toString()));
}
}
return "Total number of queues created: "
Expand All @@ -47,12 +47,12 @@ public static String getStats() {
public static ListTag getStatsRawData() {
ListTag result = new ListTag();
for (ScriptEvent event : ScriptEvent.events) {
if (event.stats.fires > 0) {
if (event.eventData.stats_fires > 0) {
MapTag map = new MapTag();
map.putObject("name", new ElementTag(event.getName()));
map.putObject("total_fires", new ElementTag(event.stats.fires));
map.putObject("script_fires", new ElementTag(event.stats.scriptFires));
map.putObject("total_time", new DurationTag(event.stats.nanoTimes / 1000.0));
map.putObject("total_fires", new ElementTag(event.eventData.stats_fires));
map.putObject("script_fires", new ElementTag(event.eventData.stats_scriptFires));
map.putObject("total_time", new DurationTag(event.eventData.stats_nanoTimes / 1000.0));
result.addObject(map);
}
}
Expand Down
Expand Up @@ -8,9 +8,9 @@ public class AsciiMatcher {

public boolean[] accepted = new boolean[256];

public AsciiMatcher(String allowThese) {
for (int i = 0; i < allowThese.length(); i++) {
accepted[allowThese.charAt(i)] = true;
public AsciiMatcher(String matchThese) {
for (int i = 0; i < matchThese.length(); i++) {
accepted[matchThese.charAt(i)] = true;
}
}

Expand Down

0 comments on commit b2159ad

Please sign in to comment.