diff --git a/pom.xml b/pom.xml index 575ac05..086f32a 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 net.sourcewriters.minecraft vcompat-parent - 3.0.1 + 3.0.2 pom diff --git a/vcompat-1_17_R1/pom.xml b/vcompat-1_17_R1/pom.xml index 2c79d40..55ad03c 100644 --- a/vcompat-1_17_R1/pom.xml +++ b/vcompat-1_17_R1/pom.xml @@ -5,7 +5,7 @@ net.sourcewriters.minecraft vcompat-parent - 3.0.1 + 3.0.2 vcompat-1_17_R1 diff --git a/vcompat-1_17_R1/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_17_R1/TextureProvider1_17_R1.java b/vcompat-1_17_R1/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_17_R1/TextureProvider1_17_R1.java index 027c11b..fcd72c8 100644 --- a/vcompat-1_17_R1/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_17_R1/TextureProvider1_17_R1.java +++ b/vcompat-1_17_R1/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_17_R1/TextureProvider1_17_R1.java @@ -2,6 +2,7 @@ import org.bukkit.Material; import org.bukkit.block.Block; +import org.bukkit.block.BlockState; import org.bukkit.craftbukkit.v1_17_R1.block.CraftBlockEntityState; import org.bukkit.craftbukkit.v1_17_R1.block.CraftSkull; import org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack; @@ -15,25 +16,32 @@ import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.block.entity.SkullBlockEntity; import net.sourcewriters.minecraft.vcompat.provider.TextureProvider; +import net.sourcewriters.minecraft.vcompat.provider.lookup.ClassLookupProvider; import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup; public class TextureProvider1_17_R1 extends TextureProvider { - private final ClassLookup craftEntityStateRef = ClassLookup.of(CraftBlockEntityState.class).searchField("tileEntity", "tileEntity"); - private final ClassLookup craftItemStackRef = ClassLookup.of(CraftItemStack.class).searchField("handle", "handle"); - private final ClassLookup craftMetaSkullRef = ClassLookup.of("org.bukkit.craftbukkit.v1_17_R1.inventory.CraftMetaSkull") - .searchField("serialized", "serializedProfile").searchField("profile", "profile"); + private final ClassLookup craftEntityStateRef; + private final ClassLookup craftItemStackRef; + private final ClassLookup craftMetaSkullRef; protected TextureProvider1_17_R1(VersionControl1_17_R1 versionControl) { super(versionControl); + ClassLookupProvider provider = versionControl.getLookupProvider(); + craftEntityStateRef = provider.createLookup("CraftBlockEntityState", CraftBlockEntityState.class).searchField("tileEntity", + "tileEntity"); + craftItemStackRef = provider.createLookup("CraftItemStack", CraftItemStack.class).searchField("handle", "handle"); + craftMetaSkullRef = provider.createCBLookup("CraftMetaSkull", "inventory.CraftMetaSkull") + .searchField("serialized", "serializedProfile").searchField("profile", "profile"); } @Override public GameProfile profileFromBlock(Block block) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return null; } - SkullBlockEntity entitySkull = (SkullBlockEntity) craftEntityStateRef.getFieldValue(block, "tileEntity"); + SkullBlockEntity entitySkull = (SkullBlockEntity) craftEntityStateRef.getFieldValue(state, "tileEntity"); return entitySkull.owner; } @@ -90,10 +98,11 @@ public boolean applyItem(org.bukkit.inventory.ItemStack itemStack, GameProfile p @Override public boolean applyBlock(Block block, GameProfile profile) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return false; } - SkullBlockEntity entitySkull = (SkullBlockEntity) craftEntityStateRef.getFieldValue(block, "tileEntity"); + SkullBlockEntity entitySkull = (SkullBlockEntity) craftEntityStateRef.getFieldValue(state, "tileEntity"); entitySkull.setOwner(profile); return true; } diff --git a/vcompat-1_17_R1/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_17_R1/data/hook/BukkitContainerAdapterHook1_17_R1.java b/vcompat-1_17_R1/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_17_R1/data/hook/BukkitContainerAdapterHook1_17_R1.java index 28d86a3..68bcffa 100644 --- a/vcompat-1_17_R1/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_17_R1/data/hook/BukkitContainerAdapterHook1_17_R1.java +++ b/vcompat-1_17_R1/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_17_R1/data/hook/BukkitContainerAdapterHook1_17_R1.java @@ -18,6 +18,7 @@ import net.sourcewriters.minecraft.vcompat.provider.data.WrappedContainer; import net.sourcewriters.minecraft.vcompat.provider.impl.v1_17_R1.data.BukkitContainer1_17_R1; import net.sourcewriters.minecraft.vcompat.provider.impl.v1_17_R1.data.SyntaxContainer1_17_R1; +import net.sourcewriters.minecraft.vcompat.provider.lookup.ClassLookupProvider; import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup; import net.sourcewriters.minecraft.vcompat.shaded.syntaxapi.nbt.NbtCompound; @@ -29,13 +30,16 @@ public final class BukkitContainerAdapterHook1_17_R1 { private static final BukkitContainerAdapterHook1_17_R1 HOOK = new BukkitContainerAdapterHook1_17_R1(); - private final ClassLookup registryRef = ClassLookup.of(CraftPersistentDataTypeRegistry.class) - .searchMethod("create", "createAdapter", Class.class, Class.class, Function.class, Function.class) - .searchField("adapters", "adapters") - .searchField("function", "CREATE_ADAPTER"); - private final ClassLookup entityRef = ClassLookup.of(CraftEntity.class).searchField("registry", "DATA_TYPE_REGISTRY"); + private final ClassLookup registryRef; + private final ClassLookup entityRef; - private BukkitContainerAdapterHook1_17_R1() {} + private BukkitContainerAdapterHook1_17_R1() { + ClassLookupProvider provider = VersionCompatProvider.get().getLookupProvider(); + registryRef = provider.createLookup("CraftPersistentDataTypeRegistry", CraftPersistentDataTypeRegistry.class) + .searchMethod("create", "createAdapter", Class.class, Class.class, Function.class, Function.class) + .searchField("adapters", "adapters").searchField("function", "CREATE_ADAPTER"); + entityRef = provider.createLookup("CraftEntity", CraftEntity.class).searchField("registry", "DATA_TYPE_REGISTRY"); + } private final HashMap map = new HashMap<>(); @@ -58,7 +62,7 @@ private void inject(CraftPersistentDataTypeRegistry registry) { return; } map.put(registry, (Function) registryRef.getFieldValue(registry, "function")); - Function function = clazz -> createAdapter(registry, registryRef.getMethod("create").type().returnType(), (Class) clazz); + Function function = clazz -> createAdapter(registry, registryRef.getMethod("create").getReturnType(), (Class) clazz); registryRef.setFieldValue(registry, "function", function); } diff --git a/vcompat-1_17_R1/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_17_R1/tools/BlockTools1_17_R1.java b/vcompat-1_17_R1/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_17_R1/tools/BlockTools1_17_R1.java index 8beb61e..7b36432 100644 --- a/vcompat-1_17_R1/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_17_R1/tools/BlockTools1_17_R1.java +++ b/vcompat-1_17_R1/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_17_R1/tools/BlockTools1_17_R1.java @@ -1,26 +1,34 @@ package net.sourcewriters.minecraft.vcompat.provider.impl.v1_17_R1.tools; import org.bukkit.block.Block; +import org.bukkit.block.BlockState; import org.bukkit.craftbukkit.v1_17_R1.block.CraftSkull; import com.mojang.authlib.properties.Property; import com.mojang.authlib.properties.PropertyMap; import net.minecraft.world.level.block.entity.SkullBlockEntity; +import net.sourcewriters.minecraft.vcompat.VersionCompatProvider; import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup; import net.sourcewriters.minecraft.vcompat.provider.tools.BlockTools; import net.sourcewriters.minecraft.vcompat.util.constants.MinecraftConstants; public class BlockTools1_17_R1 extends BlockTools { - private final ClassLookup craftEntityStateRef = ClassLookup.of(CraftSkull.class).searchField("tileEntity", "tileEntity"); + private final ClassLookup craftEntityStateRef; + + public BlockTools1_17_R1() { + craftEntityStateRef = VersionCompatProvider.get().getLookupProvider().createLookup("CraftSkull", CraftSkull.class) + .searchField("tileEntity", "tileEntity"); + } @Override public void setHeadTexture(Block block, String texture) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return; } - SkullBlockEntity entitySkull = (SkullBlockEntity) craftEntityStateRef.getFieldValue(block, "tileEntity"); + SkullBlockEntity entitySkull = (SkullBlockEntity) craftEntityStateRef.getFieldValue(state, "tileEntity"); PropertyMap map = entitySkull.owner.getProperties(); map.removeAll("textures"); map.put("textures", new Property("textures", MinecraftConstants.TEXTURE_SIGNATURE, texture)); @@ -28,10 +36,11 @@ public void setHeadTexture(Block block, String texture) { @Override public String getHeadTexture(Block block) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return null; } - SkullBlockEntity entitySkull = (SkullBlockEntity) craftEntityStateRef.getFieldValue(block, "tileEntity"); + SkullBlockEntity entitySkull = (SkullBlockEntity) craftEntityStateRef.getFieldValue(state, "tileEntity"); return entitySkull.owner.getProperties().get("textures").iterator().next().getValue(); } diff --git a/vcompat-1_18_R1/pom.xml b/vcompat-1_18_R1/pom.xml index df7dc73..41efb16 100644 --- a/vcompat-1_18_R1/pom.xml +++ b/vcompat-1_18_R1/pom.xml @@ -5,7 +5,7 @@ net.sourcewriters.minecraft vcompat-parent - 3.0.1 + 3.0.2 vcompat-1_18_R1 diff --git a/vcompat-1_18_R1/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_18_R1/TextureProvider1_18_R1.java b/vcompat-1_18_R1/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_18_R1/TextureProvider1_18_R1.java index c58a0fa..ce8570d 100644 --- a/vcompat-1_18_R1/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_18_R1/TextureProvider1_18_R1.java +++ b/vcompat-1_18_R1/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_18_R1/TextureProvider1_18_R1.java @@ -2,6 +2,7 @@ import org.bukkit.Material; import org.bukkit.block.Block; +import org.bukkit.block.BlockState; import org.bukkit.craftbukkit.v1_18_R1.block.CraftBlockEntityState; import org.bukkit.craftbukkit.v1_18_R1.block.CraftSkull; import org.bukkit.craftbukkit.v1_18_R1.inventory.CraftItemStack; @@ -15,25 +16,33 @@ import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.block.entity.SkullBlockEntity; import net.sourcewriters.minecraft.vcompat.provider.TextureProvider; +import net.sourcewriters.minecraft.vcompat.provider.lookup.ClassLookupProvider; import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup; public class TextureProvider1_18_R1 extends TextureProvider { - private final ClassLookup craftEntityStateRef = ClassLookup.of(CraftBlockEntityState.class).searchField("tileEntity", "tileEntity"); - private final ClassLookup craftItemStackRef = ClassLookup.of(CraftItemStack.class).searchField("handle", "handle"); - private final ClassLookup craftMetaSkullRef = ClassLookup.of("org.bukkit.craftbukkit.v1_18_R1.inventory.CraftMetaSkull") - .searchField("serialized", "serializedProfile").searchField("profile", "profile"); + private final ClassLookup craftEntityStateRef; + private final ClassLookup craftItemStackRef; + private final ClassLookup craftMetaSkullRef; protected TextureProvider1_18_R1(VersionControl1_18_R1 versionControl) { super(versionControl); + ClassLookupProvider provider = versionControl.getLookupProvider(); + craftEntityStateRef = provider.createLookup("CraftBlockEntityState", CraftBlockEntityState.class).searchField("tileEntity", + "tileEntity"); + craftItemStackRef = provider.createLookup("CraftItemStack", CraftItemStack.class).searchField("handle", "handle"); + craftMetaSkullRef = provider.createCBLookup("CraftMetaSkull", "inventory.CraftMetaSkull") + .searchField("serialized", "serializedProfile").searchField("profile", "profile"); + } @Override public GameProfile profileFromBlock(Block block) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return null; } - SkullBlockEntity entitySkull = (SkullBlockEntity) craftEntityStateRef.getFieldValue(block, "tileEntity"); + SkullBlockEntity entitySkull = (SkullBlockEntity) craftEntityStateRef.getFieldValue(state, "tileEntity"); return entitySkull.owner; } @@ -90,10 +99,11 @@ public boolean applyItem(org.bukkit.inventory.ItemStack itemStack, GameProfile p @Override public boolean applyBlock(Block block, GameProfile profile) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return false; } - SkullBlockEntity entitySkull = (SkullBlockEntity) craftEntityStateRef.getFieldValue(block, "tileEntity"); + SkullBlockEntity entitySkull = (SkullBlockEntity) craftEntityStateRef.getFieldValue(state, "tileEntity"); entitySkull.setOwner(profile); return true; } diff --git a/vcompat-1_18_R1/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_18_R1/data/hook/BukkitContainerAdapterHook1_18_R1.java b/vcompat-1_18_R1/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_18_R1/data/hook/BukkitContainerAdapterHook1_18_R1.java index acb914c..b58e624 100644 --- a/vcompat-1_18_R1/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_18_R1/data/hook/BukkitContainerAdapterHook1_18_R1.java +++ b/vcompat-1_18_R1/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_18_R1/data/hook/BukkitContainerAdapterHook1_18_R1.java @@ -18,6 +18,7 @@ import net.sourcewriters.minecraft.vcompat.provider.data.WrappedContainer; import net.sourcewriters.minecraft.vcompat.provider.impl.v1_18_R1.data.BukkitContainer1_18_R1; import net.sourcewriters.minecraft.vcompat.provider.impl.v1_18_R1.data.SyntaxContainer1_18_R1; +import net.sourcewriters.minecraft.vcompat.provider.lookup.ClassLookupProvider; import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup; import net.sourcewriters.minecraft.vcompat.shaded.syntaxapi.nbt.NbtCompound; @@ -29,13 +30,16 @@ public final class BukkitContainerAdapterHook1_18_R1 { private static final BukkitContainerAdapterHook1_18_R1 HOOK = new BukkitContainerAdapterHook1_18_R1(); - private final ClassLookup registryRef = ClassLookup.of(CraftPersistentDataTypeRegistry.class) - .searchMethod("create", "createAdapter", Class.class, Class.class, Function.class, Function.class) - .searchField("adapters", "adapters") - .searchField("function", "CREATE_ADAPTER"); - private final ClassLookup entityRef = ClassLookup.of(CraftEntity.class).searchField("registry", "DATA_TYPE_REGISTRY"); + private final ClassLookup registryRef; + private final ClassLookup entityRef; - private BukkitContainerAdapterHook1_18_R1() {} + private BukkitContainerAdapterHook1_18_R1() { + ClassLookupProvider provider = VersionCompatProvider.get().getLookupProvider(); + registryRef = provider.createLookup("CraftPersistentDataTypeRegistry", CraftPersistentDataTypeRegistry.class) + .searchMethod("create", "createAdapter", Class.class, Class.class, Function.class, Function.class) + .searchField("adapters", "adapters").searchField("function", "CREATE_ADAPTER"); + entityRef = provider.createLookup("CraftEntity", CraftEntity.class).searchField("registry", "DATA_TYPE_REGISTRY"); + } private final HashMap map = new HashMap<>(); @@ -58,7 +62,7 @@ private void inject(CraftPersistentDataTypeRegistry registry) { return; } map.put(registry, (Function) registryRef.getFieldValue(registry, "function")); - Function function = clazz -> createAdapter(registry, registryRef.getMethod("create").type().returnType(), (Class) clazz); + Function function = clazz -> createAdapter(registry, registryRef.getMethod("create").getReturnType(), (Class) clazz); registryRef.setFieldValue(registry, "function", function); } @@ -73,8 +77,7 @@ private E createAdapter(CraftPersistentDataTypeRegistry registry, Class a } private Object buildAdapter(Object handle, Class type, Function function) { - return registryRef.run(handle, "create", type, CompoundTag.class, (Function) input -> toPrimitive(input), - function); + return registryRef.run(handle, "create", type, CompoundTag.class, (Function) input -> toPrimitive(input), function); } private CompoundTag toPrimitive(WrappedContainer input) { @@ -88,7 +91,8 @@ private CompoundTag toPrimitive(WrappedContainer input) { } if (handle instanceof IDataContainer) { if (handle instanceof NbtContainer) { - return (CompoundTag) VersionCompatProvider.get().getControl().getBukkitConversion().toMinecraftCompound(((NbtContainer) handle).asNbt()); + return (CompoundTag) VersionCompatProvider.get().getControl().getBukkitConversion() + .toMinecraftCompound(((NbtContainer) handle).asNbt()); } throw new IllegalArgumentException( "Expected 'CraftPersistentDataContainer' got '" + handle.getClass().getSimpleName() + " instead'!"); diff --git a/vcompat-1_18_R1/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_18_R1/tools/BlockTools1_18_R1.java b/vcompat-1_18_R1/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_18_R1/tools/BlockTools1_18_R1.java index 7f81b89..16c6f89 100644 --- a/vcompat-1_18_R1/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_18_R1/tools/BlockTools1_18_R1.java +++ b/vcompat-1_18_R1/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_18_R1/tools/BlockTools1_18_R1.java @@ -1,26 +1,34 @@ package net.sourcewriters.minecraft.vcompat.provider.impl.v1_18_R1.tools; import org.bukkit.block.Block; +import org.bukkit.block.BlockState; import org.bukkit.craftbukkit.v1_18_R1.block.CraftSkull; import com.mojang.authlib.properties.Property; import com.mojang.authlib.properties.PropertyMap; import net.minecraft.world.level.block.entity.SkullBlockEntity; +import net.sourcewriters.minecraft.vcompat.VersionCompatProvider; import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup; import net.sourcewriters.minecraft.vcompat.provider.tools.BlockTools; import net.sourcewriters.minecraft.vcompat.util.constants.MinecraftConstants; public class BlockTools1_18_R1 extends BlockTools { - private final ClassLookup craftEntityStateRef = ClassLookup.of(CraftSkull.class).searchField("tileEntity", "tileEntity"); + private final ClassLookup craftEntityStateRef; + + public BlockTools1_18_R1() { + craftEntityStateRef = VersionCompatProvider.get().getLookupProvider().createLookup("CraftSkull", CraftSkull.class) + .searchField("tileEntity", "tileEntity"); + } @Override public void setHeadTexture(Block block, String texture) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return; } - SkullBlockEntity entitySkull = (SkullBlockEntity) craftEntityStateRef.getFieldValue(block, "tileEntity"); + SkullBlockEntity entitySkull = (SkullBlockEntity) craftEntityStateRef.getFieldValue(state, "tileEntity"); PropertyMap map = entitySkull.owner.getProperties(); map.removeAll("textures"); map.put("textures", new Property("textures", MinecraftConstants.TEXTURE_SIGNATURE, texture)); @@ -28,10 +36,11 @@ public void setHeadTexture(Block block, String texture) { @Override public String getHeadTexture(Block block) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return null; } - SkullBlockEntity entitySkull = (SkullBlockEntity) craftEntityStateRef.getFieldValue(block, "tileEntity"); + SkullBlockEntity entitySkull = (SkullBlockEntity) craftEntityStateRef.getFieldValue(state, "tileEntity"); return entitySkull.owner.getProperties().get("textures").iterator().next().getValue(); } diff --git a/vcompat-api/pom.xml b/vcompat-api/pom.xml index 5a7724d..92ac765 100644 --- a/vcompat-api/pom.xml +++ b/vcompat-api/pom.xml @@ -3,7 +3,7 @@ net.sourcewriters.minecraft vcompat-parent - 3.0.1 + 3.0.2 vcompat-api diff --git a/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/provider/VersionControl.java b/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/provider/VersionControl.java index 7a95c6c..339363c 100644 --- a/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/provider/VersionControl.java +++ b/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/provider/VersionControl.java @@ -1,12 +1,20 @@ package net.sourcewriters.minecraft.vcompat.provider; +import net.sourcewriters.minecraft.vcompat.VersionCompatProvider; +import net.sourcewriters.minecraft.vcompat.provider.lookup.ClassLookupProvider; + public abstract class VersionControl { protected final DataProvider dataProvider = new DataProvider(this); + protected final ClassLookupProvider lookupProvider = VersionCompatProvider.get().getLookupProvider(); public DataProvider getDataProvider() { return dataProvider; } + + public final ClassLookupProvider getLookupProvider() { + return lookupProvider; + } public abstract ToolProvider getToolProvider(); diff --git a/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/provider/lookup/JavaAccessor.java b/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/provider/lookup/JavaAccessor.java new file mode 100644 index 0000000..ba14abb --- /dev/null +++ b/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/provider/lookup/JavaAccessor.java @@ -0,0 +1,412 @@ +package net.sourcewriters.minecraft.vcompat.provider.lookup; + +import java.lang.annotation.Annotation; +import java.lang.invoke.MethodHandles.Lookup; +import java.lang.reflect.AnnotatedElement; +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.Optional; + +import sun.misc.Unsafe; + +public final class JavaAccessor { + + private static final JavaAccessor INSTANCE = new JavaAccessor(); + + private final AccessUnsuccessful unsuccessful = new AccessUnsuccessful(); + + private Unsafe unsafe; + private Lookup lookup; + + private JavaAccessor() { + Optional> option = JavaTracker.getCallerClass(); + if (option.isEmpty() || option.get() != JavaAccessor.class) { + throw new UnsupportedOperationException("Utility class"); + } + } + + public Unsafe unsafe() { + if (unsafe != null) { + return unsafe; + } + try { + Field field = Unsafe.class.getDeclaredField("theUnsafe"); + field.setAccessible(true); + return unsafe = (Unsafe) field.get(null); + } catch (Exception exp) { + return null; + } + } + + public Lookup lookup() { + if (lookup != null) { + return lookup; + } + return lookup = (Lookup) getStaticValueUnsafe(getField(Lookup.class, "IMPL_LOOKUP")); + } + + /* + * Object creation + */ + + public Object initialize(Constructor constructor, Object... arguments) { + if (constructor == null || (constructor.getParameterCount() != arguments.length)) { + return null; + } + try { + if (arguments.length == 0) { + return lookup.unreflectConstructor(constructor).invoke(); + } + return lookup().unreflectConstructor(constructor).invokeWithArguments(arguments); + } catch (Throwable e) { + return null; + } + } + + /* + * Method invokation + */ + + public Object execute(Object instance, Method method, Object... arguments) { + if (method == null || (method.getParameterCount() != arguments.length)) { + return null; + } + try { + if (!Modifier.isStatic(method.getModifiers())) { + if (instance == null) { + return null; + } + if (arguments.length == 0) { + return lookup().unreflect(method).invokeWithArguments(instance); + } + Object[] input = new Object[arguments.length + 1]; + input[0] = instance; + System.arraycopy(arguments, 0, input, 1, arguments.length); + return lookup().unreflect(method).invokeWithArguments(input); + } + return lookup().unreflect(method).invokeWithArguments(arguments); + } catch (Throwable e) { + return null; + } + } + + /* + * Safe Accessors + */ + + public Object getObjectValueSafe(Object instance, Field field) { + if (instance == null || field == null) { + return null; + } + try { + return lookup().unreflectGetter(field).invoke(instance); + } catch (Throwable e) { + throw unsuccessful; + } + } + + public Object getStaticValueSafe(Field field) { + if (field == null) { + return null; + } + try { + return lookup().unreflectGetter(field).invoke(); + } catch (Throwable e) { + throw unsuccessful; + } + } + + public void setObjectValueSafe(Object instance, Field field, Object value) { + if (instance == null || field == null || (value != null && !field.getType().isAssignableFrom(value.getClass()))) { + return; + } + unfinalize(field); + try { + lookup().unreflectSetter(field).invokeWithArguments(instance, value); + } catch (Throwable e) { + throw unsuccessful; + } + } + + public void setStaticValueSafe(Field field, Object value) { + if (field == null || (value != null && !field.getType().isAssignableFrom(value.getClass()))) { + return; + } + unfinalize(field); + try { + lookup().unreflectSetter(field).invokeWithArguments(value); + } catch (Throwable e) { + throw unsuccessful; + } + } + + /* + * Unsafe Accessors + */ + + public Object getObjectValueUnsafe(Object instance, Field field) { + if (instance == null || field == null) { + return null; + } + Unsafe unsafe = unsafe(); + return unsafe.getObjectVolatile(instance, unsafe.objectFieldOffset(field)); + } + + public Object getStaticValueUnsafe(Field field) { + if (field == null) { + return null; + } + Unsafe unsafe = unsafe(); + return unsafe.getObjectVolatile(unsafe.staticFieldBase(field), unsafe.staticFieldOffset(field)); + } + + public void setObjectValueUnsafe(Object instance, Field field, Object value) { + if (instance == null || field == null) { + return; + } + unfinalize(field); + Unsafe unsafe = unsafe(); + if (value == null) { + unsafe.putObject(instance, unsafe.objectFieldOffset(field), null); + return; + } + if (field.getType().isAssignableFrom(value.getClass())) { + unsafe.putObject(instance, unsafe.objectFieldOffset(field), field.getType().cast(value)); + } + } + + public void setStaticValueUnsafe(Field field, Object value) { + if (field == null) { + return; + } + unfinalize(field); + Unsafe unsafe = unsafe(); + if (value == null) { + unsafe.putObject(unsafe.staticFieldBase(field), unsafe.staticFieldOffset(field), null); + return; + } + if (field.getType().isAssignableFrom(value.getClass())) { + unsafe.putObject(unsafe.staticFieldBase(field), unsafe.staticFieldOffset(field), field.getType().cast(value)); + } + } + + /* + * Internal Utilities + */ + + private void unfinalize(Field field) { + if (!Modifier.isFinal(field.getModifiers())) { + return; + } + try { + lookup().findSetter(Field.class, "modifiers", int.class).invokeExact(field, field.getModifiers() & ~Modifier.FINAL); + } catch (Throwable e) { + // Ignore + } + } + + /* + * Static Accessors + */ + + // Initialisation + + public static Object init(Constructor constructor, Object... arguments) { + return INSTANCE.initialize(constructor, arguments); + } + + // Invokation + + public static Object invokeStatic(Method method, Object... arguments) { + return INSTANCE.execute(null, method, arguments); + } + + public static Object invoke(Object instance, Method method, Object... arguments) { + return INSTANCE.execute(instance, method, arguments); + } + + // Setter + + public static void setValue(Object instance, Class clazz, String fieldName, Object value) { + setValue(instance, getField(clazz, fieldName), value); + } + + public static void setObjectValue(Object instance, Class clazz, String fieldName, Object value) { + setObjectValue(instance, getField(clazz, fieldName), value); + } + + public static void setStaticValue(Class clazz, String fieldName, Object value) { + setStaticValue(getField(clazz, fieldName), value); + } + + public static void setValue(Object instance, Field field, Object value) { + if (field == null) { + return; + } + if (Modifier.isStatic(field.getModifiers())) { + setStaticValue(field, value); + return; + } + setObjectValue(instance, field, value); + } + + public static void setObjectValue(Object instance, Field field, Object value) { + if (instance == null || field == null) { + return; + } + try { + INSTANCE.setObjectValueSafe(instance, field, value); + } catch (AccessUnsuccessful unsafe) { + INSTANCE.setObjectValueUnsafe(instance, field, value); + } + } + + public static void setStaticValue(Field field, Object value) { + if (field == null) { + return; + } + try { + INSTANCE.setStaticValueSafe(field, value); + } catch (AccessUnsuccessful unsafe) { + INSTANCE.setStaticValueUnsafe(field, value); + } + } + + // Getter + + public static Object getValue(Object instance, Class clazz, String fieldName) { + return getValue(instance, getField(clazz, fieldName)); + } + + public static Object getObjectValue(Object instance, Class clazz, String fieldName) { + return getObjectValue(instance, getField(clazz, fieldName)); + } + + public static Object getStaticValue(Class clazz, String fieldName) { + return getStaticValue(getField(clazz, fieldName)); + } + + public static Object getValue(Object instance, Field field) { + if (field == null) { + return null; + } + if (Modifier.isStatic(field.getModifiers())) { + return getStaticValue(field); + } + return getObjectValue(instance, field); + } + + public static Object getObjectValue(Object instance, Field field) { + if (instance == null || field == null) { + return null; + } + try { + return INSTANCE.getObjectValueSafe(instance, field); + } catch (AccessUnsuccessful unsafe) { + return INSTANCE.getObjectValueUnsafe(instance, field); + } + } + + public static Object getStaticValue(Field field) { + if (field == null) { + return null; + } + try { + return INSTANCE.getStaticValueSafe(field); + } catch (AccessUnsuccessful unsafe) { + return INSTANCE.getStaticValueUnsafe(field); + } + } + + /* + * Static Utilities + */ + + public static Field getField(Class clazz, String field) { + if (clazz == null || field == null) { + return null; + } + try { + return clazz.getDeclaredField(field); + } catch (NoSuchFieldException ignore) { + try { + return clazz.getField(field); + } catch (NoSuchFieldException ignore0) { + return null; + } + } + } + + public static Method getMethod(Class clazz, String method, Class... arguments) { + if (clazz == null || method == null) { + return null; + } + try { + return clazz.getDeclaredMethod(method, arguments); + } catch (NoSuchMethodException ignore) { + try { + return clazz.getMethod(method, arguments); + } catch (NoSuchMethodException ignore0) { + return null; + } + } + } + + public static Constructor getConstructor(Class clazz, Class... arguments) { + if (clazz == null) { + return null; + } + try { + return clazz.getDeclaredConstructor(arguments); + } catch (NoSuchMethodException ignore) { + try { + return clazz.getConstructor(arguments); + } catch (NoSuchMethodException ignore0) { + return null; + } + } + } + + public static Class getClass(Class clazz, String name) { + if (clazz == null || name == null) { + return null; + } + int size = clazz.getClasses().length + clazz.getDeclaredClasses().length; + if (size == 0) { + return null; + } + Class[] classes = new Class[size]; + Class[] tmp = clazz.getClasses(); + System.arraycopy(tmp, 0, classes, 0, tmp.length); + System.arraycopy(clazz.getDeclaredClasses(), tmp.length, classes, tmp.length, size - tmp.length); + for (int i = 0; i < size; i++) { + String target = classes[i].getSimpleName(); + if (target.contains(".")) { + target = target.split(".", 2)[0]; + } + if (target.equals(name)) { + return classes[i]; + } + } + return null; + } + + public static A getAnnotation(AnnotatedElement element, Class annotationType) { + A annotation = element.getAnnotation(annotationType); + if (annotation != null) { + return annotation; + } + return element.getDeclaredAnnotation(annotationType); + } + + /* + * Internal Exceptions + */ + + private static final class AccessUnsuccessful extends RuntimeException { + private static final long serialVersionUID = 1L; + } + +} diff --git a/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/provider/lookup/JavaTracker.java b/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/provider/lookup/JavaTracker.java new file mode 100644 index 0000000..f889f7d --- /dev/null +++ b/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/provider/lookup/JavaTracker.java @@ -0,0 +1,26 @@ +package net.sourcewriters.minecraft.vcompat.provider.lookup; + +import java.util.Optional; + +import com.syntaxphoenix.syntaxapi.reflection.ClassCache; + +public final class JavaTracker { + + private JavaTracker() { + throw new UnsupportedOperationException("Utility class"); + } + + public static Optional> getClassFromStack(int offset) { + StackTraceElement element = getStack()[3 + offset]; + return element == null ? Optional.empty() : ClassCache.getOptionalClass(element.getClassName()); + } + + public static Optional> getCallerClass() { + return getClassFromStack(1); + } + + private static StackTraceElement[] getStack() { + return new Throwable().getStackTrace(); + } + +} \ No newline at end of file diff --git a/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/provider/lookup/handle/ClassLookup.java b/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/provider/lookup/handle/ClassLookup.java index c5b2507..ade3785 100644 --- a/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/provider/lookup/handle/ClassLookup.java +++ b/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/provider/lookup/handle/ClassLookup.java @@ -1,36 +1,24 @@ package net.sourcewriters.minecraft.vcompat.provider.lookup.handle; -import java.lang.invoke.MethodHandle; -import java.lang.invoke.MethodHandles; -import java.lang.invoke.MethodHandles.Lookup; -import java.lang.invoke.VarHandle; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; -import java.lang.reflect.Modifier; import java.util.Collection; import java.util.HashMap; import java.util.function.Predicate; import com.syntaxphoenix.syntaxapi.utils.java.Arrays; -import com.syntaxphoenix.syntaxapi.utils.java.Exceptions; -import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.field.IFieldHandle; -import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.field.SafeFieldHandle; -import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.field.UnsafeDeclaredFieldHandle; -import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.field.UnsafeStaticFieldHandle; +import net.sourcewriters.minecraft.vcompat.provider.lookup.JavaAccessor; import net.sourcewriters.minecraft.vcompat.util.java.tools.ReflectionTools; public class ClassLookup { - - public static final Lookup LOOKUP = MethodHandles.lookup(); private Class owner; - private Lookup privateLookup; - private final HashMap constructors = new HashMap<>(); - private final HashMap methods = new HashMap<>(); - private final HashMap> fields = new HashMap<>(); + private final HashMap> constructors = new HashMap<>(); + private final HashMap methods = new HashMap<>(); + private final HashMap fields = new HashMap<>(); protected ClassLookup(final String classPath) throws IllegalAccessException { this(ReflectionTools.getClass(classPath)); @@ -38,7 +26,6 @@ protected ClassLookup(final String classPath) throws IllegalAccessException { protected ClassLookup(final Class owner) throws IllegalAccessException { this.owner = owner; - this.privateLookup = owner != null ? MethodHandles.privateLookupIn(owner, LOOKUP) : null; } /* @@ -49,10 +36,6 @@ public Class getOwner() { return owner; } - public Lookup getPrivateLockup() { - return privateLookup; - } - /* * */ @@ -62,7 +45,6 @@ public void delete() { methods.clear(); fields.clear(); owner = null; - privateLookup = null; } public boolean isValid() { @@ -73,15 +55,15 @@ public boolean isValid() { * */ - public Collection getConstructors() { + public Collection> getConstructors() { return constructors.values(); } - public Collection getMethods() { + public Collection getMethods() { return methods.values(); } - public Collection> getFields() { + public Collection getFields() { return fields.values(); } @@ -89,15 +71,15 @@ public Collection> getFields() { * */ - public MethodHandle getConstructor(final String name) { + public Constructor getConstructor(final String name) { return isValid() ? constructors.get(name) : null; } - public MethodHandle getMethod(final String name) { + public Method getMethod(final String name) { return isValid() ? methods.get(name) : null; } - public IFieldHandle getField(final String name) { + public Field getField(final String name) { return isValid() ? fields.get(name) : null; } @@ -125,35 +107,19 @@ public Object init() { if (!isValid()) { return null; } - final MethodHandle handle = constructors.computeIfAbsent("$base#empty", ignore -> { - try { - return LOOKUP.unreflectConstructor(owner.getConstructor()); - } catch (IllegalAccessException | NoSuchMethodException | SecurityException e) { - return null; - } - }); - if (handle == null) { + final Constructor constructor = constructors.computeIfAbsent("$base#empty", ignore -> JavaAccessor.getConstructor(owner)); + if (constructor == null) { constructors.remove("$base#empty"); return null; } - try { - return handle.invoke(); - } catch (final Throwable e) { - e.printStackTrace(); - } - return null; + return JavaAccessor.init(constructor); } public Object init(final String name, final Object... args) { if (!isValid() || !constructors.containsKey(name)) { return null; } - try { - return constructors.get(name).invokeWithArguments(args); - } catch (final Throwable e) { - e.printStackTrace(); - } - return null; + return JavaAccessor.init(constructors.get(name), args); } /* @@ -174,24 +140,14 @@ public Object run(final String name, final Object... args) { if (!isValid() || !methods.containsKey(name)) { return null; } - try { - return methods.get(name).invokeWithArguments(args); - } catch (final Throwable e) { - e.printStackTrace(); - } - return null; + return JavaAccessor.invokeStatic(methods.get(name), args); } public Object run(final Object source, final String name, final Object... args) { if (!isValid() || !methods.containsKey(name)) { return null; } - try { - return methods.get(name).invokeWithArguments(mergeBack(args, source)); - } catch (final Throwable e) { - e.printStackTrace(); - } - return null; + return JavaAccessor.invoke(source, methods.get(name), args); } /* @@ -199,25 +155,31 @@ public Object run(final Object source, final String name, final Object... args) */ public Object getFieldValue(final String name) { - return isValid() && fields.containsKey(name) ? fields.get(name).getValue() : null; + if (!isValid() || !fields.containsKey(name)) { + return null; + } + return JavaAccessor.getStaticValue(fields.get(name)); } public Object getFieldValue(final Object source, final String name) { - return isValid() && fields.containsKey(name) ? fields.get(name).getValue(source) : null; + if (!isValid() || !fields.containsKey(name)) { + return null; + } + return JavaAccessor.getObjectValue(source, fields.get(name)); } public void setFieldValue(final String name, final Object value) { if (!isValid() || !fields.containsKey(name)) { return; } - fields.get(name).setValue(value); + JavaAccessor.setStaticValue(fields.get(name), value); } public void setFieldValue(final Object source, final String name, final Object value) { if (!isValid() || !fields.containsKey(name)) { return; } - fields.get(name).setValue(source, value); + JavaAccessor.setObjectValue(source, fields.get(name), value); } /* @@ -229,30 +191,20 @@ public ClassLookup searchConstructor(final Predicate predicate, fin } public ClassLookup searchConstructor(final String name, final Class... arguments) { - if (hasConstructor(name)) { + if (!isValid() || hasConstructor(name)) { return this; } - Constructor constructor = null; - try { - constructor = owner.getDeclaredConstructor(arguments); - } catch (NoSuchMethodException | SecurityException e) { - } - if (constructor == null) { - try { - constructor = owner.getConstructor(arguments); - } catch (NoSuchMethodException | SecurityException e) { - } - } + Constructor constructor = JavaAccessor.getConstructor(owner, arguments); if (constructor != null) { - try { - constructors.put(name, unreflect(constructor)); - } catch (final IllegalAccessException e) { - } + constructors.put(name, constructor); } return this; } public ClassLookup searchConstructorsByArguments(String base, final Class... arguments) { + if (!isValid()) { + return this; + } final Constructor[] constructors = Arrays.merge(Constructor[]::new, owner.getDeclaredConstructors(), owner.getConstructors()); if (constructors.length == 0) { return this; @@ -264,12 +216,9 @@ public ClassLookup searchConstructorsByArguments(String base, final Class... if (args.length != arguments.length) { continue; } - try { - if (ReflectionTools.hasSameArguments(arguments, args)) { - this.constructors.put(base + current, unreflect(constructor)); - current++; - } - } catch (final IllegalAccessException e) { + if (ReflectionTools.hasSameArguments(arguments, args)) { + this.constructors.put(base + current, constructor); + current++; } } return this; @@ -285,31 +234,20 @@ public ClassLookup searchMethod(final Predicate predicate, final St } public ClassLookup searchMethod(final String name, final String methodName, final Class... arguments) { - if (hasMethod(name)) { + if (!isValid() || hasMethod(name)) { return this; } - Method method = null; - try { - method = owner.getDeclaredMethod(methodName, arguments); - } catch (NoSuchMethodException | SecurityException e) { - } - if (method == null) { - try { - method = owner.getMethod(methodName, arguments); - } catch (NoSuchMethodException | SecurityException e) { - } - } + Method method = JavaAccessor.getMethod(owner, methodName, arguments); if (method != null) { - try { - methods.put(name, unreflect(method)); - } catch (IllegalAccessException | SecurityException e) { - System.out.println(Exceptions.stackTraceToString(e)); - } + methods.put(name, method); } return this; } public ClassLookup searchMethodsByArguments(String base, final Class... arguments) { + if (!isValid()) { + return this; + } final Method[] methods = Arrays.merge(Method[]::new, owner.getDeclaredMethods(), owner.getMethods()); if (methods.length == 0) { return this; @@ -321,12 +259,9 @@ public ClassLookup searchMethodsByArguments(String base, final Class... argum if (args.length != arguments.length) { continue; } - try { - if (ReflectionTools.hasSameArguments(arguments, args)) { - this.methods.put(base + current, unreflect(method)); - current++; - } - } catch (IllegalAccessException | SecurityException e) { + if (ReflectionTools.hasSameArguments(arguments, args)) { + this.methods.put(base + current, method); + current++; } } return this; @@ -336,48 +271,17 @@ public ClassLookup searchMethodsByArguments(String base, final Class... argum * */ - public ClassLookup searchField(final Predicate predicate, final String name, final String fieldName, final Class type) { - return predicate.test(this) ? searchField(name, fieldName, type) : this; + public ClassLookup searchField(final Predicate predicate, final String name, final String fieldName) { + return predicate.test(this) ? searchField(name, fieldName) : this; } public ClassLookup searchField(final String name, final String fieldName) { - if (hasMethod(name)) { + if (!isValid() || hasField(name)) { return this; } - Field field = null; - try { - field = owner.getDeclaredField(fieldName); - } catch (NoSuchFieldException | SecurityException e) { - } - if (field == null) { - try { - field = owner.getField(fieldName); - } catch (NoSuchFieldException | SecurityException e) { - } - } + Field field = JavaAccessor.getField(owner, fieldName); if (field != null) { - storeField(name, field); - } - return this; - } - - public ClassLookup searchField(final String name, final String fieldName, final Class type) { - if (hasField(name)) { - return this; - } - VarHandle handle = null; - try { - handle = privateLookup.findVarHandle(owner, fieldName, type); - } catch (NoSuchFieldException | IllegalAccessException e) { - } - if (handle == null) { - try { - handle = privateLookup.findStaticVarHandle(owner, fieldName, type); - } catch (SecurityException | NoSuchFieldException | IllegalAccessException e) { - } - } - if (handle != null) { - fields.put(name, new SafeFieldHandle(handle)); + fields.put(name, field); } return this; } @@ -387,106 +291,13 @@ public ClassLookup searchField(final String name, final String fieldName, final */ public boolean putField(final String name, final Field field) { - return putField(name, field, false); - } - - public boolean putField(final String name, final Field field, final boolean forceSafe) { - if (field == null || name == null || field.getDeclaringClass() != owner || fields.containsKey(name)) { + if (!isValid() || field == null || name == null || field.getDeclaringClass() != owner || fields.containsKey(name)) { return false; } - storeField(name, field, forceSafe); + fields.put(name, field); return true; } - /* - * - */ - - private void storeField(final String name, final Field field) { - storeField(name, field, false); - } - - private void storeField(final String name, final Field field, final boolean forceSafe) { - if (forceSafe || !Modifier.isFinal(field.getModifiers())) { - try { - fields.put(name, new SafeFieldHandle(unreflect(field))); - return; - } catch (IllegalAccessException | SecurityException e) { - if (forceSafe) { - return; - } - } - } - if (!Modifier.isStatic(field.getModifiers())) { - fields.put(name, new UnsafeDeclaredFieldHandle(field)); - return; - } - fields.put(name, new UnsafeStaticFieldHandle(field)); - } - - private VarHandle unreflect(final Field field) throws IllegalAccessException, SecurityException { - if (Modifier.isStatic(field.getModifiers())) { - final boolean access = field.canAccess(null); - if (!access) { - field.setAccessible(true); - } - final VarHandle out = LOOKUP.unreflectVarHandle(field); - if (!access) { - field.setAccessible(false); - } - return out; - } - if (field.trySetAccessible()) { - final VarHandle out = LOOKUP.unreflectVarHandle(field); - field.setAccessible(false); - return out; - } - return LOOKUP.unreflectVarHandle(field); - } - - private MethodHandle unreflect(final Method method) throws IllegalAccessException, SecurityException { - if (Modifier.isStatic(method.getModifiers())) { - final boolean access = method.canAccess(null); - if (!access) { - method.setAccessible(true); - } - final MethodHandle out = LOOKUP.unreflect(method); - if (!access) { - method.setAccessible(false); - } - return out; - } - if (method.trySetAccessible()) { - final MethodHandle out = LOOKUP.unreflect(method); - method.setAccessible(false); - return out; - } - return LOOKUP.unreflect(method); - } - - private MethodHandle unreflect(final Constructor constructor) throws IllegalAccessException { - final boolean access = constructor.canAccess(null); - if (!access) { - constructor.setAccessible(true); - } - final MethodHandle out = LOOKUP.unreflectConstructor(constructor); - if (!access) { - constructor.setAccessible(false); - } - return out; - } - - /* - * - */ - - public static Object[] mergeBack(final Object[] array1, final Object... array2) { - final Object[] output = new Object[array1.length + array2.length]; - System.arraycopy(array2, 0, output, 0, array2.length); - System.arraycopy(array1, 0, output, array2.length, array1.length); - return output; - } - /* * */ diff --git a/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/provider/lookup/handle/field/IFieldHandle.java b/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/provider/lookup/handle/field/IFieldHandle.java deleted file mode 100644 index e6391dc..0000000 --- a/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/provider/lookup/handle/field/IFieldHandle.java +++ /dev/null @@ -1,17 +0,0 @@ -package net.sourcewriters.minecraft.vcompat.provider.lookup.handle.field; - -public interface IFieldHandle { - - Object getValue(); - - Object getValue(Object source); - - IFieldHandle setValue(Object value); - - IFieldHandle setValue(Object source, Object value); - - O getHandle(); - - boolean isUnsafe(); - -} diff --git a/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/provider/lookup/handle/field/SafeFieldHandle.java b/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/provider/lookup/handle/field/SafeFieldHandle.java deleted file mode 100644 index 7993890..0000000 --- a/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/provider/lookup/handle/field/SafeFieldHandle.java +++ /dev/null @@ -1,45 +0,0 @@ -package net.sourcewriters.minecraft.vcompat.provider.lookup.handle.field; - -import java.lang.invoke.VarHandle; - -public class SafeFieldHandle implements IFieldHandle { - - private final VarHandle handle; - - public SafeFieldHandle(VarHandle handle) { - this.handle = handle; - } - - @Override - public Object getValue() { - return handle.get(); - } - - @Override - public Object getValue(Object source) { - return handle.get(source); - } - - @Override - public IFieldHandle setValue(Object value) { - handle.set(value); - return this; - } - - @Override - public IFieldHandle setValue(Object source, Object value) { - handle.set(source, value); - return this; - } - - @Override - public VarHandle getHandle() { - return handle; - } - - @Override - public boolean isUnsafe() { - return false; - } - -} diff --git a/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/provider/lookup/handle/field/UnsafeDeclaredFieldHandle.java b/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/provider/lookup/handle/field/UnsafeDeclaredFieldHandle.java deleted file mode 100644 index ce9bd1c..0000000 --- a/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/provider/lookup/handle/field/UnsafeDeclaredFieldHandle.java +++ /dev/null @@ -1,40 +0,0 @@ -package net.sourcewriters.minecraft.vcompat.provider.lookup.handle.field; - -import java.lang.reflect.Field; - -public final class UnsafeDeclaredFieldHandle extends UnsafeFieldHandle { - - private final Field handle; - private final long offset; - - public UnsafeDeclaredFieldHandle(Field handle) { - this.handle = handle; - this.offset = UNSAFE.objectFieldOffset(handle); - } - - @Override - public Object getValue() { - return null; // Stay null because its not static - } - - @Override - public Object getValue(Object source) { - return getMemoryValue(source, offset); - } - - @Override - public IFieldHandle setValue(Object value) { - return this; // Do nothing because its not static - } - - @Override - public IFieldHandle setValue(Object source, Object value) { - return setMemoryValue(source, offset, value); - } - - @Override - public Field getHandle() { - return handle; - } - -} diff --git a/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/provider/lookup/handle/field/UnsafeFieldHandle.java b/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/provider/lookup/handle/field/UnsafeFieldHandle.java deleted file mode 100644 index b0b3a7f..0000000 --- a/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/provider/lookup/handle/field/UnsafeFieldHandle.java +++ /dev/null @@ -1,35 +0,0 @@ -package net.sourcewriters.minecraft.vcompat.provider.lookup.handle.field; - -import java.lang.reflect.Field; - -import sun.misc.Unsafe; - -public abstract class UnsafeFieldHandle implements IFieldHandle { - - protected static final Unsafe UNSAFE = getUnsafe(); - - protected final IFieldHandle setMemoryValue(Object base, long offset, Object value) { - UNSAFE.putObject(base, offset, value); - return this; - } - - protected final Object getMemoryValue(Object base, long offset) { - return UNSAFE.getObject(base, offset); - } - - @Override - public final boolean isUnsafe() { - return true; - } - - private static Unsafe getUnsafe() { - try { - Field field = Unsafe.class.getDeclaredField("theUnsafe"); - field.setAccessible(true); - return (Unsafe) field.get(null); - } catch (Exception ignore) { - return null; - } - } - -} diff --git a/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/provider/lookup/handle/field/UnsafeStaticFieldHandle.java b/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/provider/lookup/handle/field/UnsafeStaticFieldHandle.java deleted file mode 100644 index df013ba..0000000 --- a/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/provider/lookup/handle/field/UnsafeStaticFieldHandle.java +++ /dev/null @@ -1,42 +0,0 @@ -package net.sourcewriters.minecraft.vcompat.provider.lookup.handle.field; - -import java.lang.reflect.Field; - -public final class UnsafeStaticFieldHandle extends UnsafeFieldHandle { - - private final Field handle; - private final Object base; - private final long offset; - - public UnsafeStaticFieldHandle(Field handle) { - this.handle = handle; - this.base = UNSAFE.staticFieldBase(handle); - this.offset = UNSAFE.staticFieldOffset(handle); - } - - @Override - public Object getValue() { - return getMemoryValue(base, offset); - } - - @Override - public Object getValue(Object source) { - return null; // Stay null because its static - } - - @Override - public IFieldHandle setValue(Object value) { - return setMemoryValue(base, offset, value); - } - - @Override - public IFieldHandle setValue(Object source, Object value) { - return this; // Do nothing because its static - } - - @Override - public Field getHandle() { - return handle; - } - -} diff --git a/vcompat-core/pom.xml b/vcompat-core/pom.xml index ac88914..6dce272 100644 --- a/vcompat-core/pom.xml +++ b/vcompat-core/pom.xml @@ -3,7 +3,7 @@ net.sourcewriters.minecraft vcompat-parent - 3.0.1 + 3.0.2 vcompat diff --git a/vcompat-legacy/pom.xml b/vcompat-legacy/pom.xml index 454dc64..f29cd21 100644 --- a/vcompat-legacy/pom.xml +++ b/vcompat-legacy/pom.xml @@ -3,7 +3,7 @@ net.sourcewriters.minecraft vcompat-parent - 3.0.1 + 3.0.2 vcompat-legacy diff --git a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_10_R1/TextureProvider1_10_R1.java b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_10_R1/TextureProvider1_10_R1.java index eea1e46..ef69a12 100644 --- a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_10_R1/TextureProvider1_10_R1.java +++ b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_10_R1/TextureProvider1_10_R1.java @@ -2,6 +2,7 @@ import org.bukkit.Material; import org.bukkit.block.Block; +import org.bukkit.block.BlockState; import org.bukkit.craftbukkit.v1_10_R1.block.CraftSkull; import org.bukkit.craftbukkit.v1_10_R1.inventory.CraftItemStack; import org.bukkit.inventory.meta.ItemMeta; @@ -10,6 +11,7 @@ import com.mojang.authlib.GameProfile; +import net.sourcewriters.minecraft.vcompat.provider.lookup.ClassLookupProvider; import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup; import net.minecraft.server.v1_10_R1.GameProfileSerializer; @@ -20,21 +22,25 @@ @SuppressWarnings("deprecation") public class TextureProvider1_10_R1 extends TextureProvider { - private final ClassLookup craftItemStackRef = ClassLookup.of(CraftItemStack.class).searchField("handle", "handle"); - private final ClassLookup craftMetaSkullRef = ClassLookup.of("org.bukkit.craftbukkit.v1_10_R1.inventory.CraftMetaSkull") - .searchField("serialized", "serializedProfile").searchField("profile", "profile"); + private final ClassLookup craftItemStackRef; + private final ClassLookup craftMetaSkullRef; private final Material skullMaterial = Material.valueOf("SKULL"); protected TextureProvider1_10_R1(VersionControl1_10_R1 versionControl) { super(versionControl); + ClassLookupProvider provider = versionControl.getLookupProvider(); + craftItemStackRef = provider.createLookup("CraftItemStack", CraftItemStack.class).searchField("handle", "handle"); + craftMetaSkullRef = provider.createCBLookup("CraftMetaSkull", "inventory.CraftMetaSkull") + .searchField("serialized", "serializedProfile").searchField("profile", "profile"); } @Override public GameProfile profileFromBlock(Block block) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return null; } - return ((CraftSkull) block).getTileEntity().getGameProfile(); + return ((CraftSkull) state).getTileEntity().getGameProfile(); } @Override @@ -95,10 +101,11 @@ public boolean applyItem(org.bukkit.inventory.ItemStack itemStack, GameProfile p @Override public boolean applyBlock(Block block, GameProfile profile) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return false; } - ((CraftSkull) block).getTileEntity().setGameProfile(profile); + ((CraftSkull) state).getTileEntity().setGameProfile(profile); return true; } diff --git a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_10_R1/tools/BlockTools1_10_R1.java b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_10_R1/tools/BlockTools1_10_R1.java index 8bafd8e..4ed8fa2 100644 --- a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_10_R1/tools/BlockTools1_10_R1.java +++ b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_10_R1/tools/BlockTools1_10_R1.java @@ -1,27 +1,25 @@ package net.sourcewriters.minecraft.vcompat.provider.impl.v1_10_R1.tools; import org.bukkit.block.Block; +import org.bukkit.block.BlockState; import org.bukkit.craftbukkit.v1_10_R1.block.CraftSkull; import com.mojang.authlib.properties.Property; import com.mojang.authlib.properties.PropertyMap; -import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup; - import net.minecraft.server.v1_10_R1.TileEntitySkull; import net.sourcewriters.minecraft.vcompat.provider.tools.BlockTools; import net.sourcewriters.minecraft.vcompat.util.constants.MinecraftConstants; public class BlockTools1_10_R1 extends BlockTools { - private final ClassLookup craftEntityStateRef = ClassLookup.of(CraftSkull.class).searchField("tileEntity", "tileEntity"); - @Override public void setHeadTexture(Block block, String texture) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return; } - TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(block, "tileEntity"); + TileEntitySkull entitySkull = ((CraftSkull) state).getTileEntity(); PropertyMap map = entitySkull.getGameProfile().getProperties(); map.removeAll("textures"); map.put("textures", new Property("textures", MinecraftConstants.TEXTURE_SIGNATURE, texture)); @@ -29,10 +27,11 @@ public void setHeadTexture(Block block, String texture) { @Override public String getHeadTexture(Block block) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return null; } - TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(block, "tileEntity"); + TileEntitySkull entitySkull = ((CraftSkull) state).getTileEntity(); return entitySkull.getGameProfile().getProperties().get("textures").iterator().next().getValue(); } diff --git a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_11_R1/TextureProvider1_11_R1.java b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_11_R1/TextureProvider1_11_R1.java index ee263d1..133d9de 100644 --- a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_11_R1/TextureProvider1_11_R1.java +++ b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_11_R1/TextureProvider1_11_R1.java @@ -2,6 +2,7 @@ import org.bukkit.Material; import org.bukkit.block.Block; +import org.bukkit.block.BlockState; import org.bukkit.craftbukkit.v1_11_R1.block.CraftSkull; import org.bukkit.craftbukkit.v1_11_R1.inventory.CraftItemStack; import org.bukkit.inventory.meta.ItemMeta; @@ -10,6 +11,7 @@ import com.mojang.authlib.GameProfile; +import net.sourcewriters.minecraft.vcompat.provider.lookup.ClassLookupProvider; import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup; import net.minecraft.server.v1_11_R1.GameProfileSerializer; @@ -20,21 +22,25 @@ @SuppressWarnings("deprecation") public class TextureProvider1_11_R1 extends TextureProvider { - private final ClassLookup craftItemStackRef = ClassLookup.of(CraftItemStack.class).searchField("handle", "handle"); - private final ClassLookup craftMetaSkullRef = ClassLookup.of("org.bukkit.craftbukkit.v1_11_R1.inventory.CraftMetaSkull") - .searchField("serialized", "serializedProfile").searchField("profile", "profile"); + private final ClassLookup craftItemStackRef; + private final ClassLookup craftMetaSkullRef; private final Material skullMaterial = Material.valueOf("SKULL"); protected TextureProvider1_11_R1(VersionControl1_11_R1 versionControl) { super(versionControl); + ClassLookupProvider provider = versionControl.getLookupProvider(); + craftItemStackRef = provider.createLookup("CraftItemStack", CraftItemStack.class).searchField("handle", "handle"); + craftMetaSkullRef = provider.createCBLookup("CraftMetaSkull", "inventory.CraftMetaSkull") + .searchField("serialized", "serializedProfile").searchField("profile", "profile"); } @Override public GameProfile profileFromBlock(Block block) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return null; } - return ((CraftSkull) block).getTileEntity().getGameProfile(); + return ((CraftSkull) state).getTileEntity().getGameProfile(); } @Override @@ -93,10 +99,11 @@ public boolean applyItem(org.bukkit.inventory.ItemStack itemStack, GameProfile p @Override public boolean applyBlock(Block block, GameProfile profile) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return false; } - ((CraftSkull) block).getTileEntity().setGameProfile(profile); + ((CraftSkull) state).getTileEntity().setGameProfile(profile); return true; } diff --git a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_11_R1/tools/BlockTools1_11_R1.java b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_11_R1/tools/BlockTools1_11_R1.java index 029f1d4..76fc1cc 100644 --- a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_11_R1/tools/BlockTools1_11_R1.java +++ b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_11_R1/tools/BlockTools1_11_R1.java @@ -1,27 +1,25 @@ package net.sourcewriters.minecraft.vcompat.provider.impl.v1_11_R1.tools; import org.bukkit.block.Block; +import org.bukkit.block.BlockState; import org.bukkit.craftbukkit.v1_11_R1.block.CraftSkull; import com.mojang.authlib.properties.Property; import com.mojang.authlib.properties.PropertyMap; -import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup; - import net.minecraft.server.v1_11_R1.TileEntitySkull; import net.sourcewriters.minecraft.vcompat.provider.tools.BlockTools; import net.sourcewriters.minecraft.vcompat.util.constants.MinecraftConstants; public class BlockTools1_11_R1 extends BlockTools { - private final ClassLookup craftEntityStateRef = ClassLookup.of(CraftSkull.class).searchField("tileEntity", "tileEntity"); - @Override public void setHeadTexture(Block block, String texture) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return; } - TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(block, "tileEntity"); + TileEntitySkull entitySkull = ((CraftSkull) state).getTileEntity(); PropertyMap map = entitySkull.getGameProfile().getProperties(); map.removeAll("textures"); map.put("textures", new Property("textures", MinecraftConstants.TEXTURE_SIGNATURE, texture)); @@ -29,10 +27,11 @@ public void setHeadTexture(Block block, String texture) { @Override public String getHeadTexture(Block block) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return null; } - TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(block, "tileEntity"); + TileEntitySkull entitySkull = ((CraftSkull) state).getTileEntity(); return entitySkull.getGameProfile().getProperties().get("textures").iterator().next().getValue(); } diff --git a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_12_R1/TextureProvider1_12_R1.java b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_12_R1/TextureProvider1_12_R1.java index 623ce0b..4ed0963 100644 --- a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_12_R1/TextureProvider1_12_R1.java +++ b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_12_R1/TextureProvider1_12_R1.java @@ -2,6 +2,7 @@ import org.bukkit.Material; import org.bukkit.block.Block; +import org.bukkit.block.BlockState; import org.bukkit.craftbukkit.v1_12_R1.block.CraftBlockEntityState; import org.bukkit.craftbukkit.v1_12_R1.block.CraftSkull; import org.bukkit.craftbukkit.v1_12_R1.inventory.CraftItemStack; @@ -11,6 +12,7 @@ import com.mojang.authlib.GameProfile; +import net.sourcewriters.minecraft.vcompat.provider.lookup.ClassLookupProvider; import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup; import net.minecraft.server.v1_12_R1.GameProfileSerializer; @@ -22,22 +24,28 @@ @SuppressWarnings("deprecation") public class TextureProvider1_12_R1 extends TextureProvider { - private final ClassLookup craftEntityStateRef = ClassLookup.of(CraftBlockEntityState.class).searchField("tileEntity", "tileEntity"); - private final ClassLookup craftItemStackRef = ClassLookup.of(CraftItemStack.class).searchField("handle", "handle"); - private final ClassLookup craftMetaSkullRef = ClassLookup.of("org.bukkit.craftbukkit.v1_12_R1.inventory.CraftMetaSkull") - .searchField("serialized", "serializedProfile").searchField("profile", "profile"); + private final ClassLookup craftEntityStateRef; + private final ClassLookup craftItemStackRef; + private final ClassLookup craftMetaSkullRef; private final Material skullMaterial = Material.valueOf("SKULL"); protected TextureProvider1_12_R1(VersionControl1_12_R1 versionControl) { super(versionControl); + ClassLookupProvider provider = versionControl.getLookupProvider(); + craftEntityStateRef = provider.createLookup("CraftBlockEntityState", CraftBlockEntityState.class).searchField("tileEntity", + "tileEntity"); + craftItemStackRef = provider.createLookup("CraftItemStack", CraftItemStack.class).searchField("handle", "handle"); + craftMetaSkullRef = provider.createCBLookup("CraftMetaSkull", "inventory.CraftMetaSkull") + .searchField("serialized", "serializedProfile").searchField("profile", "profile"); } @Override public GameProfile profileFromBlock(Block block) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return null; } - TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(block, "tileEntity"); + TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(state, "tileEntity"); return entitySkull.getGameProfile(); } @@ -97,10 +105,11 @@ public boolean applyItem(org.bukkit.inventory.ItemStack itemStack, GameProfile p @Override public boolean applyBlock(Block block, GameProfile profile) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return false; } - TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(block, "tileEntity"); + TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(state, "tileEntity"); entitySkull.setGameProfile(profile); return true; } diff --git a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_12_R1/tools/BlockTools1_12_R1.java b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_12_R1/tools/BlockTools1_12_R1.java index 043205c..49a5e86 100644 --- a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_12_R1/tools/BlockTools1_12_R1.java +++ b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_12_R1/tools/BlockTools1_12_R1.java @@ -1,11 +1,13 @@ package net.sourcewriters.minecraft.vcompat.provider.impl.v1_12_R1.tools; import org.bukkit.block.Block; +import org.bukkit.block.BlockState; import org.bukkit.craftbukkit.v1_12_R1.block.CraftSkull; import com.mojang.authlib.properties.Property; import com.mojang.authlib.properties.PropertyMap; +import net.sourcewriters.minecraft.vcompat.VersionCompatProvider; import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup; import net.minecraft.server.v1_12_R1.TileEntitySkull; @@ -14,14 +16,20 @@ public class BlockTools1_12_R1 extends BlockTools { - private final ClassLookup craftEntityStateRef = ClassLookup.of(CraftSkull.class).searchField("tileEntity", "tileEntity"); + private final ClassLookup craftEntityStateRef; + + public BlockTools1_12_R1() { + craftEntityStateRef = VersionCompatProvider.get().getLookupProvider().createLookup("CraftSkull", CraftSkull.class) + .searchField("tileEntity", "tileEntity"); + } @Override public void setHeadTexture(Block block, String texture) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return; } - TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(block, "tileEntity"); + TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(state, "tileEntity"); PropertyMap map = entitySkull.getGameProfile().getProperties(); map.removeAll("textures"); map.put("textures", new Property("textures", MinecraftConstants.TEXTURE_SIGNATURE, texture)); @@ -29,10 +37,11 @@ public void setHeadTexture(Block block, String texture) { @Override public String getHeadTexture(Block block) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return null; } - TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(block, "tileEntity"); + TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(state, "tileEntity"); return entitySkull.getGameProfile().getProperties().get("textures").iterator().next().getValue(); } diff --git a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_13_R1/TextureProvider1_13_R1.java b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_13_R1/TextureProvider1_13_R1.java index ea5511e..b00da19 100644 --- a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_13_R1/TextureProvider1_13_R1.java +++ b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_13_R1/TextureProvider1_13_R1.java @@ -2,6 +2,7 @@ import org.bukkit.Material; import org.bukkit.block.Block; +import org.bukkit.block.BlockState; import org.bukkit.craftbukkit.v1_13_R1.block.CraftBlockEntityState; import org.bukkit.craftbukkit.v1_13_R1.block.CraftSkull; import org.bukkit.craftbukkit.v1_13_R1.inventory.CraftItemStack; @@ -10,6 +11,7 @@ import com.mojang.authlib.GameProfile; +import net.sourcewriters.minecraft.vcompat.provider.lookup.ClassLookupProvider; import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup; import net.minecraft.server.v1_13_R1.GameProfileSerializer; @@ -20,21 +22,27 @@ public class TextureProvider1_13_R1 extends TextureProvider { - private final ClassLookup craftEntityStateRef = ClassLookup.of(CraftBlockEntityState.class).searchField("tileEntity", "tileEntity"); - private final ClassLookup craftItemStackRef = ClassLookup.of(CraftItemStack.class).searchField("handle", "handle"); - private final ClassLookup craftMetaSkullRef = ClassLookup.of("org.bukkit.craftbukkit.v1_13_R1.inventory.CraftMetaSkull") - .searchField("serialized", "serializedProfile").searchField("profile", "profile"); + private final ClassLookup craftEntityStateRef; + private final ClassLookup craftItemStackRef; + private final ClassLookup craftMetaSkullRef; protected TextureProvider1_13_R1(VersionControl1_13_R1 versionControl) { super(versionControl); + ClassLookupProvider provider = versionControl.getLookupProvider(); + craftEntityStateRef = provider.createLookup("CraftBlockEntityState", CraftBlockEntityState.class).searchField("tileEntity", + "tileEntity"); + craftItemStackRef = provider.createLookup("CraftItemStack", CraftItemStack.class).searchField("handle", "handle"); + craftMetaSkullRef = provider.createCBLookup("CraftMetaSkull", "inventory.CraftMetaSkull") + .searchField("serialized", "serializedProfile").searchField("profile", "profile"); } @Override public GameProfile profileFromBlock(Block block) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return null; } - TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(block, "tileEntity"); + TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(state, "tileEntity"); return entitySkull.getGameProfile(); } @@ -91,10 +99,11 @@ public boolean applyItem(org.bukkit.inventory.ItemStack itemStack, GameProfile p @Override public boolean applyBlock(Block block, GameProfile profile) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return false; } - TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(block, "tileEntity"); + TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(state, "tileEntity"); entitySkull.setGameProfile(profile); return true; } diff --git a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_13_R1/tools/BlockTools1_13_R1.java b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_13_R1/tools/BlockTools1_13_R1.java index b24b913..379246e 100644 --- a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_13_R1/tools/BlockTools1_13_R1.java +++ b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_13_R1/tools/BlockTools1_13_R1.java @@ -1,11 +1,13 @@ package net.sourcewriters.minecraft.vcompat.provider.impl.v1_13_R1.tools; import org.bukkit.block.Block; +import org.bukkit.block.BlockState; import org.bukkit.craftbukkit.v1_13_R1.block.CraftSkull; import com.mojang.authlib.properties.Property; import com.mojang.authlib.properties.PropertyMap; +import net.sourcewriters.minecraft.vcompat.VersionCompatProvider; import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup; import net.minecraft.server.v1_13_R1.TileEntitySkull; @@ -14,14 +16,21 @@ public class BlockTools1_13_R1 extends BlockTools { - private final ClassLookup craftEntityStateRef = ClassLookup.of(CraftSkull.class).searchField("tileEntity", "tileEntity"); + + private final ClassLookup craftEntityStateRef; + + public BlockTools1_13_R1() { + craftEntityStateRef = VersionCompatProvider.get().getLookupProvider().createLookup("CraftSkull", CraftSkull.class) + .searchField("tileEntity", "tileEntity"); + } @Override public void setHeadTexture(Block block, String texture) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return; } - TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(block, "tileEntity"); + TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(state, "tileEntity"); PropertyMap map = entitySkull.getGameProfile().getProperties(); map.removeAll("textures"); map.put("textures", new Property("textures", MinecraftConstants.TEXTURE_SIGNATURE, texture)); @@ -29,10 +38,11 @@ public void setHeadTexture(Block block, String texture) { @Override public String getHeadTexture(Block block) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return null; } - TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(block, "tileEntity"); + TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(state, "tileEntity"); return entitySkull.getGameProfile().getProperties().get("textures").iterator().next().getValue(); } diff --git a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_13_R2/TextureProvider1_13_R2.java b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_13_R2/TextureProvider1_13_R2.java index 086f5d3..46bafa0 100644 --- a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_13_R2/TextureProvider1_13_R2.java +++ b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_13_R2/TextureProvider1_13_R2.java @@ -2,6 +2,7 @@ import org.bukkit.Material; import org.bukkit.block.Block; +import org.bukkit.block.BlockState; import org.bukkit.craftbukkit.v1_13_R2.block.CraftBlockEntityState; import org.bukkit.craftbukkit.v1_13_R2.block.CraftSkull; import org.bukkit.craftbukkit.v1_13_R2.inventory.CraftItemStack; @@ -10,6 +11,7 @@ import com.mojang.authlib.GameProfile; +import net.sourcewriters.minecraft.vcompat.provider.lookup.ClassLookupProvider; import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup; import net.minecraft.server.v1_13_R2.GameProfileSerializer; @@ -20,21 +22,27 @@ public class TextureProvider1_13_R2 extends TextureProvider { - private final ClassLookup craftEntityStateRef = ClassLookup.of(CraftBlockEntityState.class).searchField("tileEntity", "tileEntity"); - private final ClassLookup craftItemStackRef = ClassLookup.of(CraftItemStack.class).searchField("handle", "handle"); - private final ClassLookup craftMetaSkullRef = ClassLookup.of("org.bukkit.craftbukkit.v1_13_R2.inventory.CraftMetaSkull") - .searchField("serialized", "serializedProfile").searchField("profile", "profile"); + private final ClassLookup craftEntityStateRef; + private final ClassLookup craftItemStackRef; + private final ClassLookup craftMetaSkullRef; protected TextureProvider1_13_R2(VersionControl1_13_R2 versionControl) { super(versionControl); + ClassLookupProvider provider = versionControl.getLookupProvider(); + craftEntityStateRef = provider.createLookup("CraftBlockEntityState", CraftBlockEntityState.class).searchField("tileEntity", + "tileEntity"); + craftItemStackRef = provider.createLookup("CraftItemStack", CraftItemStack.class).searchField("handle", "handle"); + craftMetaSkullRef = provider.createCBLookup("CraftMetaSkull", "inventory.CraftMetaSkull") + .searchField("serialized", "serializedProfile").searchField("profile", "profile"); } @Override public GameProfile profileFromBlock(Block block) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return null; } - TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(block, "tileEntity"); + TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(state, "tileEntity"); return entitySkull.getGameProfile(); } @@ -91,10 +99,11 @@ public boolean applyItem(org.bukkit.inventory.ItemStack itemStack, GameProfile p @Override public boolean applyBlock(Block block, GameProfile profile) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return false; } - TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(block, "tileEntity"); + TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(state, "tileEntity"); entitySkull.setGameProfile(profile); return true; } diff --git a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_13_R2/tools/BlockTools1_13_R2.java b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_13_R2/tools/BlockTools1_13_R2.java index e6bddd4..2597728 100644 --- a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_13_R2/tools/BlockTools1_13_R2.java +++ b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_13_R2/tools/BlockTools1_13_R2.java @@ -1,11 +1,13 @@ package net.sourcewriters.minecraft.vcompat.provider.impl.v1_13_R2.tools; import org.bukkit.block.Block; +import org.bukkit.block.BlockState; import org.bukkit.craftbukkit.v1_13_R2.block.CraftSkull; import com.mojang.authlib.properties.Property; import com.mojang.authlib.properties.PropertyMap; +import net.sourcewriters.minecraft.vcompat.VersionCompatProvider; import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup; import net.minecraft.server.v1_13_R2.TileEntitySkull; @@ -14,14 +16,20 @@ public class BlockTools1_13_R2 extends BlockTools { - private final ClassLookup craftEntityStateRef = ClassLookup.of(CraftSkull.class).searchField("tileEntity", "tileEntity"); + private final ClassLookup craftEntityStateRef; + + public BlockTools1_13_R2() { + craftEntityStateRef = VersionCompatProvider.get().getLookupProvider().createLookup("CraftSkull", CraftSkull.class) + .searchField("tileEntity", "tileEntity"); + } @Override public void setHeadTexture(Block block, String texture) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return; } - TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(block, "tileEntity"); + TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(state, "tileEntity"); PropertyMap map = entitySkull.getGameProfile().getProperties(); map.removeAll("textures"); map.put("textures", new Property("textures", MinecraftConstants.TEXTURE_SIGNATURE, texture)); @@ -29,10 +37,11 @@ public void setHeadTexture(Block block, String texture) { @Override public String getHeadTexture(Block block) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return null; } - TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(block, "tileEntity"); + TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(state, "tileEntity"); return entitySkull.getGameProfile().getProperties().get("textures").iterator().next().getValue(); } diff --git a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_14_R1/TextureProvider1_14_R1.java b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_14_R1/TextureProvider1_14_R1.java index 4b42cbe..b28393d 100644 --- a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_14_R1/TextureProvider1_14_R1.java +++ b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_14_R1/TextureProvider1_14_R1.java @@ -2,6 +2,7 @@ import org.bukkit.Material; import org.bukkit.block.Block; +import org.bukkit.block.BlockState; import org.bukkit.craftbukkit.v1_14_R1.block.CraftBlockEntityState; import org.bukkit.craftbukkit.v1_14_R1.block.CraftSkull; import org.bukkit.craftbukkit.v1_14_R1.inventory.CraftItemStack; @@ -10,6 +11,7 @@ import com.mojang.authlib.GameProfile; +import net.sourcewriters.minecraft.vcompat.provider.lookup.ClassLookupProvider; import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup; import net.minecraft.server.v1_14_R1.GameProfileSerializer; @@ -20,21 +22,27 @@ public class TextureProvider1_14_R1 extends TextureProvider { - private final ClassLookup craftEntityStateRef = ClassLookup.of(CraftBlockEntityState.class).searchField("tileEntity", "tileEntity"); - private final ClassLookup craftItemStackRef = ClassLookup.of(CraftItemStack.class).searchField("handle", "handle"); - private final ClassLookup craftMetaSkullRef = ClassLookup.of("org.bukkit.craftbukkit.v1_14_R1.inventory.CraftMetaSkull") - .searchField("serialized", "serializedProfile").searchField("profile", "profile"); + private final ClassLookup craftEntityStateRef; + private final ClassLookup craftItemStackRef; + private final ClassLookup craftMetaSkullRef; protected TextureProvider1_14_R1(VersionControl1_14_R1 versionControl) { super(versionControl); + ClassLookupProvider provider = versionControl.getLookupProvider(); + craftEntityStateRef = provider.createLookup("CraftBlockEntityState", CraftBlockEntityState.class).searchField("tileEntity", + "tileEntity"); + craftItemStackRef = provider.createLookup("CraftItemStack", CraftItemStack.class).searchField("handle", "handle"); + craftMetaSkullRef = provider.createCBLookup("CraftMetaSkull", "inventory.CraftMetaSkull") + .searchField("serialized", "serializedProfile").searchField("profile", "profile"); } @Override public GameProfile profileFromBlock(Block block) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return null; } - TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(block, "tileEntity"); + TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(state, "tileEntity"); return entitySkull.gameProfile; } @@ -91,10 +99,11 @@ public boolean applyItem(org.bukkit.inventory.ItemStack itemStack, GameProfile p @Override public boolean applyBlock(Block block, GameProfile profile) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return false; } - TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(block, "tileEntity"); + TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(state, "tileEntity"); entitySkull.setGameProfile(profile); return true; } diff --git a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_14_R1/data/hook/BukkitContainerAdapterHook1_14_R1.java b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_14_R1/data/hook/BukkitContainerAdapterHook1_14_R1.java index 5541f61..d81e18d 100644 --- a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_14_R1/data/hook/BukkitContainerAdapterHook1_14_R1.java +++ b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_14_R1/data/hook/BukkitContainerAdapterHook1_14_R1.java @@ -14,7 +14,7 @@ import net.sourcewriters.minecraft.vcompat.data.api.IDataContainer; import net.sourcewriters.minecraft.vcompat.data.nbt.NbtContainer; import net.sourcewriters.minecraft.vcompat.shaded.syntaxapi.nbt.NbtCompound; - +import net.sourcewriters.minecraft.vcompat.provider.lookup.ClassLookupProvider; import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup; import net.minecraft.server.v1_14_R1.NBTTagCompound; @@ -31,13 +31,16 @@ public final class BukkitContainerAdapterHook1_14_R1 { private static final BukkitContainerAdapterHook1_14_R1 HOOK = new BukkitContainerAdapterHook1_14_R1(); - private final ClassLookup registryRef = ClassLookup.of(CraftPersistentDataTypeRegistry.class) - .searchMethod("create", "createAdapter", Class.class, Class.class, Function.class, Function.class) - .searchField("adapters", "adapters") - .searchField("function", "CREATE_ADAPTER"); - private final ClassLookup entityRef = ClassLookup.of(CraftEntity.class).searchField("registry", "DATA_TYPE_REGISTRY"); + private final ClassLookup registryRef; + private final ClassLookup entityRef; - private BukkitContainerAdapterHook1_14_R1() {} + private BukkitContainerAdapterHook1_14_R1() { + ClassLookupProvider provider = VersionCompatProvider.get().getLookupProvider(); + registryRef = provider.createLookup("CraftPersistentDataTypeRegistry", CraftPersistentDataTypeRegistry.class) + .searchMethod("create", "createAdapter", Class.class, Class.class, Function.class, Function.class) + .searchField("adapters", "adapters").searchField("function", "CREATE_ADAPTER"); + entityRef = provider.createLookup("CraftEntity", CraftEntity.class).searchField("registry", "DATA_TYPE_REGISTRY"); + } private final HashMap map = new HashMap<>(); @@ -60,7 +63,7 @@ private void inject(CraftPersistentDataTypeRegistry registry) { return; } map.put(registry, (Function) registryRef.getFieldValue(registry, "function")); - Function function = clazz -> createAdapter(registry, registryRef.getMethod("create").type().returnType(), (Class) clazz); + Function function = clazz -> createAdapter(registry, registryRef.getMethod("create").getReturnType(), (Class) clazz); registryRef.setFieldValue(registry, "function", function); } @@ -90,7 +93,8 @@ private NBTTagCompound toPrimitive(WrappedContainer input) { } if (handle instanceof IDataContainer) { if (handle instanceof NbtContainer) { - return (NBTTagCompound) VersionCompatProvider.get().getControl().getBukkitConversion().toMinecraftCompound(((NbtContainer) handle).asNbt()); + return (NBTTagCompound) VersionCompatProvider.get().getControl().getBukkitConversion() + .toMinecraftCompound(((NbtContainer) handle).asNbt()); } throw new IllegalArgumentException( "Expected 'CraftPersistentDataContainer' got '" + handle.getClass().getSimpleName() + " instead'!"); diff --git a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_14_R1/tools/BlockTools1_14_R1.java b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_14_R1/tools/BlockTools1_14_R1.java index 3b452bf..766a395 100644 --- a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_14_R1/tools/BlockTools1_14_R1.java +++ b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_14_R1/tools/BlockTools1_14_R1.java @@ -1,11 +1,13 @@ package net.sourcewriters.minecraft.vcompat.provider.impl.v1_14_R1.tools; import org.bukkit.block.Block; +import org.bukkit.block.BlockState; import org.bukkit.craftbukkit.v1_14_R1.block.CraftSkull; import com.mojang.authlib.properties.Property; import com.mojang.authlib.properties.PropertyMap; +import net.sourcewriters.minecraft.vcompat.VersionCompatProvider; import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup; import net.minecraft.server.v1_14_R1.TileEntitySkull; @@ -14,14 +16,20 @@ public class BlockTools1_14_R1 extends BlockTools { - private final ClassLookup craftEntityStateRef = ClassLookup.of(CraftSkull.class).searchField("tileEntity", "tileEntity"); + private final ClassLookup craftEntityStateRef; + + public BlockTools1_14_R1() { + craftEntityStateRef = VersionCompatProvider.get().getLookupProvider().createLookup("CraftSkull", CraftSkull.class) + .searchField("tileEntity", "tileEntity"); + } @Override public void setHeadTexture(Block block, String texture) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return; } - TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(block, "tileEntity"); + TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(state, "tileEntity"); PropertyMap map = entitySkull.gameProfile.getProperties(); map.removeAll("textures"); map.put("textures", new Property("textures", MinecraftConstants.TEXTURE_SIGNATURE, texture)); @@ -29,10 +37,11 @@ public void setHeadTexture(Block block, String texture) { @Override public String getHeadTexture(Block block) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return null; } - TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(block, "tileEntity"); + TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(state, "tileEntity"); return entitySkull.gameProfile.getProperties().get("textures").iterator().next().getValue(); } diff --git a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_15_R1/TextureProvider1_15_R1.java b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_15_R1/TextureProvider1_15_R1.java index d02bafc..8489dc0 100644 --- a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_15_R1/TextureProvider1_15_R1.java +++ b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_15_R1/TextureProvider1_15_R1.java @@ -2,6 +2,7 @@ import org.bukkit.Material; import org.bukkit.block.Block; +import org.bukkit.block.BlockState; import org.bukkit.craftbukkit.v1_15_R1.block.CraftBlockEntityState; import org.bukkit.craftbukkit.v1_15_R1.block.CraftSkull; import org.bukkit.craftbukkit.v1_15_R1.inventory.CraftItemStack; @@ -10,6 +11,7 @@ import com.mojang.authlib.GameProfile; +import net.sourcewriters.minecraft.vcompat.provider.lookup.ClassLookupProvider; import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup; import net.minecraft.server.v1_15_R1.GameProfileSerializer; @@ -20,21 +22,27 @@ public class TextureProvider1_15_R1 extends TextureProvider { - private final ClassLookup craftEntityStateRef = ClassLookup.of(CraftBlockEntityState.class).searchField("tileEntity", "tileEntity"); - private final ClassLookup craftItemStackRef = ClassLookup.of(CraftItemStack.class).searchField("handle", "handle"); - private final ClassLookup craftMetaSkullRef = ClassLookup.of("org.bukkit.craftbukkit.v1_15_R1.inventory.CraftMetaSkull") - .searchField("serialized", "serializedProfile").searchField("profile", "profile"); + private final ClassLookup craftEntityStateRef; + private final ClassLookup craftItemStackRef; + private final ClassLookup craftMetaSkullRef; protected TextureProvider1_15_R1(VersionControl1_15_R1 versionControl) { super(versionControl); + ClassLookupProvider provider = versionControl.getLookupProvider(); + craftEntityStateRef = provider.createLookup("CraftBlockEntityState", CraftBlockEntityState.class).searchField("tileEntity", + "tileEntity"); + craftItemStackRef = provider.createLookup("CraftItemStack", CraftItemStack.class).searchField("handle", "handle"); + craftMetaSkullRef = provider.createCBLookup("CraftMetaSkull", "inventory.CraftMetaSkull") + .searchField("serialized", "serializedProfile").searchField("profile", "profile"); } @Override public GameProfile profileFromBlock(Block block) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return null; } - TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(block, "tileEntity"); + TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(state, "tileEntity"); return entitySkull.gameProfile; } @@ -91,10 +99,11 @@ public boolean applyItem(org.bukkit.inventory.ItemStack itemStack, GameProfile p @Override public boolean applyBlock(Block block, GameProfile profile) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return false; } - TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(block, "tileEntity"); + TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(state, "tileEntity"); entitySkull.setGameProfile(profile); return true; } diff --git a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_15_R1/data/hook/BukkitContainerAdapterHook1_15_R1.java b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_15_R1/data/hook/BukkitContainerAdapterHook1_15_R1.java index 236deb4..46aee8e 100644 --- a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_15_R1/data/hook/BukkitContainerAdapterHook1_15_R1.java +++ b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_15_R1/data/hook/BukkitContainerAdapterHook1_15_R1.java @@ -14,7 +14,7 @@ import net.sourcewriters.minecraft.vcompat.data.api.IDataContainer; import net.sourcewriters.minecraft.vcompat.data.nbt.NbtContainer; import net.sourcewriters.minecraft.vcompat.shaded.syntaxapi.nbt.NbtCompound; - +import net.sourcewriters.minecraft.vcompat.provider.lookup.ClassLookupProvider; import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup; import net.minecraft.server.v1_15_R1.NBTTagCompound; @@ -31,13 +31,16 @@ public final class BukkitContainerAdapterHook1_15_R1 { private static final BukkitContainerAdapterHook1_15_R1 HOOK = new BukkitContainerAdapterHook1_15_R1(); - private final ClassLookup registryRef = ClassLookup.of(CraftPersistentDataTypeRegistry.class) - .searchMethod("create", "createAdapter", Class.class, Class.class, Function.class, Function.class) - .searchField("adapters", "adapters") - .searchField("function", "CREATE_ADAPTER"); - private final ClassLookup entityRef = ClassLookup.of(CraftEntity.class).searchField("registry", "DATA_TYPE_REGISTRY"); + private final ClassLookup registryRef; + private final ClassLookup entityRef; - private BukkitContainerAdapterHook1_15_R1() {} + private BukkitContainerAdapterHook1_15_R1() { + ClassLookupProvider provider = VersionCompatProvider.get().getLookupProvider(); + registryRef = provider.createLookup("CraftPersistentDataTypeRegistry", CraftPersistentDataTypeRegistry.class) + .searchMethod("create", "createAdapter", Class.class, Class.class, Function.class, Function.class) + .searchField("adapters", "adapters").searchField("function", "CREATE_ADAPTER"); + entityRef = provider.createLookup("CraftEntity", CraftEntity.class).searchField("registry", "DATA_TYPE_REGISTRY"); + } private final HashMap map = new HashMap<>(); @@ -60,7 +63,7 @@ private void inject(CraftPersistentDataTypeRegistry registry) { return; } map.put(registry, (Function) registryRef.getFieldValue(registry, "function")); - Function function = clazz -> createAdapter(registry, registryRef.getMethod("create").type().returnType(), (Class) clazz); + Function function = clazz -> createAdapter(registry, registryRef.getMethod("create").getReturnType(), (Class) clazz); registryRef.setFieldValue(registry, "function", function); } @@ -90,7 +93,8 @@ private NBTTagCompound toPrimitive(WrappedContainer input) { } if (handle instanceof IDataContainer) { if (handle instanceof NbtContainer) { - return (NBTTagCompound) VersionCompatProvider.get().getControl().getBukkitConversion().toMinecraftCompound(((NbtContainer) handle).asNbt()); + return (NBTTagCompound) VersionCompatProvider.get().getControl().getBukkitConversion() + .toMinecraftCompound(((NbtContainer) handle).asNbt()); } throw new IllegalArgumentException( "Expected 'CraftPersistentDataContainer' got '" + handle.getClass().getSimpleName() + " instead'!"); diff --git a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_15_R1/tools/BlockTools1_15_R1.java b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_15_R1/tools/BlockTools1_15_R1.java index bfdf2a2..212c274 100644 --- a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_15_R1/tools/BlockTools1_15_R1.java +++ b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_15_R1/tools/BlockTools1_15_R1.java @@ -1,11 +1,13 @@ package net.sourcewriters.minecraft.vcompat.provider.impl.v1_15_R1.tools; import org.bukkit.block.Block; +import org.bukkit.block.BlockState; import org.bukkit.craftbukkit.v1_15_R1.block.CraftSkull; import com.mojang.authlib.properties.Property; import com.mojang.authlib.properties.PropertyMap; +import net.sourcewriters.minecraft.vcompat.VersionCompatProvider; import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup; import net.minecraft.server.v1_15_R1.TileEntitySkull; @@ -14,14 +16,20 @@ public class BlockTools1_15_R1 extends BlockTools { - private final ClassLookup craftEntityStateRef = ClassLookup.of(CraftSkull.class).searchField("tileEntity", "tileEntity"); + private final ClassLookup craftEntityStateRef; + + public BlockTools1_15_R1() { + craftEntityStateRef = VersionCompatProvider.get().getLookupProvider().createLookup("CraftSkull", CraftSkull.class) + .searchField("tileEntity", "tileEntity"); + } @Override public void setHeadTexture(Block block, String texture) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return; } - TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(block, "tileEntity"); + TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(state, "tileEntity"); PropertyMap map = entitySkull.gameProfile.getProperties(); map.removeAll("textures"); map.put("textures", new Property("textures", MinecraftConstants.TEXTURE_SIGNATURE, texture)); @@ -29,10 +37,11 @@ public void setHeadTexture(Block block, String texture) { @Override public String getHeadTexture(Block block) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return null; } - TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(block, "tileEntity"); + TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(state, "tileEntity"); return entitySkull.gameProfile.getProperties().get("textures").iterator().next().getValue(); } diff --git a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_16_R1/TextureProvider1_16_R1.java b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_16_R1/TextureProvider1_16_R1.java index 91a23c1..b682291 100644 --- a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_16_R1/TextureProvider1_16_R1.java +++ b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_16_R1/TextureProvider1_16_R1.java @@ -2,6 +2,7 @@ import org.bukkit.Material; import org.bukkit.block.Block; +import org.bukkit.block.BlockState; import org.bukkit.craftbukkit.v1_16_R1.block.CraftBlockEntityState; import org.bukkit.craftbukkit.v1_16_R1.block.CraftSkull; import org.bukkit.craftbukkit.v1_16_R1.inventory.CraftItemStack; @@ -10,6 +11,7 @@ import com.mojang.authlib.GameProfile; +import net.sourcewriters.minecraft.vcompat.provider.lookup.ClassLookupProvider; import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup; import net.minecraft.server.v1_16_R1.GameProfileSerializer; @@ -20,21 +22,27 @@ public class TextureProvider1_16_R1 extends TextureProvider { - private final ClassLookup craftEntityStateRef = ClassLookup.of(CraftBlockEntityState.class).searchField("tileEntity", "tileEntity"); - private final ClassLookup craftItemStackRef = ClassLookup.of(CraftItemStack.class).searchField("handle", "handle"); - private final ClassLookup craftMetaSkullRef = ClassLookup.of("org.bukkit.craftbukkit.v1_16_R1.inventory.CraftMetaSkull") - .searchField("serialized", "serializedProfile").searchField("profile", "profile"); + private final ClassLookup craftEntityStateRef; + private final ClassLookup craftItemStackRef; + private final ClassLookup craftMetaSkullRef; protected TextureProvider1_16_R1(VersionControl1_16_R1 versionControl) { super(versionControl); + ClassLookupProvider provider = versionControl.getLookupProvider(); + craftEntityStateRef = provider.createLookup("CraftBlockEntityState", CraftBlockEntityState.class).searchField("tileEntity", + "tileEntity"); + craftItemStackRef = provider.createLookup("CraftItemStack", CraftItemStack.class).searchField("handle", "handle"); + craftMetaSkullRef = provider.createCBLookup("CraftMetaSkull", "inventory.CraftMetaSkull") + .searchField("serialized", "serializedProfile").searchField("profile", "profile"); } @Override public GameProfile profileFromBlock(Block block) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return null; } - TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(block, "tileEntity"); + TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(state, "tileEntity"); return entitySkull.gameProfile; } @@ -91,10 +99,11 @@ public boolean applyItem(org.bukkit.inventory.ItemStack itemStack, GameProfile p @Override public boolean applyBlock(Block block, GameProfile profile) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return false; } - TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(block, "tileEntity"); + TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(state, "tileEntity"); entitySkull.setGameProfile(profile); return true; } diff --git a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_16_R1/data/hook/BukkitContainerAdapterHook1_16_R1.java b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_16_R1/data/hook/BukkitContainerAdapterHook1_16_R1.java index 26c0bb6..aff71c5 100644 --- a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_16_R1/data/hook/BukkitContainerAdapterHook1_16_R1.java +++ b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_16_R1/data/hook/BukkitContainerAdapterHook1_16_R1.java @@ -14,7 +14,7 @@ import net.sourcewriters.minecraft.vcompat.data.api.IDataContainer; import net.sourcewriters.minecraft.vcompat.data.nbt.NbtContainer; import net.sourcewriters.minecraft.vcompat.shaded.syntaxapi.nbt.NbtCompound; - +import net.sourcewriters.minecraft.vcompat.provider.lookup.ClassLookupProvider; import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup; import net.minecraft.server.v1_16_R1.NBTTagCompound; @@ -31,13 +31,16 @@ public final class BukkitContainerAdapterHook1_16_R1 { private static final BukkitContainerAdapterHook1_16_R1 HOOK = new BukkitContainerAdapterHook1_16_R1(); - private final ClassLookup registryRef = ClassLookup.of(CraftPersistentDataTypeRegistry.class) - .searchMethod("create", "createAdapter", Class.class, Class.class, Function.class, Function.class) - .searchField("adapters", "adapters") - .searchField("function", "CREATE_ADAPTER"); - private final ClassLookup entityRef = ClassLookup.of(CraftEntity.class).searchField("registry", "DATA_TYPE_REGISTRY"); + private final ClassLookup registryRef; + private final ClassLookup entityRef; - private BukkitContainerAdapterHook1_16_R1() {} + private BukkitContainerAdapterHook1_16_R1() { + ClassLookupProvider provider = VersionCompatProvider.get().getLookupProvider(); + registryRef = provider.createLookup("CraftPersistentDataTypeRegistry", CraftPersistentDataTypeRegistry.class) + .searchMethod("create", "createAdapter", Class.class, Class.class, Function.class, Function.class) + .searchField("adapters", "adapters").searchField("function", "CREATE_ADAPTER"); + entityRef = provider.createLookup("CraftEntity", CraftEntity.class).searchField("registry", "DATA_TYPE_REGISTRY"); + } private final HashMap map = new HashMap<>(); @@ -60,7 +63,7 @@ private void inject(CraftPersistentDataTypeRegistry registry) { return; } map.put(registry, (Function) registryRef.getFieldValue(registry, "function")); - Function function = clazz -> createAdapter(registry, registryRef.getMethod("create").type().returnType(), (Class) clazz); + Function function = clazz -> createAdapter(registry, registryRef.getMethod("create").getReturnType(), (Class) clazz); registryRef.setFieldValue(registry, "function", function); } @@ -90,7 +93,8 @@ private NBTTagCompound toPrimitive(WrappedContainer input) { } if (handle instanceof IDataContainer) { if (handle instanceof NbtContainer) { - return (NBTTagCompound) VersionCompatProvider.get().getControl().getBukkitConversion().toMinecraftCompound(((NbtContainer) handle).asNbt()); + return (NBTTagCompound) VersionCompatProvider.get().getControl().getBukkitConversion() + .toMinecraftCompound(((NbtContainer) handle).asNbt()); } throw new IllegalArgumentException( "Expected 'CraftPersistentDataContainer' got '" + handle.getClass().getSimpleName() + " instead'!"); diff --git a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_16_R1/tools/BlockTools1_16_R1.java b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_16_R1/tools/BlockTools1_16_R1.java index 012e4a7..81dd405 100644 --- a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_16_R1/tools/BlockTools1_16_R1.java +++ b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_16_R1/tools/BlockTools1_16_R1.java @@ -1,11 +1,13 @@ package net.sourcewriters.minecraft.vcompat.provider.impl.v1_16_R1.tools; import org.bukkit.block.Block; +import org.bukkit.block.BlockState; import org.bukkit.craftbukkit.v1_16_R1.block.CraftSkull; import com.mojang.authlib.properties.Property; import com.mojang.authlib.properties.PropertyMap; +import net.sourcewriters.minecraft.vcompat.VersionCompatProvider; import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup; import net.minecraft.server.v1_16_R1.TileEntitySkull; @@ -14,14 +16,20 @@ public class BlockTools1_16_R1 extends BlockTools { - private final ClassLookup craftEntityStateRef = ClassLookup.of(CraftSkull.class).searchField("tileEntity", "tileEntity"); + private final ClassLookup craftEntityStateRef; + + public BlockTools1_16_R1() { + craftEntityStateRef = VersionCompatProvider.get().getLookupProvider().createLookup("CraftSkull", CraftSkull.class) + .searchField("tileEntity", "tileEntity"); + } @Override public void setHeadTexture(Block block, String texture) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return; } - TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(block, "tileEntity"); + TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(state, "tileEntity"); PropertyMap map = entitySkull.gameProfile.getProperties(); map.removeAll("textures"); map.put("textures", new Property("textures", MinecraftConstants.TEXTURE_SIGNATURE, texture)); @@ -29,10 +37,11 @@ public void setHeadTexture(Block block, String texture) { @Override public String getHeadTexture(Block block) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return null; } - TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(block, "tileEntity"); + TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(state, "tileEntity"); return entitySkull.gameProfile.getProperties().get("textures").iterator().next().getValue(); } diff --git a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_16_R2/TextureProvider1_16_R2.java b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_16_R2/TextureProvider1_16_R2.java index cc7d1dc..159c465 100644 --- a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_16_R2/TextureProvider1_16_R2.java +++ b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_16_R2/TextureProvider1_16_R2.java @@ -2,6 +2,7 @@ import org.bukkit.Material; import org.bukkit.block.Block; +import org.bukkit.block.BlockState; import org.bukkit.craftbukkit.v1_16_R2.block.CraftBlockEntityState; import org.bukkit.craftbukkit.v1_16_R2.block.CraftSkull; import org.bukkit.craftbukkit.v1_16_R2.inventory.CraftItemStack; @@ -10,6 +11,7 @@ import com.mojang.authlib.GameProfile; +import net.sourcewriters.minecraft.vcompat.provider.lookup.ClassLookupProvider; import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup; import net.minecraft.server.v1_16_R2.GameProfileSerializer; @@ -20,21 +22,27 @@ public class TextureProvider1_16_R2 extends TextureProvider { - private final ClassLookup craftEntityStateRef = ClassLookup.of(CraftBlockEntityState.class).searchField("tileEntity", "tileEntity"); - private final ClassLookup craftItemStackRef = ClassLookup.of(CraftItemStack.class).searchField("handle", "handle"); - private final ClassLookup craftMetaSkullRef = ClassLookup.of("org.bukkit.craftbukkit.v1_16_R2.inventory.CraftMetaSkull") - .searchField("serialized", "serializedProfile").searchField("profile", "profile"); + private final ClassLookup craftEntityStateRef; + private final ClassLookup craftItemStackRef; + private final ClassLookup craftMetaSkullRef; protected TextureProvider1_16_R2(VersionControl1_16_R2 versionControl) { super(versionControl); + ClassLookupProvider provider = versionControl.getLookupProvider(); + craftEntityStateRef = provider.createLookup("CraftBlockEntityState", CraftBlockEntityState.class).searchField("tileEntity", + "tileEntity"); + craftItemStackRef = provider.createLookup("CraftItemStack", CraftItemStack.class).searchField("handle", "handle"); + craftMetaSkullRef = provider.createCBLookup("CraftMetaSkull", "inventory.CraftMetaSkull") + .searchField("serialized", "serializedProfile").searchField("profile", "profile"); } @Override public GameProfile profileFromBlock(Block block) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return null; } - TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(block, "tileEntity"); + TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(state, "tileEntity"); return entitySkull.gameProfile; } @@ -91,10 +99,11 @@ public boolean applyItem(org.bukkit.inventory.ItemStack itemStack, GameProfile p @Override public boolean applyBlock(Block block, GameProfile profile) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return false; } - TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(block, "tileEntity"); + TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(state, "tileEntity"); entitySkull.setGameProfile(profile); return true; } diff --git a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_16_R2/data/hook/BukkitContainerAdapterHook1_16_R2.java b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_16_R2/data/hook/BukkitContainerAdapterHook1_16_R2.java index 1e320cb..67a9eb0 100644 --- a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_16_R2/data/hook/BukkitContainerAdapterHook1_16_R2.java +++ b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_16_R2/data/hook/BukkitContainerAdapterHook1_16_R2.java @@ -14,7 +14,7 @@ import net.sourcewriters.minecraft.vcompat.data.api.IDataContainer; import net.sourcewriters.minecraft.vcompat.data.nbt.NbtContainer; import net.sourcewriters.minecraft.vcompat.shaded.syntaxapi.nbt.NbtCompound; - +import net.sourcewriters.minecraft.vcompat.provider.lookup.ClassLookupProvider; import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup; import net.minecraft.server.v1_16_R2.NBTTagCompound; @@ -31,13 +31,16 @@ public final class BukkitContainerAdapterHook1_16_R2 { private static final BukkitContainerAdapterHook1_16_R2 HOOK = new BukkitContainerAdapterHook1_16_R2(); - private final ClassLookup registryRef = ClassLookup.of(CraftPersistentDataTypeRegistry.class) - .searchMethod("create", "createAdapter", Class.class, Class.class, Function.class, Function.class) - .searchField("adapters", "adapters") - .searchField("function", "CREATE_ADAPTER"); - private final ClassLookup entityRef = ClassLookup.of(CraftEntity.class).searchField("registry", "DATA_TYPE_REGISTRY"); + private final ClassLookup registryRef; + private final ClassLookup entityRef; - private BukkitContainerAdapterHook1_16_R2() {} + private BukkitContainerAdapterHook1_16_R2() { + ClassLookupProvider provider = VersionCompatProvider.get().getLookupProvider(); + registryRef = provider.createLookup("CraftPersistentDataTypeRegistry", CraftPersistentDataTypeRegistry.class) + .searchMethod("create", "createAdapter", Class.class, Class.class, Function.class, Function.class) + .searchField("adapters", "adapters").searchField("function", "CREATE_ADAPTER"); + entityRef = provider.createLookup("CraftEntity", CraftEntity.class).searchField("registry", "DATA_TYPE_REGISTRY"); + } private final HashMap map = new HashMap<>(); @@ -60,7 +63,7 @@ private void inject(CraftPersistentDataTypeRegistry registry) { return; } map.put(registry, (Function) registryRef.getFieldValue(registry, "function")); - Function function = clazz -> createAdapter(registry, registryRef.getMethod("create").type().returnType(), (Class) clazz); + Function function = clazz -> createAdapter(registry, registryRef.getMethod("create").getReturnType(), (Class) clazz); registryRef.setFieldValue(registry, "function", function); } diff --git a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_16_R2/tools/BlockTools1_16_R2.java b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_16_R2/tools/BlockTools1_16_R2.java index 3804321..b3b51f7 100644 --- a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_16_R2/tools/BlockTools1_16_R2.java +++ b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_16_R2/tools/BlockTools1_16_R2.java @@ -1,11 +1,13 @@ package net.sourcewriters.minecraft.vcompat.provider.impl.v1_16_R2.tools; import org.bukkit.block.Block; +import org.bukkit.block.BlockState; import org.bukkit.craftbukkit.v1_16_R2.block.CraftSkull; import com.mojang.authlib.properties.Property; import com.mojang.authlib.properties.PropertyMap; +import net.sourcewriters.minecraft.vcompat.VersionCompatProvider; import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup; import net.minecraft.server.v1_16_R2.TileEntitySkull; @@ -14,14 +16,20 @@ public class BlockTools1_16_R2 extends BlockTools { - private final ClassLookup craftEntityStateRef = ClassLookup.of(CraftSkull.class).searchField("tileEntity", "tileEntity"); + private final ClassLookup craftEntityStateRef; + + public BlockTools1_16_R2() { + craftEntityStateRef = VersionCompatProvider.get().getLookupProvider().createLookup("CraftSkull", CraftSkull.class) + .searchField("tileEntity", "tileEntity"); + } @Override public void setHeadTexture(Block block, String texture) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return; } - TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(block, "tileEntity"); + TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(state, "tileEntity"); PropertyMap map = entitySkull.gameProfile.getProperties(); map.removeAll("textures"); map.put("textures", new Property("textures", MinecraftConstants.TEXTURE_SIGNATURE, texture)); @@ -29,10 +37,11 @@ public void setHeadTexture(Block block, String texture) { @Override public String getHeadTexture(Block block) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return null; } - TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(block, "tileEntity"); + TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(state, "tileEntity"); return entitySkull.gameProfile.getProperties().get("textures").iterator().next().getValue(); } diff --git a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_16_R3/TextureProvider1_16_R3.java b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_16_R3/TextureProvider1_16_R3.java index 4a2f7a7..e2b4db4 100644 --- a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_16_R3/TextureProvider1_16_R3.java +++ b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_16_R3/TextureProvider1_16_R3.java @@ -2,6 +2,7 @@ import org.bukkit.Material; import org.bukkit.block.Block; +import org.bukkit.block.BlockState; import org.bukkit.craftbukkit.v1_16_R3.block.CraftBlockEntityState; import org.bukkit.craftbukkit.v1_16_R3.block.CraftSkull; import org.bukkit.craftbukkit.v1_16_R3.inventory.CraftItemStack; @@ -10,6 +11,7 @@ import com.mojang.authlib.GameProfile; +import net.sourcewriters.minecraft.vcompat.provider.lookup.ClassLookupProvider; import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup; import net.minecraft.server.v1_16_R3.GameProfileSerializer; @@ -20,21 +22,27 @@ public class TextureProvider1_16_R3 extends TextureProvider { - private final ClassLookup craftEntityStateRef = ClassLookup.of(CraftBlockEntityState.class).searchField("tileEntity", "tileEntity"); - private final ClassLookup craftItemStackRef = ClassLookup.of(CraftItemStack.class).searchField("handle", "handle"); - private final ClassLookup craftMetaSkullRef = ClassLookup.of("org.bukkit.craftbukkit.v1_16_R3.inventory.CraftMetaSkull") - .searchField("serialized", "serializedProfile").searchField("profile", "profile"); + private final ClassLookup craftEntityStateRef; + private final ClassLookup craftItemStackRef; + private final ClassLookup craftMetaSkullRef; protected TextureProvider1_16_R3(VersionControl1_16_R3 versionControl) { super(versionControl); + ClassLookupProvider provider = versionControl.getLookupProvider(); + craftEntityStateRef = provider.createLookup("CraftBlockEntityState", CraftBlockEntityState.class).searchField("tileEntity", + "tileEntity"); + craftItemStackRef = provider.createLookup("CraftItemStack", CraftItemStack.class).searchField("handle", "handle"); + craftMetaSkullRef = provider.createCBLookup("CraftMetaSkull", "inventory.CraftMetaSkull") + .searchField("serialized", "serializedProfile").searchField("profile", "profile"); } @Override public GameProfile profileFromBlock(Block block) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return null; } - TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(block, "tileEntity"); + TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(state, "tileEntity"); return entitySkull.gameProfile; } @@ -91,10 +99,11 @@ public boolean applyItem(org.bukkit.inventory.ItemStack itemStack, GameProfile p @Override public boolean applyBlock(Block block, GameProfile profile) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return false; } - TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(block, "tileEntity"); + TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(state, "tileEntity"); entitySkull.setGameProfile(profile); return true; } diff --git a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_16_R3/data/hook/BukkitContainerAdapterHook1_16_R3.java b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_16_R3/data/hook/BukkitContainerAdapterHook1_16_R3.java index bceb96f..0d1fead 100644 --- a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_16_R3/data/hook/BukkitContainerAdapterHook1_16_R3.java +++ b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_16_R3/data/hook/BukkitContainerAdapterHook1_16_R3.java @@ -14,7 +14,7 @@ import net.sourcewriters.minecraft.vcompat.data.api.IDataContainer; import net.sourcewriters.minecraft.vcompat.data.nbt.NbtContainer; import net.sourcewriters.minecraft.vcompat.shaded.syntaxapi.nbt.NbtCompound; - +import net.sourcewriters.minecraft.vcompat.provider.lookup.ClassLookupProvider; import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup; import net.minecraft.server.v1_16_R3.NBTTagCompound; @@ -30,14 +30,16 @@ public final class BukkitContainerAdapterHook1_16_R3 { private static final BukkitContainerAdapterHook1_16_R3 HOOK = new BukkitContainerAdapterHook1_16_R3(); + + private final ClassLookup registryRef; + private final ClassLookup entityRef; - private final ClassLookup registryRef = ClassLookup.of(CraftPersistentDataTypeRegistry.class) - .searchMethod("create", "createAdapter", Class.class, Class.class, Function.class, Function.class) - .searchField("adapters", "adapters") - .searchField("function", "CREATE_ADAPTER"); - private final ClassLookup entityRef = ClassLookup.of(CraftEntity.class).searchField("registry", "DATA_TYPE_REGISTRY"); - - private BukkitContainerAdapterHook1_16_R3() {} + private BukkitContainerAdapterHook1_16_R3() { + ClassLookupProvider provider = VersionCompatProvider.get().getLookupProvider(); + registryRef = provider.createLookup("CraftPersistentDataTypeRegistry", CraftPersistentDataTypeRegistry.class) + .searchMethod("create", "createAdapter", Class.class, Class.class, Function.class, Function.class) + .searchField("adapters", "adapters").searchField("function", "CREATE_ADAPTER"); + entityRef = provider.createLookup("CraftEntity", CraftEntity.class).searchField("registry", "DATA_TYPE_REGISTRY");} private final HashMap map = new HashMap<>(); @@ -60,7 +62,7 @@ private void inject(CraftPersistentDataTypeRegistry registry) { return; } map.put(registry, (Function) registryRef.getFieldValue(registry, "function")); - Function function = clazz -> createAdapter(registry, registryRef.getMethod("create").type().returnType(), (Class) clazz); + Function function = clazz -> createAdapter(registry, registryRef.getMethod("create").getReturnType(), (Class) clazz); registryRef.setFieldValue(registry, "function", function); } diff --git a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_16_R3/tools/BlockTools1_16_R3.java b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_16_R3/tools/BlockTools1_16_R3.java index 2d5378d..f3d06b6 100644 --- a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_16_R3/tools/BlockTools1_16_R3.java +++ b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_16_R3/tools/BlockTools1_16_R3.java @@ -1,11 +1,13 @@ package net.sourcewriters.minecraft.vcompat.provider.impl.v1_16_R3.tools; import org.bukkit.block.Block; +import org.bukkit.block.BlockState; import org.bukkit.craftbukkit.v1_16_R3.block.CraftSkull; import com.mojang.authlib.properties.Property; import com.mojang.authlib.properties.PropertyMap; +import net.sourcewriters.minecraft.vcompat.VersionCompatProvider; import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup; import net.minecraft.server.v1_16_R3.TileEntitySkull; @@ -14,14 +16,20 @@ public class BlockTools1_16_R3 extends BlockTools { - private final ClassLookup craftEntityStateRef = ClassLookup.of(CraftSkull.class).searchField("tileEntity", "tileEntity"); + private final ClassLookup craftEntityStateRef; + + public BlockTools1_16_R3() { + craftEntityStateRef = VersionCompatProvider.get().getLookupProvider().createLookup("CraftSkull", CraftSkull.class) + .searchField("tileEntity", "tileEntity"); + } @Override public void setHeadTexture(Block block, String texture) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return; } - TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(block, "tileEntity"); + TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(state, "tileEntity"); PropertyMap map = entitySkull.gameProfile.getProperties(); map.removeAll("textures"); map.put("textures", new Property("textures", MinecraftConstants.TEXTURE_SIGNATURE, texture)); @@ -29,10 +37,11 @@ public void setHeadTexture(Block block, String texture) { @Override public String getHeadTexture(Block block) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return null; } - TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(block, "tileEntity"); + TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(state, "tileEntity"); return entitySkull.gameProfile.getProperties().get("textures").iterator().next().getValue(); } diff --git a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_8_R1/TextureProvider1_8_R1.java b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_8_R1/TextureProvider1_8_R1.java index a115c63..1902499 100644 --- a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_8_R1/TextureProvider1_8_R1.java +++ b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_8_R1/TextureProvider1_8_R1.java @@ -2,6 +2,7 @@ import org.bukkit.Material; import org.bukkit.block.Block; +import org.bukkit.block.BlockState; import org.bukkit.craftbukkit.v1_8_R1.block.CraftSkull; import org.bukkit.craftbukkit.v1_8_R1.inventory.CraftItemStack; import org.bukkit.inventory.meta.ItemMeta; @@ -10,6 +11,7 @@ import com.mojang.authlib.GameProfile; +import net.sourcewriters.minecraft.vcompat.provider.lookup.ClassLookupProvider; import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup; import net.minecraft.server.v1_8_R1.GameProfileSerializer; @@ -21,22 +23,27 @@ @SuppressWarnings("deprecation") public class TextureProvider1_8_R1 extends TextureProvider { - private final ClassLookup craftItemStackRef = ClassLookup.of(CraftItemStack.class).searchField("handle", "handle"); - private final ClassLookup craftMetaSkullRef = ClassLookup.of("org.bukkit.craftbukkit.v1_8_R1.inventory.CraftMetaSkull") - .searchField("serialized", "serializedProfile").searchField("profile", "profile"); - private final ClassLookup craftSkullRef = ClassLookup.of(CraftSkull.class).searchField("tileEntity", "skull"); + private final ClassLookup craftItemStackRef; + private final ClassLookup craftMetaSkullRef; + private final ClassLookup craftSkullRef; private final Material skullMaterial = Material.valueOf("SKULL"); protected TextureProvider1_8_R1(VersionControl1_8_R1 versionControl) { super(versionControl); + ClassLookupProvider provider = versionControl.getLookupProvider(); + craftSkullRef = provider.createLookup("CraftSkull", CraftSkull.class).searchField("tileEntity", "skull"); + craftItemStackRef = provider.createLookup("CraftItemStack", CraftItemStack.class).searchField("handle", "handle"); + craftMetaSkullRef = provider.createCBLookup("CraftMetaSkull", "inventory.CraftMetaSkull") + .searchField("serialized", "serializedProfile").searchField("profile", "profile"); } @Override public GameProfile profileFromBlock(Block block) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return null; } - return ((TileEntitySkull) craftSkullRef.getFieldValue(block, "tileEntity")).getGameProfile(); + return ((TileEntitySkull) craftSkullRef.getFieldValue(state, "tileEntity")).getGameProfile(); } @Override @@ -97,10 +104,11 @@ public boolean applyItem(org.bukkit.inventory.ItemStack itemStack, GameProfile p @Override public boolean applyBlock(Block block, GameProfile profile) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return false; } - ((TileEntitySkull) craftSkullRef.getFieldValue(block, "tileEntity")).setGameProfile(profile); + ((TileEntitySkull) craftSkullRef.getFieldValue(state, "tileEntity")).setGameProfile(profile); return true; } diff --git a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_8_R1/tools/BlockTools1_8_R1.java b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_8_R1/tools/BlockTools1_8_R1.java index 2354abb..bfbe977 100644 --- a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_8_R1/tools/BlockTools1_8_R1.java +++ b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_8_R1/tools/BlockTools1_8_R1.java @@ -1,11 +1,13 @@ package net.sourcewriters.minecraft.vcompat.provider.impl.v1_8_R1.tools; import org.bukkit.block.Block; +import org.bukkit.block.BlockState; import org.bukkit.craftbukkit.v1_8_R1.block.CraftSkull; import com.mojang.authlib.properties.Property; import com.mojang.authlib.properties.PropertyMap; +import net.sourcewriters.minecraft.vcompat.VersionCompatProvider; import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup; import net.minecraft.server.v1_8_R1.TileEntitySkull; @@ -14,14 +16,20 @@ public class BlockTools1_8_R1 extends BlockTools { - private final ClassLookup craftEntityStateRef = ClassLookup.of(CraftSkull.class).searchField("tileEntity", "tileEntity"); + private final ClassLookup craftEntityStateRef; + + public BlockTools1_8_R1() { + craftEntityStateRef = VersionCompatProvider.get().getLookupProvider().createLookup("CraftSkull", CraftSkull.class) + .searchField("tileEntity", "skull"); + } @Override public void setHeadTexture(Block block, String texture) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return; } - TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(block, "tileEntity"); + TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(state, "tileEntity"); PropertyMap map = entitySkull.getGameProfile().getProperties(); map.removeAll("textures"); map.put("textures", new Property("textures", MinecraftConstants.TEXTURE_SIGNATURE, texture)); @@ -29,10 +37,11 @@ public void setHeadTexture(Block block, String texture) { @Override public String getHeadTexture(Block block) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return null; } - TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(block, "tileEntity"); + TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(state, "tileEntity"); return entitySkull.getGameProfile().getProperties().get("textures").iterator().next().getValue(); } diff --git a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_8_R2/TextureProvider1_8_R2.java b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_8_R2/TextureProvider1_8_R2.java index a03b96b..f1e946c 100644 --- a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_8_R2/TextureProvider1_8_R2.java +++ b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_8_R2/TextureProvider1_8_R2.java @@ -2,6 +2,7 @@ import org.bukkit.Material; import org.bukkit.block.Block; +import org.bukkit.block.BlockState; import org.bukkit.craftbukkit.v1_8_R2.block.CraftSkull; import org.bukkit.craftbukkit.v1_8_R2.inventory.CraftItemStack; import org.bukkit.inventory.meta.ItemMeta; @@ -10,6 +11,7 @@ import com.mojang.authlib.GameProfile; +import net.sourcewriters.minecraft.vcompat.provider.lookup.ClassLookupProvider; import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup; import net.minecraft.server.v1_8_R2.GameProfileSerializer; @@ -20,21 +22,25 @@ @SuppressWarnings("deprecation") public class TextureProvider1_8_R2 extends TextureProvider { - private final ClassLookup craftItemStackRef = ClassLookup.of(CraftItemStack.class).searchField("handle", "handle"); - private final ClassLookup craftMetaSkullRef = ClassLookup.of("org.bukkit.craftbukkit.v1_8_R2.inventory.CraftMetaSkull") - .searchField("serialized", "serializedProfile").searchField("profile", "profile"); + private final ClassLookup craftItemStackRef; + private final ClassLookup craftMetaSkullRef; private final Material skullMaterial = Material.valueOf("SKULL"); protected TextureProvider1_8_R2(VersionControl1_8_R2 versionControl) { super(versionControl); + ClassLookupProvider provider = versionControl.getLookupProvider(); + craftItemStackRef = provider.createLookup("CraftItemStack", CraftItemStack.class).searchField("handle", "handle"); + craftMetaSkullRef = provider.createCBLookup("CraftMetaSkull", "inventory.CraftMetaSkull") + .searchField("serialized", "serializedProfile").searchField("profile", "profile"); } @Override public GameProfile profileFromBlock(Block block) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return null; } - return ((CraftSkull) block).getTileEntity().getGameProfile(); + return ((CraftSkull) state).getTileEntity().getGameProfile(); } @Override @@ -95,10 +101,11 @@ public boolean applyItem(org.bukkit.inventory.ItemStack itemStack, GameProfile p @Override public boolean applyBlock(Block block, GameProfile profile) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return false; } - ((CraftSkull) block).getTileEntity().setGameProfile(profile); + ((CraftSkull) state).getTileEntity().setGameProfile(profile); return true; } diff --git a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_8_R2/tools/BlockTools1_8_R2.java b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_8_R2/tools/BlockTools1_8_R2.java index a8fce17..c3d1288 100644 --- a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_8_R2/tools/BlockTools1_8_R2.java +++ b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_8_R2/tools/BlockTools1_8_R2.java @@ -1,27 +1,25 @@ package net.sourcewriters.minecraft.vcompat.provider.impl.v1_8_R2.tools; import org.bukkit.block.Block; +import org.bukkit.block.BlockState; import org.bukkit.craftbukkit.v1_8_R2.block.CraftSkull; import com.mojang.authlib.properties.Property; import com.mojang.authlib.properties.PropertyMap; -import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup; - import net.minecraft.server.v1_8_R2.TileEntitySkull; import net.sourcewriters.minecraft.vcompat.provider.tools.BlockTools; import net.sourcewriters.minecraft.vcompat.util.constants.MinecraftConstants; public class BlockTools1_8_R2 extends BlockTools { - private final ClassLookup craftEntityStateRef = ClassLookup.of(CraftSkull.class).searchField("tileEntity", "tileEntity"); - @Override public void setHeadTexture(Block block, String texture) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return; } - TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(block, "tileEntity"); + TileEntitySkull entitySkull = ((CraftSkull) state).getTileEntity(); PropertyMap map = entitySkull.getGameProfile().getProperties(); map.removeAll("textures"); map.put("textures", new Property("textures", MinecraftConstants.TEXTURE_SIGNATURE, texture)); @@ -29,10 +27,11 @@ public void setHeadTexture(Block block, String texture) { @Override public String getHeadTexture(Block block) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return null; } - TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(block, "tileEntity"); + TileEntitySkull entitySkull = ((CraftSkull) state).getTileEntity(); return entitySkull.getGameProfile().getProperties().get("textures").iterator().next().getValue(); } diff --git a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_8_R3/TextureProvider1_8_R3.java b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_8_R3/TextureProvider1_8_R3.java index 2d625de..51c9be5 100644 --- a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_8_R3/TextureProvider1_8_R3.java +++ b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_8_R3/TextureProvider1_8_R3.java @@ -2,6 +2,7 @@ import org.bukkit.Material; import org.bukkit.block.Block; +import org.bukkit.block.BlockState; import org.bukkit.craftbukkit.v1_8_R3.block.CraftSkull; import org.bukkit.craftbukkit.v1_8_R3.inventory.CraftItemStack; import org.bukkit.inventory.meta.ItemMeta; @@ -10,6 +11,7 @@ import com.mojang.authlib.GameProfile; +import net.sourcewriters.minecraft.vcompat.provider.lookup.ClassLookupProvider; import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup; import net.minecraft.server.v1_8_R3.GameProfileSerializer; @@ -20,21 +22,25 @@ @SuppressWarnings("deprecation") public class TextureProvider1_8_R3 extends TextureProvider { - private final ClassLookup craftItemStackRef = ClassLookup.of(CraftItemStack.class).searchField("handle", "handle"); - private final ClassLookup craftMetaSkullRef = ClassLookup.of("org.bukkit.craftbukkit.v1_8_R3.inventory.CraftMetaSkull") - .searchField("serialized", "serializedProfile").searchField("profile", "profile"); + private final ClassLookup craftItemStackRef; + private final ClassLookup craftMetaSkullRef; private final Material skullMaterial = Material.valueOf("SKULL"); protected TextureProvider1_8_R3(VersionControl1_8_R3 versionControl) { super(versionControl); + ClassLookupProvider provider = versionControl.getLookupProvider(); + craftItemStackRef = provider.createLookup("CraftItemStack", CraftItemStack.class).searchField("handle", "handle"); + craftMetaSkullRef = provider.createCBLookup("CraftMetaSkull", "inventory.CraftMetaSkull") + .searchField("serialized", "serializedProfile").searchField("profile", "profile"); } @Override public GameProfile profileFromBlock(Block block) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return null; } - return ((CraftSkull) block).getTileEntity().getGameProfile(); + return ((CraftSkull) state).getTileEntity().getGameProfile(); } @Override @@ -95,10 +101,11 @@ public boolean applyItem(org.bukkit.inventory.ItemStack itemStack, GameProfile p @Override public boolean applyBlock(Block block, GameProfile profile) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return false; } - ((CraftSkull) block).getTileEntity().setGameProfile(profile); + ((CraftSkull) state).getTileEntity().setGameProfile(profile); return true; } diff --git a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_8_R3/tools/BlockTools1_8_R3.java b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_8_R3/tools/BlockTools1_8_R3.java index b64af2d..caa271a 100644 --- a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_8_R3/tools/BlockTools1_8_R3.java +++ b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_8_R3/tools/BlockTools1_8_R3.java @@ -1,27 +1,25 @@ package net.sourcewriters.minecraft.vcompat.provider.impl.v1_8_R3.tools; import org.bukkit.block.Block; +import org.bukkit.block.BlockState; import org.bukkit.craftbukkit.v1_8_R3.block.CraftSkull; import com.mojang.authlib.properties.Property; import com.mojang.authlib.properties.PropertyMap; -import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup; - import net.minecraft.server.v1_8_R3.TileEntitySkull; import net.sourcewriters.minecraft.vcompat.provider.tools.BlockTools; import net.sourcewriters.minecraft.vcompat.util.constants.MinecraftConstants; public class BlockTools1_8_R3 extends BlockTools { - private final ClassLookup craftEntityStateRef = ClassLookup.of(CraftSkull.class).searchField("tileEntity", "tileEntity"); - @Override public void setHeadTexture(Block block, String texture) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return; } - TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(block, "tileEntity"); + TileEntitySkull entitySkull = ((CraftSkull) state).getTileEntity(); PropertyMap map = entitySkull.getGameProfile().getProperties(); map.removeAll("textures"); map.put("textures", new Property("textures", MinecraftConstants.TEXTURE_SIGNATURE, texture)); @@ -29,10 +27,11 @@ public void setHeadTexture(Block block, String texture) { @Override public String getHeadTexture(Block block) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return null; } - TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(block, "tileEntity"); + TileEntitySkull entitySkull = ((CraftSkull) state).getTileEntity(); return entitySkull.getGameProfile().getProperties().get("textures").iterator().next().getValue(); } diff --git a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_9_R1/TextureProvider1_9_R1.java b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_9_R1/TextureProvider1_9_R1.java index 37203b8..de13854 100644 --- a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_9_R1/TextureProvider1_9_R1.java +++ b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_9_R1/TextureProvider1_9_R1.java @@ -2,6 +2,7 @@ import org.bukkit.Material; import org.bukkit.block.Block; +import org.bukkit.block.BlockState; import org.bukkit.craftbukkit.v1_9_R1.block.CraftSkull; import org.bukkit.craftbukkit.v1_9_R1.inventory.CraftItemStack; import org.bukkit.inventory.meta.ItemMeta; @@ -10,6 +11,7 @@ import com.mojang.authlib.GameProfile; +import net.sourcewriters.minecraft.vcompat.provider.lookup.ClassLookupProvider; import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup; import net.minecraft.server.v1_9_R1.GameProfileSerializer; @@ -20,21 +22,25 @@ @SuppressWarnings("deprecation") public class TextureProvider1_9_R1 extends TextureProvider { - private final ClassLookup craftItemStackRef = ClassLookup.of(CraftItemStack.class).searchField("handle", "handle"); - private final ClassLookup craftMetaSkullRef = ClassLookup.of("org.bukkit.craftbukkit.v1_9_R1.inventory.CraftMetaSkull") - .searchField("serialized", "serializedProfile").searchField("profile", "profile"); + private final ClassLookup craftItemStackRef; + private final ClassLookup craftMetaSkullRef; private final Material skullMaterial = Material.valueOf("SKULL"); protected TextureProvider1_9_R1(VersionControl1_9_R1 versionControl) { super(versionControl); + ClassLookupProvider provider = versionControl.getLookupProvider(); + craftItemStackRef = provider.createLookup("CraftItemStack", CraftItemStack.class).searchField("handle", "handle"); + craftMetaSkullRef = provider.createCBLookup("CraftMetaSkull", "inventory.CraftMetaSkull") + .searchField("serialized", "serializedProfile").searchField("profile", "profile"); } @Override public GameProfile profileFromBlock(Block block) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return null; } - return ((CraftSkull) block).getTileEntity().getGameProfile(); + return ((CraftSkull) state).getTileEntity().getGameProfile(); } @Override @@ -95,10 +101,11 @@ public boolean applyItem(org.bukkit.inventory.ItemStack itemStack, GameProfile p @Override public boolean applyBlock(Block block, GameProfile profile) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return false; } - ((CraftSkull) block).getTileEntity().setGameProfile(profile); + ((CraftSkull) state).getTileEntity().setGameProfile(profile); return true; } diff --git a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_9_R1/tools/BlockTools1_9_R1.java b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_9_R1/tools/BlockTools1_9_R1.java index 2a46bd8..e211bbe 100644 --- a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_9_R1/tools/BlockTools1_9_R1.java +++ b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_9_R1/tools/BlockTools1_9_R1.java @@ -1,27 +1,25 @@ package net.sourcewriters.minecraft.vcompat.provider.impl.v1_9_R1.tools; import org.bukkit.block.Block; +import org.bukkit.block.BlockState; import org.bukkit.craftbukkit.v1_9_R1.block.CraftSkull; import com.mojang.authlib.properties.Property; import com.mojang.authlib.properties.PropertyMap; -import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup; - import net.minecraft.server.v1_9_R1.TileEntitySkull; import net.sourcewriters.minecraft.vcompat.provider.tools.BlockTools; import net.sourcewriters.minecraft.vcompat.util.constants.MinecraftConstants; public class BlockTools1_9_R1 extends BlockTools { - private final ClassLookup craftEntityStateRef = ClassLookup.of(CraftSkull.class).searchField("tileEntity", "tileEntity"); - @Override public void setHeadTexture(Block block, String texture) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return; } - TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(block, "tileEntity"); + TileEntitySkull entitySkull = ((CraftSkull) state).getTileEntity(); PropertyMap map = entitySkull.getGameProfile().getProperties(); map.removeAll("textures"); map.put("textures", new Property("textures", MinecraftConstants.TEXTURE_SIGNATURE, texture)); @@ -29,10 +27,11 @@ public void setHeadTexture(Block block, String texture) { @Override public String getHeadTexture(Block block) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return null; } - TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(block, "tileEntity"); + TileEntitySkull entitySkull = ((CraftSkull) state).getTileEntity(); return entitySkull.getGameProfile().getProperties().get("textures").iterator().next().getValue(); } diff --git a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_9_R2/TextureProvider1_9_R2.java b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_9_R2/TextureProvider1_9_R2.java index 4292f91..0a39ce0 100644 --- a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_9_R2/TextureProvider1_9_R2.java +++ b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_9_R2/TextureProvider1_9_R2.java @@ -2,6 +2,7 @@ import org.bukkit.Material; import org.bukkit.block.Block; +import org.bukkit.block.BlockState; import org.bukkit.craftbukkit.v1_9_R2.block.CraftSkull; import org.bukkit.craftbukkit.v1_9_R2.inventory.CraftItemStack; import org.bukkit.inventory.meta.ItemMeta; @@ -10,6 +11,7 @@ import com.mojang.authlib.GameProfile; +import net.sourcewriters.minecraft.vcompat.provider.lookup.ClassLookupProvider; import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup; import net.minecraft.server.v1_9_R2.GameProfileSerializer; @@ -20,21 +22,26 @@ @SuppressWarnings("deprecation") public class TextureProvider1_9_R2 extends TextureProvider { - private final ClassLookup craftItemStackRef = ClassLookup.of(CraftItemStack.class).searchField("handle", "handle"); - private final ClassLookup craftMetaSkullRef = ClassLookup.of("org.bukkit.craftbukkit.v1_9_R2.inventory.CraftMetaSkull") - .searchField("serialized", "serializedProfile").searchField("profile", "profile"); private final Material skullMaterial = Material.valueOf("SKULL"); + + private final ClassLookup craftItemStackRef; + private final ClassLookup craftMetaSkullRef; protected TextureProvider1_9_R2(VersionControl1_9_R2 versionControl) { super(versionControl); + ClassLookupProvider provider = versionControl.getLookupProvider(); + craftItemStackRef = provider.createLookup("CraftItemStack", CraftItemStack.class).searchField("handle", "handle"); + craftMetaSkullRef = provider.createCBLookup("CraftMetaSkull", "inventory.CraftMetaSkull") + .searchField("serialized", "serializedProfile").searchField("profile", "profile"); } @Override public GameProfile profileFromBlock(Block block) { + BlockState state = block.getState(); if (!(block instanceof CraftSkull)) { return null; } - return ((CraftSkull) block).getTileEntity().getGameProfile(); + return ((CraftSkull) state).getTileEntity().getGameProfile(); } @Override @@ -95,10 +102,11 @@ public boolean applyItem(org.bukkit.inventory.ItemStack itemStack, GameProfile p @Override public boolean applyBlock(Block block, GameProfile profile) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return false; } - ((CraftSkull) block).getTileEntity().setGameProfile(profile); + ((CraftSkull) state).getTileEntity().setGameProfile(profile); return true; } diff --git a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_9_R2/tools/BlockTools1_9_R2.java b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_9_R2/tools/BlockTools1_9_R2.java index 2a27a5a..c42b0c8 100644 --- a/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_9_R2/tools/BlockTools1_9_R2.java +++ b/vcompat-legacy/src/main/java/net/sourcewriters/minecraft/vcompat/provider/impl/v1_9_R2/tools/BlockTools1_9_R2.java @@ -1,27 +1,25 @@ package net.sourcewriters.minecraft.vcompat.provider.impl.v1_9_R2.tools; import org.bukkit.block.Block; +import org.bukkit.block.BlockState; import org.bukkit.craftbukkit.v1_9_R2.block.CraftSkull; import com.mojang.authlib.properties.Property; import com.mojang.authlib.properties.PropertyMap; -import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup; - import net.minecraft.server.v1_9_R2.TileEntitySkull; import net.sourcewriters.minecraft.vcompat.provider.tools.BlockTools; import net.sourcewriters.minecraft.vcompat.util.constants.MinecraftConstants; public class BlockTools1_9_R2 extends BlockTools { - private final ClassLookup craftEntityStateRef = ClassLookup.of(CraftSkull.class).searchField("tileEntity", "tileEntity"); - @Override public void setHeadTexture(Block block, String texture) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return; } - TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(block, "tileEntity"); + TileEntitySkull entitySkull = ((CraftSkull) state).getTileEntity(); PropertyMap map = entitySkull.getGameProfile().getProperties(); map.removeAll("textures"); map.put("textures", new Property("textures", MinecraftConstants.TEXTURE_SIGNATURE, texture)); @@ -29,10 +27,11 @@ public void setHeadTexture(Block block, String texture) { @Override public String getHeadTexture(Block block) { - if (!(block instanceof CraftSkull)) { + BlockState state = block.getState(); + if (!(state instanceof CraftSkull)) { return null; } - TileEntitySkull entitySkull = (TileEntitySkull) craftEntityStateRef.getFieldValue(block, "tileEntity"); + TileEntitySkull entitySkull = ((CraftSkull) state).getTileEntity(); return entitySkull.getGameProfile().getProperties().get("textures").iterator().next().getValue(); }