+ 修复:插件主动调用依赖注入方法后重复注入的问题
This commit is contained in:
坏黑 2018-07-09 20:02:21 +08:00
parent 655e3c2619
commit dd2e1f7d1c
4 changed files with 41 additions and 31 deletions

View File

@ -6,7 +6,7 @@
<groupId>me.skymc</groupId>
<artifactId>TabooLib</artifactId>
<version>4.13</version>
<version>4.14</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

View File

@ -12,21 +12,22 @@ import java.util.logging.LogRecord;
*/
public class TLoggerFilter implements Filter {
public static void init() {
Bukkit.getLogger().setFilter(new TLoggerFilter());
}
public static void init() {
Bukkit.getLogger().setFilter(new TLoggerFilter());
}
@Override
public boolean isLoggable(LogRecord e) {
if (e.getMessage().contains("Cannot load configuration from stream")) {
StackTraceElement[] elements = Thread.currentThread().getStackTrace();
for (StackTraceElement element : elements) {
if (element.getClassName().contains("ConfigUtils")) {
System.out.println(Arrays.asList(e.getParameters()));
}
}
return false;
}
else return !e.getMessage().contains("Enabled plugin with unregistered PluginClassLoader");
@Override
public boolean isLoggable(LogRecord e) {
if (e.getMessage().contains("Cannot load configuration from stream")) {
StackTraceElement[] elements = Thread.currentThread().getStackTrace();
for (StackTraceElement element : elements) {
if (element.getClassName().contains("ConfigUtils")) {
System.out.println(Arrays.asList(e.getParameters()));
}
}
return false;
} else {
return !e.getMessage().contains("Enabled plugin with unregistered PluginClassLoader");
}
}
}

View File

@ -13,10 +13,17 @@ import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* @author Izzel_Aliz
*/
public class TDependencyInjector {
private static List<String> injected = new ArrayList<>();
public static Dependency[] getDependencies(Object o) {
Dependency[] dependencies = new Dependency[0];
Dependencies d = o.getClass().getAnnotation(Dependencies.class);
@ -30,26 +37,28 @@ public class TDependencyInjector {
return dependencies;
}
public static void inject(Plugin plugin, Object o) {
TLocaleLoader.load(plugin, true);
injectDependencies(plugin, o);
injectLogger(plugin, o);
injectConfig(plugin, o);
injectPluginInstance(plugin, o);
}
static void injectOnEnable(Plugin plugin) {
if (!plugin.equals(Main.getInst())) {
inject(plugin, plugin);
}
inject(plugin, plugin);
}
static void onDisable(Plugin plugin) {
static void ejectOnDisable(Plugin plugin) {
eject(plugin, plugin);
}
public static void inject(Plugin plugin, Object o) {
if (!plugin.equals(Main.getInst()) && !injected.contains(plugin.getName())) {
injected.add(plugin.getName());
TLocaleLoader.load(plugin, true);
injectDependencies(plugin, o);
injectLogger(plugin, o);
injectConfig(plugin, o);
injectPluginInstance(plugin, o);
}
}
public static void eject(Plugin plugin, Object o) {
try {
injected.remove(plugin.getName());
ejectConfig(plugin, o);
} catch (Throwable ignored) {
}

View File

@ -155,7 +155,7 @@ public class TPluginManager implements PluginManager {
@Override
public void disablePlugin(Plugin plugin) {
TDependencyInjector.onDisable(plugin);
TDependencyInjector.ejectOnDisable(plugin);
instance.disablePlugin(plugin);
}