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