mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-01 22:15:08 +00:00
Use 1.16.2 paper itemstack hover API
This commit is contained in:
1 parent
71b502cbfc
commit
aa569a79e7
3 files changed
+32
-46
No files matched your search
@@ -12,7 +12,6 @@ import net.draycia.carbon.storage.UserService;
|
||||
import net.draycia.carbon.storage.impl.JSONUserService;
|
||||
import net.draycia.carbon.storage.impl.MySQLUserService;
|
||||
import net.draycia.carbon.util.CarbonPlaceholders;
|
||||
import net.draycia.carbon.util.CarbonUtils;
|
||||
import net.draycia.carbon.util.Metrics;
|
||||
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||
import net.milkbowl.vault.economy.Economy;
|
||||
@@ -38,8 +37,6 @@ public final class CarbonChat extends JavaPlugin {
|
||||
private UserService userService;
|
||||
private MessageManager messageManager;
|
||||
|
||||
private CarbonUtils itemStackUtils;
|
||||
|
||||
private YamlConfiguration modConfig;
|
||||
private YamlConfiguration languageConfig;
|
||||
|
||||
@@ -74,12 +71,6 @@ public final class CarbonChat extends JavaPlugin {
|
||||
// Setup Adventure
|
||||
adventureManager = new AdventureManager(this);
|
||||
|
||||
try {
|
||||
itemStackUtils = new CarbonUtils();
|
||||
} catch (ClassNotFoundException | NoSuchMethodException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// Setup vault and permissions
|
||||
permission = getServer().getServicesManager().getRegistration(Permission.class).getProvider();
|
||||
|
||||
@@ -200,10 +191,6 @@ public final class CarbonChat extends JavaPlugin {
|
||||
return userService;
|
||||
}
|
||||
|
||||
public CarbonUtils getItemStackUtils() {
|
||||
return itemStackUtils;
|
||||
}
|
||||
|
||||
public AdventureManager getAdventureManager() {
|
||||
return adventureManager;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package net.draycia.carbon.listeners;
|
||||
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.events.ChatComponentEvent;
|
||||
import net.draycia.carbon.util.CarbonUtils;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
@@ -33,7 +34,7 @@ public class ItemLinkHandler implements Listener {
|
||||
|
||||
if (event.getOriginalMessage().contains(patternContent)) {
|
||||
TextComponent component = event.getComponent().replaceFirst(pattern, (input) -> {
|
||||
return TextComponent.builder().append(carbonChat.getItemStackUtils().createComponent(player));
|
||||
return TextComponent.builder().append(CarbonUtils.createComponent(player));
|
||||
});
|
||||
|
||||
event.setComponent(component);
|
||||
|
||||
@@ -26,54 +26,52 @@ import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.channels.ChatChannel;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import net.kyori.adventure.platform.bukkit.MinecraftComponentSerializer;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
import net.kyori.adventure.text.serializer.bungeecord.BungeeCordComponentSerializer;
|
||||
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
import net.md_5.bungee.api.chat.*;
|
||||
import net.md_5.bungee.api.chat.hover.content.Content;
|
||||
import org.apache.commons.lang.NotImplementedException;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public final class CarbonUtils {
|
||||
|
||||
private final Method asNMSCopyMethod;
|
||||
private final Method cMethod;
|
||||
|
||||
public CarbonUtils() throws NoSuchMethodException, ClassNotFoundException {
|
||||
final String version = Bukkit.getServer().getClass().getPackage().getName()
|
||||
.substring("org.bukkit.craftbukkit.".length());
|
||||
|
||||
Class<?> craftItemStackClass = Class.forName("org.bukkit.craftbukkit." + version + ".inventory.CraftItemStack");
|
||||
this.asNMSCopyMethod = craftItemStackClass.getMethod("asNMSCopy", ItemStack.class);
|
||||
Class<?> itemStackClass = Class.forName("net.minecraft.server." + version + ".ItemStack");
|
||||
this.cMethod = itemStackClass.getMethod("C");
|
||||
}
|
||||
|
||||
public Component createComponent(final Player player) {
|
||||
final ItemStack itemStack = player.getInventory().getItemInMainHand();
|
||||
|
||||
if (itemStack == null || itemStack.getType() == Material.AIR) {
|
||||
return TextComponent.empty();
|
||||
}
|
||||
|
||||
public static Component createComponent(final Player player) {
|
||||
try {
|
||||
final Object cbItemStack = this.asNMSCopyMethod.invoke(null, itemStack);
|
||||
final Object mojangComponent = this.cMethod.invoke(cbItemStack);
|
||||
final ItemStack itemStack = player.getInventory().getItemInMainHand();
|
||||
|
||||
return MinecraftComponentSerializer.INSTANCE.deserialize(mojangComponent);
|
||||
} catch (final IllegalAccessException | InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
return TextComponent.empty();
|
||||
}
|
||||
if (itemStack == null || itemStack.getType() == Material.AIR) {
|
||||
return net.kyori.adventure.text.TextComponent.empty();
|
||||
}
|
||||
|
||||
Content content = Bukkit.getItemFactory().hoverContentOf(itemStack);
|
||||
HoverEvent event = new HoverEvent(HoverEvent.Action.SHOW_ITEM, content);
|
||||
|
||||
ComponentBuilder component = new ComponentBuilder();
|
||||
component.color(ChatColor.WHITE).append("[");
|
||||
|
||||
if (itemStack.getItemMeta().hasDisplayName()) {
|
||||
component.append(TextComponent.fromLegacyText(itemStack.getItemMeta().getDisplayName()));
|
||||
} else {
|
||||
component.append(new TranslatableComponent(itemStack.getItemMeta().getLocalizedName()));
|
||||
}
|
||||
|
||||
component.color(ChatColor.WHITE).append("]");
|
||||
|
||||
component.event(event);
|
||||
|
||||
return BungeeCordComponentSerializer.get().deserialize(component.create());
|
||||
} catch (NoSuchMethodError ignored) {}
|
||||
|
||||
return net.kyori.adventure.text.TextComponent.empty();
|
||||
}
|
||||
|
||||
public static TextColor parseColor(ChatUser user, String input) {
|
||||
|
||||
Reference in new issue
Block a user