mirror of
https://github.com/IntellectualSites/PlotSquared.git
synced 2025-10-24 15:13:44 +02:00
chore: only support Lin
latest 1.19.4 FAWE has support for it
This commit is contained in:
@@ -48,7 +48,6 @@ import java.lang.reflect.Modifier;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.function.Consumer;
|
|
||||||
|
|
||||||
final class StateWrapperSpigot implements StateWrapper {
|
final class StateWrapperSpigot implements StateWrapper {
|
||||||
|
|
||||||
@@ -57,12 +56,12 @@ final class StateWrapperSpigot implements StateWrapper {
|
|||||||
private static final String CRAFTBUKKIT_PACKAGE = Bukkit.getServer().getClass().getPackageName();
|
private static final String CRAFTBUKKIT_PACKAGE = Bukkit.getServer().getClass().getPackageName();
|
||||||
|
|
||||||
private static final Logger LOGGER = LogManager.getLogger("PlotSquared/" + StateWrapperSpigot.class.getSimpleName());
|
private static final Logger LOGGER = LogManager.getLogger("PlotSquared/" + StateWrapperSpigot.class.getSimpleName());
|
||||||
private static final Gson GSON = new GsonBuilder().registerTypeHierarchyAdapter(Tag.class, new NbtGsonSerializer()).create();
|
private static final Gson GSON = new GsonBuilder().disableHtmlEscaping()
|
||||||
|
.registerTypeHierarchyAdapter(Tag.class, new NbtGsonSerializer()).create();
|
||||||
private static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
|
private static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
|
||||||
|
|
||||||
private static BukkitImplAdapter ADAPTER = null;
|
private static BukkitImplAdapter ADAPTER = null;
|
||||||
private static Class<?> LIN_TAG_CLASS = null;
|
private static Class<?> LIN_TAG_CLASS = null;
|
||||||
private static Class<?> JNBT_TAG_CLASS = null;
|
|
||||||
private static Class<?> CRAFT_BLOCK_ENTITY_STATE_CLASS = null;
|
private static Class<?> CRAFT_BLOCK_ENTITY_STATE_CLASS = null;
|
||||||
private static Class<?> MINECRAFT_CHAT_COMPONENT_CLASS = null;
|
private static Class<?> MINECRAFT_CHAT_COMPONENT_CLASS = null;
|
||||||
private static Field CRAFT_SIGN_SIDE_SIGN_TEXT = null;
|
private static Field CRAFT_SIGN_SIDE_SIGN_TEXT = null;
|
||||||
@@ -80,7 +79,6 @@ final class StateWrapperSpigot implements StateWrapper {
|
|||||||
|
|
||||||
public StateWrapperSpigot() {
|
public StateWrapperSpigot() {
|
||||||
try {
|
try {
|
||||||
findNbtCompoundClassType(clazz -> LIN_TAG_CLASS = clazz, clazz -> JNBT_TAG_CLASS = clazz);
|
|
||||||
ReflectionUtils.RefClass worldEditPluginRefClass = ReflectionUtils.getRefClass(WorldEditPlugin.class);
|
ReflectionUtils.RefClass worldEditPluginRefClass = ReflectionUtils.getRefClass(WorldEditPlugin.class);
|
||||||
WorldEditPlugin worldEditPlugin = (WorldEditPlugin) worldEditPluginRefClass
|
WorldEditPlugin worldEditPlugin = (WorldEditPlugin) worldEditPluginRefClass
|
||||||
.getMethod("getInstance")
|
.getMethod("getInstance")
|
||||||
@@ -90,12 +88,11 @@ final class StateWrapperSpigot implements StateWrapper {
|
|||||||
.getMethod("getBukkitImplAdapter")
|
.getMethod("getBukkitImplAdapter")
|
||||||
.of(worldEditPlugin)
|
.of(worldEditPlugin)
|
||||||
.call();
|
.call();
|
||||||
PAPERWEIGHT_ADAPTER_FROM_NATIVE = findPaperweightAdapterFromNativeMethodHandle(
|
LIN_TAG_CLASS = Class.forName("org.enginehub.linbus.tree.LinTag"); // provided WE / FAWE version is too old
|
||||||
ADAPTER.getClass(), LIN_TAG_CLASS, JNBT_TAG_CLASS
|
PAPERWEIGHT_ADAPTER_FROM_NATIVE = findPaperweightAdapterFromNativeMethodHandle(ADAPTER.getClass());
|
||||||
);
|
TO_LIN_TAG = findToLinTagMethodHandle();
|
||||||
TO_LIN_TAG = findToLinTagMethodHandle(LIN_TAG_CLASS);
|
|
||||||
} catch (NoSuchMethodException | ClassNotFoundException | IllegalAccessException | NoCapablePlatformException e) {
|
} catch (NoSuchMethodException | ClassNotFoundException | IllegalAccessException | NoCapablePlatformException e) {
|
||||||
throw new RuntimeException("Failed to access required WorldEdit methods", e);
|
throw new RuntimeException("Failed to access required WorldEdit classes or methods", e);
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
final Class<?> SIGN_TEXT_CLASS = Class.forName("net.minecraft.world.level.block.entity.SignText");
|
final Class<?> SIGN_TEXT_CLASS = Class.forName("net.minecraft.world.level.block.entity.SignText");
|
||||||
@@ -145,7 +142,7 @@ final class StateWrapperSpigot implements StateWrapper {
|
|||||||
// get native tag
|
// get native tag
|
||||||
Object nativeTag = PAPERWEIGHT_ADAPTER_FROM_NATIVE.invoke(
|
Object nativeTag = PAPERWEIGHT_ADAPTER_FROM_NATIVE.invoke(
|
||||||
ADAPTER,
|
ADAPTER,
|
||||||
LIN_TAG_CLASS == null ? data : TO_LIN_TAG.invoke(data)
|
TO_LIN_TAG.invoke(data)
|
||||||
);
|
);
|
||||||
// load block entity data
|
// load block entity data
|
||||||
CRAFT_BLOCK_ENTITY_STATE_LOAD_DATA.invoke(blockState, nativeTag);
|
CRAFT_BLOCK_ENTITY_STATE_LOAD_DATA.invoke(blockState, nativeTag);
|
||||||
@@ -212,46 +209,28 @@ final class StateWrapperSpigot implements StateWrapper {
|
|||||||
final Object signText = SIGN_TEXT_CONSTRUCTOR.invoke(typedComponents, typedComponents, dyeColor, glowing);
|
final Object signText = SIGN_TEXT_CONSTRUCTOR.invoke(typedComponents, typedComponents, dyeColor, glowing);
|
||||||
|
|
||||||
// blockState == org.bukkit.craftbukkit.block.CraftBlockEntityState
|
// blockState == org.bukkit.craftbukkit.block.CraftBlockEntityState
|
||||||
// --> net.minecraft.world.level.block.entity.SignBlockEntity
|
// --> getSnapshot() == net.minecraft.world.level.block.entity.SignBlockEntity
|
||||||
SIGN_BLOCK_ENTITY_SET_TEXT.invoke(CRAFT_BLOCK_ENTITY_STATE_GET_SNAPSHOT.invoke(blockState), signText, front);
|
SIGN_BLOCK_ENTITY_SET_TEXT.invoke(CRAFT_BLOCK_ENTITY_STATE_GET_SNAPSHOT.invoke(blockState), signText, front);
|
||||||
CRAFT_SIGN_SIDE_SIGN_TEXT.set(side, signText);
|
CRAFT_SIGN_SIDE_SIGN_TEXT.set(side, signText);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Initialize the used NBT tag class. For modern FAWE and WE that'll be Lin - for older ones JNBT.
|
|
||||||
*
|
|
||||||
* @throws ClassNotFoundException if neither can be found.
|
|
||||||
*/
|
|
||||||
private static void findNbtCompoundClassType(Consumer<Class<?>> linClass, Consumer<Class<?>> jnbtClass) throws
|
|
||||||
ClassNotFoundException {
|
|
||||||
try {
|
|
||||||
linClass.accept(Class.forName("org.enginehub.linbus.tree.LinTag"));
|
|
||||||
} catch (ClassNotFoundException e) {
|
|
||||||
jnbtClass.accept(Class.forName("com.sk89q.jnbt.Tag"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Finds the {@code toLinTag} method on the {@code ToLinTag} interface, if lin-bus is available in the classpath.
|
* Finds the {@code toLinTag} method on the {@code ToLinTag} interface, if lin-bus is available in the classpath.
|
||||||
* <br />
|
* <br />
|
||||||
* Required to access the underlying lin tag of the used JNBT tag by PlotSquared, so it can be converted into the platforms
|
* Required to access the underlying lin tag of the used JNBT tag by PlotSquared, so it can be converted into the platforms
|
||||||
* native tag later.
|
* native tag later.
|
||||||
*
|
*
|
||||||
* @param linTagClass {@code Tag} class of lin-bus, or {@code null} if not available.
|
|
||||||
* @return the MethodHandle for {@code toLinTag}, or {@code null} if lin-bus is not available in the classpath.
|
* @return the MethodHandle for {@code toLinTag}, or {@code null} if lin-bus is not available in the classpath.
|
||||||
* @throws ClassNotFoundException if the {@code ToLinTag} class could not be found.
|
* @throws ClassNotFoundException if the {@code ToLinTag} class could not be found.
|
||||||
* @throws NoSuchMethodException if no {@code toLinTag} method exists.
|
* @throws NoSuchMethodException if no {@code toLinTag} method exists.
|
||||||
* @throws IllegalAccessException shouldn't happen.
|
* @throws IllegalAccessException shouldn't happen.
|
||||||
*/
|
*/
|
||||||
private static MethodHandle findToLinTagMethodHandle(Class<?> linTagClass) throws ClassNotFoundException,
|
private static MethodHandle findToLinTagMethodHandle() throws ClassNotFoundException,
|
||||||
NoSuchMethodException, IllegalAccessException {
|
NoSuchMethodException, IllegalAccessException {
|
||||||
if (linTagClass == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return LOOKUP.findVirtual(
|
return LOOKUP.findVirtual(
|
||||||
Class.forName("org.enginehub.linbus.tree.ToLinTag"),
|
Class.forName("org.enginehub.linbus.tree.ToLinTag"),
|
||||||
"toLinTag",
|
"toLinTag",
|
||||||
MethodType.methodType(linTagClass)
|
MethodType.methodType(LIN_TAG_CLASS)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -266,26 +245,19 @@ final class StateWrapperSpigot implements StateWrapper {
|
|||||||
* </ul>
|
* </ul>
|
||||||
*
|
*
|
||||||
* @param adapterClass The bukkit adapter implementation class
|
* @param adapterClass The bukkit adapter implementation class
|
||||||
* @param linTagClass The lin-bus {@code Tag} class, if existing - otherwise {@code null}
|
|
||||||
* @param jnbtTagClass The jnbt {@code Tag} class, if lin-bus was not found in classpath - otherwise {@code null}
|
|
||||||
* @return the method.
|
* @return the method.
|
||||||
* @throws IllegalAccessException shouldn't happen as private lookup is used.
|
* @throws IllegalAccessException shouldn't happen as private lookup is used.
|
||||||
* @throws NoSuchMethodException if the method couldn't be found.
|
* @throws NoSuchMethodException if the method couldn't be found.
|
||||||
*/
|
*/
|
||||||
private static MethodHandle findPaperweightAdapterFromNativeMethodHandle(
|
private static MethodHandle findPaperweightAdapterFromNativeMethodHandle(Class<?> adapterClass) throws
|
||||||
Class<?> adapterClass, Class<?> linTagClass, Class<?> jnbtTagClass
|
IllegalAccessException, NoSuchMethodException {
|
||||||
) throws IllegalAccessException, NoSuchMethodException {
|
|
||||||
final MethodHandles.Lookup lookup = MethodHandles.privateLookupIn(adapterClass, LOOKUP);
|
final MethodHandles.Lookup lookup = MethodHandles.privateLookupIn(adapterClass, LOOKUP);
|
||||||
if (jnbtTagClass != null) {
|
|
||||||
// usage of JNBT = identical method signatures for WE and FAWE
|
|
||||||
return lookup.findVirtual(adapterClass, "fromNative", MethodType.methodType(Object.class, jnbtTagClass));
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
// FAWE
|
// FAWE
|
||||||
return lookup.findVirtual(adapterClass, "fromNativeLin", MethodType.methodType(Object.class, linTagClass));
|
return lookup.findVirtual(adapterClass, "fromNativeLin", MethodType.methodType(Object.class, LIN_TAG_CLASS));
|
||||||
} catch (NoSuchMethodException e) {
|
} catch (NoSuchMethodException e) {
|
||||||
// WE
|
// WE
|
||||||
return lookup.findVirtual(adapterClass, "fromNative", MethodType.methodType(Object.class, linTagClass));
|
return lookup.findVirtual(adapterClass, "fromNative", MethodType.methodType(Object.class, LIN_TAG_CLASS));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user