Skip to content

Commit

Permalink
Add a ridiculous 'on console output' event
Browse files Browse the repository at this point in the history
Disabled by default for sanity's sake
  • Loading branch information
mcmonkey4eva committed Dec 15, 2014
1 parent 0c979de commit 1d7e202
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 4 deletions.
12 changes: 9 additions & 3 deletions src/main/java/net/aufdemrand/denizen/Denizen.java
Expand Up @@ -16,6 +16,7 @@
import net.aufdemrand.denizen.scripts.commands.BukkitCommandRegistry;
import net.aufdemrand.denizen.scripts.queues.ScriptQueue;
import net.aufdemrand.denizen.utilities.*;
import net.aufdemrand.denizen.utilities.debugging.LogInterceptor;
import net.aufdemrand.denizen.utilities.maps.DenizenMapManager;
import net.aufdemrand.denizencore.interfaces.dExternal;
import net.aufdemrand.denizen.listeners.ListenerRegistry;
Expand Down Expand Up @@ -58,10 +59,12 @@


public class Denizen extends JavaPlugin implements DenizenImplementation {
public final static int configVersion = 7;
public final static int configVersion = 9;
public static String versionTag = null;
private boolean startedSuccessful = false;

public static final LogInterceptor logInterceptor = new LogInterceptor();


private CommandManager commandManager;

Expand Down Expand Up @@ -270,9 +273,9 @@ public void onEnable() {
try {
// Warn if configuration is outdated / too new
if (!getConfig().isSet("Config.Version") ||
getConfig().getInt("Config.Version", 0) != configVersion) {
getConfig().getInt("Config.Version", 0) < configVersion) {

dB.echoError("Your Denizen config file is from a different version. " +
dB.echoError("Your Denizen config file is from an older version. " +
"Some settings will not be available unless you generate a new one. " +
"This is easily done by stopping the server, deleting the current config.yml file in the Denizen folder " +
"and restarting the server.");
Expand Down Expand Up @@ -393,6 +396,9 @@ public void run() {
public void onDisable() {
if(!startedSuccessful) return;

// Disable the log interceptor... otherwise bad things on /reload
logInterceptor.standardOutput();

// Save notables
notableManager.saveNotables();

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/net/aufdemrand/denizen/Settings.java
Expand Up @@ -48,6 +48,11 @@ public static boolean showExHelp() {
.getBoolean("Debug.Ex command help", true);
}

public static boolean allowConsoleRedirection() {
return DenizenAPI.getCurrentInstance().getConfig()
.getBoolean("Debug.Allow console redirection", false);
}

/*
# Sets the default speed between execution of commands in queues
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/net/aufdemrand/denizen/tags/core/UtilTags.java
Expand Up @@ -728,6 +728,30 @@ public static void adjustServer(Mechanism mechanism) {
}
}

// <--[mechanism]
// @object server
// @name redirect_logging
// @input Element
// @description
// Tells the server to redirect logging to a world event or not.
// Note that this redirects *all console output* not just Denizen output.
// Note: don't enable /denizen debug -e while this is active.
// @tags
// None
// -->
if (mechanism.matches("redirect_logging") && mechanism.hasValue()) {
if (!Settings.allowConsoleRedirection()) {
dB.echoError("Console redirection disabled by administrator.");
return;
}
if (mechanism.getValue().asBoolean()) {
Denizen.logInterceptor.redirectOutput();
}
else {
Denizen.logInterceptor.standardOutput();
}
}

// TODO: Properties somehow?

if (!mechanism.fulfilled())
Expand Down
Expand Up @@ -3,6 +3,8 @@
import net.aufdemrand.denizen.Denizen;
import net.aufdemrand.denizen.objects.Duration;
import net.aufdemrand.denizen.objects.dPlayer;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizencore.DenizenCore;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.World;
Expand Down Expand Up @@ -78,10 +80,12 @@ public void run() {
+ "&pastecontents=" + URLEncoder.encode("Java Version: " + System.getProperty("java.version")
+ "\nUp-time: " + new Duration((System.currentTimeMillis() - Denizen.startTime) / 50).formatted()
+ "\nCraftBukkit Version: " + Bukkit.getServer().getVersion()
+ "\nDenizen Version: Core: " + DenizenCore.VERSION + ", CraftBukkit: " + DenizenAPI.getCurrentInstance().getImplementationVersion()
+ "\nActive Plugins (" + pluginCount + "): " + pluginlist.substring(0, pluginlist.length() - 2)
+ "\nLoaded Worlds (" + worldCount + "): " + worldlist.substring(0, worldlist.length() - 2)
+ "\nOnline Players (" + playerCount + "): " + playerlist.substring(0, playerlist.length() - 2)
+ "\nOffline Players: " + (dPlayer.getAllPlayers().size() - playerCount)
+ "\nMode: " + (Bukkit.getServer().getOnlineMode() ? "online": "offline")
+ "\n\n") + recording)
.getBytes("UTF-8"));
// Wait for a response from the server
Expand Down
@@ -0,0 +1,100 @@
package net.aufdemrand.denizen.utilities.debugging;

import net.aufdemrand.denizen.events.EventManager;
import net.aufdemrand.denizen.objects.Element;
import net.aufdemrand.denizen.objects.dObject;
import net.aufdemrand.denizencore.utilities.CoreUtilities;
import org.apache.commons.lang.StringUtils;
import org.bukkit.ChatColor;

import java.io.PrintStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
* Intercepts system.out operations for the sake of blocking messages at request.
* Disabled by default in config.yml
*/
public class LogInterceptor extends PrintStream {
boolean redirected = false;
public PrintStream standardOut;

public LogInterceptor()
{
super(System.out, true);
}

// <--[event]
// @Events
// console output
//
// @Warning Disable debug on this event or you'll get an infinite loop!
//
// @Triggers when any message is printed to console. (Requires <@link mechanism server.redirect_logging> be set true.)
// @Context
// <context.message> returns the messsage that is being printed to console.
//
// @Determine
// "CANCELLED" to disable the output.
//
// -->
@Override
public void print(String s) {
HashMap<String, dObject> context = new HashMap<String, dObject>();
context.put("message", new Element(cleanse(s)));
List<String> Determinations = EventManager.doEvents1(Arrays.asList("console output"), null, null, context);
for (String str: Determinations) {
if (str.equalsIgnoreCase("cancelled")) {
return;
}
}
super.print(s);
}

public String cleanse(String input) {
String esc = String.valueOf((char) 0x1b);
String repc = String.valueOf(ChatColor.COLOR_CHAR);
if (input.contains(esc))
{
input = StringUtils.replace(input, esc + "[0;30;22m", repc + "0");
input = StringUtils.replace(input, esc + "[0;34;22m", repc + "1");
input = StringUtils.replace(input, esc + "[0;32;22m", repc + "2");
input = StringUtils.replace(input, esc + "[0;36;22m", repc + "3");
input = StringUtils.replace(input, esc + "[0;31;22m", repc + "4");
input = StringUtils.replace(input, esc + "[0;35;22m", repc + "5");
input = StringUtils.replace(input, esc + "[0;33;22m", repc + "6");
input = StringUtils.replace(input, esc + "[0;37;22m", repc + "7");
input = StringUtils.replace(input, esc + "[0;30;1m", repc + "8");
input = StringUtils.replace(input, esc + "[0;34;1m", repc + "9");
input = StringUtils.replace(input, esc + "[0;32;1m", repc + "a");
input = StringUtils.replace(input, esc + "[0;36;1m", repc + "b");
input = StringUtils.replace(input, esc + "[0;31;1m", repc + "c");
input = StringUtils.replace(input, esc + "[0;35;1m", repc + "d");
input = StringUtils.replace(input, esc + "[0;33;1m", repc + "e");
input = StringUtils.replace(input, esc + "[0;37;1m", repc + "f");
input = StringUtils.replace(input, esc + "[5m", repc + "k");
input = StringUtils.replace(input, esc + "[21m", repc + "l");
input = StringUtils.replace(input, esc + "[9m", repc + "m");
input = StringUtils.replace(input, esc + "[4m", repc + "n");
input = StringUtils.replace(input, esc + "[3m", repc + "o");
input = StringUtils.replace(input, esc + "[m", repc + "r");
}
return input;
}

public void redirectOutput() {
if (redirected) {
return;
}
standardOut = System.out;
System.setOut(this);
}

public void standardOutput() {
if (!redirected) {
return;
}
System.setOut(standardOut);
}
}
4 changes: 3 additions & 1 deletion src/main/resources/config.yml
Expand Up @@ -6,6 +6,8 @@ Debug:
# Adjust to your console width minus 16. Ideally, we'd calculate this, but we currently can't.
Console width: 64
Trim length: 512
# Whether to permit the "server.redirect_logging" mechanism
Allow console redirection: false

Scripts:
World:
Expand Down Expand Up @@ -111,4 +113,4 @@ Tags:
# The version of this configuration file, used to check if your
# configuration file is outdated or too new
Config:
Version: 8
Version: 9

0 comments on commit 1d7e202

Please sign in to comment.