Search in sources :

Example 1 with CommandContext

use of net.citizensnpcs.api.command.CommandContext in project Denizen-For-Bukkit by DenizenScript.

the class ServerTagBase method adjustServer.

// <--[ObjectType]
// @name server
// @prefix None
// @base None
// @format
// N/A
// 
// @description
// "server" is an internal pseudo-ObjectType that is used as a mechanism adjust target for some global mechanisms.
// 
// -->
public static void adjustServer(Mechanism mechanism) {
    // -->
    if (mechanism.matches("delete_file") && mechanism.hasValue()) {
        if (!Settings.allowDelete()) {
            Debug.echoError("File deletion disabled by administrator (refer to mechanism documentation).");
            return;
        }
        File file = new File(Denizen.getInstance().getDataFolder(), mechanism.getValue().asString());
        if (!Utilities.canWriteToFile(file)) {
            Debug.echoError("Cannot write to that file path due to security settings in Denizen/config.yml.");
            return;
        }
        try {
            if (!file.delete()) {
                Debug.echoError("Failed to delete file: returned false");
            }
        } catch (Exception e) {
            Debug.echoError("Failed to delete file: " + e.getMessage());
        }
    }
    if (mechanism.matches("redirect_logging") && mechanism.hasValue()) {
        Deprecations.serverRedirectLogging.warn(mechanism.context);
        if (!Settings.allowConsoleRedirection()) {
            Debug.echoError("Console redirection disabled by administrator.");
            return;
        }
        if (mechanism.getValue().asBoolean()) {
            DenizenCore.logInterceptor.redirectOutput();
        } else {
            DenizenCore.logInterceptor.standardOutput();
        }
    }
    // -->
    if (mechanism.matches("reset_event_stats")) {
        for (ScriptEvent se : ScriptEvent.events) {
            se.eventData.stats_fires = 0;
            se.eventData.stats_scriptFires = 0;
            se.eventData.stats_nanoTimes = 0;
        }
    }
    // -->
    if (mechanism.matches("reset_recipes")) {
        Bukkit.resetRecipes();
        Denizen.getInstance().itemScriptHelper.rebuildRecipes();
    }
    // -->
    if (mechanism.matches("remove_recipes")) {
        ListTag list = mechanism.valueAsType(ListTag.class);
        for (String str : list) {
            NMSHandler.getItemHelper().removeRecipe(Utilities.parseNamespacedKey(str));
        }
    }
    // -->
    if (mechanism.matches("idle_timeout") && mechanism.requireObject(DurationTag.class)) {
        Bukkit.setIdleTimeout((int) Math.round(mechanism.valueAsType(DurationTag.class).getSeconds() / 60));
    }
    // -->
    if (mechanism.matches("cleanmem")) {
        System.gc();
    }
    // -->
    if (mechanism.matches("restart")) {
        if (!Settings.allowServerRestart()) {
            Debug.echoError("Server restart disabled by administrator (refer to mechanism documentation). Consider using 'shutdown'.");
            return;
        }
        Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "+> Server restarted by a Denizen script, see config to prevent this!");
        Bukkit.spigot().restart();
    }
    // -->
    if (mechanism.matches("save")) {
        DenizenCore.saveAll();
        Denizen.getInstance().saveSaves(true);
    }
    // -->
    if (Depends.citizens != null && mechanism.matches("save_citizens")) {
        Depends.citizens.storeNPCs(new CommandContext(new String[0]));
    }
    // -->
    if (mechanism.matches("shutdown")) {
        if (!Settings.allowServerStop()) {
            Debug.echoError("Server stop disabled by administrator (refer to mechanism documentation). Consider using 'restart'.");
            return;
        }
        Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "+> Server shutdown by a Denizen script, see config to prevent this!");
        Bukkit.shutdown();
    }
    // -->
    if (mechanism.matches("has_whitelist") && mechanism.requireBoolean()) {
        Bukkit.setWhitelist(mechanism.getValue().asBoolean());
    }
}
Also used : CommandContext(net.citizensnpcs.api.command.CommandContext) File(java.io.File) SQLException(java.sql.SQLException) BukkitScriptEvent(com.denizenscript.denizen.events.BukkitScriptEvent) TickScriptEvent(com.denizenscript.denizencore.events.core.TickScriptEvent) ScriptEvent(com.denizenscript.denizencore.events.ScriptEvent)

Aggregations

BukkitScriptEvent (com.denizenscript.denizen.events.BukkitScriptEvent)1 ScriptEvent (com.denizenscript.denizencore.events.ScriptEvent)1 TickScriptEvent (com.denizenscript.denizencore.events.core.TickScriptEvent)1 File (java.io.File)1 SQLException (java.sql.SQLException)1 CommandContext (net.citizensnpcs.api.command.CommandContext)1