From c100466494df4fa9bbdfcdb4abefb2e34aab0454 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 20 May 2025 00:04:45 +0000 Subject: [PATCH 1/2] Initial plan for issue From cae3e014d51bf4961cf60d916f60ebd2161d0ba2 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 20 May 2025 00:14:14 +0000 Subject: [PATCH 2/2] Implement configuration system with initial options Co-authored-by: RyanHecht <5344055+RyanHecht@users.noreply.github.com> --- res/config.yml | 9 ++ src/us/mcparks/showscript/Main.java | 25 +++- src/us/mcparks/showscript/Shows.java | 58 +++++++++ .../showscript/config/Configuration.java | 114 ++++++++++++++++++ .../AsyncTimecodeShowScheduler.java | 9 +- .../framework/schedulers/ShowScheduler.java | 5 +- .../schedulers/TimecodeShowScheduler.java | 105 +++++++++------- 7 files changed, 272 insertions(+), 53 deletions(-) create mode 100644 res/config.yml create mode 100644 src/us/mcparks/showscript/config/Configuration.java diff --git a/res/config.yml b/res/config.yml new file mode 100644 index 0000000..924a7de --- /dev/null +++ b/res/config.yml @@ -0,0 +1,9 @@ +# ShowScript Configuration File + +# Debug logging - Set to true to enable debug messages in console +# Default: true +debug-logging: true + +# Enable timings - Set to false to disable Aikar's timings +# Default: true (set to false on 1.20+ to avoid console spam) +enable-timings: true \ No newline at end of file diff --git a/src/us/mcparks/showscript/Main.java b/src/us/mcparks/showscript/Main.java index 9ed9d38..cb75687 100644 --- a/src/us/mcparks/showscript/Main.java +++ b/src/us/mcparks/showscript/Main.java @@ -24,6 +24,7 @@ import us.mcparks.showscript.event.show.ShowStopEvent; import us.mcparks.showscript.showscript.framework.schedulers.ShowScheduler; import us.mcparks.showscript.showscript.framework.TimecodeShowExecutor; +import us.mcparks.showscript.config.Configuration; import us.mcparks.showscript.showscript.groovy.GroovyShowConfig; import us.mcparks.showscript.util.DebugLogger; import us.mcparks.showscript.util.Lag; @@ -46,8 +47,8 @@ public class Main extends JavaPlugin implements Listener { public TimecodeShowExecutor timecodeExecutor; public List activeShows = new CopyOnWriteArrayList<>(); private RegionShowListener regionShowListener; - private RegionListener regionListener; + private Configuration configuration; public Supplier> showFileNames = new AsynchronouslyRefreshingSupplier<>(() -> { File fs = new File(getRealDataFolder(), "Shows"); @@ -77,7 +78,12 @@ private static void searchFilesRecursively(File root, File directory, List") + @CommandPermission("castmember") + public void getConfigValue(CommandSender sender, @Argument(value="key", suggestions="configKeys") String key) { + if (key.equalsIgnoreCase("debug-logging")) { + sender.sendMessage(ChatColor.GREEN + "debug-logging: " + + ChatColor.AQUA + main.getConfiguration().isDebugLoggingEnabled()); + } else if (key.equalsIgnoreCase("enable-timings")) { + sender.sendMessage(ChatColor.GREEN + "enable-timings: " + + ChatColor.AQUA + main.getConfiguration().areTimingsEnabled()); + } else { + sender.sendMessage(ChatColor.RED + "Unknown configuration key: " + key); + } + } + + @CommandMethod("show config set ") + @CommandPermission("castmember") + public void setConfigValue(CommandSender sender, @Argument(value="key", suggestions="configKeys") String key, @Argument("value") String value) { + boolean boolValue; + + if (!value.equalsIgnoreCase("true") && !value.equalsIgnoreCase("false")) { + sender.sendMessage(ChatColor.RED + "Value must be 'true' or 'false'"); + return; + } + + boolValue = Boolean.parseBoolean(value); + + if (key.equalsIgnoreCase("debug-logging")) { + main.getConfiguration().setDebugLoggingEnabled(boolValue); + DebugLogger.setLogging(boolValue); + sender.sendMessage(ChatColor.GREEN + "debug-logging set to: " + ChatColor.AQUA + boolValue); + } else if (key.equalsIgnoreCase("enable-timings")) { + main.getConfiguration().setTimingsEnabled(boolValue); + sender.sendMessage(ChatColor.GREEN + "enable-timings set to: " + ChatColor.AQUA + boolValue); + sender.sendMessage(ChatColor.YELLOW + "Note: Changes to timings will take effect after server restart"); + } else { + sender.sendMessage(ChatColor.RED + "Unknown configuration key: " + key); + } + } + + @Suggestions("configKeys") + public List configKeyNames(CommandContext sender, String input) { + List keys = new ArrayList<>(); + keys.add("debug-logging"); + keys.add("enable-timings"); + return keys.stream().filter(s -> s.startsWith(input.toLowerCase())).collect(Collectors.toList()); + } + @CommandMethod("show stopall") @CommandPermission("castmember") public void stopAllShows(CommandSender sender) { diff --git a/src/us/mcparks/showscript/config/Configuration.java b/src/us/mcparks/showscript/config/Configuration.java new file mode 100644 index 0000000..5f943a6 --- /dev/null +++ b/src/us/mcparks/showscript/config/Configuration.java @@ -0,0 +1,114 @@ +package us.mcparks.showscript.config; + +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.plugin.java.JavaPlugin; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; + +public class Configuration { + + private final JavaPlugin plugin; + private FileConfiguration config; + private File configFile; + + // Configuration keys + private static final String DEBUG_LOGGING_KEY = "debug-logging"; + private static final String ENABLE_TIMINGS_KEY = "enable-timings"; + + public Configuration(JavaPlugin plugin) { + this.plugin = plugin; + loadConfig(); + } + + /** + * Loads/reloads the configuration from disk + */ + public void loadConfig() { + if (configFile == null) { + configFile = new File(plugin.getDataFolder(), "config.yml"); + } + + if (!configFile.exists()) { + plugin.saveResource("config.yml", false); + } + + config = YamlConfiguration.loadConfiguration(configFile); + + // Set defaults if they don't exist + setDefaults(); + } + + /** + * Sets default values for configuration options if they don't exist + */ + private void setDefaults() { + boolean saveNeeded = false; + + // Debug Logging - default: true + if (!config.contains(DEBUG_LOGGING_KEY)) { + config.set(DEBUG_LOGGING_KEY, true); + saveNeeded = true; + } + + // Enable Timings - default: true + if (!config.contains(ENABLE_TIMINGS_KEY)) { + config.set(ENABLE_TIMINGS_KEY, true); + saveNeeded = true; + } + + if (saveNeeded) { + saveConfig(); + } + } + + /** + * Saves the configuration to disk + */ + public void saveConfig() { + try { + config.save(configFile); + } catch (IOException ex) { + plugin.getLogger().severe("Could not save config to " + configFile); + ex.printStackTrace(); + } + } + + /** + * Gets whether debug logging is enabled + * @return true if debug logging is enabled + */ + public boolean isDebugLoggingEnabled() { + return config.getBoolean(DEBUG_LOGGING_KEY, true); + } + + /** + * Sets whether debug logging is enabled + * @param enabled true to enable debug logging + */ + public void setDebugLoggingEnabled(boolean enabled) { + config.set(DEBUG_LOGGING_KEY, enabled); + saveConfig(); + } + + /** + * Gets whether timings are enabled + * @return true if timings are enabled + */ + public boolean areTimingsEnabled() { + return config.getBoolean(ENABLE_TIMINGS_KEY, true); + } + + /** + * Sets whether timings are enabled + * @param enabled true to enable timings + */ + public void setTimingsEnabled(boolean enabled) { + config.set(ENABLE_TIMINGS_KEY, enabled); + saveConfig(); + } +} \ No newline at end of file diff --git a/src/us/mcparks/showscript/showscript/framework/schedulers/AsyncTimecodeShowScheduler.java b/src/us/mcparks/showscript/showscript/framework/schedulers/AsyncTimecodeShowScheduler.java index a19ca7f..8b6ff6c 100644 --- a/src/us/mcparks/showscript/showscript/framework/schedulers/AsyncTimecodeShowScheduler.java +++ b/src/us/mcparks/showscript/showscript/framework/schedulers/AsyncTimecodeShowScheduler.java @@ -36,8 +36,13 @@ public void setTask(ScheduledFuture task) { @Override protected void executeShowAction(ShowAction action) { Bukkit.getScheduler().runTask(main, () -> { - try (MCTiming timing = getTiming().startTiming()) { - super.executeShowAction(action); + MCTiming timing = getTiming(); + if (timing != null) { + try (MCTiming t = timing.startTiming()) { + super.executeActionLogic(action); + } + } else { + super.executeActionLogic(action); } }); } diff --git a/src/us/mcparks/showscript/showscript/framework/schedulers/ShowScheduler.java b/src/us/mcparks/showscript/showscript/framework/schedulers/ShowScheduler.java index 27fde7b..1eef8a1 100644 --- a/src/us/mcparks/showscript/showscript/framework/schedulers/ShowScheduler.java +++ b/src/us/mcparks/showscript/showscript/framework/schedulers/ShowScheduler.java @@ -19,7 +19,10 @@ public interface ShowScheduler extends Runnable { default MCTiming getTiming() { - return TimingManager.of(Main.getPlugin(Main.class)).of(getName()); + if (Main.timingManager != null) { + return TimingManager.of(Main.getPlugin(Main.class)).of(getName()); + } + return null; } diff --git a/src/us/mcparks/showscript/showscript/framework/schedulers/TimecodeShowScheduler.java b/src/us/mcparks/showscript/showscript/framework/schedulers/TimecodeShowScheduler.java index 2aa054d..ac54e6a 100644 --- a/src/us/mcparks/showscript/showscript/framework/schedulers/TimecodeShowScheduler.java +++ b/src/us/mcparks/showscript/showscript/framework/schedulers/TimecodeShowScheduler.java @@ -68,39 +68,44 @@ public BukkitRunnable getTask() { @Override public void run() { - try (MCTiming timing = getTiming().startTiming()) { - - // this.showTaskId = this.getTaskId(); - // Increment the timecode - timecode++; - //System.out.println("It's timecode " + timecode + " for show " + name + " at time " + System.currentTimeMillis() + " with recursion depth " + recursionDepth); - if (!initialized) { - DebugLogger.log(getName(), "starting"); - init(); - initialized = true; - } - - Integer next = show.getNextActionTick(); - - if (next == null) { - main.cancelShowById(getShowTaskId()); - } else if (next <= timecode && waitCounter == 0) { - // Reset wait counter - waitCounter = 0; - - // Get actions for this tick, execute each - List actions = show.getNextActions(); - for (ShowAction action : actions) { - //System.out.println("executing " + actions.size() + " actions for timecode " + next + " at time " + timecode + " for show " + name + " with recursion depth " + recursionDepth); - executeShowAction(action); - } - } else { - setTimeToWait(); - } + // Check if timings are enabled in the configuration + MCTiming timing = getTiming(); + if (timing != null) { + try (MCTiming t = timing.startTiming()) { + runShowLogic(); + } + } else { + runShowLogic(); + } + } + private void runShowLogic() { + // Increment the timecode + timecode++; + //System.out.println("It's timecode " + timecode + " for show " + name + " at time " + System.currentTimeMillis() + " with recursion depth " + recursionDepth); + if (!initialized) { + DebugLogger.log(getName(), "starting"); + init(); + initialized = true; } + Integer next = show.getNextActionTick(); + if (next == null) { + main.cancelShowById(getShowTaskId()); + } else if (next <= timecode && waitCounter == 0) { + // Reset wait counter + waitCounter = 0; + + // Get actions for this tick, execute each + List actions = show.getNextActions(); + for (ShowAction action : actions) { + //System.out.println("executing " + actions.size() + " actions for timecode " + next + " at time " + timecode + " for show " + name + " with recursion depth " + recursionDepth); + executeShowAction(action); + } + } else { + setTimeToWait(); + } } @Override @@ -114,24 +119,32 @@ public void restart() { protected void executeShowAction(ShowAction action) { - //try (MCTiming timing = Main.timingManager.ofStart("ticks: " + timecode + " " + action.toString(), getTiming())) { - // System.out.println("EXEC: " + action.toString()); - // display if the flag is set - if (display && sender != null) { - sender.sendMessage(action.toString()); + // Check if timings are enabled in the configuration + if (Main.timingManager != null) { + try (MCTiming timing = Main.timingManager.ofStart("ticks: " + timecode + " " + action.toString(), getTiming())) { + executeActionLogic(action); } - try { - executor.execute(action); - } catch (Exception e) { - e.printStackTrace(); - sender.sendMessage(ChatColor.RED + "ERROR IN SHOW " + name + " at timecode " + timecode - + "\n" + - "Could not parse Show Action: " + action.toString()); - sender.sendMessage(e.getMessage()); - //sender.sendMessage(); - stopShow(); - } - //} + } else { + executeActionLogic(action); + } + } + + private void executeActionLogic(ShowAction action) { + // display if the flag is set + if (display && sender != null) { + sender.sendMessage(action.toString()); + } + try { + executor.execute(action); + } catch (Exception e) { + e.printStackTrace(); + sender.sendMessage(ChatColor.RED + "ERROR IN SHOW " + name + " at timecode " + timecode + + "\n" + + "Could not parse Show Action: " + action.toString()); + sender.sendMessage(e.getMessage()); + //sender.sendMessage(); + stopShow(); + } } private void init() {