Add item linking/hovers

This commit is contained in:
Draycia committed 2020-06-30 07:59:49 -07:00
1 parent d1b35bd95d
commit 66aa6d6449
3 files changed
+145

No files matched your search

@@ -11,6 +11,7 @@ import net.draycia.simplechat.managers.ChannelManager;
import net.draycia.simplechat.managers.CommandManager;
import net.draycia.simplechat.managers.DiscordManager;
import net.draycia.simplechat.managers.PluginMessageManager;
import net.draycia.simplechat.util.ItemStackUtils;
import net.kyori.adventure.platform.bukkit.BukkitPlatform;
import net.milkbowl.vault.permission.Permission;
import org.bukkit.OfflinePlayer;
@@ -42,6 +43,8 @@ public final class SimpleChat extends JavaPlugin {
private CommandManager commandManager;
private ChannelManager channelManager;
private ItemStackUtils itemStackUtils;
private BukkitPlatform platform;
private Gson gson = new GsonBuilder().setPrettyPrinting().create();
@@ -51,6 +54,12 @@ public final class SimpleChat extends JavaPlugin {
// Setup Adventure
platform = BukkitPlatform.of(this);
try {
itemStackUtils = new ItemStackUtils();
} catch (ClassNotFoundException | NoSuchMethodException e) {
e.printStackTrace();
}
// Setup vault and permissions
permission = getServer().getServicesManager().getRegistration(Permission.class).getProvider();
@@ -300,4 +309,8 @@ public final class SimpleChat extends JavaPlugin {
public BukkitPlatform getPlatform() {
return platform;
}
public ItemStackUtils getItemStackUtils() {
return itemStackUtils;
}
}
@@ -7,6 +7,8 @@ import net.draycia.simplechat.SimpleChat;
import net.draycia.simplechat.channels.ChatChannel;
import net.draycia.simplechat.events.ChannelChatEvent;
import net.draycia.simplechat.util.DiscordWebhook;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.TranslatableComponent;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
@@ -25,6 +27,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
public class SimpleChatChannel extends ChatChannel {
@@ -47,6 +50,8 @@ public class SimpleChatChannel extends ChatChannel {
private SimpleChat simpleChat;
private Pattern itemPattern = Pattern.compile("[item]");
private SimpleChatChannel() { }
SimpleChatChannel(TextColor color, long id, Map<String, String> formats, String webhook, boolean isDefault, boolean ignorable, String name, double distance, String switchMessage, String toggleOffMessage, String toggleOnMessage, boolean forwardFormatting, boolean shouldBungee, boolean filterEnabled, SimpleChat simpleChat) {
@@ -194,6 +199,12 @@ public class SimpleChatChannel extends ChatChannel {
// Send message to players who can see it
Component formattedMessage = MiniMessageParser.parseFormat(messageFormat);
if (formattedMessage instanceof TextComponent && player.isOnline()) {
((TextComponent) formattedMessage).replace(itemPattern, (input) -> {
return simpleChat.getItemStackUtils().createComponent(player.getPlayer()).toBuilder();
});
}
for (Player onlinePlayer : getAudience(player)) {
simpleChat.getPlatform().player(onlinePlayer).sendMessage(formattedMessage);
}
@@ -0,0 +1,121 @@
/*
Simple channel based chat plugin for Spigot
Copyright (C) 2020 Alexander Söderberg
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.draycia.simplechat.util;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.nbt.BinaryTagHolder;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
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;
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;
public ItemStackUtils() throws NoSuchMethodException, ClassNotFoundException {
final String version = Bukkit.getServer().getClass().getPackage().getName()
.substring("org.bukkit.craftbukkit.".length());
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);
}
public TextComponent createComponent(final CommandSender player) {
if (player instanceof Player) {
final ItemStack itemStack = ((Player)player).getInventory().getItemInMainHand();
if (itemStack.getType() == Material.AIR) {
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 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) {
e.printStackTrace();
return TextComponent.empty();
}
} else {
return TextComponent.empty();
}
}
private static Class<?> cbClass(final String version, final String name) throws ClassNotFoundException {
return Class.forName("org.bukkit.craftbukkit." + version + "." + name);
}
private static Class<?> nmsClass(final String version, final String name) throws ClassNotFoundException {
return Class.forName("net.minecraft.server." + version + "." + name);
}
}