Update JDK 11

This commit is contained in:
sky 2020-02-02 14:55:20 +08:00
parent 7563559fda
commit 498854260d
10 changed files with 38 additions and 33 deletions

View File

@ -7,6 +7,7 @@ import io.izzel.taboolib.TabooLibAPI;
import io.izzel.taboolib.TabooLibLoader; import io.izzel.taboolib.TabooLibLoader;
import io.izzel.taboolib.client.packet.impl.PacketEmpty; import io.izzel.taboolib.client.packet.impl.PacketEmpty;
import io.izzel.taboolib.module.inject.TListener; import io.izzel.taboolib.module.inject.TListener;
import io.izzel.taboolib.util.Ref;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
@ -68,7 +69,7 @@ public class PacketSerializer implements Listener {
Arrays.stream(packet.getClass().getDeclaredFields()).filter(field -> field.isAnnotationPresent(PacketValue.class)).forEach(field -> { Arrays.stream(packet.getClass().getDeclaredFields()).filter(field -> field.isAnnotationPresent(PacketValue.class)).forEach(field -> {
field.setAccessible(true); field.setAccessible(true);
try { try {
Object obj = field.get(packet); Object obj = Ref.getField(packet, field);
if (obj instanceof Number) { if (obj instanceof Number) {
json.addProperty(field.getName(), (Number) obj); json.addProperty(field.getName(), (Number) obj);
} else if (obj instanceof Boolean) { } else if (obj instanceof Boolean) {

View File

@ -30,7 +30,7 @@ public class InternalPathfinderExecutor extends PathfinderExecutor {
try { try {
SimpleReflection.saveField(PathfinderGoalSelector.class); SimpleReflection.saveField(PathfinderGoalSelector.class);
SimpleReflection.saveField(ControllerJump.class); SimpleReflection.saveField(ControllerJump.class);
pathfinderGoalSelectorSet =SimpleReflection.getField(PathfinderGoalSelector.class, "b"); pathfinderGoalSelectorSet = SimpleReflection.getField(PathfinderGoalSelector.class, "b");
controllerJumpCurrent = SimpleReflection.getField(ControllerJump.class, "a"); controllerJumpCurrent = SimpleReflection.getField(ControllerJump.class, "a");
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@ -86,7 +86,7 @@ public class InternalPathfinderExecutor extends PathfinderExecutor {
@Override @Override
public Object getPathEntity(LivingEntity entity) { public Object getPathEntity(LivingEntity entity) {
try { try {
return pathEntity.get(getNavigation(entity)); return Ref.getField(getNavigation(entity), pathEntity);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -115,7 +115,7 @@ public class InternalPathfinderExecutor extends PathfinderExecutor {
@Override @Override
public void clearGoalAi(LivingEntity entity) { public void clearGoalAi(LivingEntity entity) {
try { try {
((Collection) pathfinderGoalSelectorSet.get(((EntityInsentient) getEntityInsentient(entity)).goalSelector)).clear(); ((Collection) Ref.getField(((EntityInsentient) getEntityInsentient(entity)).goalSelector, pathfinderGoalSelectorSet)).clear();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -124,7 +124,7 @@ public class InternalPathfinderExecutor extends PathfinderExecutor {
@Override @Override
public void clearTargetAi(LivingEntity entity) { public void clearTargetAi(LivingEntity entity) {
try { try {
((Collection) pathfinderGoalSelectorSet.get(((EntityInsentient) getEntityInsentient(entity)).targetSelector)).clear(); ((Collection) Ref.getField(((EntityInsentient) getEntityInsentient(entity)).targetSelector, pathfinderGoalSelectorSet)).clear();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -133,7 +133,7 @@ public class InternalPathfinderExecutor extends PathfinderExecutor {
@Override @Override
public Iterable getGoalAi(LivingEntity entity) { public Iterable getGoalAi(LivingEntity entity) {
try { try {
return ((Collection) pathfinderGoalSelectorSet.get(((EntityInsentient) getEntityInsentient(entity)).goalSelector)); return ((Collection) Ref.getField(((EntityInsentient) getEntityInsentient(entity)).goalSelector, pathfinderGoalSelectorSet));
} catch (Throwable t) { } catch (Throwable t) {
t.printStackTrace(); t.printStackTrace();
} }
@ -143,7 +143,7 @@ public class InternalPathfinderExecutor extends PathfinderExecutor {
@Override @Override
public Iterable getTargetAi(LivingEntity entity) { public Iterable getTargetAi(LivingEntity entity) {
try { try {
return ((Collection) pathfinderGoalSelectorSet.get(((EntityInsentient) getEntityInsentient(entity)).targetSelector)); return ((Collection) Ref.getField(((EntityInsentient) getEntityInsentient(entity)).targetSelector, pathfinderGoalSelectorSet));
} catch (Throwable t) { } catch (Throwable t) {
t.printStackTrace(); t.printStackTrace();
} }
@ -153,8 +153,7 @@ public class InternalPathfinderExecutor extends PathfinderExecutor {
@Override @Override
public void setGoalAi(LivingEntity entity, Iterable ai) { public void setGoalAi(LivingEntity entity, Iterable ai) {
try { try {
Ref.putField(((EntityInsentient) getEntityInsentient(entity)).goalSelector, Ref.putField(((EntityInsentient) getEntityInsentient(entity)).goalSelector, this.pathfinderGoalSelectorSet, ai);
this.pathfinderGoalSelectorSet, ai);
} catch (Throwable t) { } catch (Throwable t) {
t.printStackTrace(); t.printStackTrace();
} }
@ -164,7 +163,7 @@ public class InternalPathfinderExecutor extends PathfinderExecutor {
public void setTargetAi(LivingEntity entity, Iterable ai) { public void setTargetAi(LivingEntity entity, Iterable ai) {
try { try {
Ref.putField(((EntityInsentient) getEntityInsentient(entity)).targetSelector, Ref.putField(((EntityInsentient) getEntityInsentient(entity)).targetSelector,
this.pathfinderGoalSelectorSet, ai); this.pathfinderGoalSelectorSet, ai);
} catch (Throwable t) { } catch (Throwable t) {
t.printStackTrace(); t.printStackTrace();
} }

View File

@ -83,8 +83,7 @@ public abstract class BaseMainCommand implements CommandExecutor, TabExecutor {
fields.sort(Comparator.comparingDouble(commandField -> commandField.getField().getAnnotation(SubCommand.class).priority())); fields.sort(Comparator.comparingDouble(commandField -> commandField.getField().getAnnotation(SubCommand.class).priority()));
fields.forEach(commandField -> { fields.forEach(commandField -> {
try { try {
commandField.getField().setAccessible(true); BaseSubCommand subCommand = Ref.getField(commandField.getParent().newInstance(), commandField.getField(), BaseSubCommand.class);
BaseSubCommand subCommand = (BaseSubCommand) commandField.getField().get(commandField.getParent().newInstance());
subCommand.label(commandField.getField().getName()).annotation(commandField.getField().getAnnotation(SubCommand.class)); subCommand.label(commandField.getField().getName()).annotation(commandField.getField().getAnnotation(SubCommand.class));
baseMainCommand.registerSubCommand(subCommand); baseMainCommand.registerSubCommand(subCommand);
} catch (Throwable ignored) { } catch (Throwable ignored) {

View File

@ -2,6 +2,7 @@ package io.izzel.taboolib.module.inject;
import io.izzel.taboolib.TabooLibLoader; import io.izzel.taboolib.TabooLibLoader;
import io.izzel.taboolib.module.locale.logger.TLogger; import io.izzel.taboolib.module.locale.logger.TLogger;
import io.izzel.taboolib.util.Ref;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
@ -34,7 +35,7 @@ public class PlayerContainerLoader implements Listener, TabooLibLoader.Loader {
field.setAccessible(true); field.setAccessible(true);
for (Object instance : TInjectHelper.getInstance(field, pluginClass, plugin)) { for (Object instance : TInjectHelper.getInstance(field, pluginClass, plugin)) {
try { try {
pluginContainer.computeIfAbsent(plugin.getName(), name -> new ArrayList<>()).add(new Container(field.get(instance), annotation.uniqueId())); pluginContainer.computeIfAbsent(plugin.getName(), name -> new ArrayList<>()).add(new Container(Ref.getField(instance, field), annotation.uniqueId()));
} catch (Throwable t) { } catch (Throwable t) {
t.printStackTrace(); t.printStackTrace();
} }

View File

@ -98,7 +98,7 @@ public class TInjectLoader implements TabooLibLoader.Loader {
// SimpleCommandBuilder Inject // SimpleCommandBuilder Inject
injectTypes.put(CommandBuilder.class, (plugin, field, args, pluginClass, instance) -> { injectTypes.put(CommandBuilder.class, (plugin, field, args, pluginClass, instance) -> {
try { try {
CommandBuilder builder = (CommandBuilder) field.get(instance); CommandBuilder builder = Ref.getField(instance, field, CommandBuilder.class);
if (!builder.isBuild()) { if (!builder.isBuild()) {
if (builder.isSimpleMode()) { if (builder.isSimpleMode()) {
builder.command(field.getName()); builder.command(field.getName());
@ -115,7 +115,7 @@ public class TInjectLoader implements TabooLibLoader.Loader {
// CooldownPack Inject // CooldownPack Inject
injectTypes.put(Cooldown.class, (plugin, field, args, pluginClass, instance) -> { injectTypes.put(Cooldown.class, (plugin, field, args, pluginClass, instance) -> {
try { try {
Cooldowns.register((Cooldown) field.get(instance), plugin); Cooldowns.register((Cooldown) Ref.getField(instance, field), plugin);
} catch (Throwable t) { } catch (Throwable t) {
t.printStackTrace(); t.printStackTrace();
} }

View File

@ -6,6 +6,7 @@ import io.izzel.taboolib.Version;
import io.izzel.taboolib.module.lite.SimpleReflection; import io.izzel.taboolib.module.lite.SimpleReflection;
import io.izzel.taboolib.module.nms.nbt.*; import io.izzel.taboolib.module.nms.nbt.*;
import io.izzel.taboolib.module.packet.TPacketHandler; import io.izzel.taboolib.module.packet.TPacketHandler;
import io.izzel.taboolib.util.Ref;
import net.minecraft.server.v1_12_R1.ChatMessageType; import net.minecraft.server.v1_12_R1.ChatMessageType;
import net.minecraft.server.v1_12_R1.EntityVillager; import net.minecraft.server.v1_12_R1.EntityVillager;
import net.minecraft.server.v1_12_R1.MinecraftServer; import net.minecraft.server.v1_12_R1.MinecraftServer;
@ -130,7 +131,7 @@ public class NMSImpl extends NMS {
return "entity.minecraft." + ((net.minecraft.server.v1_14_R1.MinecraftKey) minecraftKey).getKey(); return "entity.minecraft." + ((net.minecraft.server.v1_14_R1.MinecraftKey) minecraftKey).getKey();
} else if (Version.isAfter(Version.v1_13)) { } else if (Version.isAfter(Version.v1_13)) {
try { try {
String name = "entity.minecraft." + IRegistry.ENTITY_TYPE.getKey((net.minecraft.server.v1_13_R2.EntityTypes<?>) entityTypesField.get(((org.bukkit.craftbukkit.v1_13_R2.entity.CraftEntity) entity).getHandle())).getKey(); String name = "entity.minecraft." + IRegistry.ENTITY_TYPE.getKey((net.minecraft.server.v1_13_R2.EntityTypes<?>) Ref.getField(((org.bukkit.craftbukkit.v1_13_R2.entity.CraftEntity) entity).getHandle(), entityTypesField)).getKey();
if (entity instanceof Villager && ((CraftVillager) entity).getCareer() != null) { if (entity instanceof Villager && ((CraftVillager) entity).getCareer() != null) {
name += "." + String.valueOf(((CraftVillager) entity).getCareer()).toLowerCase(); name += "." + String.valueOf(((CraftVillager) entity).getCareer()).toLowerCase();
} }

View File

@ -1,6 +1,5 @@
package io.izzel.taboolib.util; package io.izzel.taboolib.util;
import com.google.common.primitives.Primitives;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
import io.izzel.taboolib.TabooLib; import io.izzel.taboolib.TabooLib;
import io.izzel.taboolib.TabooLibAPI; import io.izzel.taboolib.TabooLibAPI;
@ -92,6 +91,11 @@ public class Ref {
} }
} }
public static <T> T getField(Object src, Field field, Class<T> cast) {
Object obj = getField(src, field);
return obj == null ? null : (T) obj;
}
public static Object getField(Object src, Field field) { public static Object getField(Object src, Field field) {
if (Modifier.isStatic(field.getModifiers())) { if (Modifier.isStatic(field.getModifiers())) {
Object base = getUnsafe().staticFieldBase(field); Object base = getUnsafe().staticFieldBase(field);
@ -165,7 +169,7 @@ public class Ref {
} catch (Exception | Error e) { } catch (Exception | Error e) {
try { try {
List<Field> list = Arrays.stream(clazz.getDeclaredFields()) List<Field> list = Arrays.stream(clazz.getDeclaredFields())
.filter(field -> (field.getModifiers() & excludeModifiers) == 0).collect(Collectors.toList()); .filter(field -> (field.getModifiers() & excludeModifiers) == 0).collect(Collectors.toList());
cachedFields.putIfAbsent(clazz.getName(), list); cachedFields.putIfAbsent(clazz.getName(), list);
return list; return list;
} catch (Error err) { } catch (Error err) {
@ -209,7 +213,7 @@ public class Ref {
} catch (Exception | Error e) { } catch (Exception | Error e) {
try { try {
List<Method> list = Arrays.stream(clazz.getDeclaredMethods()) List<Method> list = Arrays.stream(clazz.getDeclaredMethods())
.filter(field -> (field.getModifiers() & excludeModifiers) == 0).collect(Collectors.toList()); .filter(field -> (field.getModifiers() & excludeModifiers) == 0).collect(Collectors.toList());
cacheMethods.putIfAbsent(clazz.getName(), list); cacheMethods.putIfAbsent(clazz.getName(), list);
return list; return list;
} catch (Error err) { } catch (Error err) {
@ -257,10 +261,8 @@ public class Ref {
return cachePlugin.computeIfAbsent(callerClass.getName(), n -> { return cachePlugin.computeIfAbsent(callerClass.getName(), n -> {
try { try {
ClassLoader loader = callerClass.getClassLoader(); ClassLoader loader = callerClass.getClassLoader();
Field pluginF = loader.getClass().getDeclaredField("plugin"); Object instance = getField(loader, loader.getClass().getDeclaredField("plugin"));
pluginF.setAccessible(true); return (JavaPlugin) instance;
Object o = pluginF.get(loader);
return (JavaPlugin) o;
} catch (Exception e) { } catch (Exception e) {
return TabooLib.getPlugin(); return TabooLib.getPlugin();
} }

View File

@ -1,6 +1,7 @@
package io.izzel.taboolib.util.asm; package io.izzel.taboolib.util.asm;
import io.izzel.taboolib.util.Ref;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.objectweb.asm.*; import org.objectweb.asm.*;
@ -36,7 +37,7 @@ public class AsmClassTransformer extends ClassVisitor implements Opcodes {
Class<?> clazz = AsmClassLoader.createNewClass(newClassName, writer.toByteArray()); Class<?> clazz = AsmClassLoader.createNewClass(newClassName, writer.toByteArray());
Field field = from.getClassLoader().getClass().getDeclaredField("classes"); Field field = from.getClassLoader().getClass().getDeclaredField("classes");
field.setAccessible(true); field.setAccessible(true);
((Map<String, Class<?>>) field.get(from.getClassLoader())).put(newClassName, clazz); Ref.getField(from.getClassLoader(), field, Map.class).put(newClassName, clazz);
Constructor<?> constructor = clazz.getDeclaredConstructor(); Constructor<?> constructor = clazz.getDeclaredConstructor();
constructor.setAccessible(true); constructor.setAccessible(true);
return constructor.newInstance(); return constructor.newInstance();

View File

@ -2,6 +2,7 @@ package io.izzel.taboolib.util.plugin;
import com.google.common.base.Joiner; import com.google.common.base.Joiner;
import io.izzel.taboolib.TabooLib; import io.izzel.taboolib.TabooLib;
import io.izzel.taboolib.util.Ref;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;
@ -248,26 +249,26 @@ public class PluginUtils {
try { try {
Field pluginsField = Bukkit.getPluginManager().getClass().getDeclaredField("plugins"); Field pluginsField = Bukkit.getPluginManager().getClass().getDeclaredField("plugins");
pluginsField.setAccessible(true); pluginsField.setAccessible(true);
plugins = (List) pluginsField.get(pluginManager); plugins = (List) Ref.getField(pluginManager, pluginsField);
Field lookupNamesField = Bukkit.getPluginManager().getClass().getDeclaredField("lookupNames"); Field lookupNamesField = Bukkit.getPluginManager().getClass().getDeclaredField("lookupNames");
lookupNamesField.setAccessible(true); lookupNamesField.setAccessible(true);
names = (Map) lookupNamesField.get(pluginManager); names = (Map) Ref.getField(pluginManager, lookupNamesField);
Field commandMapField; Field commandMapField;
try { try {
commandMapField = Bukkit.getPluginManager().getClass().getDeclaredField("listeners"); commandMapField = Bukkit.getPluginManager().getClass().getDeclaredField("listeners");
commandMapField.setAccessible(true); commandMapField.setAccessible(true);
listeners = (Map) commandMapField.get(pluginManager); listeners = (Map) Ref.getField(pluginManager, commandMapField);
} catch (Exception ignored) { } catch (Exception ignored) {
} }
commandMapField = Bukkit.getPluginManager().getClass().getDeclaredField("commandMap"); commandMapField = Bukkit.getPluginManager().getClass().getDeclaredField("commandMap");
commandMapField.setAccessible(true); commandMapField.setAccessible(true);
commandMap = (SimpleCommandMap) commandMapField.get(pluginManager); commandMap = (SimpleCommandMap) Ref.getField(pluginManager, commandMapField);
Field knownCommandsField = SimpleCommandMap.class.getDeclaredField("knownCommands"); Field knownCommandsField = SimpleCommandMap.class.getDeclaredField("knownCommands");
knownCommandsField.setAccessible(true); knownCommandsField.setAccessible(true);
commands = (Map) knownCommandsField.get(commandMap); commands = (Map) Ref.getField(commandMap, knownCommandsField);
} catch (NoSuchFieldException | IllegalAccessException e) { } catch (NoSuchFieldException e) {
return new PluginUnloadState(true, e.toString()); return new PluginUnloadState(true, e.toString());
} }
} }

View File

@ -50,7 +50,7 @@ public class TSerializer {
if (serializer == null) { if (serializer == null) {
serializable.read(jsonElementEntry.getKey(), jsonElementEntry.getValue().getAsString()); serializable.read(jsonElementEntry.getKey(), jsonElementEntry.getValue().getAsString());
} else { } else {
readCollection((Collection) declaredField.get(serializable), jsonElementEntry.getValue().getAsString(), checkCustom(listType, serializer)); readCollection((Collection) Ref.getField(serializable, declaredField), jsonElementEntry.getValue().getAsString(), checkCustom(listType, serializer));
} }
} }
// Map // Map
@ -65,7 +65,7 @@ public class TSerializer {
if (serializerK == null || serializerV == null) { if (serializerK == null || serializerV == null) {
serializable.read(jsonElementEntry.getKey(), jsonElementEntry.getValue().getAsString()); serializable.read(jsonElementEntry.getKey(), jsonElementEntry.getValue().getAsString());
} else { } else {
readMap((Map) declaredField.get(serializable), jsonElementEntry.getValue().getAsString(), checkCustom(mapType[0], serializerK), checkCustom(mapType[1], serializerV)); readMap((Map) Ref.getField(serializable, declaredField), jsonElementEntry.getValue().getAsString(), checkCustom(mapType[0], serializerK), checkCustom(mapType[1], serializerV));
} }
} }
// 未声明类型 // 未声明类型
@ -95,7 +95,7 @@ public class TSerializer {
declaredField.setAccessible(true); declaredField.setAccessible(true);
try { try {
if (!declaredField.isAnnotationPresent(DoNotSerialize.class) && !Modifier.isStatic(declaredField.getModifiers())) { if (!declaredField.isAnnotationPresent(DoNotSerialize.class) && !Modifier.isStatic(declaredField.getModifiers())) {
Object fieldObject = declaredField.get(serializable); Object fieldObject = Ref.getField(serializable, declaredField);
if (fieldObject == null) { if (fieldObject == null) {
continue; continue;
} }