2019-07-05 16:17:54 +00:00
|
|
|
package io.izzel.taboolib.module.inject;
|
2018-10-08 11:44:36 +00:00
|
|
|
|
2020-01-13 16:54:32 +00:00
|
|
|
import com.google.common.collect.Lists;
|
2018-10-08 11:44:36 +00:00
|
|
|
import com.google.common.collect.Maps;
|
2019-07-05 16:17:54 +00:00
|
|
|
import io.izzel.taboolib.TabooLibAPI;
|
|
|
|
import io.izzel.taboolib.TabooLibLoader;
|
2019-07-06 04:03:15 +00:00
|
|
|
import io.izzel.taboolib.module.command.lite.CommandBuilder;
|
2019-07-05 16:17:54 +00:00
|
|
|
import io.izzel.taboolib.module.config.TConfig;
|
2020-01-13 16:54:32 +00:00
|
|
|
import io.izzel.taboolib.module.locale.TLocaleLoader;
|
2019-07-06 04:03:15 +00:00
|
|
|
import io.izzel.taboolib.module.locale.logger.TLogger;
|
2019-07-05 16:17:54 +00:00
|
|
|
import io.izzel.taboolib.module.packet.TPacketHandler;
|
|
|
|
import io.izzel.taboolib.module.packet.TPacketListener;
|
2019-09-18 15:57:04 +00:00
|
|
|
import io.izzel.taboolib.util.Ref;
|
2020-01-13 16:54:32 +00:00
|
|
|
import io.izzel.taboolib.util.Strings;
|
2019-07-06 04:03:15 +00:00
|
|
|
import io.izzel.taboolib.util.lite.cooldown.Cooldown;
|
|
|
|
import io.izzel.taboolib.util.lite.cooldown.Cooldowns;
|
2019-08-18 07:07:39 +00:00
|
|
|
import org.bukkit.Bukkit;
|
2018-10-08 11:44:36 +00:00
|
|
|
import org.bukkit.plugin.Plugin;
|
2019-08-18 07:07:39 +00:00
|
|
|
import org.bukkit.plugin.java.JavaPlugin;
|
2018-10-08 11:44:36 +00:00
|
|
|
|
|
|
|
import java.lang.reflect.Field;
|
2020-01-13 16:54:32 +00:00
|
|
|
import java.util.List;
|
2018-10-08 11:44:36 +00:00
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Author sky
|
|
|
|
* @Since 2018-10-05 13:40
|
|
|
|
*/
|
|
|
|
public class TInjectLoader implements TabooLibLoader.Loader {
|
|
|
|
|
2020-04-29 07:21:06 +00:00
|
|
|
private static final Map<Class<?>, TInjectTask> injectTypes = Maps.newLinkedHashMap();
|
2018-10-08 11:44:36 +00:00
|
|
|
|
|
|
|
static {
|
2018-11-25 08:48:12 +00:00
|
|
|
// Instance Inject
|
2019-08-17 16:38:45 +00:00
|
|
|
injectTypes.put(Plugin.class, (plugin, field, args, pluginClass, instance) -> {
|
2018-11-25 08:48:12 +00:00
|
|
|
try {
|
2020-02-01 09:04:36 +00:00
|
|
|
Ref.putField(instance, field, plugin);
|
2018-11-25 08:48:12 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
});
|
2018-10-08 11:44:36 +00:00
|
|
|
// TLogger Inject
|
2019-08-17 16:38:45 +00:00
|
|
|
injectTypes.put(TLogger.class, (plugin, field, args, pluginClass, instance) -> {
|
2018-10-08 11:44:36 +00:00
|
|
|
try {
|
2020-02-01 09:04:36 +00:00
|
|
|
Ref.putField(instance, field, args.value().length == 0 ? TLogger.getUnformatted(plugin) : TLogger.getUnformatted(args.value()[0]));
|
2018-11-25 08:48:12 +00:00
|
|
|
} catch (Exception e) {
|
2018-10-08 11:44:36 +00:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
});
|
2018-10-28 08:24:46 +00:00
|
|
|
// TPacketListener Inject
|
2019-08-17 16:38:45 +00:00
|
|
|
injectTypes.put(TPacketListener.class, (plugin, field, args, pluginClass, instance) -> {
|
2018-10-28 08:24:46 +00:00
|
|
|
try {
|
2020-02-01 09:04:36 +00:00
|
|
|
TPacketHandler.addListener(plugin, ((TPacketListener) Ref.getField(instance, field)));
|
2018-10-28 08:24:46 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
});
|
2018-10-08 11:44:36 +00:00
|
|
|
// TConfiguration Inject
|
2019-08-17 16:38:45 +00:00
|
|
|
injectTypes.put(TConfig.class, (plugin, field, args, pluginClass, instance) -> {
|
2018-10-08 11:44:36 +00:00
|
|
|
try {
|
2019-08-17 16:38:45 +00:00
|
|
|
TConfig config = TConfig.create(plugin, args.value().length == 0 ? "config.yml" : args.value()[0]);
|
2020-02-01 09:04:36 +00:00
|
|
|
Ref.putField(instance, field, config);
|
2020-01-13 16:54:32 +00:00
|
|
|
if (Strings.nonEmpty(args.locale())) {
|
|
|
|
config.listener(() -> {
|
|
|
|
List<String> localePriority = Lists.newArrayList();
|
|
|
|
if (config.isList(args.locale())) {
|
|
|
|
localePriority.addAll(config.getStringList(args.locale()));
|
|
|
|
} else {
|
|
|
|
localePriority.add(String.valueOf(config.get(args.locale())));
|
|
|
|
}
|
|
|
|
TLocaleLoader.setLocalePriority(plugin, localePriority);
|
|
|
|
TLocaleLoader.load(plugin, true, true);
|
2020-01-16 04:25:56 +00:00
|
|
|
}).runListener();
|
2020-01-13 16:54:32 +00:00
|
|
|
}
|
|
|
|
if (Strings.nonEmpty(args.reload())) {
|
2019-08-17 16:38:45 +00:00
|
|
|
try {
|
2019-11-04 07:56:04 +00:00
|
|
|
Ref.getDeclaredMethods(pluginClass).forEach(method -> {
|
|
|
|
if (method.getName().equals(args.reload())) {
|
|
|
|
method.setAccessible(true);
|
|
|
|
config.listener(() -> {
|
|
|
|
try {
|
|
|
|
method.invoke(instance);
|
2020-03-24 12:15:25 +00:00
|
|
|
} catch (NullPointerException ignored) {
|
2019-11-04 07:56:04 +00:00
|
|
|
} catch (Throwable t) {
|
|
|
|
t.printStackTrace();
|
|
|
|
}
|
|
|
|
});
|
2019-08-17 16:38:45 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch (Throwable t) {
|
|
|
|
t.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2020-04-29 07:28:04 +00:00
|
|
|
if (args.migrate()) {
|
2020-04-29 07:21:06 +00:00
|
|
|
config.migrate();
|
|
|
|
}
|
2020-01-13 16:54:32 +00:00
|
|
|
TabooLibLoader.runTask(config::runListener);
|
2018-11-25 08:48:12 +00:00
|
|
|
} catch (Exception e) {
|
2018-10-08 11:44:36 +00:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
});
|
2018-10-13 13:36:49 +00:00
|
|
|
// SimpleCommandBuilder Inject
|
2019-08-17 16:38:45 +00:00
|
|
|
injectTypes.put(CommandBuilder.class, (plugin, field, args, pluginClass, instance) -> {
|
2018-10-13 13:36:49 +00:00
|
|
|
try {
|
2020-02-02 06:55:20 +00:00
|
|
|
CommandBuilder builder = Ref.getField(instance, field, CommandBuilder.class);
|
2019-08-17 16:38:45 +00:00
|
|
|
if (!builder.isBuild()) {
|
2019-09-11 09:01:39 +00:00
|
|
|
if (builder.isSimpleMode()) {
|
|
|
|
builder.command(field.getName());
|
|
|
|
}
|
2018-11-25 10:16:21 +00:00
|
|
|
if (builder.getPlugin() == null) {
|
|
|
|
builder.plugin(plugin);
|
|
|
|
}
|
2018-11-25 08:48:12 +00:00
|
|
|
builder.build();
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
2018-10-13 13:36:49 +00:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
});
|
2019-04-25 16:02:30 +00:00
|
|
|
// CooldownPack Inject
|
2019-08-17 16:38:45 +00:00
|
|
|
injectTypes.put(Cooldown.class, (plugin, field, args, pluginClass, instance) -> {
|
|
|
|
try {
|
2020-02-02 06:55:20 +00:00
|
|
|
Cooldowns.register((Cooldown) Ref.getField(instance, field), plugin);
|
2019-08-17 16:38:45 +00:00
|
|
|
} catch (Throwable t) {
|
|
|
|
t.printStackTrace();
|
|
|
|
}
|
2019-04-25 16:02:30 +00:00
|
|
|
});
|
2019-08-18 07:07:39 +00:00
|
|
|
// PluginExists Inject
|
|
|
|
injectTypes.put(Boolean.TYPE, (plugin, field, args, pluginClass, instance) -> {
|
|
|
|
try {
|
|
|
|
if (args.value().length > 0) {
|
2020-02-01 09:04:36 +00:00
|
|
|
Ref.putField(instance, field, Bukkit.getPluginManager().getPlugin(args.value()[0]) != null);
|
2019-08-18 07:07:39 +00:00
|
|
|
}
|
|
|
|
} catch (Throwable t) {
|
|
|
|
t.printStackTrace();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// PluginHook Inject
|
|
|
|
injectTypes.put(JavaPlugin.class, (plugin, field, args, pluginClass, instance) -> {
|
|
|
|
try {
|
|
|
|
if (args.value().length > 0) {
|
2020-02-01 09:04:36 +00:00
|
|
|
Ref.putField(instance, field, Bukkit.getPluginManager().getPlugin(args.value()[0]));
|
2019-08-18 07:07:39 +00:00
|
|
|
}
|
|
|
|
} catch (Throwable t) {
|
|
|
|
t.printStackTrace();
|
|
|
|
}
|
|
|
|
});
|
2018-10-08 11:44:36 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:57:19 +00:00
|
|
|
@Override
|
|
|
|
public int priority() {
|
|
|
|
return -999;
|
|
|
|
}
|
|
|
|
|
2018-10-08 11:44:36 +00:00
|
|
|
@Override
|
2018-11-25 08:48:12 +00:00
|
|
|
public void preLoad(Plugin plugin, Class<?> pluginClass) {
|
2018-10-08 11:44:36 +00:00
|
|
|
for (Field declaredField : pluginClass.getDeclaredFields()) {
|
|
|
|
TInject annotation = declaredField.getAnnotation(TInject.class);
|
2018-12-05 07:36:05 +00:00
|
|
|
if (annotation == null || !declaredField.getType().equals(plugin.getClass())) {
|
2018-10-08 11:44:36 +00:00
|
|
|
continue;
|
|
|
|
}
|
2019-10-25 17:16:08 +00:00
|
|
|
Ref.forcedAccess(declaredField);
|
|
|
|
TInjectHelper.getInstance(declaredField, pluginClass, plugin).forEach(instance -> inject(plugin, declaredField, instance, annotation, injectTypes.get(Plugin.class), pluginClass));
|
2018-11-25 08:48:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void postLoad(Plugin plugin, Class<?> pluginClass) {
|
|
|
|
for (Field declaredField : pluginClass.getDeclaredFields()) {
|
|
|
|
TInject annotation = declaredField.getAnnotation(TInject.class);
|
|
|
|
if (annotation == null || declaredField.getType().equals(plugin.getClass())) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-10-25 17:16:08 +00:00
|
|
|
Ref.forcedAccess(declaredField);
|
2018-10-08 11:44:36 +00:00
|
|
|
TInjectTask tInjectTask = injectTypes.get(declaredField.getType());
|
2018-12-05 07:36:05 +00:00
|
|
|
if (tInjectTask != null) {
|
2019-10-25 17:16:08 +00:00
|
|
|
TInjectHelper.getInstance(declaredField, pluginClass, plugin).forEach(instance -> inject(plugin, declaredField, instance, annotation, tInjectTask, pluginClass));
|
2018-10-08 11:44:36 +00:00
|
|
|
}
|
2018-12-05 07:36:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-17 16:38:45 +00:00
|
|
|
public void inject(Plugin plugin, Field field, Object instance, TInject annotation, TInjectTask injectTask, Class pluginClass) {
|
2018-12-05 07:36:05 +00:00
|
|
|
try {
|
2019-08-17 16:38:45 +00:00
|
|
|
injectTask.run(plugin, field, annotation, pluginClass, instance);
|
2019-07-05 16:17:54 +00:00
|
|
|
TabooLibAPI.debug(field.getName() + " injected. (" + field.getType().getName() + ")");
|
2018-12-05 07:36:05 +00:00
|
|
|
} catch (Throwable e) {
|
2019-08-17 17:13:30 +00:00
|
|
|
TLogger.getGlobalLogger().error(field.getName() + " inject failed: " + e.getMessage() + " (" + field.getName() + ")");
|
2018-12-05 07:36:05 +00:00
|
|
|
if (e.getMessage() == null) {
|
|
|
|
e.printStackTrace();
|
2018-10-08 11:44:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|