From 95b9551c704fc8c9a20604d286f53bb317cd4462 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sat, 11 Sep 2021 16:19:43 +0200 Subject: [PATCH] Flatteningfix, vanilla java error logger (<1.12 support!) --- .../de/steamwar/core/FlatteningWrapper14.java | 5 +- .../src/de/steamwar/core/Core.java | 8 +- .../src/de/steamwar/core/ErrorHandler.java | 140 ++++++++++++++++++ .../src/de/steamwar/core/ErrorLogger.java | 63 -------- .../src/de/steamwar/inventory/SWItem.java | 1 + .../src/de/steamwar/sql/SWException.java | 93 +----------- 6 files changed, 149 insertions(+), 161 deletions(-) create mode 100644 SpigotCore_Main/src/de/steamwar/core/ErrorHandler.java delete mode 100644 SpigotCore_Main/src/de/steamwar/core/ErrorLogger.java diff --git a/SpigotCore_14/src/de/steamwar/core/FlatteningWrapper14.java b/SpigotCore_14/src/de/steamwar/core/FlatteningWrapper14.java index b5da68a..63ded8e 100644 --- a/SpigotCore_14/src/de/steamwar/core/FlatteningWrapper14.java +++ b/SpigotCore_14/src/de/steamwar/core/FlatteningWrapper14.java @@ -256,9 +256,8 @@ public class FlatteningWrapper14 implements FlatteningWrapper.IFlatteningWrapper renamedLegacy.put("RECORD_12", Material.MUSIC_DISC_WAIT); } - private static final Class chatComponent = Reflection.getClass("{nms}.ChatComponentText"); - private static final Reflection.FieldAccessor scoreboardName = Reflection.getField(SWScoreboard.scoreboardObjective, chatComponent, 0); - private static final Reflection.ConstructorInvoker chatComponentConstructor = Reflection.getConstructor(chatComponent, String.class); + private static final Reflection.FieldAccessor scoreboardName = Reflection.getField(SWScoreboard.scoreboardObjective, Reflection.getClass("{nms}.IChatBaseComponent"), 0); + private static final Reflection.ConstructorInvoker chatComponentConstructor = Reflection.getConstructor(Reflection.getClass("{nms}.ChatComponentText"), String.class); @Override public void setScoreboardTitle(Object packet, String title) { diff --git a/SpigotCore_Main/src/de/steamwar/core/Core.java b/SpigotCore_Main/src/de/steamwar/core/Core.java index bba0e3d..46ceb2f 100644 --- a/SpigotCore_Main/src/de/steamwar/core/Core.java +++ b/SpigotCore_Main/src/de/steamwar/core/Core.java @@ -19,8 +19,8 @@ package de.steamwar.core; -import de.steamwar.core.authlib.AuthlibInjector; import de.steamwar.comms.BungeeReceiver; +import de.steamwar.core.authlib.AuthlibInjector; import de.steamwar.core.events.ChattingEvent; import de.steamwar.core.events.ChunkListener; import de.steamwar.core.events.PlayerJoinedEvent; @@ -57,14 +57,14 @@ public class Core extends JavaPlugin{ @Override public void onEnable() { + new ErrorHandler(); + Bukkit.getPluginManager().registerEvents(new PlayerJoinedEvent(), this); Bukkit.getPluginManager().registerEvents(new ChattingEvent(), this); Bukkit.getPluginManager().registerEvents(new WorldLoadEvent(), this); - ChunkListener.init(); - if(version >= 12) - ErrorLogger.init(); getServer().getMessenger().registerIncomingPluginChannel(this, "sw:bridge", new BungeeReceiver()); getServer().getMessenger().registerOutgoingPluginChannel(this, "sw:bridge"); + ChunkListener.init(); AuthlibInjector.inject(); } diff --git a/SpigotCore_Main/src/de/steamwar/core/ErrorHandler.java b/SpigotCore_Main/src/de/steamwar/core/ErrorHandler.java new file mode 100644 index 0000000..324fe15 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/core/ErrorHandler.java @@ -0,0 +1,140 @@ +/* + This file is a part of the SteamWar software. + + Copyright (C) 2020 SteamWar.de-Serverteam + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +package de.steamwar.core; + +import de.steamwar.sql.SWException; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.logging.Handler; +import java.util.logging.Level; +import java.util.logging.LogRecord; +import java.util.logging.Logger; + +public class ErrorHandler extends Handler { + + private boolean logDisabled = false; + + public ErrorHandler(){ + Logger.getLogger("").addHandler(this); + } + + @Override + public void publish(LogRecord logRecord) { + if(logRecord.getLevel().intValue() < Level.WARNING.intValue()) + return; + + if(logDisabled) + return; + + String message = logRecord.getMessage(); + for(String reason : ignoreStartsWith) + if(message.startsWith(reason)) + return; + for(String reason : ignoreContains) + if(message.contains(reason)) + return; + + switch (message) { + case "The server has stopped responding!": + logDisabled = true; + return; + case "------------------------------": + message = "Server stopped responding"; + logDisabled = true; + break; + case "Exception stopping the server": + logDisabled = true; + break; + default: + } + + ByteArrayOutputStream stacktraceOutput = new ByteArrayOutputStream(); + logRecord.getThrown().printStackTrace(new PrintStream(stacktraceOutput)); + String stacktrace = stacktraceOutput.toString(); + if(stacktrace.contains("POI data mismatch")) + return; + + SWException.log(message, stacktrace); + } + + @Override + public void flush() { + //This is task of the database + } + + @Override + public void close() throws SecurityException { + //Done in the database + } + + private static final List ignoreStartsWith; + private static final List ignoreContains; + + static { + List startsWith = new ArrayList<>(); + startsWith.add("Could not save the list after adding a user."); + startsWith.add("Could not save spigot.yml"); + startsWith.add("Failed to save operators list:"); + startsWith.add("Block at"); + startsWith.add("POI data mismatch"); + startsWith.add("handleDisconnection() called twice"); + startsWith.add("**** SERVER IS RUNNING IN OFFLINE/INSECURE MODE!"); + startsWith.add("The server will make no attempt to authenticate usernames. Beware."); + startsWith.add("Whilst this makes it possible to use BungeeCord,"); + startsWith.add("Please see http://www.spigotmc.org/wiki/firewall-guide/ for further information."); + startsWith.add("To change this, set \"online-mode\" to \"true\" in the server.properties file."); + startsWith.add("This crash report has been saved to:"); + startsWith.add("Could not pass event PlayerQuitEvent to WorldEditSUI"); + startsWith.add("[ViaVersion] "); + startsWith.add("[ViaBackwards] "); + startsWith.add("Something went wrong upgrading!"); + startsWith.add("Tried to load unrecognized recipe"); + startsWith.add("Invalid BlockState in palette:"); + startsWith.add("Could not register alias"); + startsWith.add("Can't keep up! Is the server overloaded?"); + startsWith.add("\tat "); + startsWith.add("java.lang.Exception"); + startsWith.add("An exceptionCaught()"); + startsWith.add("Exception verifying"); + startsWith.add("[WorldEditSUI]"); + startsWith.add("Unsupported key:"); + startsWith.add("ThrownPotion entity"); + startsWith.add("Couldn't load custom particle"); + startsWith.add("Chunk file at ["); + startsWith.add("Ignoring unknown attribute"); + startsWith.add("Skipping player strafe phase because no player was found"); + startsWith.add("Couldn't save chunk; already in use by another instance of Minecraft?"); + startsWith.add("Failed to save player data for "); + startsWith.add("Failed to check session lock for world located at"); + startsWith.add("Saving oversized chunk "); + ignoreStartsWith = Collections.unmodifiableList(startsWith); + + List contains = new ArrayList<>(); + contains.add("moved too quickly!"); + contains.add("moved wrongly!"); + contains.add("was kicked for floating too long!"); + contains.add("just tried to change non-editable sign"); + ignoreContains = Collections.unmodifiableList(contains); + } +} diff --git a/SpigotCore_Main/src/de/steamwar/core/ErrorLogger.java b/SpigotCore_Main/src/de/steamwar/core/ErrorLogger.java deleted file mode 100644 index b4ba2f1..0000000 --- a/SpigotCore_Main/src/de/steamwar/core/ErrorLogger.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - This file is a part of the SteamWar software. - - Copyright (C) 2020 SteamWar.de-Serverteam - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . -*/ - -package de.steamwar.core; - -import de.steamwar.sql.SWException; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.core.Appender; -import org.apache.logging.log4j.core.Core; -import org.apache.logging.log4j.core.LogEvent; -import org.apache.logging.log4j.core.LoggerContext; -import org.apache.logging.log4j.core.appender.AbstractAppender; -import org.apache.logging.log4j.core.config.plugins.Plugin; -import org.apache.logging.log4j.core.config.plugins.PluginAttribute; -import org.apache.logging.log4j.core.config.plugins.PluginFactory; -import org.apache.logging.log4j.spi.StandardLevel; - -@Plugin(name = "ErrorLogger", category = Core.CATEGORY_NAME, elementType = Appender.ELEMENT_TYPE) -public class ErrorLogger extends AbstractAppender { - - public static void init(){ - final LoggerContext lc = (LoggerContext) LogManager.getContext(false); - ErrorLogger el = ErrorLogger.createAppender("SWErrorLogger"); - el.start(); - lc.getConfiguration().addAppender(el); - lc.getRootLogger().addAppender(lc.getConfiguration().getAppender(el.getName())); - lc.updateLoggers(); - } - - private ErrorLogger(String name) { - super(name, null, null); - } - - @PluginFactory - public static ErrorLogger createAppender( - @PluginAttribute("name") String name) { - return new ErrorLogger(name); - } - - @Override - public void append(LogEvent logEvent) { - if(logEvent.getLevel().intLevel() > StandardLevel.WARN.intLevel()) - return; - - SWException.log(logEvent); - } -} diff --git a/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java b/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java index 02e89b2..9f7c2b0 100644 --- a/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java +++ b/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java @@ -87,6 +87,7 @@ public class SWItem { this(material, (byte)0, name, lore, enchanted, c); } + @SuppressWarnings("deprecation") public SWItem(Material material, byte meta, String name, List lore, boolean enchanted, InvCallback c) { try { itemStack = new ItemStack(material, 1, (short)0, meta); diff --git a/SpigotCore_Main/src/de/steamwar/sql/SWException.java b/SpigotCore_Main/src/de/steamwar/sql/SWException.java index efee8ad..850c124 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SWException.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SWException.java @@ -19,101 +19,13 @@ package de.steamwar.sql; -import org.apache.logging.log4j.core.LogEvent; import org.bukkit.Bukkit; import org.bukkit.entity.Player; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - public class SWException { private SWException(){} - private static boolean logDisabled = false; - private static final List ignorereasons; - - static { - List reasons = new ArrayList<>(); - reasons.add("Could not save the list after adding a user."); - reasons.add("Could not save spigot.yml"); - reasons.add("Failed to save operators list:"); - reasons.add("Block at"); - reasons.add("POI data mismatch"); - reasons.add("handleDisconnection() called twice"); - reasons.add("**** SERVER IS RUNNING IN OFFLINE/INSECURE MODE!"); - reasons.add("The server will make no attempt to authenticate usernames. Beware."); - reasons.add("Whilst this makes it possible to use BungeeCord,"); - reasons.add("Please see http://www.spigotmc.org/wiki/firewall-guide/ for further information."); - reasons.add("To change this, set \"online-mode\" to \"true\" in the server.properties file."); - reasons.add("This crash report has been saved to:"); - reasons.add("Could not pass event PlayerQuitEvent to WorldEditSUI"); - reasons.add("[ViaVersion] "); - reasons.add("[ViaBackwards] "); - reasons.add("Something went wrong upgrading!"); - reasons.add("Tried to load unrecognized recipe"); - reasons.add("Invalid BlockState in palette:"); - reasons.add("Could not register alias"); - reasons.add("Can't keep up! Is the server overloaded?"); - reasons.add("\tat "); - reasons.add("java.lang.Exception"); - reasons.add("An exceptionCaught()"); - reasons.add("Exception verifying"); - reasons.add("[WorldEditSUI]"); - reasons.add("Unsupported key:"); - reasons.add("ThrownPotion entity"); - reasons.add("Couldn't load custom particle"); - reasons.add("Chunk file at ["); - reasons.add("Ignoring unknown attribute"); - reasons.add("Skipping player strafe phase because no player was found"); - reasons.add("Couldn't save chunk; already in use by another instance of Minecraft?"); - reasons.add("Failed to save player data for "); - reasons.add("Failed to check session lock for world located at"); - reasons.add("Saving oversized chunk "); - ignorereasons = Collections.unmodifiableList(reasons); - } - - public static void log(LogEvent logEvent){ - if(logDisabled) - return; - - String message = logEvent.getMessage().getFormattedMessage(); - for(String reason : ignorereasons) - if(message.startsWith(reason)) - return; - - if(message.contains("moved too quickly!") || message.contains("moved wrongly!") || message.contains("was kicked for floating too long!") || message.contains("just tried to change non-editable sign")) - return; - - switch (message) { - case "The server has stopped responding!": - logDisabled = true; - return; - case "------------------------------": - message = "Server stopped responding"; - logDisabled = true; - break; - case "Exception stopping the server": - logDisabled = true; - break; - default: - } - - StringBuilder stacktrace = new StringBuilder(logEvent.getSource().toString()); - Throwable throwable = logEvent.getThrown(); - while(throwable != null){ - stacktrace.append("\nCaused by ").append(throwable.getClass().getName()).append(": ").append(throwable.getMessage()); - - for(StackTraceElement ste : throwable.getStackTrace()) - stacktrace.append("\n").append(ste.toString()); - - throwable = throwable.getCause(); - } - - String st = stacktrace.toString(); - if(st.contains("POI data mismatch")) - return; - + public static void log(String message, String stacktrace){ message += "\n"; for(Player player : Bukkit.getOnlinePlayers()) message += player.getName() + " "; @@ -124,7 +36,6 @@ public class SWException { else server = Bukkit.getWorlds().get(0).getName(); - SQL.update("INSERT INTO Exception (server, message, stacktrace) VALUES (?, ?, ?)", - server, message, st); + SQL.update("INSERT INTO Exception (server, message, stacktrace) VALUES (?, ?, ?)", server, message, stacktrace); } }