Skip to content

Commit

Permalink
Official Purpur Patches : Redirect System.out calls to plugin loggers (
Browse files Browse the repository at this point in the history
  • Loading branch information
Euphillya committed Jul 1, 2021
1 parent 45607f0 commit 25570d7
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
2 changes: 1 addition & 1 deletion patches/api/0002-Purpur-config-files.patch
Expand Up @@ -5,7 +5,7 @@ Subject: [PATCH] Purpur config files


diff --git a/pom.xml b/pom.xml
index 6fd6066b16d4dbf8695be16ec8b2d135d4d04901..4a0922ef102ab08bd4930e3d7dd3a725c2db3ddc 100644
index 6fd6066b16d4dbf8695be16ec8b2d135d4d04901..5d4130c7bb837ca3271993b5749831f44c3ae17c 100644
--- a/pom.xml
+++ b/pom.xml
@@ -68,7 +68,7 @@
Expand Down
@@ -0,0 +1,88 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: William Blake Galbreath <blake.galbreath@gmail.com>
Date: Thu, 1 Jul 2021 03:46:03 +0200
Subject: [PATCH] Redirect System.out calls to plugin loggers


diff --git a/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java b/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
index de917e53fdbd4d8965237864db5fe8bf63cca61b..23f629bce3738c1ad151e480b34f7dbe79b1ffa8 100644
--- a/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
+++ b/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
@@ -171,8 +171,8 @@ public class DedicatedServer extends MinecraftServer implements IMinecraftServer
*/
// Paper end

- System.setOut(IoBuilder.forLogger(logger).setLevel(Level.INFO).buildPrintStream());
- System.setErr(IoBuilder.forLogger(logger).setLevel(Level.WARN).buildPrintStream());
+ System.setOut(new net.pl3x.purpur.PurpurPrintStream(IoBuilder.forLogger(logger).setLevel(Level.INFO).buildPrintStream())); // Purpur
+ System.setErr(new net.pl3x.purpur.PurpurPrintStream(IoBuilder.forLogger(logger).setLevel(Level.WARN).buildPrintStream())); // Purpur
// CraftBukkit end

thread.setDaemon(true);
diff --git a/src/main/java/net/pl3x/purpur/PurpurPrintStream.java b/src/main/java/net/pl3x/purpur/PurpurPrintStream.java
new file mode 100644
index 0000000000000000000000000000000000000000..96e897f013f250e1d356052efc68e38ba60ad8b8
--- /dev/null
+++ b/src/main/java/net/pl3x/purpur/PurpurPrintStream.java
@@ -0,0 +1,60 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2020 Josh (Vicarious)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+package net.pl3x.purpur;
+
+import org.bukkit.plugin.java.JavaPlugin;
+import org.jetbrains.annotations.NotNull;
+
+import java.io.OutputStream;
+import java.io.PrintStream;
+
+/**
+ * Logic borrowed from SysoutCatcher
+ *
+ * https://www.spigotmc.org/resources/sysoutcatcher.79076/
+ */
+
+public class PurpurPrintStream extends PrintStream {
+ public PurpurPrintStream(@NotNull OutputStream out) {
+ super(out);
+ }
+
+ @Override
+ public void println(String line) {
+ // Get the current stack trace and the calling method (index 2)
+ StackTraceElement element = Thread.currentThread().getStackTrace()[2];
+ try {
+ // Get the class name at that index and the JavaPlugin that "owns" it
+ Class<?> clazz = Class.forName(element.getClassName());
+ JavaPlugin plugin = JavaPlugin.getProvidingPlugin(clazz);
+
+ // Instead of just printing the message, send it to the plugin's logger
+ plugin.getLogger().info(line);
+ } catch (ClassNotFoundException | IllegalArgumentException e) {
+ // If anything happens, the calling class doesn't exist, there is no JavaPlugin that "owns" the calling class, etc
+ // Just print out normally, with some added information
+ super.printf("(%s:%d) %s\n", element.getClassName(), element.getLineNumber(), line);
+ }
+ }
+}
\ No newline at end of file

0 comments on commit 25570d7

Please sign in to comment.