diff --git a/src/main/java/com/denizenscript/denizencore/scripts/commands/core/AdjustCommand.java b/src/main/java/com/denizenscript/denizencore/scripts/commands/core/AdjustCommand.java index 85c43d50..4ca566e5 100644 --- a/src/main/java/com/denizenscript/denizencore/scripts/commands/core/AdjustCommand.java +++ b/src/main/java/com/denizenscript/denizencore/scripts/commands/core/AdjustCommand.java @@ -117,10 +117,6 @@ else if (arg.hasPrefix()) { public static HashMap> specialAdjustables = new HashMap<>(); - static { - specialAdjustables.put("system", UtilTagBase::adjustSystem); - } - public ObjectTag adjust(ObjectTag object, String mechanismName, ObjectTag value, ScriptEntry entry) { Mechanism mechanism = new Mechanism(mechanismName, value, entry.entryData.getTagContext()); return adjust(object, mechanism, entry); diff --git a/src/main/java/com/denizenscript/denizencore/tags/core/UtilTagBase.java b/src/main/java/com/denizenscript/denizencore/tags/core/UtilTagBase.java index 1b6317c8..37d3c810 100644 --- a/src/main/java/com/denizenscript/denizencore/tags/core/UtilTagBase.java +++ b/src/main/java/com/denizenscript/denizencore/tags/core/UtilTagBase.java @@ -1,6 +1,7 @@ package com.denizenscript.denizencore.tags.core; import com.denizenscript.denizencore.DenizenCore; +import com.denizenscript.denizencore.events.ScriptEvent; import com.denizenscript.denizencore.events.core.TickScriptEvent; import com.denizenscript.denizencore.objects.ArgumentHelper; import com.denizenscript.denizencore.objects.Mechanism; @@ -10,6 +11,7 @@ import com.denizenscript.denizencore.objects.notable.NoteManager; import com.denizenscript.denizencore.scripts.ScriptRegistry; import com.denizenscript.denizencore.scripts.commands.CommandRegistry; +import com.denizenscript.denizencore.scripts.commands.core.AdjustCommand; import com.denizenscript.denizencore.scripts.commands.core.MongoCommand; import com.denizenscript.denizencore.scripts.commands.core.SQLCommand; import com.denizenscript.denizencore.scripts.commands.queue.RunLaterCommand; @@ -35,6 +37,7 @@ public class UtilTagBase extends PseudoObjectTagBase { public UtilTagBase() { instance = this; TagManager.registerStaticTagBaseHandler(UtilTagBase.class, "util", (t) -> instance); + AdjustCommand.specialAdjustables.put("system", UtilTagBase::adjustSystem); } @Override @@ -990,6 +993,16 @@ else if (attribute.startsWith("format", 2) && attribute.hasParam()) { return new ListTag((Collection) StackWalker.getInstance(StackWalker.Option.SHOW_HIDDEN_FRAMES).walk(stackFrameStream -> stackFrameStream.map(stackFrame -> new ElementTag(stackFrame.getClassName(), true)).toList())); }); + + // <--[tag] + // @attribute + // @returns ElementTag(Boolean) + // @description + // Returns whether script debug is currently globally enabled. + // --> + tagProcessor.registerTag(ElementTag.class, "debug_enabled", (attribute, object) -> { + return new ElementTag(CoreConfiguration.shouldShowDebug); + }); } public static final long serverStartTimeMillis = CoreUtilities.monotonicMillis(); @@ -1018,11 +1031,12 @@ public static void adjustSystem(Mechanism mechanism) { // Note that this redirects *all console output* not just Denizen output. // Note: don't enable /denizen debug -e while this is active. // Requires config file setting "Debug.Allow console redirection"! - // For example: - adjust system redirect_logging:true + // @example + // - adjust system redirect_logging:true // --> if (mechanism.matches("redirect_logging") && mechanism.hasValue()) { if (!CoreConfiguration.allowConsoleRedirection) { - Debug.echoError("Console redirection disabled by administrator (refer to mechanism documentation)."); + mechanism.echoError("Console redirection disabled by administrator (refer to mechanism documentation)."); return; } if (mechanism.getValue().asBoolean()) { @@ -1043,14 +1057,78 @@ public static void adjustSystem(Mechanism mechanism) { // Use <@link tag util.runlater_ids> to check whether there is already a scheduled task with the given ID. // --> if (mechanism.matches("cancel_runlater") && mechanism.hasValue()) { - RunLaterCommand.FutureRunData runner = RunLaterCommand.trackedById.remove(CoreUtilities.toLowerCase(mechanism.getValue().asString())); + RunLaterCommand.FutureRunData runner = RunLaterCommand.trackedById.remove(mechanism.getValue().asLowerString()); if (runner != null) { runner.cancelled = true; } } - if (!mechanism.fulfilled()) { - mechanism.reportInvalid(); + // <--[mechanism] + // @object system + // @name reset_event_stats + // @input None + // @description + // Resets the statistics on events used for <@link tag util.event_stats> + // @tags + // + // + // --> + if (mechanism.matches("reset_event_stats")) { + for (ScriptEvent scriptEvent : ScriptEvent.events) { + scriptEvent.eventData.stats_fires = 0; + scriptEvent.eventData.stats_scriptFires = 0; + scriptEvent.eventData.stats_nanoTimes = 0; + } + } + + // <--[mechanism] + // @object system + // @name cleanmem + // @input None + // @description + // Suggests to the internal systems that it's a good time to clean the memory. + // Does NOT force a memory cleaning. + // This should generally not be used unless you have a very good specific reason to use it. + // @tags + // + // --> + if (mechanism.matches("cleanmem")) { + System.gc(); + } + + // <--[mechanism] + // @object system + // @name delete_file + // @input ElementTag + // @description + // Deletes the given file from the server. + // File path starts in the Denizen folder. + // Require config file setting "Commands.Delete.Allow file deletion". + // @example + // - adjust system delete_file:data/logs/latest.txt + // @tags + // ]> + // --> + if (mechanism.matches("delete_file") && mechanism.hasValue()) { + if (!CoreConfiguration.allowFileDeletion) { + mechanism.echoError("File deletion disabled by administrator (refer to mechanism documentation)."); + return; + } + File file = new File(DenizenCore.implementation.getDataFolder(), mechanism.getValue().asString()); + if (!DenizenCore.implementation.canWriteToFile(file)) { + mechanism.echoError("Cannot write to that file path due to security settings in Denizen/config.yml."); + return; + } + try { + if (!file.delete()) { + mechanism.echoError("Failed to delete file: returned false"); + } + } + catch (Exception e) { + mechanism.echoError("Failed to delete file: " + e.getMessage()); + } } + + mechanism.autoReport(); } } diff --git a/src/main/java/com/denizenscript/denizencore/utilities/CoreConfiguration.java b/src/main/java/com/denizenscript/denizencore/utilities/CoreConfiguration.java index e7d2f8e0..74a0ec85 100644 --- a/src/main/java/com/denizenscript/denizencore/utilities/CoreConfiguration.java +++ b/src/main/java/com/denizenscript/denizencore/utilities/CoreConfiguration.java @@ -18,7 +18,7 @@ public class CoreConfiguration { public static int debugLimitPerTick = 5000, debugTrimLength = 1024, debugLineLength = 300; - public static boolean allowWebget = false, allowSQL = false, allowRedis = false, allowMongo = false, allowLog = false, allowFileCopy = false, allowWebserver = false, allowFileRead = false, allowFileWrite = false; + public static boolean allowWebget = false, allowSQL = false, allowRedis = false, allowMongo = false, allowLog = false, allowFileCopy = false, allowWebserver = false, allowFileRead = false, allowFileWrite = false, allowFileDeletion = false; public static boolean allowConsoleRedirection = false, allowRestrictedActions = false, allowStrangeFileSaves = false;