Skip to content

Commit ff098ce

Browse files
committed
Merge branch 'github-pr-14' into 'master'
From [GitHub PR 14: Add Configuration System with Debug and Timings... See merge request base/ShowsRebuild!16 GitOrigin-RevId: 11f10a5b312b1bafa7de5c25ffdcb79bab15c4c7
1 parent a478c53 commit ff098ce

File tree

7 files changed

+272
-53
lines changed

7 files changed

+272
-53
lines changed

res/config.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# ShowScript Configuration File
2+
3+
# Debug logging - Set to true to enable debug messages in console
4+
# Default: true
5+
debug-logging: true
6+
7+
# Enable timings - Set to false to disable Aikar's timings
8+
# Default: true (set to false on 1.20+ to avoid console spam)
9+
enable-timings: true

src/us/mcparks/showscript/Main.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import us.mcparks.showscript.event.show.ShowStopEvent;
2525
import us.mcparks.showscript.showscript.framework.schedulers.ShowScheduler;
2626
import us.mcparks.showscript.showscript.framework.TimecodeShowExecutor;
27+
import us.mcparks.showscript.config.Configuration;
2728
import us.mcparks.showscript.showscript.groovy.GroovyShowConfig;
2829
import us.mcparks.showscript.util.DebugLogger;
2930
import us.mcparks.showscript.util.Lag;
@@ -46,8 +47,8 @@ public class Main extends JavaPlugin implements Listener {
4647
public TimecodeShowExecutor timecodeExecutor;
4748
public List<ShowScheduler> activeShows = new CopyOnWriteArrayList<>();
4849
private RegionShowListener regionShowListener;
49-
5050
private RegionListener regionListener;
51+
private Configuration configuration;
5152

5253
public Supplier<List<String>> showFileNames = new AsynchronouslyRefreshingSupplier<>(() -> {
5354
File fs = new File(getRealDataFolder(), "Shows");
@@ -77,7 +78,12 @@ private static void searchFilesRecursively(File root, File directory, List<Strin
7778

7879
@Override
7980
public void onEnable() {
80-
DebugLogger.setLogging(true);
81+
// Load configuration
82+
configuration = new Configuration(this);
83+
84+
// Set debug logging based on configuration
85+
DebugLogger.setLogging(configuration.isDebugLoggingEnabled());
86+
8187
setupCloud();
8288

8389
// Internally, this plugin also handles a legacy command. Ask Ryan for the story on why.
@@ -105,8 +111,11 @@ public void onEnable() {
105111
Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Lag(),
106112
100L, 1L);
107113

108-
timingManager = TimingManager.of(this);
109-
baseTiming = timingManager.of("Shows");
114+
// Initialize timings if enabled in configuration
115+
if (configuration.areTimingsEnabled()) {
116+
timingManager = TimingManager.of(this);
117+
baseTiming = timingManager.of("Shows");
118+
}
110119

111120
// We evaluate _something_ to instantiate a GroovyShell now so it's cached before shows start running
112121
GroovyShowConfig.evaluator.evaluateExpression("println 'Hello, World! Warming up the Groovy engine.'");
@@ -154,6 +163,14 @@ public RegionListener getRegionListener() {
154163
return regionListener;
155164
}
156165

166+
/**
167+
* Gets the plugin configuration
168+
* @return the configuration
169+
*/
170+
public Configuration getConfiguration() {
171+
return configuration;
172+
}
173+
157174

158175
public void addActiveShow(ShowScheduler show) {
159176
activeShows.add(show);

src/us/mcparks/showscript/Shows.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,64 @@ public void toggleDebug(CommandSender sender) {
5454
sender.sendMessage("Console debug has been set to: " + DebugLogger.isEnabled());
5555
}
5656

57+
@CommandMethod("show config reload")
58+
@CommandPermission("castmember")
59+
public void reloadConfig(CommandSender sender) {
60+
main.getConfiguration().loadConfig();
61+
62+
// Update settings from newly loaded config
63+
DebugLogger.setLogging(main.getConfiguration().isDebugLoggingEnabled());
64+
65+
sender.sendMessage(ChatColor.GREEN + "Configuration reloaded successfully!");
66+
}
67+
68+
@CommandMethod("show config get <key>")
69+
@CommandPermission("castmember")
70+
public void getConfigValue(CommandSender sender, @Argument(value="key", suggestions="configKeys") String key) {
71+
if (key.equalsIgnoreCase("debug-logging")) {
72+
sender.sendMessage(ChatColor.GREEN + "debug-logging: " +
73+
ChatColor.AQUA + main.getConfiguration().isDebugLoggingEnabled());
74+
} else if (key.equalsIgnoreCase("enable-timings")) {
75+
sender.sendMessage(ChatColor.GREEN + "enable-timings: " +
76+
ChatColor.AQUA + main.getConfiguration().areTimingsEnabled());
77+
} else {
78+
sender.sendMessage(ChatColor.RED + "Unknown configuration key: " + key);
79+
}
80+
}
81+
82+
@CommandMethod("show config set <key> <value>")
83+
@CommandPermission("castmember")
84+
public void setConfigValue(CommandSender sender, @Argument(value="key", suggestions="configKeys") String key, @Argument("value") String value) {
85+
boolean boolValue;
86+
87+
if (!value.equalsIgnoreCase("true") && !value.equalsIgnoreCase("false")) {
88+
sender.sendMessage(ChatColor.RED + "Value must be 'true' or 'false'");
89+
return;
90+
}
91+
92+
boolValue = Boolean.parseBoolean(value);
93+
94+
if (key.equalsIgnoreCase("debug-logging")) {
95+
main.getConfiguration().setDebugLoggingEnabled(boolValue);
96+
DebugLogger.setLogging(boolValue);
97+
sender.sendMessage(ChatColor.GREEN + "debug-logging set to: " + ChatColor.AQUA + boolValue);
98+
} else if (key.equalsIgnoreCase("enable-timings")) {
99+
main.getConfiguration().setTimingsEnabled(boolValue);
100+
sender.sendMessage(ChatColor.GREEN + "enable-timings set to: " + ChatColor.AQUA + boolValue);
101+
sender.sendMessage(ChatColor.YELLOW + "Note: Changes to timings will take effect after server restart");
102+
} else {
103+
sender.sendMessage(ChatColor.RED + "Unknown configuration key: " + key);
104+
}
105+
}
106+
107+
@Suggestions("configKeys")
108+
public List<String> configKeyNames(CommandContext<CommandSender> sender, String input) {
109+
List<String> keys = new ArrayList<>();
110+
keys.add("debug-logging");
111+
keys.add("enable-timings");
112+
return keys.stream().filter(s -> s.startsWith(input.toLowerCase())).collect(Collectors.toList());
113+
}
114+
57115
@CommandMethod("show stopall")
58116
@CommandPermission("castmember")
59117
public void stopAllShows(CommandSender sender) {
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package us.mcparks.showscript.config;
2+
3+
import org.bukkit.configuration.file.FileConfiguration;
4+
import org.bukkit.configuration.file.YamlConfiguration;
5+
import org.bukkit.plugin.java.JavaPlugin;
6+
7+
import java.io.File;
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.io.InputStreamReader;
11+
import java.nio.charset.StandardCharsets;
12+
13+
public class Configuration {
14+
15+
private final JavaPlugin plugin;
16+
private FileConfiguration config;
17+
private File configFile;
18+
19+
// Configuration keys
20+
private static final String DEBUG_LOGGING_KEY = "debug-logging";
21+
private static final String ENABLE_TIMINGS_KEY = "enable-timings";
22+
23+
public Configuration(JavaPlugin plugin) {
24+
this.plugin = plugin;
25+
loadConfig();
26+
}
27+
28+
/**
29+
* Loads/reloads the configuration from disk
30+
*/
31+
public void loadConfig() {
32+
if (configFile == null) {
33+
configFile = new File(plugin.getDataFolder(), "config.yml");
34+
}
35+
36+
if (!configFile.exists()) {
37+
plugin.saveResource("config.yml", false);
38+
}
39+
40+
config = YamlConfiguration.loadConfiguration(configFile);
41+
42+
// Set defaults if they don't exist
43+
setDefaults();
44+
}
45+
46+
/**
47+
* Sets default values for configuration options if they don't exist
48+
*/
49+
private void setDefaults() {
50+
boolean saveNeeded = false;
51+
52+
// Debug Logging - default: true
53+
if (!config.contains(DEBUG_LOGGING_KEY)) {
54+
config.set(DEBUG_LOGGING_KEY, true);
55+
saveNeeded = true;
56+
}
57+
58+
// Enable Timings - default: true
59+
if (!config.contains(ENABLE_TIMINGS_KEY)) {
60+
config.set(ENABLE_TIMINGS_KEY, true);
61+
saveNeeded = true;
62+
}
63+
64+
if (saveNeeded) {
65+
saveConfig();
66+
}
67+
}
68+
69+
/**
70+
* Saves the configuration to disk
71+
*/
72+
public void saveConfig() {
73+
try {
74+
config.save(configFile);
75+
} catch (IOException ex) {
76+
plugin.getLogger().severe("Could not save config to " + configFile);
77+
ex.printStackTrace();
78+
}
79+
}
80+
81+
/**
82+
* Gets whether debug logging is enabled
83+
* @return true if debug logging is enabled
84+
*/
85+
public boolean isDebugLoggingEnabled() {
86+
return config.getBoolean(DEBUG_LOGGING_KEY, true);
87+
}
88+
89+
/**
90+
* Sets whether debug logging is enabled
91+
* @param enabled true to enable debug logging
92+
*/
93+
public void setDebugLoggingEnabled(boolean enabled) {
94+
config.set(DEBUG_LOGGING_KEY, enabled);
95+
saveConfig();
96+
}
97+
98+
/**
99+
* Gets whether timings are enabled
100+
* @return true if timings are enabled
101+
*/
102+
public boolean areTimingsEnabled() {
103+
return config.getBoolean(ENABLE_TIMINGS_KEY, true);
104+
}
105+
106+
/**
107+
* Sets whether timings are enabled
108+
* @param enabled true to enable timings
109+
*/
110+
public void setTimingsEnabled(boolean enabled) {
111+
config.set(ENABLE_TIMINGS_KEY, enabled);
112+
saveConfig();
113+
}
114+
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,13 @@ public void setTask(ScheduledFuture<?> task) {
3636
@Override
3737
protected void executeShowAction(ShowAction action) {
3838
Bukkit.getScheduler().runTask(main, () -> {
39-
try (MCTiming timing = getTiming().startTiming()) {
40-
super.executeShowAction(action);
39+
MCTiming timing = getTiming();
40+
if (timing != null) {
41+
try (MCTiming t = timing.startTiming()) {
42+
super.executeActionLogic(action);
43+
}
44+
} else {
45+
super.executeActionLogic(action);
4146
}
4247
});
4348
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ public interface ShowScheduler extends Runnable {
1919

2020

2121
default MCTiming getTiming() {
22-
return TimingManager.of(Main.getPlugin(Main.class)).of(getName());
22+
if (Main.timingManager != null) {
23+
return TimingManager.of(Main.getPlugin(Main.class)).of(getName());
24+
}
25+
return null;
2326
}
2427

2528

0 commit comments

Comments
 (0)