Skip to content

Commit 29d9e2b

Browse files
committed
Enhanced timings support
GitOrigin-RevId: 8d034cb7b3798dc25c3f34d53f1af0bfce132731
1 parent 5c0b4e8 commit 29d9e2b

File tree

7 files changed

+97
-48
lines changed

7 files changed

+97
-48
lines changed

build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ plugins {
1414

1515
shadowJar {
1616
relocate 'cloud.commandframework', 'us.mcparks.showscript.cloud.commandframework'
17+
relocate 'co.aikar.timings.lib', 'us.mcparks.showscript.aikar.timings.lib'
1718
}
1819

1920
sourceSets {
@@ -71,6 +72,7 @@ dependencies {
7172
implementation 'cloud.commandframework:cloud-minecraft-extras:1.8.3'
7273
implementation "net.kyori:adventure-platform-bukkit:4.3.0"
7374
implementation "commons-io:commons-io:2.16.1"
75+
implementation "co.aikar:minecraft-timings:1.0.4"
7476

7577
}
7678

@@ -126,6 +128,10 @@ publishing {
126128
password = project.findProperty("gpr.key") ?: System.getenv("TOKEN")
127129
}
128130
}
131+
132+
maven {
133+
url = uri('http://repo.aikar.co/nexus/content/groups/aikar/')
134+
}
129135
}
130136
}
131137

src/us/mcparks/showscript/Main.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import java.util.function.Function;
1616
import java.util.function.Supplier;
1717

18+
import co.aikar.timings.lib.MCTiming;
19+
import co.aikar.timings.lib.TimingManager;
1820
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
1921
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
2022
import us.mcparks.showscript.event.region.RegionListener;
@@ -37,6 +39,8 @@ public class Main extends JavaPlugin implements Listener {
3739
public static AnnotationParser<CommandSender> cloudAnnotationParser;
3840
public static BukkitAudiences audiences;
3941
public static AudienceProvider<CommandSender> commandSenderAudienceProvider;
42+
public static MCTiming baseTiming;
43+
public static TimingManager timingManager;
4044
public File rbFolder = new File(getRealDataFolder(), "Rebuilds");
4145
public File fs = new File(getRealDataFolder(), "Shows");
4246
public TimecodeShowExecutor timecodeExecutor;
@@ -100,6 +104,8 @@ public void onEnable() {
100104
Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Lag(),
101105
100L, 1L);
102106

107+
timingManager = TimingManager.of(this);
108+
baseTiming = timingManager.of("Shows");
103109

104110
// We evaluate _something_ to instantiate a GroovyShell now so it's cached before shows start running
105111
GroovyShowConfig.evaluator.evaluateExpression("println 'Hello, World! Warming up the Groovy engine.'");

src/us/mcparks/showscript/RegionShowListener.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import cloud.commandframework.context.CommandContext;
99
import com.google.common.collect.Multimap;
1010
import com.google.common.collect.MultimapBuilder;
11+
import com.google.common.hash.HashCode;
12+
import com.google.common.hash.Hashing;
1113
import us.mcparks.showscript.event.region.PlayerEnterRegionEvent;
1214
import us.mcparks.showscript.event.region.PlayerLeaveRegionEvent;
1315
import us.mcparks.showscript.showscript.framework.TimecodeShow;
@@ -50,6 +52,9 @@ public class RegionShowListener implements Listener {
5052
// regions -> ignored schemas, for messaging to users when `/regionshows --ignored` is run
5153
Multimap<String, RegionShowInstance> ignoredSchemas;
5254

55+
// Store a map of schema filepaths to their hash, so we can detect if a duplicate schema has been loaded
56+
Map<HashCode, String> schemaHashes = new HashMap<>();
57+
5358
public RegionShowListener(Main main) {
5459
this.main = main;
5560
main.getServer().getPluginManager().registerEvents(this, main);
@@ -99,6 +104,17 @@ private boolean loadRegionShow(File f, CommandSender sender) {
99104
try {
100105
List<String> regions = new ArrayList<>();
101106
YamlConfiguration yaml = YamlConfiguration.loadConfiguration(f);
107+
108+
String configAsString = yaml.saveToString();
109+
HashCode hash = Hashing.sha256().hashBytes(configAsString.getBytes());
110+
if (schemaHashes.containsKey(hash)) {
111+
if (sender != null) {
112+
sender.sendMessage("Error loading region show " + f.getName() + ": A schema with identical " +
113+
"contents has already been loaded: " + schemaHashes.get(hash));
114+
}
115+
return false;
116+
}
117+
102118
if (yaml.isList("region")) {
103119
regions.addAll(yaml.getStringList("region"));
104120
} else if (yaml.isString("region")) {

src/us/mcparks/showscript/showscript/framework/actions/ShowAction.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,9 @@ public Map<?,?> getPropertyMap() {
3131

3232
@Override
3333
public String toString() {
34-
String res = "";
35-
res += action.toString() + ":";
36-
for (Object prop : propMap.keySet()) {
37-
res += "\n " + prop + ": " + propMap.get(prop);
38-
}
39-
return res;
34+
return action.toString() + ": " + propMap.entrySet().stream()
35+
.map(entry -> entry.getKey() + ": " + entry.getValue())
36+
.collect(Collectors.joining(", "));
4037
}
4138

4239
}

src/us/mcparks/showscript/showscript/framework/schedulers/AsyncTimecodeShowScheduler.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package us.mcparks.showscript.showscript.framework.schedulers;
22

3+
import co.aikar.timings.lib.MCTiming;
4+
import co.aikar.timings.lib.TimingManager;
5+
import jdk.nashorn.internal.runtime.Timing;
36
import us.mcparks.showscript.Main;
47
import us.mcparks.showscript.showscript.framework.actions.ShowAction;
58
import us.mcparks.showscript.showscript.framework.TimecodeShow;
@@ -34,7 +37,11 @@ public void setTask(ScheduledFuture<?> task) {
3437

3538
@Override
3639
protected void executeShowAction(ShowAction action) {
37-
Bukkit.getScheduler().runTask(main, () -> super.executeShowAction(action));
40+
Bukkit.getScheduler().runTask(main, () -> {
41+
try (MCTiming timing = getTiming().startTiming()) {
42+
super.executeShowAction(action);
43+
}
44+
});
3845
}
3946

4047
@Override

src/us/mcparks/showscript/showscript/framework/schedulers/ShowScheduler.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package us.mcparks.showscript.showscript.framework.schedulers;
22

3+
import co.aikar.timings.lib.MCTiming;
4+
import co.aikar.timings.lib.TimingManager;
5+
import us.mcparks.showscript.Main;
6+
37
public interface ShowScheduler extends Runnable {
48
public String getName();
59

@@ -14,4 +18,9 @@ public interface ShowScheduler extends Runnable {
1418
public void restart();
1519

1620

21+
default MCTiming getTiming() {
22+
return TimingManager.of(Main.getPlugin(Main.class)).of(getClass().getSimpleName() + " - " + getName(), Main.baseTiming);
23+
}
24+
25+
1726
}

src/us/mcparks/showscript/showscript/framework/schedulers/TimecodeShowScheduler.java

Lines changed: 49 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.List;
44
import java.util.concurrent.atomic.AtomicInteger;
55

6+
import co.aikar.timings.lib.MCTiming;
67
import us.mcparks.showscript.util.DebugLogger;
78
import us.mcparks.showscript.util.Lag;
89
import us.mcparks.showscript.Main;
@@ -35,7 +36,6 @@ public class TimecodeShowScheduler implements ShowScheduler, Runnable {
3536
int waitCounter;
3637
int actionCounter;
3738

38-
3939
public TimecodeShowScheduler(Main main, TimecodeShow show, String name, boolean display,
4040
CommandSender sender, long recursionDepth, int startAt) {
4141
this.main = main;
@@ -68,34 +68,41 @@ public BukkitRunnable getTask() {
6868

6969
@Override
7070
public void run() {
71-
// this.showTaskId = this.getTaskId();
72-
// Increment the timecode
73-
timecode++;
74-
//System.out.println("It's timecode " + timecode + " for show " + name + " at time " + System.currentTimeMillis() + " with recursion depth " + recursionDepth);
75-
if (!initialized) {
76-
DebugLogger.log(getName(), "starting");
77-
init();
78-
initialized = true;
79-
}
71+
try (MCTiming timing = getTiming().startTiming()) {
72+
73+
// this.showTaskId = this.getTaskId();
74+
// Increment the timecode
75+
timecode++;
76+
//System.out.println("It's timecode " + timecode + " for show " + name + " at time " + System.currentTimeMillis() + " with recursion depth " + recursionDepth);
77+
if (!initialized) {
78+
DebugLogger.log(getName(), "starting");
79+
init();
80+
initialized = true;
81+
}
8082

81-
Integer next = show.getNextActionTick();
83+
Integer next = show.getNextActionTick();
84+
85+
if (next == null) {
86+
main.cancelShowById(getShowTaskId());
87+
} else if (next <= timecode && waitCounter == 0) {
88+
// Reset wait counter
89+
waitCounter = 0;
90+
91+
// Get actions for this tick, execute each
92+
List<ShowAction> actions = show.getNextActions();
93+
for (ShowAction action : actions) {
94+
try (MCTiming actionTiming = Main.timingManager.of(action.toString(), timing)) {
95+
//System.out.println("executing " + actions.size() + " actions for timecode " + next + " at time " + timecode + " for show " + name + " with recursion depth " + recursionDepth);
96+
executeShowAction(action);
97+
}
98+
}
99+
} else {
100+
setTimeToWait();
101+
}
82102

83-
if (next == null) {
84-
main.cancelShowById(getShowTaskId());
85-
} else if (next <= timecode && waitCounter == 0) {
86-
// Reset wait counter
87-
waitCounter = 0;
88-
89-
// Get actions for this tick, execute each
90-
List<ShowAction> actions = show.getNextActions();
91-
for (ShowAction action : actions) {
92-
//System.out.println("executing " + actions.size() + " actions for timecode " + next + " at time " + timecode + " for show " + name + " with recursion depth " + recursionDepth);
93-
executeShowAction(action);
94-
}
95-
} else {
96-
setTimeToWait();
97103
}
98104

105+
99106
}
100107

101108
@Override
@@ -109,23 +116,24 @@ public void restart() {
109116

110117

111118
protected void executeShowAction(ShowAction action) {
112-
// System.out.println("EXEC: " + action.toString());
113-
// display if the flag is set
114-
if (display && sender != null) {
115-
sender.sendMessage(action.toString());
116-
}
117-
try {
118-
executor.execute(action);
119-
} catch (Exception e) {
120-
e.printStackTrace();
121-
sender.sendMessage(ChatColor.RED + "ERROR IN SHOW " + name + " at timecode " + timecode
122-
+ "\n" +
123-
"Could not parse Show Action: " + action.toString());
124-
sender.sendMessage(e.getMessage());
125-
//sender.sendMessage();
126-
stopShow();
119+
try (MCTiming timing = Main.timingManager.ofStart("ticks: " + timecode + " " + action.toString(), getTiming())) {
120+
// System.out.println("EXEC: " + action.toString());
121+
// display if the flag is set
122+
if (display && sender != null) {
123+
sender.sendMessage(action.toString());
124+
}
125+
try {
126+
executor.execute(action);
127+
} catch (Exception e) {
128+
e.printStackTrace();
129+
sender.sendMessage(ChatColor.RED + "ERROR IN SHOW " + name + " at timecode " + timecode
130+
+ "\n" +
131+
"Could not parse Show Action: " + action.toString());
132+
sender.sendMessage(e.getMessage());
133+
//sender.sendMessage();
134+
stopShow();
135+
}
127136
}
128-
129137
}
130138

131139
private void init() {

0 commit comments

Comments
 (0)