Skip to content

Commit

Permalink
[publish] 6.0.12 Fix TellrawJson#hoverItem(ItemStack) & Fix XSkull
Browse files Browse the repository at this point in the history
  • Loading branch information
Bkm016 committed Oct 21, 2023
1 parent 9da35a7 commit f5f4450
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ class ResourceReader(val clazz: Class<*>, val migrate: Boolean = true) {
}
}

@Suppress("SimplifiableCallChain")
fun loadNodes(file: Configuration, nodesMap: HashMap<String, Type>, code: String) {
migrateLegacyVersion(file)
file.getKeys(false).forEach { node ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class NMSItemImpl : NMSItem() {
val nmsItemStack = getNMSCopy(itemStack) as net.minecraft.server.v1_8_R3.ItemStack
val nmsItem = nmsItemStack.item
val name = nmsItem.getProperty<String>("name")!!
name.toCharArray().joinToString { if (it.isUpperCase()) "_${it.lowercase()}" else it.toString() }
name.toCharArray().joinToString("") { if (it.isUpperCase()) "_${it.lowercase()}" else it.toString() }
}
}

Expand Down
2 changes: 0 additions & 2 deletions platform/platform-bukkit/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ dependencies {
compileOnly(project(":module:module-configuration"))
compileOnly("org.tabooproject.reflex:reflex:1.0.19")
compileOnly("org.tabooproject.reflex:analyser:1.0.19")

compileOnly(fileTree("libs"))
}

tasks {
Expand Down
Binary file removed platform/platform-bukkit/libs/authlib-5.0.47.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@

import com.google.common.collect.Lists;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
import java.lang.invoke.MethodType;

import com.mojang.authlib.properties.Property;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.block.Block;
Expand Down Expand Up @@ -65,9 +66,10 @@
* @see XMaterial
*/
public class XSkull {

protected static final MethodHandle
CRAFT_META_SKULL_PROFILE_GETTER, CRAFT_META_SKULL_PROFILE_SETTER,
CRAFT_META_SKULL_BLOCK_SETTER, PROPERTY_GETVALUE;
CRAFT_META_SKULL_BLOCK_SETTER, PROPERTY_GET_VALUE;

/**
* Some people use this without quotes surrounding the keys, not sure what that'd work.
Expand All @@ -91,7 +93,7 @@ public class XSkull {
/**
* In v1.20.2 there were some changes to the mojang API.
*/
private static final boolean NULLABILITY_RECORD_UPDATE = ReflectionUtils.VERSION.equals("v1_20_R2");
private static final boolean NULLABILITY_RECORD_UPDATE = ReflectionUtils.VERSION.equals("v1_20_R2") || ReflectionUtils.supports(21);

/**
* Does using a random UUID have any advantage?
Expand All @@ -106,10 +108,17 @@ public class XSkull {
* get base64 information from player's UUID.
*/
private static final String TEXTURES = "https://textures.minecraft.net/texture/";
private static final Class<?> CLASS_PROPERTY;

static {
try {
CLASS_PROPERTY = Class.forName("com.mojang.authlib.properties.Property");
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}

MethodHandles.Lookup lookup = MethodHandles.lookup();
MethodHandle profileSetter = null, profileGetter = null, blockSetter = null, propGetval = null;
MethodHandle profileSetter = null, profileGetter = null, blockSetter = null, propGetval = null, propConstructor = null;

try {
Class<?> CraftMetaSkull = ReflectionUtils.getCraftClass("inventory.CraftMetaSkull");
Expand Down Expand Up @@ -139,16 +148,21 @@ public class XSkull {
e.printStackTrace();
}

if (!NULLABILITY_RECORD_UPDATE) {
if (NULLABILITY_RECORD_UPDATE) {
try {
//noinspection JavaLangInvokeHandleSignature
propGetval = lookup.findVirtual(Property.class, "getValue", MethodType.methodType(String.class));
propGetval = lookup.findVirtual(CLASS_PROPERTY, "value", MethodType.methodType(String.class));
} catch (Throwable ex) {
ex.printStackTrace();
}
} else {
try {
propGetval = lookup.findVirtual(CLASS_PROPERTY, "getValue", MethodType.methodType(String.class));
} catch (Throwable ex) {
ex.printStackTrace();
}
}

PROPERTY_GETVALUE = propGetval;
PROPERTY_GET_VALUE = propGetval;
CRAFT_META_SKULL_PROFILE_SETTER = profileSetter;
CRAFT_META_SKULL_PROFILE_GETTER = profileGetter;
CRAFT_META_SKULL_BLOCK_SETTER = blockSetter;
Expand Down Expand Up @@ -310,7 +324,7 @@ public static ItemBuilder.SkullTexture getSkinValue(@NotNull ItemMeta skull) {
} catch (Exception ignored) {
}
if (profile != null && !profile.getProperties().get("textures").isEmpty()) {
for (Property property : profile.getProperties().get("textures")) {
for (Object property : profile.getProperties().get("textures")) {
String value = getPropertyValue(property);
if (!value.isEmpty()) {
return new ItemBuilder.SkullTexture(value, profile.getId());
Expand All @@ -321,19 +335,15 @@ public static ItemBuilder.SkullTexture getSkinValue(@NotNull ItemMeta skull) {
}

/**
* They changed {@link Property} to a Java record in 1.20.2
* They changed Property to a Java record in 1.20.2
*
* @since 4.0.1
*/
private static String getPropertyValue(Property property) {
if (NULLABILITY_RECORD_UPDATE) {
return property.value();
} else {
try {
return (String) PROPERTY_GETVALUE.invoke(property);
} catch (Throwable e) {
throw new RuntimeException(e);
}
private static String getPropertyValue(Object property) {
try {
return (String) PROPERTY_GET_VALUE.invoke(property);
} catch (Throwable e) {
throw new RuntimeException(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,22 @@ fun ItemStack.toNMSKeyAndItemData(): Pair<String, String> {
val nmsKey = try {
type.key.key
} catch (ex: NoSuchMethodError) {
// #359
// 错误的获取方式
// val nmsItem = nmsItemStack.invokeMethod<Any>("getItem")!!
// val name = nmsItem.getProperty<String>("name")!!
// var key = ""
// name.forEach { c ->
// if (c.isUpperCase()) {
// key += "_" + c.lowercase()
// } else {
// key += c
// }
// }
// key
val nmsItem = nmsItemStack.invokeMethod<Any>("getItem")!!
val name = nmsItem.getProperty<String>("name")!!
var key = ""
name.forEach { c ->
if (c.isUpperCase()) {
key += "_" + c.lowercase()
} else {
key += c
}
}
key
val nmsKey = classNMSItem.getProperty<Any>("REGISTRY", isStatic = true)!!.invokeMethod<Any>("b", nmsItem)!!
nmsKey.invokeMethod<String>("getKey")!!
}
return nmsKey to (nmsItemStack.invokeMethod<Any>("getTag")?.toString() ?: "{}")
}
Expand All @@ -44,6 +49,18 @@ private val classCraftItemStack by unsafeLazy {
obcClassLegacy("inventory.CraftItemStack")
}

private val classCraftMagicNumbers by unsafeLazy {
obcClassLegacy("util.CraftMagicNumbers")
}

private val classNMSItem by unsafeLazy {
nmsClassLegacy("Item")
}

private fun obcClassLegacy(name: String): Class<*> {
return Class.forName("org.bukkit.craftbukkit.${Bukkit.getServer().javaClass.name.split('.')[3]}.$name")
}

private fun nmsClassLegacy(name: String): Class<*> {
return Class.forName("net.minecraft.server.${Bukkit.getServer().javaClass.name.split('.')[3]}.$name")
}

0 comments on commit f5f4450

Please sign in to comment.