mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-01 22:15:08 +00:00
Fix item linking
This commit is contained in:
1 parent
b8ee341f9b
commit
7150b4c755
3 files changed
+22
-52
No files matched your search
@@ -201,7 +201,7 @@ public class SimpleChatChannel extends ChatChannel {
|
||||
|
||||
if (formattedMessage instanceof TextComponent && player.isOnline()) {
|
||||
formattedMessage = ((TextComponent) formattedMessage).replace(itemPattern, (input) -> {
|
||||
return simpleChat.getItemStackUtils().createComponent(player.getPlayer()).toBuilder();
|
||||
return TextComponent.builder().append(simpleChat.getItemStackUtils().createComponent(player.getPlayer()));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -17,8 +17,8 @@
|
||||
*/
|
||||
package net.draycia.simplechat.util;
|
||||
|
||||
import net.kyori.adventure.key.Key;
|
||||
import net.kyori.adventure.nbt.BinaryTagHolder;
|
||||
import net.kyori.adventure.platform.bukkit.MinecraftComponentSerializer;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import net.kyori.adventure.text.event.HoverEvent;
|
||||
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||
@@ -29,7 +29,6 @@ import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.regex.Pattern;
|
||||
@@ -38,10 +37,8 @@ public final class ItemStackUtils {
|
||||
|
||||
private final Class<?> craftItemStackClass;
|
||||
private final Method asNMSCopyMethod;
|
||||
private final Class<?> nbtTagCompoundClass;
|
||||
private final Constructor<?> nbtTagCompoundConstructor;
|
||||
private final Class<?> itemStackClass;
|
||||
private final Method saveMethod;
|
||||
private final Method cMethod;
|
||||
|
||||
public ItemStackUtils() throws NoSuchMethodException, ClassNotFoundException {
|
||||
final String version = Bukkit.getServer().getClass().getPackage().getName()
|
||||
@@ -49,13 +46,11 @@ public final class ItemStackUtils {
|
||||
|
||||
this.craftItemStackClass = cbClass(version, "inventory.CraftItemStack");
|
||||
this.asNMSCopyMethod = this.craftItemStackClass.getMethod("asNMSCopy", ItemStack.class);
|
||||
this.nbtTagCompoundClass = nmsClass(version, "NBTTagCompound");
|
||||
this.nbtTagCompoundConstructor = this.nbtTagCompoundClass.getConstructor();
|
||||
this.itemStackClass = nmsClass(version, "ItemStack");
|
||||
this.saveMethod = this.itemStackClass.getMethod("save", this.nbtTagCompoundClass);
|
||||
this.cMethod = this.itemStackClass.getMethod("C");
|
||||
}
|
||||
|
||||
public TextComponent createComponent(final CommandSender player) {
|
||||
public Component createComponent(final CommandSender player) {
|
||||
if (player instanceof Player) {
|
||||
final ItemStack itemStack = ((Player)player).getInventory().getItemInMainHand();
|
||||
|
||||
@@ -63,45 +58,12 @@ public final class ItemStackUtils {
|
||||
return TextComponent.empty();
|
||||
}
|
||||
|
||||
final ItemMeta itemMeta;
|
||||
|
||||
if (itemStack.hasItemMeta()) {
|
||||
itemMeta = itemStack.getItemMeta();
|
||||
} else {
|
||||
itemMeta = null;
|
||||
}
|
||||
|
||||
final String name;
|
||||
|
||||
if (itemMeta != null && itemMeta.hasDisplayName()) {
|
||||
name = itemMeta.getDisplayName();
|
||||
} else if (itemMeta != null && itemMeta.hasLocalizedName()) {
|
||||
name = itemMeta.getLocalizedName();
|
||||
} else {
|
||||
final Material material = itemStack.getType();
|
||||
final StringBuilder nameBuilder = new StringBuilder();
|
||||
final String[] nameParts = material.name().split(Pattern.quote("_"));
|
||||
for (int i = 0; i < nameParts.length; i++) {
|
||||
nameBuilder.append(nameParts[i].charAt(0)).append(nameParts[i].substring(1).toLowerCase());
|
||||
if ((i + 1) < nameParts.length) {
|
||||
nameBuilder.append(" ");
|
||||
}
|
||||
}
|
||||
name = nameBuilder.toString();
|
||||
}
|
||||
|
||||
try {
|
||||
final Object cbItemStack = this.asNMSCopyMethod.invoke(null, itemStack);
|
||||
final Object nbtTag = this.nbtTagCompoundConstructor.newInstance();
|
||||
this.saveMethod.invoke(cbItemStack, nbtTag);
|
||||
final TextComponent nameComponent = LegacyComponentSerializer.legacy().deserialize(name);
|
||||
final Object nbtTag = this.cMethod.invoke(cbItemStack);
|
||||
|
||||
final Key key = Key.of(itemStack.getType().getKey().getKey());
|
||||
final int count = itemStack.getAmount();
|
||||
final BinaryTagHolder tagHolder = BinaryTagHolder.of(nbtTag.toString());
|
||||
|
||||
return nameComponent.hoverEvent(HoverEvent.showItem(new HoverEvent.ShowItem(key, count, tagHolder)));
|
||||
} catch (final IllegalAccessException | InvocationTargetException | InstantiationException e) {
|
||||
return MinecraftComponentSerializer.INSTANCE.deserialize(nbtTag);
|
||||
} catch (final IllegalAccessException | InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
return TextComponent.empty();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user