Skip to content

Commit

Permalink
Fix #133
Browse files Browse the repository at this point in the history
  • Loading branch information
Elikill58 committed May 20, 2024
1 parent 00ef83b commit 6fb8071
Show file tree
Hide file tree
Showing 15 changed files with 173 additions and 92 deletions.
7 changes: 3 additions & 4 deletions src/main/java/me/dadus33/chatitem/ChatItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,14 @@
import me.dadus33.chatitem.platform.hook.SpigotPlatform;
import me.dadus33.chatitem.playernamer.PlayerNamerManager;
import me.dadus33.chatitem.utils.Colors;
import me.dadus33.chatitem.utils.PacketUtils;
import me.dadus33.chatitem.utils.ReflectionUtils;
import me.dadus33.chatitem.utils.SemVer;
import me.dadus33.chatitem.utils.Utils;
import me.dadus33.chatitem.utils.Version;

public class ChatItem extends JavaPlugin {

private static final IPlatform platform = PacketUtils.IS_PAPER ? new PaperPlatform() : new SpigotPlatform();
private static final IPlatform platform = Utils.IS_PAPER ? new PaperPlatform() : new SpigotPlatform();
public static IPlatform getPlatform() {
return platform;
}
Expand Down Expand Up @@ -98,12 +97,12 @@ private void chooseManagers() {
case "all":
this.chatManager.add(new PacketEditingChatManager(this));
this.chatManager.add(new ChatListenerChatManager(this));
if(PacketUtils.IS_PAPER)
if(Utils.IS_PAPER)
this.chatManager.add(new PaperChatManager(this));
getLogger().info("Manager automatically chosen: " + getVisualChatManagers());
break;
case "auto":
if(PacketUtils.IS_PAPER)
if(Utils.IS_PAPER)
this.chatManager.add(new PaperChatManager(this));
else if (getPluginThatRequirePacket().stream().map(pm::getPlugin).anyMatch(Objects::nonNull) && Version.getVersion().isNewerThan(Version.V1_7))
this.chatManager.add(new PacketEditingChatManager(this));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ public static Object createSystemChatPacket(String json, Object old) throws Exce
Object packet = internalCreateSystemChatPacket(json, old);
if(packet != null)
return packet;
packet = internalCreateSystemChatPacket(PacketUtils.ICB_FROM_JSON.invoke(null, json), old);
Object icb = ChatItem.getPlatform().jsonToBaseComponent(json);
if(icb != null)
packet = internalCreateSystemChatPacket(icb, old);
if(packet != null)
return packet;
ChatItem.getInstance().getLogger().warning("Can't create a new packet for json " + json);
Expand All @@ -89,7 +91,7 @@ public static Object createSystemChatPacket(String json, Object old) throws Exce

private static Object internalCreateSystemChatPacket(Object obj, Object old) throws Exception {
Class<?> packetClass = PacketUtils.getNmsClass("ClientboundPlayerChatPacket", "network.protocol.game.", "ClientboundSystemChatPacket", "PacketPlayOutChat");
Class<?> chatMessageTypeClass = PacketUtils.isClassExist("net.minecraft.network.chat.ChatMessageType") ? PacketUtils.getNmsClass("ChatMessageType", "network.chat.") : null;
Class<?> chatMessageTypeClass = Utils.isClassExist("net.minecraft.network.chat.ChatMessageType") ? PacketUtils.getNmsClass("ChatMessageType", "network.chat.") : null;
Constructor<?> betterOne = null;
Object[] betterParam = null;
for (Constructor<?> cons : packetClass.getDeclaredConstructors()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import me.dadus33.chatitem.hook.DiscordSrvSupport;
import me.dadus33.chatitem.utils.Messages;
import me.dadus33.chatitem.utils.PacketUtils;
import me.dadus33.chatitem.utils.Utils;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.nbt.api.BinaryTagHolder;
Expand Down Expand Up @@ -78,7 +79,7 @@ public Object manageItem(Player p, Chat chat, ChatItemPacket packet, ItemStack i
String itemName = ChatManager.getNameOfItem(chat.getPlayer(), item, c);
ChatItem.debug("NBT tag: " + PacketUtils.getNbtTag(item));
HoverEvent<?> hover;
if(PacketUtils.IS_PAPER)
if(Utils.IS_PAPER)
hover = item.asHoverEvent();
else
hover = HoverEvent.showItem(Key.key(item.getType().getKey().getKey()), item.getAmount(), BinaryTagHolder.of(PacketUtils.getNbtTag(item)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,20 @@
import me.dadus33.chatitem.chatmanager.v1.basecomp.IComponentManager;
import me.dadus33.chatitem.chatmanager.v1.packets.ChatItemPacket;
import me.dadus33.chatitem.hook.DiscordSrvSupport;
import me.dadus33.chatitem.utils.PacketUtils;

public class ComponentNMSManager implements IComponentManager {

@Override
public boolean hasConditions() {
return PacketUtils.COMPONENT_CLASS != null;
return ChatItem.getPlatform().hasBaseComponentSerializer();
}

@Override
public String getBaseComponentAsJSON(ChatItemPacket packet) {
Object chatBaseComp = packet.getContent().getChatComponents().readSafely(0);
if (chatBaseComp != null) {
try {
Object o = PacketUtils.CHAT_SERIALIZER.getMethod("a", PacketUtils.COMPONENT_CLASS).invoke(null, chatBaseComp);
Object o = ChatItem.getPlatform().baseComponentToJson(chatBaseComp);
if(o != null && o instanceof String && JsonParser.parseString((String) o).isJsonObject()) {
return (String) o;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,44 +1,22 @@
package me.dadus33.chatitem.chatmanager.v1.basecomp.hook;

import java.lang.reflect.Method;

import me.dadus33.chatitem.ChatItem;
import me.dadus33.chatitem.chatmanager.v1.PacketEditingChatManager;
import me.dadus33.chatitem.chatmanager.v1.basecomp.IComponentManager;
import me.dadus33.chatitem.chatmanager.v1.packets.ChatItemPacket;
import me.dadus33.chatitem.utils.PacketUtils;

public class IChatBaseComponentManager implements IComponentManager {

private Method serializerGetJson;

public IChatBaseComponentManager() {
try {
Class<?> chatSerializer = PacketUtils.CHAT_SERIALIZER;
//fromJson = chatSerializer.getMethod("a", String.class);
for (Method m : chatSerializer.getDeclaredMethods()) {
if (m.getParameterCount() == 1 && m.getParameterTypes()[0].equals(PacketUtils.COMPONENT_CLASS) && m.getReturnType().equals(String.class)) {
serializerGetJson = m;
break;
}
}
if (serializerGetJson == null)
ChatItem.getInstance().getLogger().warning("Failed to find JSON serializer in class: " + chatSerializer.getCanonicalName());
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
public boolean hasConditions() {
return serializerGetJson != null;
return ChatItem.getPlatform().hasBaseComponentSerializer();
}

@Override
public String getBaseComponentAsJSON(ChatItemPacket packet) {
try {
Object obj = packet.getContent().getChatComponents().readSafely(0);
return obj == null ? null : (String) serializerGetJson.invoke(null, obj);
return obj == null ? null : (String) ChatItem.getPlatform().baseComponentToJson(obj);
} catch (Exception exc) {
exc.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public String getBaseComponentAsJSON(ChatItemPacket packet) {
Object chatBaseComp = ReflectionUtils.getMethod(pcmClass, PacketUtils.COMPONENT_CLASS).invoke(pcm);
if(chatBaseComp != null)
ChatItem.debug("[PCMManager] Founded " + chatBaseComp.getClass().getSimpleName());
return chatBaseComp == null ? null : PacketUtils.CHAT_SERIALIZER.getMethod("a", PacketUtils.COMPONENT_CLASS).invoke(null, chatBaseComp).toString();
return chatBaseComp == null ? null : ChatItem.getPlatform().baseComponentToJson(chatBaseComp);
} catch (Exception exc) {
exc.printStackTrace();
}
Expand All @@ -44,7 +44,7 @@ public String getBaseComponentAsJSON(ChatItemPacket packet) {
@Override
public void writeJson(ChatItemPacket packet, String json) {
try {
Object chatComp = PacketUtils.CHAT_SERIALIZER.getMethod("a", String.class).invoke(null, json);
Object chatComp = ChatItem.getPlatform().jsonToBaseComponent(json);
Class<?> pcmClass = PacketUtils.getNmsClass("PlayerChatMessage", "network.chat.");
ContentModifier<Object> pcmModifier = packet.getContent().getSpecificModifier((Class<Object>) pcmClass);
Object pcm = ReflectionUtils.getMethod(pcmClass, pcmClass, PacketUtils.COMPONENT_CLASS).invoke(pcmModifier.readSafely(0), chatComp);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package me.dadus33.chatitem.chatmanager.v1.listeners;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -38,24 +37,11 @@
public class ChatPacketManager extends PacketHandler {

private Object lastSentPacket = null;
private Method serializerGetJson;
private PacketEditingChatManager manager;
private final List<IComponentManager> componentManager = new ArrayList<>();

public ChatPacketManager(PacketEditingChatManager manager) {
this.manager = manager;
try {
for (Method m : PacketUtils.CHAT_SERIALIZER.getDeclaredMethods()) {
if (m.getParameterCount() == 1 && m.getParameterTypes()[0].equals(PacketUtils.COMPONENT_CLASS) && m.getReturnType().equals(String.class)) {
serializerGetJson = m;
break;
}
}
if (serializerGetJson == null)
ChatItem.getInstance().getLogger().warning("Failed to find JSON serializer in class: " + PacketUtils.CHAT_SERIALIZER.getCanonicalName());
} catch (Exception e) {
e.printStackTrace();
}

for (IComponentManager getter : Arrays.asList(new StringComponentManager(), new ComponentNMSManager(), new PCMComponentManager())) {
tryRegister(getter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import me.dadus33.chatitem.listeners.InventoryListener;
import me.dadus33.chatitem.utils.Colors;
import me.dadus33.chatitem.utils.Messages;
import me.dadus33.chatitem.utils.PacketUtils;
import me.dadus33.chatitem.utils.Utils;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.TextComponent;
Expand All @@ -35,7 +34,7 @@ public class ChatItemCommand implements CommandExecutor, TabExecutor {
private static final List<String> ORDERS;

static {
if(PacketUtils.IS_PAPER)
if(Utils.IS_PAPER)
ORDERS = Arrays.asList("packet", "chat", "paper", "all");
else
ORDERS = Arrays.asList("packet", "chat", "all");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import me.dadus33.chatitem.listeners.holder.CustomInventoryHolder;
import me.dadus33.chatitem.utils.ItemUtils;
import me.dadus33.chatitem.utils.Messages;
import me.dadus33.chatitem.utils.PacketUtils;
import me.dadus33.chatitem.utils.Utils;

public class InventoryListener implements Listener {

Expand Down Expand Up @@ -115,7 +115,7 @@ public static void open(Player p) {

int slot = 0;
for (String manager : Arrays.asList("all", "auto", "packet", "chat", "paper")) {
if (manager == "paper" && !PacketUtils.IS_PAPER)
if (manager == "paper" && !Utils.IS_PAPER)
continue;
holder.keyBySlot.put(slot, manager);
inv.setItem(slot++, getManagerItem(manager));
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/me/dadus33/chatitem/platform/IPlatform.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.bukkit.plugin.Plugin;

import me.dadus33.chatitem.utils.Messages;
import me.dadus33.chatitem.utils.Version;

public interface IPlatform {

Expand All @@ -28,4 +29,14 @@ default ItemStack createItemStack(Material type, String name, String... lore) {
ItemStack createItemStack(Material type, String name, List<String> lore);

String getPluginVersion(Plugin plugin);

Version getMinecraftVersion();

String getNMSVersion();

boolean hasBaseComponentSerializer();

String baseComponentToJson(Object obj);

Object jsonToBaseComponent(String json);
}
44 changes: 44 additions & 0 deletions src/main/java/me/dadus33/chatitem/platform/hook/PaperPlatform.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.dadus33.chatitem.platform.hook;

import java.lang.reflect.Method;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -12,6 +13,7 @@
import org.bukkit.plugin.Plugin;

import me.dadus33.chatitem.platform.IPlatform;
import me.dadus33.chatitem.utils.Version;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;

Expand Down Expand Up @@ -45,4 +47,46 @@ public ItemStack createItemStack(Material type, String name, List<String> lore)
public String getPluginVersion(Plugin plugin) {
return plugin.getPluginMeta().getVersion();
}

@Override
public Version getMinecraftVersion() {
return Version.getVersionByName("v" + Bukkit.getMinecraftVersion().replace(".", "_"));
}

@Override
public String getNMSVersion() {
String[] parts = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",");
return parts.length <= 3 ? "" : parts[3];
}

@Override
public boolean hasBaseComponentSerializer() {
return Version.getVersion().isNewerOrEquals(Version.V1_20_6) ? false : SpigotPlatform.getBaseComponentToJsonMethod() != null;
}

@Override
public String baseComponentToJson(Object obj) {
if(Version.getVersion().isNewerOrEquals(Version.V1_20_6))
return null;
Method m = SpigotPlatform.getBaseComponentToJsonMethod();
try {
return (String) m.invoke(null, obj);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

@Override
public Object jsonToBaseComponent(String json) {
if(Version.getVersion().isNewerOrEquals(Version.V1_20_6))
return null;
Method m = SpigotPlatform.getJsonToBaseComponentMethod();
try {
return m.invoke(null, json);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
Loading

0 comments on commit 6fb8071

Please sign in to comment.