mirror of
https://github.com/filoghost/HolographicDisplays.git
synced 2024-12-20 15:57:49 +01:00
Handle custom name in newer versions without serializing from/to JSON
This commit is contained in:
parent
104d9abd6c
commit
735c92a612
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.gmail.filoghost.holographicdisplays.nms.interfaces;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface ChatComponentAdapter<T> {
|
||||
|
||||
T cast(Object chatComponentObject);
|
||||
|
||||
String getText(T chatComponent);
|
||||
|
||||
List<T> 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);
|
||||
|
||||
}
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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> T replaceCustomNameChatComponent(ChatComponentAdapter<T> 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<T> 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;
|
||||
}
|
||||
|
||||
}
|
@ -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);
|
||||
|
||||
}
|
||||
|
@ -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();
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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<IChatBaseComponent> {
|
||||
|
||||
INSTANCE {
|
||||
|
||||
public ChatComponentText cast(Object chatComponentObject) {
|
||||
return (ChatComponentText) chatComponentObject;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getText(IChatBaseComponent chatComponent) {
|
||||
return chatComponent.getText();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<IChatBaseComponent> 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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<IChatBaseComponent> {
|
||||
|
||||
INSTANCE {
|
||||
|
||||
public ChatComponentText cast(Object chatComponentObject) {
|
||||
return (ChatComponentText) chatComponentObject;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getText(IChatBaseComponent chatComponent) {
|
||||
return chatComponent.getText();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<IChatBaseComponent> 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -42,7 +42,7 @@
|
||||
<dependency>
|
||||
<groupId>org.spigotmc</groupId>
|
||||
<artifactId>spigot</artifactId>
|
||||
<version>1.14-R0.1-SNAPSHOT</version>
|
||||
<version>1.14.4-R0.1-SNAPSHOT</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
@ -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;
|
||||
|
@ -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<IChatBaseComponent> {
|
||||
|
||||
INSTANCE {
|
||||
|
||||
private boolean useNewGetSiblingsMethod = true;
|
||||
private final ReflectField<List<IChatBaseComponent>> 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<IChatBaseComponent> 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -170,10 +170,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;
|
||||
|
@ -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<IChatBaseComponent> {
|
||||
|
||||
INSTANCE {
|
||||
|
||||
public ChatComponentText cast(Object chatComponentObject) {
|
||||
return (ChatComponentText) chatComponentObject;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getText(IChatBaseComponent chatComponent) {
|
||||
return chatComponent.getText();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<IChatBaseComponent> 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
@ -83,6 +82,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;
|
||||
}
|
||||
|
||||
WrappedChatComponent componentWrapper = WrappedChatComponent.fromHandle(customNameOptional.get());
|
||||
return componentWrapper.getJson();
|
||||
return ((Optional<?>) customNameNMSObject).orElse(null);
|
||||
|
||||
} else {
|
||||
if (!(customNameWatchableObjectValue instanceof String)) {
|
||||
return null;
|
||||
if (!(customNameNMSObject instanceof String)) {
|
||||
throw new IllegalArgumentException("Expected custom name of type " + String.class);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -196,19 +196,25 @@ public class ProtocolLibHookImpl implements ProtocolLibHook {
|
||||
|
||||
private boolean replaceRelativePlaceholders(WrappedWatchableObject customNameWatchableObject, Player player, Collection<RelativePlaceholder> 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;
|
||||
}
|
||||
|
||||
|
@ -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()) {
|
||||
|
Loading…
Reference in New Issue
Block a user