Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions res/config.yml
Original file line number Diff line number Diff line change
@@ -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
25 changes: 21 additions & 4 deletions src/us/mcparks/showscript/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -46,8 +47,8 @@ public class Main extends JavaPlugin implements Listener {
public TimecodeShowExecutor timecodeExecutor;
public List<ShowScheduler> activeShows = new CopyOnWriteArrayList<>();
private RegionShowListener regionShowListener;

private RegionListener regionListener;
private Configuration configuration;

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

@Override
public void onEnable() {
DebugLogger.setLogging(true);
// Load configuration
configuration = new Configuration(this);

// Set debug logging based on configuration
DebugLogger.setLogging(configuration.isDebugLoggingEnabled());

setupCloud();

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

timingManager = TimingManager.of(this);
baseTiming = timingManager.of("Shows");
// Initialize timings if enabled in configuration
if (configuration.areTimingsEnabled()) {
timingManager = TimingManager.of(this);
baseTiming = timingManager.of("Shows");
}

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

/**
* Gets the plugin configuration
* @return the configuration
*/
public Configuration getConfiguration() {
return configuration;
}


public void addActiveShow(ShowScheduler show) {
activeShows.add(show);
Expand Down
58 changes: 58 additions & 0 deletions src/us/mcparks/showscript/Shows.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,64 @@ public void toggleDebug(CommandSender sender) {
sender.sendMessage("Console debug has been set to: " + DebugLogger.isEnabled());
}

@CommandMethod("show config reload")
@CommandPermission("castmember")
public void reloadConfig(CommandSender sender) {
main.getConfiguration().loadConfig();

// Update settings from newly loaded config
DebugLogger.setLogging(main.getConfiguration().isDebugLoggingEnabled());

sender.sendMessage(ChatColor.GREEN + "Configuration reloaded successfully!");
}

@CommandMethod("show config get <key>")
@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 <key> <value>")
@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<String> configKeyNames(CommandContext<CommandSender> sender, String input) {
List<String> 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) {
Expand Down
114 changes: 114 additions & 0 deletions src/us/mcparks/showscript/config/Configuration.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


Expand Down
Loading