+ 还是异常拦截的那些问题
This commit is contained in:
parent
f0e157d3a7
commit
42215556df
@ -22,9 +22,11 @@ import java.util.logging.Logger;
|
||||
public class TLoggerFilter implements Filter {
|
||||
|
||||
private Filter filter;
|
||||
private Logger logger;
|
||||
private static List<TLoggerFilterHandler> handlers = Lists.newLinkedList();
|
||||
private static Map<String, TLoggerFilter> pluginFilter = Maps.newHashMap();
|
||||
private static TLoggerFilter globalFilter;
|
||||
private static String playerConnectionName;
|
||||
|
||||
static {
|
||||
handlers.add(new FilterConfiguration());
|
||||
@ -35,23 +37,30 @@ public class TLoggerFilter implements Filter {
|
||||
public static void preInit() {
|
||||
inject(new TLoggerFilter(), Bukkit.getLogger());
|
||||
inject(new TLoggerFilter(), TabooLib.instance().getLogger());
|
||||
try {
|
||||
playerConnectionName = Class.forName("net.minecraft.server." + TabooLib.getVersion() + ".PlayerConnection").getName();
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
public static void postInit() {
|
||||
Arrays.stream(Bukkit.getPluginManager().getPlugins()).filter(TabooLib::isDependTabooLib).forEach(plugin -> inject(new TLoggerFilter(), plugin.getLogger()));
|
||||
}
|
||||
|
||||
public static void inject(TLoggerFilter filter, Logger logger) {
|
||||
try {
|
||||
filter.filter = logger.getFilter();
|
||||
logger.setFilter(filter);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
public static void inject0() {
|
||||
inject(new TLoggerFilter(), Logger.getLogger(playerConnectionName));
|
||||
}
|
||||
|
||||
public Filter getFilter() {
|
||||
return filter;
|
||||
public static void inject(TLoggerFilter filter, Logger logger) {
|
||||
if (!(logger.getFilter() instanceof TLoggerFilter)) {
|
||||
try {
|
||||
filter.filter = logger.getFilter();
|
||||
filter.logger = logger;
|
||||
logger.setFilter(filter);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static TLoggerFilter getGlobalFilter() {
|
||||
@ -66,6 +75,14 @@ public class TLoggerFilter implements Filter {
|
||||
return handlers;
|
||||
}
|
||||
|
||||
public Filter getFilter() {
|
||||
return filter;
|
||||
}
|
||||
|
||||
public Logger getLogger() {
|
||||
return logger;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isLoggable(LogRecord e) {
|
||||
return handlers.stream().allMatch(filter -> filter.isLoggable(e)) && (filter == null || filter.isLoggable(e));
|
||||
|
@ -13,7 +13,7 @@ public class FilterConfiguration extends TLoggerFilterHandler {
|
||||
|
||||
@Override
|
||||
public boolean isLoggable(LogRecord e) {
|
||||
if (e.getMessage().contains("Cannot load configuration from stream")) {
|
||||
if (String.valueOf(e.getMessage()).contains("Cannot load configuration from stream")) {
|
||||
StackTraceElement[] elements = Thread.currentThread().getStackTrace();
|
||||
for (StackTraceElement element : elements) {
|
||||
if (element.getClassName().contains("ConfigUtils")) {
|
||||
|
@ -83,7 +83,7 @@ public class FilterExceptionMirror extends TLoggerFilterHandler {
|
||||
if (isScheduleException(e)) {
|
||||
long time = System.currentTimeMillis();
|
||||
AtomicReference<Plugin> plugin = new AtomicReference<>();
|
||||
return !printException(plugin, e.getThrown().getStackTrace(), "SCHEDULE", () -> new String[] {plugin.get().getName(), String.valueOf(System.currentTimeMillis() - time), e.getThrown().getClass().getName(), String.valueOf(e.getThrown().getMessage())});
|
||||
return !printException(plugin, e.getThrown().getStackTrace(), "SCHEDULE", () -> new String[] {plugin.get().getName(), String.valueOf(System.currentTimeMillis() - time), e.getThrown().getClass().getSimpleName(), String.valueOf(e.getThrown().getMessage())});
|
||||
}
|
||||
// 是否为其他可捕捉异常
|
||||
else if (isValidException(e.getThrown())) {
|
||||
@ -93,7 +93,7 @@ public class FilterExceptionMirror extends TLoggerFilterHandler {
|
||||
if (matcher.find()) {
|
||||
long time = System.currentTimeMillis();
|
||||
AtomicReference<Plugin> plugin = new AtomicReference<>();
|
||||
return !printException(plugin, e.getThrown().getCause().getStackTrace(), "EVENT", () -> new String[] {plugin.get().getName(), String.valueOf(System.currentTimeMillis() - time), matcher.group(1), e.getThrown().getCause().getClass().getName(), String.valueOf(e.getThrown().getCause().getMessage())});
|
||||
return !printException(plugin, e.getThrown().getCause().getStackTrace(), "EVENT", () -> new String[] {plugin.get().getName(), String.valueOf(System.currentTimeMillis() - time), matcher.group(1), e.getThrown().getCause().getClass().getSimpleName(), String.valueOf(e.getThrown().getCause().getMessage())});
|
||||
}
|
||||
}
|
||||
// 命令异常
|
||||
@ -102,14 +102,14 @@ public class FilterExceptionMirror extends TLoggerFilterHandler {
|
||||
if (matcher.find()) {
|
||||
long time = System.currentTimeMillis();
|
||||
AtomicReference<Plugin> plugin = new AtomicReference<>();
|
||||
return !printException(plugin, e.getThrown().getCause().getStackTrace(), "COMMAND", () -> new String[] {plugin.get().getName(), String.valueOf(System.currentTimeMillis() - time), matcher.group(1), e.getThrown().getCause().getClass().getName(), String.valueOf(e.getThrown().getCause().getMessage())});
|
||||
return !printException(plugin, e.getThrown().getCause().getStackTrace(), "COMMAND", () -> new String[] {plugin.get().getName(), String.valueOf(System.currentTimeMillis() - time), matcher.group(1), e.getThrown().getCause().getClass().getSimpleName(), String.valueOf(e.getThrown().getCause().getMessage())});
|
||||
}
|
||||
}
|
||||
// 其他异常
|
||||
else {
|
||||
long time = System.currentTimeMillis();
|
||||
AtomicReference<Plugin> plugin = new AtomicReference<>();
|
||||
return !printException(plugin, e.getThrown().getCause().getStackTrace(), "OTHER", () -> new String[] {plugin.get().getName(), String.valueOf(System.currentTimeMillis() - time), e.getThrown().getCause().getClass().getName(), String.valueOf(e.getThrown().getCause().getMessage())});
|
||||
return !printException(plugin, e.getThrown().getCause().getStackTrace(), "OTHER", () -> new String[] {plugin.get().getName(), String.valueOf(System.currentTimeMillis() - time), e.getThrown().getCause().getClass().getSimpleName(), String.valueOf(e.getThrown().getCause().getMessage())});
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
@ -13,6 +13,6 @@ public class FilterInvalidPluginLoader extends TLoggerFilterHandler {
|
||||
@Override
|
||||
public boolean isLoggable(LogRecord e) {
|
||||
// 屏蔽插件加载器注入导致的警告信息
|
||||
return !e.getMessage().contains("Enabled plugin with unregistered PluginClassLoader");
|
||||
return !String.valueOf(e.getMessage()).contains("Enabled plugin with unregistered PluginClassLoader");
|
||||
}
|
||||
}
|
||||
|
@ -19,7 +19,8 @@ public class TabooLib {
|
||||
|
||||
static {
|
||||
try {
|
||||
Class.forName("org.spigotmc.SpigotConfig");
|
||||
// 判断是否为独立客户端运行,不是判断 Bukkit 与 Spigot
|
||||
Class.forName("org.bukkit.Bukkit");
|
||||
spigot = true;
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package me.skymc.taboolib.listener;
|
||||
|
||||
import com.ilummc.tlib.filter.TLoggerFilter;
|
||||
import com.ilummc.tlib.logger.TLogger;
|
||||
import me.skymc.taboolib.Main;
|
||||
import me.skymc.taboolib.TabooLib;
|
||||
@ -30,7 +31,7 @@ public class ListenerPlayerCommand implements Listener {
|
||||
Bukkit.getScheduler().runTaskTimer(TabooLib.instance(), () -> {
|
||||
if (nextException) {
|
||||
nextException = false;
|
||||
throw new IllegalStateException();
|
||||
throw new IllegalStateException("TabooLib Example Exception");
|
||||
}
|
||||
}, 0, 20);
|
||||
}
|
||||
@ -38,7 +39,7 @@ public class ListenerPlayerCommand implements Listener {
|
||||
@TInject
|
||||
static SimpleCommandBuilder tExceptionCommand = SimpleCommandBuilder.create("tExceptionCommand", TabooLib.instance())
|
||||
.execute((sender, args) -> {
|
||||
throw new IllegalStateException();
|
||||
throw new IllegalStateException("TabooLib Example Exception");
|
||||
});
|
||||
|
||||
@TInject
|
||||
@ -70,13 +71,16 @@ public class ListenerPlayerCommand implements Listener {
|
||||
}
|
||||
} else if (e.getCommand().equalsIgnoreCase("tExceptionEvent")) {
|
||||
e.setCancelled(true);
|
||||
throw new IllegalStateException();
|
||||
throw new IllegalStateException("TabooLib Example Exception");
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@EventHandler
|
||||
public void cmd(PlayerCommandPreprocessEvent e) {
|
||||
// 注入异常拦截器
|
||||
TLoggerFilter.inject0();
|
||||
// 其他指令
|
||||
if (e.getMessage().equals("/unbreakable") && PermissionUtils.hasPermission(e.getPlayer(), "taboolib.unbreakable")) {
|
||||
e.setCancelled(true);
|
||||
NBTItem nbt = new NBTItem(e.getPlayer().getItemInHand());
|
||||
@ -90,6 +94,9 @@ public class ListenerPlayerCommand implements Listener {
|
||||
.append(ItemUtils.getCustomName(e.getPlayer().getItemInHand())).hoverItem(e.getPlayer().getItemInHand())
|
||||
.append("§f]")
|
||||
.send(e.getPlayer());
|
||||
} else if (e.getMessage().equalsIgnoreCase("/tExceptionEvent")) {
|
||||
e.setCancelled(true);
|
||||
throw new IllegalStateException("TabooLib Example Exception");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -32,6 +32,7 @@ import java.util.Optional;
|
||||
@TListener
|
||||
public class ListenerPlugin implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void load(TPluginLoadEvent e) {
|
||||
if (TabooLib.isDependTabooLib(e.getPlugin())) {
|
||||
TLoggerFilter.inject(new TLoggerFilter(), e.getPlugin().getLogger());
|
||||
|
Loading…
Reference in New Issue
Block a user