From 735c92a61205e5934dcce66a02f385e9d7553212 Mon Sep 17 00:00:00 2001 From: filoghost Date: Wed, 15 Apr 2020 22:25:37 +0200 Subject: [PATCH] Handle custom name in newer versions without serializing from/to JSON --- .../nms/interfaces/ChatComponentAdapter.java | 35 ++++++++ .../nms/interfaces/CustomNameHelper.java | 80 +++++++++++++++++++ .../nms/interfaces/NMSManager.java | 2 + .../nms/interfaces/entity/NMSNameable.java | 9 ++- .../nms/v1_10_R1/EntityNMSArmorStand.java | 7 +- .../nms/v1_10_R1/NmsManagerImpl.java | 6 ++ .../nms/v1_11_R1/EntityNMSArmorStand.java | 7 +- .../nms/v1_11_R1/NmsManagerImpl.java | 6 ++ .../nms/v1_12_R1/EntityNMSArmorStand.java | 7 +- .../nms/v1_12_R1/NmsManagerImpl.java | 6 ++ .../nms/v1_13_R1/EntityNMSArmorStand.java | 7 +- .../nms/v1_13_R1/NmsManagerImpl.java | 44 ++++++++++ .../nms/v1_13_R2/EntityNMSArmorStand.java | 7 +- .../nms/v1_13_R2/NmsManagerImpl.java | 44 ++++++++++ NMS/v1_14_R1/pom.xml | 2 +- .../nms/v1_14_R1/EntityNMSArmorStand.java | 7 +- .../nms/v1_14_R1/NmsManagerImpl.java | 63 +++++++++++++++ .../nms/v1_15_R1/EntityNMSArmorStand.java | 7 +- .../nms/v1_15_R1/NmsManagerImpl.java | 45 +++++++++++ .../nms/v1_8_R2/EntityNMSArmorStand.java | 7 +- .../nms/v1_8_R2/NmsManagerImpl.java | 6 ++ .../nms/v1_8_R3/EntityNMSArmorStand.java | 7 +- .../nms/v1_8_R3/NmsManagerImpl.java | 6 ++ .../nms/v1_9_R1/EntityNMSArmorStand.java | 7 +- .../nms/v1_9_R1/NmsManagerImpl.java | 6 ++ .../nms/v1_9_R2/EntityNMSArmorStand.java | 7 +- .../nms/v1_9_R2/NmsManagerImpl.java | 6 ++ .../protocollib/current/MetadataHelper.java | 54 ++++++------- .../protocollib/current/PacketHelper.java | 4 +- .../current/ProtocolLibHookImpl.java | 22 +++-- .../placeholder/PlaceholdersManager.java | 2 +- 31 files changed, 470 insertions(+), 55 deletions(-) create mode 100644 NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/ChatComponentAdapter.java create mode 100644 NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/CustomNameHelper.java diff --git a/NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/ChatComponentAdapter.java b/NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/ChatComponentAdapter.java new file mode 100644 index 00000000..b25fd123 --- /dev/null +++ b/NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/ChatComponentAdapter.java @@ -0,0 +1,35 @@ +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.interfaces; + +import java.util.List; + +public interface ChatComponentAdapter { + + T cast(Object chatComponentObject); + + String getText(T chatComponent); + + List getSiblings(T chatComponent); + + void addSibling(T chatComponent, T newSibling); + + default T cloneComponent(T chatComponent) { + return cloneComponent(chatComponent, getText(chatComponent)); + } + + T cloneComponent(T chatComponent, String newText); + +} \ No newline at end of file diff --git a/NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/CustomNameHelper.java b/NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/CustomNameHelper.java new file mode 100644 index 00000000..2d81f14c --- /dev/null +++ b/NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/CustomNameHelper.java @@ -0,0 +1,80 @@ +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.interfaces; + +import java.util.List; + +public class CustomNameHelper { + + + public static String replaceCustomNameString(Object customNameObject, String target, String replacement) { + String customName = (String) customNameObject; + if (customName.contains(target)) { + return customName.replace(target, replacement); + } else { + return customName; + } + } + + public static T replaceCustomNameChatComponent(ChatComponentAdapter versionAdapter, Object customNameObject, String target, String replacement) { + // Custom name is expected to be a ChatComponentText with empty text and child components (called "siblings") that do not contain more components. + T rootComponent = versionAdapter.cast(customNameObject); + if (!versionAdapter.getText(rootComponent).isEmpty()) { + throw new IllegalArgumentException("Expected root component with empty text"); + } + + boolean[] childrenContainingTarget = null; + List children = versionAdapter.getSiblings(rootComponent); + int childrenSize = children.size(); + + for (int i = 0; i < childrenSize; i++) { + T childComponent = versionAdapter.cast(children.get(i)); + + if (!versionAdapter.getSiblings(childComponent).isEmpty()) { + throw new IllegalArgumentException("Expected child component without sub-nodes"); + } + + if (versionAdapter.getText(childComponent).contains(target)) { + // Lazy initialization for performance, since this method can be called frequently. + if (childrenContainingTarget == null) { + childrenContainingTarget = new boolean[childrenSize]; + } + childrenContainingTarget[i] = true; + } + } + + if (childrenContainingTarget == null) { + // No match found, return original unmodified object. + return rootComponent; + } + + // Clone all the objects and apply replacements where needed. + T clonedRoot = versionAdapter.cloneComponent(rootComponent); + for (int i = 0; i < childrenSize; i++) { + T childComponent = children.get(i); + + String newText = versionAdapter.getText(childComponent); + if (childrenContainingTarget[i]) { + newText = newText.replace(target, replacement); + } + + T clonedChild = versionAdapter.cloneComponent(childComponent, newText); + versionAdapter.addSibling(clonedRoot, clonedChild); + } + + return clonedRoot; + } + +} diff --git a/NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/NMSManager.java b/NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/NMSManager.java index cbd43d93..ff439942 100644 --- a/NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/NMSManager.java +++ b/NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/NMSManager.java @@ -40,4 +40,6 @@ public interface NMSManager { public org.bukkit.entity.Entity getEntityFromID(org.bukkit.World bukkitWorld, int entityID); + public Object replaceCustomNameText(Object customNameObject, String target, String replacement); + } diff --git a/NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/entity/NMSNameable.java b/NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/entity/NMSNameable.java index 6e4e3877..ff0fdf62 100644 --- a/NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/entity/NMSNameable.java +++ b/NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/entity/NMSNameable.java @@ -16,10 +16,13 @@ package com.gmail.filoghost.holographicdisplays.nms.interfaces.entity; public interface NMSNameable extends NMSEntityBase { - // Sets a custom name for this entity. + // Sets a custom name as a String. public void setCustomNameNMS(String name); - // Returns the custom name of this entity. - public String getCustomNameNMS(); + // Returns the custom name as a String. + public String getCustomNameStringNMS(); + + // Returns the custom name as version-dependent NMS object (String for MC 1.12 and below, ChatComponent for MC 1.13+ a ChatComponent). + public Object getCustomNameObjectNMS(); } diff --git a/NMS/v1_10_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_10_R1/EntityNMSArmorStand.java b/NMS/v1_10_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_10_R1/EntityNMSArmorStand.java index 6d810441..136a7de7 100644 --- a/NMS/v1_10_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_10_R1/EntityNMSArmorStand.java +++ b/NMS/v1_10_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_10_R1/EntityNMSArmorStand.java @@ -174,10 +174,15 @@ public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorSta } @Override - public String getCustomNameNMS() { + public String getCustomNameStringNMS() { return this.customName; } + @Override + public Object getCustomNameObjectNMS() { + return super.getCustomName(); + } + @Override public void setLockTick(boolean lock) { lockTick = lock; diff --git a/NMS/v1_10_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_10_R1/NmsManagerImpl.java b/NMS/v1_10_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_10_R1/NmsManagerImpl.java index 497e4147..2c12dc31 100644 --- a/NMS/v1_10_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_10_R1/NmsManagerImpl.java +++ b/NMS/v1_10_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_10_R1/NmsManagerImpl.java @@ -25,6 +25,7 @@ import org.bukkit.inventory.ItemStack; import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.CustomNameHelper; import com.gmail.filoghost.holographicdisplays.nms.interfaces.NMSManager; import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; @@ -146,5 +147,10 @@ public class NmsManagerImpl implements NMSManager { return nmsEntity.getBukkitEntity(); } + + @Override + public Object replaceCustomNameText(Object customNameObject, String target, String replacement) { + return CustomNameHelper.replaceCustomNameString(customNameObject, target, replacement); + } } diff --git a/NMS/v1_11_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_11_R1/EntityNMSArmorStand.java b/NMS/v1_11_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_11_R1/EntityNMSArmorStand.java index 81b7acf9..38d75df0 100644 --- a/NMS/v1_11_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_11_R1/EntityNMSArmorStand.java +++ b/NMS/v1_11_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_11_R1/EntityNMSArmorStand.java @@ -183,10 +183,15 @@ public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorSta } @Override - public String getCustomNameNMS() { + public String getCustomNameStringNMS() { return this.customName; } + @Override + public Object getCustomNameObjectNMS() { + return super.getCustomName(); + } + @Override public void setLockTick(boolean lock) { lockTick = lock; diff --git a/NMS/v1_11_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_11_R1/NmsManagerImpl.java b/NMS/v1_11_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_11_R1/NmsManagerImpl.java index 75e57b15..0c701c8f 100644 --- a/NMS/v1_11_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_11_R1/NmsManagerImpl.java +++ b/NMS/v1_11_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_11_R1/NmsManagerImpl.java @@ -24,6 +24,7 @@ import org.bukkit.inventory.ItemStack; import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.CustomNameHelper; import com.gmail.filoghost.holographicdisplays.nms.interfaces.NMSManager; import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; @@ -155,5 +156,10 @@ public class NmsManagerImpl implements NMSManager { return nmsEntity.getBukkitEntity(); } + + @Override + public Object replaceCustomNameText(Object customNameObject, String target, String replacement) { + return CustomNameHelper.replaceCustomNameString(customNameObject, target, replacement); + } } diff --git a/NMS/v1_12_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_12_R1/EntityNMSArmorStand.java b/NMS/v1_12_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_12_R1/EntityNMSArmorStand.java index dd0a78ac..2bc7be44 100644 --- a/NMS/v1_12_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_12_R1/EntityNMSArmorStand.java +++ b/NMS/v1_12_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_12_R1/EntityNMSArmorStand.java @@ -183,10 +183,15 @@ public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorSta } @Override - public String getCustomNameNMS() { + public String getCustomNameStringNMS() { return this.customName; } + @Override + public Object getCustomNameObjectNMS() { + return super.getCustomName(); + } + @Override public void setLockTick(boolean lock) { lockTick = lock; diff --git a/NMS/v1_12_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_12_R1/NmsManagerImpl.java b/NMS/v1_12_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_12_R1/NmsManagerImpl.java index fb637cf0..6a6568e6 100644 --- a/NMS/v1_12_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_12_R1/NmsManagerImpl.java +++ b/NMS/v1_12_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_12_R1/NmsManagerImpl.java @@ -24,6 +24,7 @@ import org.bukkit.inventory.ItemStack; import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.CustomNameHelper; import com.gmail.filoghost.holographicdisplays.nms.interfaces.NMSManager; import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; @@ -156,4 +157,9 @@ public class NmsManagerImpl implements NMSManager { return nmsEntity.getBukkitEntity(); } + @Override + public Object replaceCustomNameText(Object customNameObject, String target, String replacement) { + return CustomNameHelper.replaceCustomNameString(customNameObject, target, replacement); + } + } diff --git a/NMS/v1_13_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R1/EntityNMSArmorStand.java b/NMS/v1_13_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R1/EntityNMSArmorStand.java index 63d18042..82a6822f 100644 --- a/NMS/v1_13_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R1/EntityNMSArmorStand.java +++ b/NMS/v1_13_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R1/EntityNMSArmorStand.java @@ -185,10 +185,15 @@ public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorSta } @Override - public String getCustomNameNMS() { + public String getCustomNameStringNMS() { return this.customName; } + @Override + public Object getCustomNameObjectNMS() { + return super.getCustomName(); + } + @Override public void setLockTick(boolean lock) { lockTick = lock; diff --git a/NMS/v1_13_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R1/NmsManagerImpl.java b/NMS/v1_13_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R1/NmsManagerImpl.java index 223b2632..73add20b 100644 --- a/NMS/v1_13_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R1/NmsManagerImpl.java +++ b/NMS/v1_13_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R1/NmsManagerImpl.java @@ -16,6 +16,7 @@ package com.gmail.filoghost.holographicdisplays.nms.v1_13_R1; import java.lang.reflect.Method; import java.util.List; + import org.bukkit.Bukkit; import org.bukkit.craftbukkit.v1_13_R1.CraftWorld; import org.bukkit.craftbukkit.v1_13_R1.entity.CraftEntity; @@ -23,7 +24,9 @@ import org.bukkit.inventory.ItemStack; import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.ChatComponentAdapter; import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.CustomNameHelper; import com.gmail.filoghost.holographicdisplays.nms.interfaces.NMSManager; import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; @@ -33,8 +36,10 @@ import com.gmail.filoghost.holographicdisplays.util.Validator; import com.gmail.filoghost.holographicdisplays.util.VersionUtils; import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; +import net.minecraft.server.v1_13_R1.ChatComponentText; import net.minecraft.server.v1_13_R1.Entity; import net.minecraft.server.v1_13_R1.EntityTypes; +import net.minecraft.server.v1_13_R1.IChatBaseComponent; import net.minecraft.server.v1_13_R1.MathHelper; import net.minecraft.server.v1_13_R1.RegistryID; import net.minecraft.server.v1_13_R1.RegistryMaterials; @@ -168,4 +173,43 @@ public class NmsManagerImpl implements NMSManager { return nmsEntity.getBukkitEntity(); } + @Override + public Object replaceCustomNameText(Object customNameObject, String target, String replacement) { + return CustomNameHelper.replaceCustomNameChatComponent(NMSChatComponentAdapter.INSTANCE, customNameObject, target, replacement); + } + + private static enum NMSChatComponentAdapter implements ChatComponentAdapter { + + INSTANCE { + + public ChatComponentText cast(Object chatComponentObject) { + return (ChatComponentText) chatComponentObject; + } + + @Override + public String getText(IChatBaseComponent chatComponent) { + return chatComponent.getText(); + } + + @Override + public List getSiblings(IChatBaseComponent chatComponent) { + return chatComponent.a(); + } + + @Override + public void addSibling(IChatBaseComponent chatComponent, IChatBaseComponent newSibling) { + chatComponent.addSibling(newSibling); + } + + @Override + public ChatComponentText cloneComponent(IChatBaseComponent chatComponent, String newText) { + ChatComponentText clonedChatComponent = new ChatComponentText(newText); + clonedChatComponent.setChatModifier(chatComponent.getChatModifier().clone()); + return clonedChatComponent; + } + + } + + } + } diff --git a/NMS/v1_13_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R2/EntityNMSArmorStand.java b/NMS/v1_13_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R2/EntityNMSArmorStand.java index dc99416d..e16de272 100644 --- a/NMS/v1_13_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R2/EntityNMSArmorStand.java +++ b/NMS/v1_13_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R2/EntityNMSArmorStand.java @@ -185,10 +185,15 @@ public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorSta } @Override - public String getCustomNameNMS() { + public String getCustomNameStringNMS() { return this.customName; } + @Override + public Object getCustomNameObjectNMS() { + return super.getCustomName(); + } + @Override public void setLockTick(boolean lock) { lockTick = lock; diff --git a/NMS/v1_13_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R2/NmsManagerImpl.java b/NMS/v1_13_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R2/NmsManagerImpl.java index ac91b666..975490c0 100644 --- a/NMS/v1_13_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R2/NmsManagerImpl.java +++ b/NMS/v1_13_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R2/NmsManagerImpl.java @@ -16,6 +16,7 @@ package com.gmail.filoghost.holographicdisplays.nms.v1_13_R2; import java.lang.reflect.Method; import java.util.List; + import org.bukkit.Bukkit; import org.bukkit.craftbukkit.v1_13_R2.CraftWorld; import org.bukkit.craftbukkit.v1_13_R2.entity.CraftEntity; @@ -23,7 +24,9 @@ import org.bukkit.inventory.ItemStack; import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.ChatComponentAdapter; import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.CustomNameHelper; import com.gmail.filoghost.holographicdisplays.nms.interfaces.NMSManager; import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; @@ -33,8 +36,10 @@ import com.gmail.filoghost.holographicdisplays.util.Validator; import com.gmail.filoghost.holographicdisplays.util.VersionUtils; import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; +import net.minecraft.server.v1_13_R2.ChatComponentText; import net.minecraft.server.v1_13_R2.Entity; import net.minecraft.server.v1_13_R2.EntityTypes; +import net.minecraft.server.v1_13_R2.IChatBaseComponent; import net.minecraft.server.v1_13_R2.IRegistry; import net.minecraft.server.v1_13_R2.MathHelper; import net.minecraft.server.v1_13_R2.RegistryID; @@ -169,4 +174,43 @@ public class NmsManagerImpl implements NMSManager { return nmsEntity.getBukkitEntity(); } + @Override + public Object replaceCustomNameText(Object customNameObject, String target, String replacement) { + return CustomNameHelper.replaceCustomNameChatComponent(NMSChatComponentAdapter.INSTANCE, customNameObject, target, replacement); + } + + private static enum NMSChatComponentAdapter implements ChatComponentAdapter { + + INSTANCE { + + public ChatComponentText cast(Object chatComponentObject) { + return (ChatComponentText) chatComponentObject; + } + + @Override + public String getText(IChatBaseComponent chatComponent) { + return chatComponent.getText(); + } + + @Override + public List getSiblings(IChatBaseComponent chatComponent) { + return chatComponent.a(); + } + + @Override + public void addSibling(IChatBaseComponent chatComponent, IChatBaseComponent newSibling) { + chatComponent.addSibling(newSibling); + } + + @Override + public ChatComponentText cloneComponent(IChatBaseComponent chatComponent, String newText) { + ChatComponentText clonedChatComponent = new ChatComponentText(newText); + clonedChatComponent.setChatModifier(chatComponent.getChatModifier().clone()); + return clonedChatComponent; + } + + } + + } + } diff --git a/NMS/v1_14_R1/pom.xml b/NMS/v1_14_R1/pom.xml index 13739c1d..025b523a 100644 --- a/NMS/v1_14_R1/pom.xml +++ b/NMS/v1_14_R1/pom.xml @@ -42,7 +42,7 @@ org.spigotmc spigot - 1.14-R0.1-SNAPSHOT + 1.14.4-R0.1-SNAPSHOT provided diff --git a/NMS/v1_14_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_14_R1/EntityNMSArmorStand.java b/NMS/v1_14_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_14_R1/EntityNMSArmorStand.java index c64473fa..43ee1c28 100644 --- a/NMS/v1_14_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_14_R1/EntityNMSArmorStand.java +++ b/NMS/v1_14_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_14_R1/EntityNMSArmorStand.java @@ -187,10 +187,15 @@ public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorSta } @Override - public String getCustomNameNMS() { + public String getCustomNameStringNMS() { return this.customName; } + @Override + public Object getCustomNameObjectNMS() { + return super.getCustomName(); + } + @Override public void setLockTick(boolean lock) { lockTick = lock; diff --git a/NMS/v1_14_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_14_R1/NmsManagerImpl.java b/NMS/v1_14_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_14_R1/NmsManagerImpl.java index 1d305cf9..66c20283 100644 --- a/NMS/v1_14_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_14_R1/NmsManagerImpl.java +++ b/NMS/v1_14_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_14_R1/NmsManagerImpl.java @@ -14,6 +14,8 @@ */ package com.gmail.filoghost.holographicdisplays.nms.v1_14_R1; +import java.util.List; + import org.bukkit.Bukkit; import org.bukkit.craftbukkit.v1_14_R1.CraftWorld; import org.bukkit.craftbukkit.v1_14_R1.entity.CraftEntity; @@ -21,6 +23,8 @@ import org.bukkit.inventory.ItemStack; import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.ChatComponentAdapter; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.CustomNameHelper; import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; import com.gmail.filoghost.holographicdisplays.nms.interfaces.NMSManager; import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; @@ -31,9 +35,12 @@ import com.gmail.filoghost.holographicdisplays.util.Validator; import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectMethod; +import net.minecraft.server.v1_14_R1.ChatBaseComponent; +import net.minecraft.server.v1_14_R1.ChatComponentText; import net.minecraft.server.v1_14_R1.Entity; import net.minecraft.server.v1_14_R1.EntityTypes; import net.minecraft.server.v1_14_R1.EnumCreatureType; +import net.minecraft.server.v1_14_R1.IChatBaseComponent; import net.minecraft.server.v1_14_R1.IRegistry; import net.minecraft.server.v1_14_R1.MathHelper; import net.minecraft.server.v1_14_R1.RegistryID; @@ -148,4 +155,60 @@ public class NmsManagerImpl implements NMSManager { return nmsEntity.getBukkitEntity(); } + @Override + public Object replaceCustomNameText(Object customNameObject, String target, String replacement) { + return CustomNameHelper.replaceCustomNameChatComponent(NMSChatComponentAdapter.INSTANCE, customNameObject, target, replacement); + } + + private static enum NMSChatComponentAdapter implements ChatComponentAdapter { + + INSTANCE { + + private boolean useNewGetSiblingsMethod = true; + private final ReflectField> OLD_SIBLINGS_FIELD = new ReflectField<>(ChatBaseComponent.class, "a"); + + public ChatComponentText cast(Object chatComponentObject) { + return (ChatComponentText) chatComponentObject; + } + + @Override + public String getText(IChatBaseComponent chatComponent) { + return chatComponent.getText(); + } + + @Override + public List getSiblings(IChatBaseComponent chatComponent) { + if (useNewGetSiblingsMethod) { + try { + return chatComponent.getSiblings(); + } catch (NoSuchMethodError e) { + // The method was named differently in older 1.14 versions, use workaround. + useNewGetSiblingsMethod = false; + } + } + + // Access siblings field directly in older 1.14 versions. + try { + return OLD_SIBLINGS_FIELD.get(chatComponent); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Override + public void addSibling(IChatBaseComponent chatComponent, IChatBaseComponent newSibling) { + chatComponent.addSibling(newSibling); + } + + @Override + public ChatComponentText cloneComponent(IChatBaseComponent chatComponent, String newText) { + ChatComponentText clonedChatComponent = new ChatComponentText(newText); + clonedChatComponent.setChatModifier(chatComponent.getChatModifier().clone()); + return clonedChatComponent; + } + + } + + } + } diff --git a/NMS/v1_15_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_15_R1/EntityNMSArmorStand.java b/NMS/v1_15_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_15_R1/EntityNMSArmorStand.java index c031dba8..d72ad137 100644 --- a/NMS/v1_15_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_15_R1/EntityNMSArmorStand.java +++ b/NMS/v1_15_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_15_R1/EntityNMSArmorStand.java @@ -170,9 +170,14 @@ public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorSta } @Override - public String getCustomNameNMS() { + public String getCustomNameStringNMS() { return this.customName; } + + @Override + public Object getCustomNameObjectNMS() { + return super.getCustomName(); + } @Override public void setLockTick(boolean lock) { diff --git a/NMS/v1_15_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_15_R1/NmsManagerImpl.java b/NMS/v1_15_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_15_R1/NmsManagerImpl.java index 4d8a9009..b685ba39 100644 --- a/NMS/v1_15_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_15_R1/NmsManagerImpl.java +++ b/NMS/v1_15_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_15_R1/NmsManagerImpl.java @@ -14,6 +14,8 @@ */ package com.gmail.filoghost.holographicdisplays.nms.v1_15_R1; +import java.util.List; + import org.bukkit.Bukkit; import org.bukkit.craftbukkit.v1_15_R1.CraftWorld; import org.bukkit.craftbukkit.v1_15_R1.entity.CraftEntity; @@ -21,7 +23,9 @@ import org.bukkit.inventory.ItemStack; import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.ChatComponentAdapter; import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.CustomNameHelper; import com.gmail.filoghost.holographicdisplays.nms.interfaces.NMSManager; import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; @@ -31,9 +35,11 @@ import com.gmail.filoghost.holographicdisplays.util.Validator; import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectMethod; +import net.minecraft.server.v1_15_R1.ChatComponentText; import net.minecraft.server.v1_15_R1.Entity; import net.minecraft.server.v1_15_R1.EntityTypes; import net.minecraft.server.v1_15_R1.EnumCreatureType; +import net.minecraft.server.v1_15_R1.IChatBaseComponent; import net.minecraft.server.v1_15_R1.IRegistry; import net.minecraft.server.v1_15_R1.MathHelper; import net.minecraft.server.v1_15_R1.RegistryID; @@ -148,4 +154,43 @@ public class NmsManagerImpl implements NMSManager { return nmsEntity.getBukkitEntity(); } + @Override + public Object replaceCustomNameText(Object customNameObject, String target, String replacement) { + return CustomNameHelper.replaceCustomNameChatComponent(NMSChatComponentAdapter.INSTANCE, customNameObject, target, replacement); + } + + private static enum NMSChatComponentAdapter implements ChatComponentAdapter { + + INSTANCE { + + public ChatComponentText cast(Object chatComponentObject) { + return (ChatComponentText) chatComponentObject; + } + + @Override + public String getText(IChatBaseComponent chatComponent) { + return chatComponent.getText(); + } + + @Override + public List getSiblings(IChatBaseComponent chatComponent) { + return chatComponent.getSiblings(); + } + + @Override + public void addSibling(IChatBaseComponent chatComponent, IChatBaseComponent newSibling) { + chatComponent.addSibling(newSibling); + } + + @Override + public ChatComponentText cloneComponent(IChatBaseComponent chatComponent, String newText) { + ChatComponentText clonedChatComponent = new ChatComponentText(newText); + clonedChatComponent.setChatModifier(chatComponent.getChatModifier().clone()); + return clonedChatComponent; + } + + } + + } + } diff --git a/NMS/v1_8_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R2/EntityNMSArmorStand.java b/NMS/v1_8_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R2/EntityNMSArmorStand.java index 9e13e7fb..4ed18c7b 100644 --- a/NMS/v1_8_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R2/EntityNMSArmorStand.java +++ b/NMS/v1_8_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R2/EntityNMSArmorStand.java @@ -173,10 +173,15 @@ public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorSta } @Override - public String getCustomNameNMS() { + public String getCustomNameStringNMS() { return this.customName; } + @Override + public Object getCustomNameObjectNMS() { + return super.getCustomName(); + } + @Override public void setLockTick(boolean lock) { lockTick = lock; diff --git a/NMS/v1_8_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R2/NmsManagerImpl.java b/NMS/v1_8_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R2/NmsManagerImpl.java index 48c8f59d..162d3232 100644 --- a/NMS/v1_8_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R2/NmsManagerImpl.java +++ b/NMS/v1_8_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R2/NmsManagerImpl.java @@ -26,6 +26,7 @@ import org.bukkit.inventory.ItemStack; import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.CustomNameHelper; import com.gmail.filoghost.holographicdisplays.nms.interfaces.NMSManager; import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; @@ -152,4 +153,9 @@ public class NmsManagerImpl implements NMSManager { return nmsEntity.getBukkitEntity(); } + @Override + public Object replaceCustomNameText(Object customNameObject, String target, String replacement) { + return CustomNameHelper.replaceCustomNameString(customNameObject, target, replacement); + } + } diff --git a/NMS/v1_8_R3/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R3/EntityNMSArmorStand.java b/NMS/v1_8_R3/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R3/EntityNMSArmorStand.java index 5953628d..c328c3f3 100644 --- a/NMS/v1_8_R3/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R3/EntityNMSArmorStand.java +++ b/NMS/v1_8_R3/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R3/EntityNMSArmorStand.java @@ -173,10 +173,15 @@ public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorSta } @Override - public String getCustomNameNMS() { + public String getCustomNameStringNMS() { return this.customName; } + @Override + public Object getCustomNameObjectNMS() { + return super.getCustomName(); + } + @Override public void setLockTick(boolean lock) { lockTick = lock; diff --git a/NMS/v1_8_R3/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R3/NmsManagerImpl.java b/NMS/v1_8_R3/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R3/NmsManagerImpl.java index 870b0e89..70f56234 100644 --- a/NMS/v1_8_R3/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R3/NmsManagerImpl.java +++ b/NMS/v1_8_R3/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R3/NmsManagerImpl.java @@ -26,6 +26,7 @@ import org.bukkit.inventory.ItemStack; import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.CustomNameHelper; import com.gmail.filoghost.holographicdisplays.nms.interfaces.NMSManager; import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; @@ -151,5 +152,10 @@ public class NmsManagerImpl implements NMSManager { return nmsEntity.getBukkitEntity(); } + + @Override + public Object replaceCustomNameText(Object customNameObject, String target, String replacement) { + return CustomNameHelper.replaceCustomNameString(customNameObject, target, replacement); + } } diff --git a/NMS/v1_9_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R1/EntityNMSArmorStand.java b/NMS/v1_9_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R1/EntityNMSArmorStand.java index c1b36dc5..3e7ddcc7 100644 --- a/NMS/v1_9_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R1/EntityNMSArmorStand.java +++ b/NMS/v1_9_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R1/EntityNMSArmorStand.java @@ -182,10 +182,15 @@ public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorSta } @Override - public String getCustomNameNMS() { + public String getCustomNameStringNMS() { return this.customName; } + @Override + public Object getCustomNameObjectNMS() { + return super.getCustomName(); + } + @Override public void setLockTick(boolean lock) { lockTick = lock; diff --git a/NMS/v1_9_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R1/NmsManagerImpl.java b/NMS/v1_9_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R1/NmsManagerImpl.java index efd4f7ca..889ea9aa 100644 --- a/NMS/v1_9_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R1/NmsManagerImpl.java +++ b/NMS/v1_9_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R1/NmsManagerImpl.java @@ -25,6 +25,7 @@ import org.bukkit.inventory.ItemStack; import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.CustomNameHelper; import com.gmail.filoghost.holographicdisplays.nms.interfaces.NMSManager; import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; @@ -147,4 +148,9 @@ public class NmsManagerImpl implements NMSManager { return nmsEntity.getBukkitEntity(); } + @Override + public Object replaceCustomNameText(Object customNameObject, String target, String replacement) { + return CustomNameHelper.replaceCustomNameString(customNameObject, target, replacement); + } + } diff --git a/NMS/v1_9_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R2/EntityNMSArmorStand.java b/NMS/v1_9_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R2/EntityNMSArmorStand.java index 8966d2ec..4f83af8c 100644 --- a/NMS/v1_9_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R2/EntityNMSArmorStand.java +++ b/NMS/v1_9_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R2/EntityNMSArmorStand.java @@ -174,10 +174,15 @@ public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorSta } @Override - public String getCustomNameNMS() { + public String getCustomNameStringNMS() { return this.customName; } + @Override + public Object getCustomNameObjectNMS() { + return super.getCustomName(); + } + @Override public void setLockTick(boolean lock) { lockTick = lock; diff --git a/NMS/v1_9_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R2/NmsManagerImpl.java b/NMS/v1_9_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R2/NmsManagerImpl.java index 38846878..48345523 100644 --- a/NMS/v1_9_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R2/NmsManagerImpl.java +++ b/NMS/v1_9_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R2/NmsManagerImpl.java @@ -25,6 +25,7 @@ import org.bukkit.inventory.ItemStack; import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.CustomNameHelper; import com.gmail.filoghost.holographicdisplays.nms.interfaces.NMSManager; import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; @@ -147,4 +148,9 @@ public class NmsManagerImpl implements NMSManager { return nmsEntity.getBukkitEntity(); } + @Override + public Object replaceCustomNameText(Object customNameObject, String target, String replacement) { + return CustomNameHelper.replaceCustomNameString(customNameObject, target, replacement); + } + } diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/protocollib/current/MetadataHelper.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/protocollib/current/MetadataHelper.java index 4fc8ba1e..e32fe7d6 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/protocollib/current/MetadataHelper.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/protocollib/current/MetadataHelper.java @@ -18,7 +18,6 @@ import java.util.List; import java.util.Optional; import com.comphenix.protocol.utility.MinecraftReflection; -import com.comphenix.protocol.wrappers.WrappedChatComponent; import com.comphenix.protocol.wrappers.WrappedDataWatcher; import com.comphenix.protocol.wrappers.WrappedDataWatcher.Registry; import com.comphenix.protocol.wrappers.WrappedDataWatcher.Serializer; @@ -82,6 +81,12 @@ public class MetadataHelper { } } + + public void setEntityStatus(WrappedDataWatcher dataWatcher, byte statusBitmask) { + requireMinimumVersion(NMSVersion.v1_9_R1); + dataWatcher.setObject(new WrappedDataWatcherObject(entityStatusIndex, byteSerializer), statusBitmask); + } + public WrappedWatchableObject getCustomNameWacthableObject(WrappedDataWatcher metadata) { return metadata.getWatchableObject(customNameIndex); @@ -101,54 +106,45 @@ public class MetadataHelper { } - public String getSerializedCustomName(WrappedWatchableObject customNameWatchableObject) { - Object customNameWatchableObjectValue = customNameWatchableObject.getValue(); + public Object getCustomNameNMSObject(WrappedWatchableObject customNameWatchableObject) { + Object customNameNMSObject = customNameWatchableObject.getRawValue(); + if (customNameNMSObject == null) { + return null; + } if (NMSVersion.isGreaterEqualThan(NMSVersion.v1_13_R1)) { - if (!(customNameWatchableObjectValue instanceof Optional)) { - return null; + if (!(customNameNMSObject instanceof Optional)) { + throw new IllegalArgumentException("Expected custom name of type " + Optional.class); } - Optional customNameOptional = (Optional) customNameWatchableObjectValue; - if (!customNameOptional.isPresent()) { - return null; + return ((Optional) customNameNMSObject).orElse(null); + + } else { + if (!(customNameNMSObject instanceof String)) { + throw new IllegalArgumentException("Expected custom name of type " + String.class); } - WrappedChatComponent componentWrapper = WrappedChatComponent.fromHandle(customNameOptional.get()); - return componentWrapper.getJson(); - - } else { - if (!(customNameWatchableObjectValue instanceof String)) { - return null; - } - - return (String) customNameWatchableObjectValue; + return (String) customNameNMSObject; } } - public void setSerializedCustomName(WrappedWatchableObject customNameWatchableObject, String serializedCustomName) { + public void setCustomNameNMSObject(WrappedWatchableObject customNameWatchableObject, Object customNameNMSObject) { if (NMSVersion.isGreaterEqualThan(NMSVersion.v1_13_R1)) { - customNameWatchableObject.setValue(Optional.of(WrappedChatComponent.fromJson(serializedCustomName).getHandle())); + customNameWatchableObject.setValue(Optional.ofNullable(customNameNMSObject)); } else { - customNameWatchableObject.setValue(serializedCustomName); + customNameWatchableObject.setValue(customNameNMSObject); } } - - - public void setEntityStatus(WrappedDataWatcher dataWatcher, byte statusBitmask) { - requireMinimumVersion(NMSVersion.v1_9_R1); - dataWatcher.setObject(new WrappedDataWatcherObject(entityStatusIndex, byteSerializer), statusBitmask); - } - public void setCustomName(WrappedDataWatcher dataWatcher, String customName) { + public void setCustomNameNMSObject(WrappedDataWatcher dataWatcher, Object customNameNMSObject) { requireMinimumVersion(NMSVersion.v1_9_R1); if (NMSVersion.isGreaterEqualThan(NMSVersion.v1_13_R1)) { - dataWatcher.setObject(new WrappedDataWatcherObject(customNameIndex, chatComponentSerializer), Optional.of(WrappedChatComponent.fromText(customName).getHandle())); + dataWatcher.setObject(new WrappedDataWatcherObject(customNameIndex, chatComponentSerializer), Optional.ofNullable(customNameNMSObject)); } else { - dataWatcher.setObject(new WrappedDataWatcherObject(customNameIndex, stringSerializer), customName); + dataWatcher.setObject(new WrappedDataWatcherObject(customNameIndex, stringSerializer), customNameNMSObject); } } diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/protocollib/current/PacketHelper.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/protocollib/current/PacketHelper.java index 3f6b1e25..e58b0276 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/protocollib/current/PacketHelper.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/protocollib/current/PacketHelper.java @@ -56,9 +56,9 @@ public class PacketHelper { metadataHelper.setEntityStatus(dataWatcher, (byte) 0x20); // Invisible - String customName = armorStand.getCustomNameNMS(); + String customName = armorStand.getCustomNameStringNMS(); if (customName != null && !customName.isEmpty()) { - metadataHelper.setCustomName(dataWatcher, customName); + metadataHelper.setCustomNameNMSObject(dataWatcher, armorStand.getCustomNameObjectNMS()); metadataHelper.setCustomNameVisible(dataWatcher, true); } diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/protocollib/current/ProtocolLibHookImpl.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/protocollib/current/ProtocolLibHookImpl.java index bf983b0e..3c0d6ee4 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/protocollib/current/ProtocolLibHookImpl.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/protocollib/current/ProtocolLibHookImpl.java @@ -196,19 +196,25 @@ public class ProtocolLibHookImpl implements ProtocolLibHook { private boolean replaceRelativePlaceholders(WrappedWatchableObject customNameWatchableObject, Player player, Collection relativePlaceholders) { if (customNameWatchableObject == null) { - return true; - } - - String customName = metadataHelper.getSerializedCustomName(customNameWatchableObject); - if (customName == null) { return false; } - for (RelativePlaceholder relativePlaceholder : relativePlaceholders) { - customName = customName.replace(relativePlaceholder.getTextPlaceholder(), relativePlaceholder.getReplacement(player)); + final Object originalCustomNameNMSObject = metadataHelper.getCustomNameNMSObject(customNameWatchableObject); + if (originalCustomNameNMSObject == null) { + return false; } - metadataHelper.setSerializedCustomName(customNameWatchableObject, customName); + Object replacedCustomNameNMSObject = originalCustomNameNMSObject; + for (RelativePlaceholder relativePlaceholder : relativePlaceholders) { + replacedCustomNameNMSObject = nmsManager.replaceCustomNameText(replacedCustomNameNMSObject, relativePlaceholder.getTextPlaceholder(), relativePlaceholder.getReplacement(player)); + } + + if (replacedCustomNameNMSObject == originalCustomNameNMSObject) { + // It means nothing has been replaced, since original custom name has been returned. + return false; + } + + metadataHelper.setCustomNameNMSObject(customNameWatchableObject, replacedCustomNameNMSObject); return true; } diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/placeholder/PlaceholdersManager.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/placeholder/PlaceholdersManager.java index b2243d74..e7c529fe 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/placeholder/PlaceholdersManager.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/placeholder/PlaceholdersManager.java @@ -345,7 +345,7 @@ public class PlaceholdersManager { private static void updatePlaceholders(DynamicLineData lineData) { - String oldCustomName = lineData.getEntity().getCustomNameNMS(); + String oldCustomName = lineData.getEntity().getCustomNameStringNMS(); String newCustomName = lineData.getOriginalName(); if (!lineData.getPlaceholders().isEmpty()) {