v4.14
+ 修复:插件主动调用依赖注入方法后重复注入的问题
This commit is contained in:
		
							
								
								
									
										2
									
								
								pom.xml
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								pom.xml
									
									
									
									
									
								
							@@ -6,7 +6,7 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    <groupId>me.skymc</groupId>
 | 
					    <groupId>me.skymc</groupId>
 | 
				
			||||||
    <artifactId>TabooLib</artifactId>
 | 
					    <artifactId>TabooLib</artifactId>
 | 
				
			||||||
    <version>4.13</version>
 | 
					    <version>4.14</version>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    <properties>
 | 
					    <properties>
 | 
				
			||||||
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 | 
					        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -11,22 +11,23 @@ import java.util.logging.LogRecord;
 | 
				
			|||||||
 * @since 2018-04-22
 | 
					 * @since 2018-04-22
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
public class TLoggerFilter implements Filter {
 | 
					public class TLoggerFilter implements Filter {
 | 
				
			||||||
	
 | 
					
 | 
				
			||||||
	public static void init() {
 | 
					    public static void init() {
 | 
				
			||||||
		Bukkit.getLogger().setFilter(new TLoggerFilter());
 | 
					        Bukkit.getLogger().setFilter(new TLoggerFilter());
 | 
				
			||||||
	}
 | 
					    }
 | 
				
			||||||
	
 | 
					
 | 
				
			||||||
	@Override
 | 
					    @Override
 | 
				
			||||||
	public boolean isLoggable(LogRecord e) {
 | 
					    public boolean isLoggable(LogRecord e) {
 | 
				
			||||||
		if (e.getMessage().contains("Cannot load configuration from stream")) {
 | 
					        if (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")) {
 | 
				
			||||||
					System.out.println(Arrays.asList(e.getParameters()));
 | 
					                    System.out.println(Arrays.asList(e.getParameters()));
 | 
				
			||||||
				}
 | 
					                }
 | 
				
			||||||
			}
 | 
					            }
 | 
				
			||||||
			return false;
 | 
					            return false;
 | 
				
			||||||
		}
 | 
					        } else {
 | 
				
			||||||
		else return !e.getMessage().contains("Enabled plugin with unregistered PluginClassLoader");
 | 
					            return !e.getMessage().contains("Enabled plugin with unregistered PluginClassLoader");
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -13,10 +13,17 @@ import org.bukkit.plugin.java.JavaPlugin;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import java.io.File;
 | 
					import java.io.File;
 | 
				
			||||||
import java.lang.reflect.Field;
 | 
					import java.lang.reflect.Field;
 | 
				
			||||||
 | 
					import java.util.ArrayList;
 | 
				
			||||||
import java.util.HashMap;
 | 
					import java.util.HashMap;
 | 
				
			||||||
 | 
					import java.util.List;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @author Izzel_Aliz
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
public class TDependencyInjector {
 | 
					public class TDependencyInjector {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private static List<String> injected = new ArrayList<>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public static Dependency[] getDependencies(Object o) {
 | 
					    public static Dependency[] getDependencies(Object o) {
 | 
				
			||||||
        Dependency[] dependencies = new Dependency[0];
 | 
					        Dependency[] dependencies = new Dependency[0];
 | 
				
			||||||
        Dependencies d = o.getClass().getAnnotation(Dependencies.class);
 | 
					        Dependencies d = o.getClass().getAnnotation(Dependencies.class);
 | 
				
			||||||
@@ -30,26 +37,28 @@ public class TDependencyInjector {
 | 
				
			|||||||
        return dependencies;
 | 
					        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) {
 | 
					    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);
 | 
					        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) {
 | 
					    public static void eject(Plugin plugin, Object o) {
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
 | 
					            injected.remove(plugin.getName());
 | 
				
			||||||
            ejectConfig(plugin, o);
 | 
					            ejectConfig(plugin, o);
 | 
				
			||||||
        } catch (Throwable ignored) {
 | 
					        } catch (Throwable ignored) {
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -155,7 +155,7 @@ public class TPluginManager implements PluginManager {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public void disablePlugin(Plugin plugin) {
 | 
					    public void disablePlugin(Plugin plugin) {
 | 
				
			||||||
        TDependencyInjector.onDisable(plugin);
 | 
					        TDependencyInjector.ejectOnDisable(plugin);
 | 
				
			||||||
        instance.disablePlugin(plugin);
 | 
					        instance.disablePlugin(plugin);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user