mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-03 22:15:45 +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
@@ -76,7 +76,7 @@
|
|||||||
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
|
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
|
||||||
</repository>
|
</repository>
|
||||||
|
|
||||||
<!-- Everything else -->
|
<!-- Kyori -->
|
||||||
<repository>
|
<repository>
|
||||||
<id>sonatype</id>
|
<id>sonatype</id>
|
||||||
<url>https://oss.sonatype.org/content/groups/public/</url>
|
<url>https://oss.sonatype.org/content/groups/public/</url>
|
||||||
@@ -94,11 +94,14 @@
|
|||||||
<url>https://jitpack.io</url>
|
<url>https://jitpack.io</url>
|
||||||
</repository>
|
</repository>
|
||||||
|
|
||||||
|
<!-- ACF -->
|
||||||
<repository>
|
<repository>
|
||||||
<name>jcenter</name>
|
<id>aikar</id>
|
||||||
<id>bintray-jcenter</id>
|
<url>https://repo.aikar.co/content/groups/aikar/</url>
|
||||||
<url>https://jcenter.bintray.com</url>
|
|
||||||
</repository>
|
</repository>
|
||||||
|
|
||||||
|
<!-- mcMMO and MiniMessage must be compiled locally -->
|
||||||
|
|
||||||
</repositories>
|
</repositories>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
@@ -122,6 +125,11 @@
|
|||||||
<artifactId>adventure-api</artifactId>
|
<artifactId>adventure-api</artifactId>
|
||||||
<version>4.0.0-SNAPSHOT</version>
|
<version>4.0.0-SNAPSHOT</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>net.kyori</groupId>
|
||||||
|
<artifactId>adventure-nbt</artifactId>
|
||||||
|
<version>4.0.0-SNAPSHOT</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>net.kyori</groupId>
|
<groupId>net.kyori</groupId>
|
||||||
<artifactId>adventure-platform-bukkit</artifactId>
|
<artifactId>adventure-platform-bukkit</artifactId>
|
||||||
@@ -161,7 +169,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.gmail.nossr50.mcMMO</groupId>
|
<groupId>com.gmail.nossr50.mcMMO</groupId>
|
||||||
<artifactId>mcMMO</artifactId>
|
<artifactId>mcMMO</artifactId>
|
||||||
<version>2.1.115-SNAPSHOT</version>
|
<version>2.1.130</version>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
|||||||
@@ -201,7 +201,7 @@ public class SimpleChatChannel extends ChatChannel {
|
|||||||
|
|
||||||
if (formattedMessage instanceof TextComponent && player.isOnline()) {
|
if (formattedMessage instanceof TextComponent && player.isOnline()) {
|
||||||
formattedMessage = ((TextComponent) formattedMessage).replace(itemPattern, (input) -> {
|
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;
|
package net.draycia.simplechat.util;
|
||||||
|
|
||||||
import net.kyori.adventure.key.Key;
|
import net.kyori.adventure.platform.bukkit.MinecraftComponentSerializer;
|
||||||
import net.kyori.adventure.nbt.BinaryTagHolder;
|
import net.kyori.adventure.text.Component;
|
||||||
import net.kyori.adventure.text.TextComponent;
|
import net.kyori.adventure.text.TextComponent;
|
||||||
import net.kyori.adventure.text.event.HoverEvent;
|
import net.kyori.adventure.text.event.HoverEvent;
|
||||||
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
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.ItemStack;
|
||||||
import org.bukkit.inventory.meta.ItemMeta;
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
|
|
||||||
import java.lang.reflect.Constructor;
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
@@ -38,10 +37,8 @@ public final class ItemStackUtils {
|
|||||||
|
|
||||||
private final Class<?> craftItemStackClass;
|
private final Class<?> craftItemStackClass;
|
||||||
private final Method asNMSCopyMethod;
|
private final Method asNMSCopyMethod;
|
||||||
private final Class<?> nbtTagCompoundClass;
|
|
||||||
private final Constructor<?> nbtTagCompoundConstructor;
|
|
||||||
private final Class<?> itemStackClass;
|
private final Class<?> itemStackClass;
|
||||||
private final Method saveMethod;
|
private final Method cMethod;
|
||||||
|
|
||||||
public ItemStackUtils() throws NoSuchMethodException, ClassNotFoundException {
|
public ItemStackUtils() throws NoSuchMethodException, ClassNotFoundException {
|
||||||
final String version = Bukkit.getServer().getClass().getPackage().getName()
|
final String version = Bukkit.getServer().getClass().getPackage().getName()
|
||||||
@@ -49,13 +46,11 @@ public final class ItemStackUtils {
|
|||||||
|
|
||||||
this.craftItemStackClass = cbClass(version, "inventory.CraftItemStack");
|
this.craftItemStackClass = cbClass(version, "inventory.CraftItemStack");
|
||||||
this.asNMSCopyMethod = this.craftItemStackClass.getMethod("asNMSCopy", ItemStack.class);
|
this.asNMSCopyMethod = this.craftItemStackClass.getMethod("asNMSCopy", ItemStack.class);
|
||||||
this.nbtTagCompoundClass = nmsClass(version, "NBTTagCompound");
|
|
||||||
this.nbtTagCompoundConstructor = this.nbtTagCompoundClass.getConstructor();
|
|
||||||
this.itemStackClass = nmsClass(version, "ItemStack");
|
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) {
|
if (player instanceof Player) {
|
||||||
final ItemStack itemStack = ((Player)player).getInventory().getItemInMainHand();
|
final ItemStack itemStack = ((Player)player).getInventory().getItemInMainHand();
|
||||||
|
|
||||||
@@ -63,45 +58,12 @@ public final class ItemStackUtils {
|
|||||||
return TextComponent.empty();
|
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 {
|
try {
|
||||||
final Object cbItemStack = this.asNMSCopyMethod.invoke(null, itemStack);
|
final Object cbItemStack = this.asNMSCopyMethod.invoke(null, itemStack);
|
||||||
final Object nbtTag = this.nbtTagCompoundConstructor.newInstance();
|
final Object nbtTag = this.cMethod.invoke(cbItemStack);
|
||||||
this.saveMethod.invoke(cbItemStack, nbtTag);
|
|
||||||
final TextComponent nameComponent = LegacyComponentSerializer.legacy().deserialize(name);
|
|
||||||
|
|
||||||
final Key key = Key.of(itemStack.getType().getKey().getKey());
|
return MinecraftComponentSerializer.INSTANCE.deserialize(nbtTag);
|
||||||
final int count = itemStack.getAmount();
|
} catch (final IllegalAccessException | InvocationTargetException e) {
|
||||||
final BinaryTagHolder tagHolder = BinaryTagHolder.of(nbtTag.toString());
|
|
||||||
|
|
||||||
return nameComponent.hoverEvent(HoverEvent.showItem(new HoverEvent.ShowItem(key, count, tagHolder)));
|
|
||||||
} catch (final IllegalAccessException | InvocationTargetException | InstantiationException e) {
|
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return TextComponent.empty();
|
return TextComponent.empty();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user