Simplify relative placeholder replacement

This commit is contained in:
filoghost 2021-05-24 09:50:31 +02:00
parent e8c4f58d9a
commit ffd0351a70
33 changed files with 143 additions and 478 deletions

View File

@ -1,77 +0,0 @@
/*
* Copyright (C) filoghost and contributors
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package me.filoghost.holographicdisplays.core.nms;
import java.util.List;
public interface ChatComponentCustomNameEditor<T> extends CustomNameEditor {
@Override
default T replaceCustomName(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.
@SuppressWarnings("unchecked")
T rootComponent = (T) customNameObject;
if (!getText(rootComponent).isEmpty()) {
throw new IllegalArgumentException("Expected root component with empty text");
}
boolean[] childrenContainingTarget = null;
List<T> children = getSiblings(rootComponent);
int childrenSize = children.size();
for (int i = 0; i < childrenSize; i++) {
T childComponent = children.get(i);
if (!getSiblings(childComponent).isEmpty()) {
throw new IllegalArgumentException("Expected child component without sub-nodes");
}
if (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 = cloneComponent(rootComponent);
for (int i = 0; i < childrenSize; i++) {
T childComponent = children.get(i);
String newText = getText(childComponent);
if (childrenContainingTarget[i]) {
newText = newText.replace(target, replacement);
}
T clonedChild = cloneComponent(childComponent, newText);
addSibling(clonedRoot, clonedChild);
}
return clonedRoot;
}
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);
}

View File

@ -1,12 +0,0 @@
/*
* Copyright (C) filoghost and contributors
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package me.filoghost.holographicdisplays.core.nms;
public interface CustomNameEditor {
Object replaceCustomName(Object customNameObject, String target, String replacement);
}

View File

@ -39,6 +39,6 @@ public interface NMSManager {
NMSEntity getNMSEntityBaseFromID(World bukkitWorld, int entityID);
CustomNameEditor getCustomNameEditor();
Object createCustomNameNMSObject(String customName);
}

View File

@ -1,22 +0,0 @@
/*
* Copyright (C) filoghost and contributors
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package me.filoghost.holographicdisplays.core.nms;
public enum StringCustomNameEditor implements CustomNameEditor {
INSTANCE;
@Override
public String replaceCustomName(Object customNameNMSObject, String target, String replacement) {
String customName = (String) customNameNMSObject;
if (customName.contains(target)) {
return customName.replace(target, replacement);
} else {
return customName;
}
}
}

View File

@ -173,9 +173,13 @@ public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorSta
return;
}
this.customName = customName;
super.setCustomName(customName != null ? Strings.truncate(customName, 256) : "");
super.setCustomName(createCustomNameNMSObject(customName));
super.setCustomNameVisible(customName != null && !customName.isEmpty());
}
protected static String createCustomNameNMSObject(String customName) {
return customName != null ? Strings.truncate(customName, 256) : "";
}
@Override
public String getCustomNameStringNMS() {

View File

@ -11,11 +11,9 @@ import me.filoghost.fcommons.reflection.ReflectField;
import me.filoghost.fcommons.reflection.ReflectMethod;
import me.filoghost.holographicdisplays.core.hologram.StandardHologramLine;
import me.filoghost.holographicdisplays.core.hologram.StandardItemLine;
import me.filoghost.holographicdisplays.core.nms.CustomNameEditor;
import me.filoghost.holographicdisplays.core.nms.NMSManager;
import me.filoghost.holographicdisplays.core.nms.ProtocolPacketSettings;
import me.filoghost.holographicdisplays.core.nms.SpawnFailedException;
import me.filoghost.holographicdisplays.core.nms.StringCustomNameEditor;
import me.filoghost.holographicdisplays.core.nms.entity.NMSArmorStand;
import me.filoghost.holographicdisplays.core.nms.entity.NMSEntity;
import me.filoghost.holographicdisplays.core.nms.entity.NMSItem;
@ -143,10 +141,10 @@ public class VersionNMSManager implements NMSManager {
return null;
}
}
@Override
public CustomNameEditor getCustomNameEditor() {
return StringCustomNameEditor.INSTANCE;
public Object createCustomNameNMSObject(String customName) {
return EntityNMSArmorStand.createCustomNameNMSObject(customName);
}
}

View File

@ -173,9 +173,13 @@ public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorSta
return;
}
this.customName = customName;
super.setCustomName(customName != null ? Strings.truncate(customName, 256) : "");
super.setCustomName(createCustomNameNMSObject(customName));
super.setCustomNameVisible(customName != null && !customName.isEmpty());
}
protected static String createCustomNameNMSObject(String customName) {
return customName != null ? Strings.truncate(customName, 256) : "";
}
@Override
public String getCustomNameStringNMS() {

View File

@ -11,11 +11,9 @@ import me.filoghost.fcommons.reflection.ReflectField;
import me.filoghost.fcommons.reflection.ReflectMethod;
import me.filoghost.holographicdisplays.core.hologram.StandardHologramLine;
import me.filoghost.holographicdisplays.core.hologram.StandardItemLine;
import me.filoghost.holographicdisplays.core.nms.CustomNameEditor;
import me.filoghost.holographicdisplays.core.nms.NMSManager;
import me.filoghost.holographicdisplays.core.nms.ProtocolPacketSettings;
import me.filoghost.holographicdisplays.core.nms.SpawnFailedException;
import me.filoghost.holographicdisplays.core.nms.StringCustomNameEditor;
import me.filoghost.holographicdisplays.core.nms.entity.NMSArmorStand;
import me.filoghost.holographicdisplays.core.nms.entity.NMSEntity;
import me.filoghost.holographicdisplays.core.nms.entity.NMSItem;
@ -151,10 +149,10 @@ public class VersionNMSManager implements NMSManager {
return null;
}
}
@Override
public CustomNameEditor getCustomNameEditor() {
return StringCustomNameEditor.INSTANCE;
public Object createCustomNameNMSObject(String customName) {
return EntityNMSArmorStand.createCustomNameNMSObject(customName);
}
}

View File

@ -173,9 +173,13 @@ public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorSta
return;
}
this.customName = customName;
super.setCustomName(customName != null ? Strings.truncate(customName, 256) : "");
super.setCustomName(createCustomNameNMSObject(customName));
super.setCustomNameVisible(customName != null && !customName.isEmpty());
}
protected static String createCustomNameNMSObject(String customName) {
return customName != null ? Strings.truncate(customName, 256) : "";
}
@Override
public String getCustomNameStringNMS() {

View File

@ -11,11 +11,9 @@ import me.filoghost.fcommons.reflection.ReflectField;
import me.filoghost.fcommons.reflection.ReflectMethod;
import me.filoghost.holographicdisplays.core.hologram.StandardHologramLine;
import me.filoghost.holographicdisplays.core.hologram.StandardItemLine;
import me.filoghost.holographicdisplays.core.nms.CustomNameEditor;
import me.filoghost.holographicdisplays.core.nms.NMSManager;
import me.filoghost.holographicdisplays.core.nms.ProtocolPacketSettings;
import me.filoghost.holographicdisplays.core.nms.SpawnFailedException;
import me.filoghost.holographicdisplays.core.nms.StringCustomNameEditor;
import me.filoghost.holographicdisplays.core.nms.entity.NMSArmorStand;
import me.filoghost.holographicdisplays.core.nms.entity.NMSEntity;
import me.filoghost.holographicdisplays.core.nms.entity.NMSItem;
@ -151,10 +149,10 @@ public class VersionNMSManager implements NMSManager {
return null;
}
}
@Override
public CustomNameEditor getCustomNameEditor() {
return StringCustomNameEditor.INSTANCE;
public Object createCustomNameNMSObject(String customName) {
return EntityNMSArmorStand.createCustomNameNMSObject(customName);
}
}

View File

@ -175,9 +175,13 @@ public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorSta
return;
}
this.customName = customName;
super.setCustomName(CraftChatMessage.fromStringOrNull(Strings.truncate(customName, 300)));
super.setCustomName(createCustomNameNMSObject(customName));
super.setCustomNameVisible(customName != null && !customName.isEmpty());
}
protected static IChatBaseComponent createCustomNameNMSObject(String customName) {
return CraftChatMessage.fromStringOrNull(Strings.truncate(customName, 300));
}
@Override
public String getCustomNameStringNMS() {

View File

@ -11,8 +11,6 @@ import me.filoghost.fcommons.reflection.ReflectField;
import me.filoghost.fcommons.reflection.ReflectMethod;
import me.filoghost.holographicdisplays.core.hologram.StandardHologramLine;
import me.filoghost.holographicdisplays.core.hologram.StandardItemLine;
import me.filoghost.holographicdisplays.core.nms.ChatComponentCustomNameEditor;
import me.filoghost.holographicdisplays.core.nms.CustomNameEditor;
import me.filoghost.holographicdisplays.core.nms.NMSCommons;
import me.filoghost.holographicdisplays.core.nms.NMSManager;
import me.filoghost.holographicdisplays.core.nms.ProtocolPacketSettings;
@ -20,10 +18,8 @@ import me.filoghost.holographicdisplays.core.nms.SpawnFailedException;
import me.filoghost.holographicdisplays.core.nms.entity.NMSArmorStand;
import me.filoghost.holographicdisplays.core.nms.entity.NMSEntity;
import me.filoghost.holographicdisplays.core.nms.entity.NMSItem;
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;
@ -169,38 +165,10 @@ public class VersionNMSManager implements NMSManager {
return null;
}
}
@Override
public CustomNameEditor getCustomNameEditor() {
return VersionChatComponentCustomNameEditor.INSTANCE;
}
private enum VersionChatComponentCustomNameEditor implements ChatComponentCustomNameEditor<IChatBaseComponent> {
INSTANCE;
@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;
}
public Object createCustomNameNMSObject(String customName) {
return EntityNMSArmorStand.createCustomNameNMSObject(customName);
}
}

View File

@ -175,9 +175,13 @@ public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorSta
return;
}
this.customName = customName;
super.setCustomName(CraftChatMessage.fromStringOrNull(Strings.truncate(customName, 300)));
super.setCustomName(createCustomNameNMSObject(customName));
super.setCustomNameVisible(customName != null && !customName.isEmpty());
}
protected static IChatBaseComponent createCustomNameNMSObject(String customName) {
return CraftChatMessage.fromStringOrNull(Strings.truncate(customName, 300));
}
@Override
public String getCustomNameStringNMS() {

View File

@ -9,8 +9,8 @@ import me.filoghost.fcommons.Preconditions;
import me.filoghost.fcommons.reflection.ClassToken;
import me.filoghost.fcommons.reflection.ReflectField;
import me.filoghost.fcommons.reflection.ReflectMethod;
import me.filoghost.holographicdisplays.core.nms.CustomNameEditor;
import me.filoghost.holographicdisplays.core.nms.ChatComponentCustomNameEditor;
import me.filoghost.holographicdisplays.core.hologram.StandardHologramLine;
import me.filoghost.holographicdisplays.core.hologram.StandardItemLine;
import me.filoghost.holographicdisplays.core.nms.NMSCommons;
import me.filoghost.holographicdisplays.core.nms.NMSManager;
import me.filoghost.holographicdisplays.core.nms.ProtocolPacketSettings;
@ -18,12 +18,8 @@ import me.filoghost.holographicdisplays.core.nms.SpawnFailedException;
import me.filoghost.holographicdisplays.core.nms.entity.NMSArmorStand;
import me.filoghost.holographicdisplays.core.nms.entity.NMSEntity;
import me.filoghost.holographicdisplays.core.nms.entity.NMSItem;
import me.filoghost.holographicdisplays.core.hologram.StandardItemLine;
import me.filoghost.holographicdisplays.core.hologram.StandardHologramLine;
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;
@ -170,38 +166,10 @@ public class VersionNMSManager implements NMSManager {
return null;
}
}
@Override
public CustomNameEditor getCustomNameEditor() {
return VersionChatComponentCustomNameEditor.INSTANCE;
}
private enum VersionChatComponentCustomNameEditor implements ChatComponentCustomNameEditor<IChatBaseComponent> {
INSTANCE;
@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;
}
public Object createCustomNameNMSObject(String customName) {
return EntityNMSArmorStand.createCustomNameNMSObject(customName);
}
}

View File

@ -177,9 +177,13 @@ public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorSta
return;
}
this.customName = customName;
super.setCustomName(CraftChatMessage.fromStringOrNull(Strings.truncate(customName, 300)));
super.setCustomName(createCustomNameNMSObject(customName));
super.setCustomNameVisible(customName != null && !customName.isEmpty());
}
protected static IChatBaseComponent createCustomNameNMSObject(String customName) {
return CraftChatMessage.fromStringOrNull(Strings.truncate(customName, 300));
}
@Override
public String getCustomNameStringNMS() {

View File

@ -11,20 +11,15 @@ import me.filoghost.fcommons.reflection.ReflectField;
import me.filoghost.fcommons.reflection.ReflectMethod;
import me.filoghost.holographicdisplays.core.hologram.StandardHologramLine;
import me.filoghost.holographicdisplays.core.hologram.StandardItemLine;
import me.filoghost.holographicdisplays.core.nms.ChatComponentCustomNameEditor;
import me.filoghost.holographicdisplays.core.nms.CustomNameEditor;
import me.filoghost.holographicdisplays.core.nms.NMSManager;
import me.filoghost.holographicdisplays.core.nms.ProtocolPacketSettings;
import me.filoghost.holographicdisplays.core.nms.SpawnFailedException;
import me.filoghost.holographicdisplays.core.nms.entity.NMSArmorStand;
import me.filoghost.holographicdisplays.core.nms.entity.NMSEntity;
import me.filoghost.holographicdisplays.core.nms.entity.NMSItem;
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;
@ -36,8 +31,6 @@ import org.bukkit.craftbukkit.v1_14_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_14_R1.entity.CraftEntity;
import org.bukkit.inventory.ItemStack;
import java.util.List;
public class VersionNMSManager implements NMSManager {
private static final ReflectField<RegistryID<EntityTypes<?>>> REGISTRY_ID_FIELD
@ -156,57 +149,10 @@ public class VersionNMSManager implements NMSManager {
return null;
}
}
@Override
public CustomNameEditor getCustomNameEditor() {
return VersionChatComponentCustomNameEditor.INSTANCE;
}
private enum VersionChatComponentCustomNameEditor implements ChatComponentCustomNameEditor<IChatBaseComponent> {
INSTANCE;
private static final ReflectField<List<IChatBaseComponent>> OLD_SIBLINGS_FIELD
= ReflectField.lookup(new ClassToken<List<IChatBaseComponent>>(){}, ChatBaseComponent.class, "a");
private boolean useNewGetSiblingsMethod = true;
@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 (ReflectiveOperationException 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;
}
public Object createCustomNameNMSObject(String customName) {
return EntityNMSArmorStand.createCustomNameNMSObject(customName);
}
}

View File

@ -177,10 +177,14 @@ public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorSta
return;
}
this.customName = customName;
super.setCustomName(CraftChatMessage.fromStringOrNull(Strings.truncate(customName, 300)));
super.setCustomName(createCustomNameNMSObject(customName));
super.setCustomNameVisible(customName != null && !customName.isEmpty());
}
protected static IChatBaseComponent createCustomNameNMSObject(String customName) {
return CraftChatMessage.fromStringOrNull(Strings.truncate(customName, 300));
}
@Override
public String getCustomNameStringNMS() {
return this.customName;

View File

@ -11,19 +11,15 @@ import me.filoghost.fcommons.reflection.ReflectField;
import me.filoghost.fcommons.reflection.ReflectMethod;
import me.filoghost.holographicdisplays.core.hologram.StandardHologramLine;
import me.filoghost.holographicdisplays.core.hologram.StandardItemLine;
import me.filoghost.holographicdisplays.core.nms.ChatComponentCustomNameEditor;
import me.filoghost.holographicdisplays.core.nms.CustomNameEditor;
import me.filoghost.holographicdisplays.core.nms.NMSManager;
import me.filoghost.holographicdisplays.core.nms.ProtocolPacketSettings;
import me.filoghost.holographicdisplays.core.nms.SpawnFailedException;
import me.filoghost.holographicdisplays.core.nms.entity.NMSArmorStand;
import me.filoghost.holographicdisplays.core.nms.entity.NMSEntity;
import me.filoghost.holographicdisplays.core.nms.entity.NMSItem;
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;
@ -35,8 +31,6 @@ import org.bukkit.craftbukkit.v1_15_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_15_R1.entity.CraftEntity;
import org.bukkit.inventory.ItemStack;
import java.util.List;
public class VersionNMSManager implements NMSManager {
private static final ReflectField<RegistryID<EntityTypes<?>>> REGISTRY_ID_FIELD
@ -155,38 +149,10 @@ public class VersionNMSManager implements NMSManager {
return null;
}
}
@Override
public CustomNameEditor getCustomNameEditor() {
return VersionChatComponentCustomNameEditor.INSTANCE;
}
private enum VersionChatComponentCustomNameEditor implements ChatComponentCustomNameEditor<IChatBaseComponent> {
INSTANCE;
@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;
}
public Object createCustomNameNMSObject(String customName) {
return EntityNMSArmorStand.createCustomNameNMSObject(customName);
}
}

View File

@ -177,10 +177,14 @@ public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorSta
return;
}
this.customName = customName;
super.setCustomName(CraftChatMessage.fromStringOrNull(Strings.truncate(customName, 300)));
super.setCustomName(createCustomNameNMSObject(customName));
super.setCustomNameVisible(customName != null && !customName.isEmpty());
}
protected static IChatBaseComponent createCustomNameNMSObject(String customName) {
return CraftChatMessage.fromStringOrNull(Strings.truncate(customName, 300));
}
@Override
public String getCustomNameStringNMS() {
return this.customName;

View File

@ -11,19 +11,15 @@ import me.filoghost.fcommons.reflection.ReflectField;
import me.filoghost.fcommons.reflection.ReflectMethod;
import me.filoghost.holographicdisplays.core.hologram.StandardHologramLine;
import me.filoghost.holographicdisplays.core.hologram.StandardItemLine;
import me.filoghost.holographicdisplays.core.nms.ChatComponentCustomNameEditor;
import me.filoghost.holographicdisplays.core.nms.CustomNameEditor;
import me.filoghost.holographicdisplays.core.nms.NMSManager;
import me.filoghost.holographicdisplays.core.nms.ProtocolPacketSettings;
import me.filoghost.holographicdisplays.core.nms.SpawnFailedException;
import me.filoghost.holographicdisplays.core.nms.entity.NMSArmorStand;
import me.filoghost.holographicdisplays.core.nms.entity.NMSEntity;
import me.filoghost.holographicdisplays.core.nms.entity.NMSItem;
import net.minecraft.server.v1_16_R1.ChatComponentText;
import net.minecraft.server.v1_16_R1.Entity;
import net.minecraft.server.v1_16_R1.EntityTypes;
import net.minecraft.server.v1_16_R1.EnumCreatureType;
import net.minecraft.server.v1_16_R1.IChatBaseComponent;
import net.minecraft.server.v1_16_R1.IRegistry;
import net.minecraft.server.v1_16_R1.MathHelper;
import net.minecraft.server.v1_16_R1.RegistryID;
@ -35,8 +31,6 @@ import org.bukkit.craftbukkit.v1_16_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_16_R1.entity.CraftEntity;
import org.bukkit.inventory.ItemStack;
import java.util.List;
public class VersionNMSManager implements NMSManager {
private static final ReflectField<RegistryID<EntityTypes<?>>> REGISTRY_ID_FIELD
@ -155,39 +149,10 @@ public class VersionNMSManager implements NMSManager {
return null;
}
}
@Override
public CustomNameEditor getCustomNameEditor() {
return VersionChatComponentCustomNameEditor.INSTANCE;
}
private enum VersionChatComponentCustomNameEditor implements ChatComponentCustomNameEditor<IChatBaseComponent> {
INSTANCE;
@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) {
newSibling.getChatModifier().setChatModifier(chatComponent.getChatModifier());
chatComponent.getSiblings().add(newSibling);
}
@Override
public ChatComponentText cloneComponent(IChatBaseComponent chatComponent, String newText) {
ChatComponentText clonedChatComponent = new ChatComponentText(newText);
clonedChatComponent.setChatModifier(chatComponent.getChatModifier().a());
return clonedChatComponent;
}
public Object createCustomNameNMSObject(String customName) {
return EntityNMSArmorStand.createCustomNameNMSObject(customName);
}
}

View File

@ -177,10 +177,14 @@ public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorSta
return;
}
this.customName = customName;
super.setCustomName(CraftChatMessage.fromStringOrNull(Strings.truncate(customName, 300)));
super.setCustomName(createCustomNameNMSObject(customName));
super.setCustomNameVisible(customName != null && !customName.isEmpty());
}
protected static IChatBaseComponent createCustomNameNMSObject(String customName) {
return CraftChatMessage.fromStringOrNull(Strings.truncate(customName, 300));
}
@Override
public String getCustomNameStringNMS() {
return this.customName;

View File

@ -11,19 +11,15 @@ import me.filoghost.fcommons.reflection.ReflectField;
import me.filoghost.fcommons.reflection.ReflectMethod;
import me.filoghost.holographicdisplays.core.hologram.StandardHologramLine;
import me.filoghost.holographicdisplays.core.hologram.StandardItemLine;
import me.filoghost.holographicdisplays.core.nms.ChatComponentCustomNameEditor;
import me.filoghost.holographicdisplays.core.nms.CustomNameEditor;
import me.filoghost.holographicdisplays.core.nms.NMSManager;
import me.filoghost.holographicdisplays.core.nms.ProtocolPacketSettings;
import me.filoghost.holographicdisplays.core.nms.SpawnFailedException;
import me.filoghost.holographicdisplays.core.nms.entity.NMSArmorStand;
import me.filoghost.holographicdisplays.core.nms.entity.NMSEntity;
import me.filoghost.holographicdisplays.core.nms.entity.NMSItem;
import net.minecraft.server.v1_16_R2.ChatComponentText;
import net.minecraft.server.v1_16_R2.Entity;
import net.minecraft.server.v1_16_R2.EntityTypes;
import net.minecraft.server.v1_16_R2.EnumCreatureType;
import net.minecraft.server.v1_16_R2.IChatBaseComponent;
import net.minecraft.server.v1_16_R2.IRegistry;
import net.minecraft.server.v1_16_R2.MathHelper;
import net.minecraft.server.v1_16_R2.RegistryMaterials;
@ -34,7 +30,6 @@ import org.bukkit.craftbukkit.v1_16_R2.CraftWorld;
import org.bukkit.craftbukkit.v1_16_R2.entity.CraftEntity;
import org.bukkit.inventory.ItemStack;
import java.util.List;
import java.util.Map;
public class VersionNMSManager implements NMSManager {
@ -145,39 +140,10 @@ public class VersionNMSManager implements NMSManager {
return null;
}
}
@Override
public CustomNameEditor getCustomNameEditor() {
return VersionChatComponentCustomNameEditor.INSTANCE;
}
private enum VersionChatComponentCustomNameEditor implements ChatComponentCustomNameEditor<IChatBaseComponent> {
INSTANCE;
@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) {
newSibling.getChatModifier().setChatModifier(chatComponent.getChatModifier());
chatComponent.getSiblings().add(newSibling);
}
@Override
public ChatComponentText cloneComponent(IChatBaseComponent chatComponent, String newText) {
ChatComponentText clonedChatComponent = new ChatComponentText(newText);
clonedChatComponent.setChatModifier(chatComponent.getChatModifier().a());
return clonedChatComponent;
}
public Object createCustomNameNMSObject(String customName) {
return EntityNMSArmorStand.createCustomNameNMSObject(customName);
}
}

View File

@ -177,10 +177,14 @@ public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorSta
return;
}
this.customName = customName;
super.setCustomName(CraftChatMessage.fromStringOrNull(Strings.truncate(customName, 300)));
super.setCustomName(createCustomNameNMSObject(customName));
super.setCustomNameVisible(customName != null && !customName.isEmpty());
}
protected static IChatBaseComponent createCustomNameNMSObject(String customName) {
return CraftChatMessage.fromStringOrNull(Strings.truncate(customName, 300));
}
@Override
public String getCustomNameStringNMS() {
return this.customName;

View File

@ -11,19 +11,15 @@ import me.filoghost.fcommons.reflection.ReflectField;
import me.filoghost.fcommons.reflection.ReflectMethod;
import me.filoghost.holographicdisplays.core.hologram.StandardHologramLine;
import me.filoghost.holographicdisplays.core.hologram.StandardItemLine;
import me.filoghost.holographicdisplays.core.nms.ChatComponentCustomNameEditor;
import me.filoghost.holographicdisplays.core.nms.CustomNameEditor;
import me.filoghost.holographicdisplays.core.nms.NMSManager;
import me.filoghost.holographicdisplays.core.nms.ProtocolPacketSettings;
import me.filoghost.holographicdisplays.core.nms.SpawnFailedException;
import me.filoghost.holographicdisplays.core.nms.entity.NMSArmorStand;
import me.filoghost.holographicdisplays.core.nms.entity.NMSEntity;
import me.filoghost.holographicdisplays.core.nms.entity.NMSItem;
import net.minecraft.server.v1_16_R3.ChatComponentText;
import net.minecraft.server.v1_16_R3.Entity;
import net.minecraft.server.v1_16_R3.EntityTypes;
import net.minecraft.server.v1_16_R3.EnumCreatureType;
import net.minecraft.server.v1_16_R3.IChatBaseComponent;
import net.minecraft.server.v1_16_R3.IRegistry;
import net.minecraft.server.v1_16_R3.MathHelper;
import net.minecraft.server.v1_16_R3.RegistryMaterials;
@ -34,7 +30,6 @@ import org.bukkit.craftbukkit.v1_16_R3.CraftWorld;
import org.bukkit.craftbukkit.v1_16_R3.entity.CraftEntity;
import org.bukkit.inventory.ItemStack;
import java.util.List;
import java.util.Map;
public class VersionNMSManager implements NMSManager {
@ -145,39 +140,10 @@ public class VersionNMSManager implements NMSManager {
return null;
}
}
@Override
public CustomNameEditor getCustomNameEditor() {
return VersionChatComponentCustomNameEditor.INSTANCE;
}
private enum VersionChatComponentCustomNameEditor implements ChatComponentCustomNameEditor<IChatBaseComponent> {
INSTANCE;
@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) {
newSibling.getChatModifier().setChatModifier(chatComponent.getChatModifier());
chatComponent.getSiblings().add(newSibling);
}
@Override
public ChatComponentText cloneComponent(IChatBaseComponent chatComponent, String newText) {
ChatComponentText clonedChatComponent = new ChatComponentText(newText);
clonedChatComponent.setChatModifier(chatComponent.getChatModifier().a());
return clonedChatComponent;
}
public Object createCustomNameNMSObject(String customName) {
return EntityNMSArmorStand.createCustomNameNMSObject(customName);
}
}

View File

@ -163,9 +163,13 @@ public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorSta
return;
}
this.customName = customName;
super.setCustomName(customName != null ? Strings.truncate(customName, 256) : "");
super.setCustomName(createCustomNameNMSObject(customName));
super.setCustomNameVisible(customName != null && !customName.isEmpty());
}
protected static String createCustomNameNMSObject(String customName) {
return customName != null ? Strings.truncate(customName, 256) : "";
}
@Override
public String getCustomNameStringNMS() {

View File

@ -11,11 +11,9 @@ import me.filoghost.fcommons.reflection.ReflectField;
import me.filoghost.fcommons.reflection.ReflectMethod;
import me.filoghost.holographicdisplays.core.hologram.StandardHologramLine;
import me.filoghost.holographicdisplays.core.hologram.StandardItemLine;
import me.filoghost.holographicdisplays.core.nms.CustomNameEditor;
import me.filoghost.holographicdisplays.core.nms.NMSManager;
import me.filoghost.holographicdisplays.core.nms.ProtocolPacketSettings;
import me.filoghost.holographicdisplays.core.nms.SpawnFailedException;
import me.filoghost.holographicdisplays.core.nms.StringCustomNameEditor;
import me.filoghost.holographicdisplays.core.nms.entity.NMSArmorStand;
import me.filoghost.holographicdisplays.core.nms.entity.NMSEntity;
import me.filoghost.holographicdisplays.core.nms.entity.NMSItem;
@ -152,10 +150,10 @@ public class VersionNMSManager implements NMSManager {
return null;
}
}
@Override
public CustomNameEditor getCustomNameEditor() {
return StringCustomNameEditor.INSTANCE;
public Object createCustomNameNMSObject(String customName) {
return EntityNMSArmorStand.createCustomNameNMSObject(customName);
}
}

View File

@ -163,9 +163,13 @@ public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorSta
return;
}
this.customName = customName;
super.setCustomName(customName != null ? Strings.truncate(customName, 256) : "");
super.setCustomName(createCustomNameNMSObject(customName));
super.setCustomNameVisible(customName != null && !customName.isEmpty());
}
protected static String createCustomNameNMSObject(String customName) {
return customName != null ? Strings.truncate(customName, 256) : "";
}
@Override
public String getCustomNameStringNMS() {

View File

@ -11,11 +11,9 @@ import me.filoghost.fcommons.reflection.ReflectField;
import me.filoghost.fcommons.reflection.ReflectMethod;
import me.filoghost.holographicdisplays.core.hologram.StandardHologramLine;
import me.filoghost.holographicdisplays.core.hologram.StandardItemLine;
import me.filoghost.holographicdisplays.core.nms.CustomNameEditor;
import me.filoghost.holographicdisplays.core.nms.NMSManager;
import me.filoghost.holographicdisplays.core.nms.ProtocolPacketSettings;
import me.filoghost.holographicdisplays.core.nms.SpawnFailedException;
import me.filoghost.holographicdisplays.core.nms.StringCustomNameEditor;
import me.filoghost.holographicdisplays.core.nms.entity.NMSArmorStand;
import me.filoghost.holographicdisplays.core.nms.entity.NMSEntity;
import me.filoghost.holographicdisplays.core.nms.entity.NMSItem;
@ -152,10 +150,10 @@ public class VersionNMSManager implements NMSManager {
return null;
}
}
@Override
public CustomNameEditor getCustomNameEditor() {
return StringCustomNameEditor.INSTANCE;
public Object createCustomNameNMSObject(String customName) {
return EntityNMSArmorStand.createCustomNameNMSObject(customName);
}
}

View File

@ -174,9 +174,13 @@ public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorSta
return;
}
this.customName = customName;
super.setCustomName(customName != null ? Strings.truncate(customName, 256) : "");
super.setCustomName(createCustomNameNMSObject(customName));
super.setCustomNameVisible(customName != null && !customName.isEmpty());
}
protected static String createCustomNameNMSObject(String customName) {
return customName != null ? Strings.truncate(customName, 256) : "";
}
@Override
public String getCustomNameStringNMS() {

View File

@ -11,11 +11,9 @@ import me.filoghost.fcommons.reflection.ReflectField;
import me.filoghost.fcommons.reflection.ReflectMethod;
import me.filoghost.holographicdisplays.core.hologram.StandardHologramLine;
import me.filoghost.holographicdisplays.core.hologram.StandardItemLine;
import me.filoghost.holographicdisplays.core.nms.CustomNameEditor;
import me.filoghost.holographicdisplays.core.nms.NMSManager;
import me.filoghost.holographicdisplays.core.nms.ProtocolPacketSettings;
import me.filoghost.holographicdisplays.core.nms.SpawnFailedException;
import me.filoghost.holographicdisplays.core.nms.StringCustomNameEditor;
import me.filoghost.holographicdisplays.core.nms.entity.NMSArmorStand;
import me.filoghost.holographicdisplays.core.nms.entity.NMSEntity;
import me.filoghost.holographicdisplays.core.nms.entity.NMSItem;
@ -143,10 +141,10 @@ public class VersionNMSManager implements NMSManager {
return null;
}
}
@Override
public CustomNameEditor getCustomNameEditor() {
return StringCustomNameEditor.INSTANCE;
public Object createCustomNameNMSObject(String customName) {
return EntityNMSArmorStand.createCustomNameNMSObject(customName);
}
}

View File

@ -174,9 +174,13 @@ public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorSta
return;
}
this.customName = customName;
super.setCustomName(customName != null ? Strings.truncate(customName, 256) : "");
super.setCustomName(createCustomNameNMSObject(customName));
super.setCustomNameVisible(customName != null && !customName.isEmpty());
}
protected static String createCustomNameNMSObject(String customName) {
return customName != null ? Strings.truncate(customName, 256) : "";
}
@Override
public String getCustomNameStringNMS() {

View File

@ -11,11 +11,9 @@ import me.filoghost.fcommons.reflection.ReflectField;
import me.filoghost.fcommons.reflection.ReflectMethod;
import me.filoghost.holographicdisplays.core.hologram.StandardHologramLine;
import me.filoghost.holographicdisplays.core.hologram.StandardItemLine;
import me.filoghost.holographicdisplays.core.nms.CustomNameEditor;
import me.filoghost.holographicdisplays.core.nms.NMSManager;
import me.filoghost.holographicdisplays.core.nms.ProtocolPacketSettings;
import me.filoghost.holographicdisplays.core.nms.SpawnFailedException;
import me.filoghost.holographicdisplays.core.nms.StringCustomNameEditor;
import me.filoghost.holographicdisplays.core.nms.entity.NMSArmorStand;
import me.filoghost.holographicdisplays.core.nms.entity.NMSEntity;
import me.filoghost.holographicdisplays.core.nms.entity.NMSItem;
@ -143,10 +141,10 @@ public class VersionNMSManager implements NMSManager {
return null;
}
}
@Override
public CustomNameEditor getCustomNameEditor() {
return StringCustomNameEditor.INSTANCE;
public Object createCustomNameNMSObject(String customName) {
return EntityNMSArmorStand.createCustomNameNMSObject(customName);
}
}

View File

@ -96,20 +96,24 @@ class PacketListener extends PacketAdapter {
}
if (packetType == PacketType.Play.Server.SPAWN_ENTITY_LIVING || packetType == PacketType.Play.Server.ENTITY_METADATA) {
if (!(hologramLine instanceof StandardTextLine)) {
return;
}
StandardTextLine textLine = (StandardTextLine) hologramLine;
if (!textLine.isAllowPlaceholders()) {
if (!(hologramLine instanceof StandardTextLine) || !(nmsEntity instanceof NMSArmorStand)) {
return;
}
Collection<RelativePlaceholder> relativePlaceholders = textLine.getRelativePlaceholders();
if (relativePlaceholders == null || relativePlaceholders.isEmpty()) {
StandardTextLine textLine = (StandardTextLine) hologramLine;
if (!textLine.isAllowPlaceholders()) {
return;
}
NMSArmorStand nmsArmorStand = (NMSArmorStand) nmsEntity;
String customName = nmsArmorStand.getCustomNameStringNMS();
String customNameWithRelativePlaceholders = replaceRelativePlaceholders(textLine, customName, player);
if (customNameWithRelativePlaceholders.equals(customName)) {
return; // No need to modify packets, custom name doesn't need changes
}
WrappedWatchableObject customNameWatchableObject;
AbstractPacket packetWrapper;
@ -122,46 +126,31 @@ class PacketListener extends PacketAdapter {
packetWrapper = spawnEntityPacket;
customNameWatchableObject = metadataHelper.getCustomNameWacthableObject(spawnEntityPacket.getMetadata());
}
if (customNameWatchableObject == null) {
return;
}
boolean modified = replaceRelativePlaceholders(customNameWatchableObject, player, relativePlaceholders);
if (modified) {
event.setPacket(packetWrapper.getHandle());
}
Object customNameNMSObject = nmsManager.createCustomNameNMSObject(customNameWithRelativePlaceholders);
metadataHelper.setCustomNameNMSObject(customNameWatchableObject, customNameNMSObject);
event.setPacket(packetWrapper.getHandle());
}
}
private boolean replaceRelativePlaceholders(
WrappedWatchableObject customNameWatchableObject,
Player player,
Collection<RelativePlaceholder> relativePlaceholders) {
if (customNameWatchableObject == null) {
return false;
private String replaceRelativePlaceholders(StandardTextLine textLine, String text, Player player) {
Collection<RelativePlaceholder> relativePlaceholders = textLine.getRelativePlaceholders();
if (relativePlaceholders != null && !relativePlaceholders.isEmpty()) {
for (RelativePlaceholder relativePlaceholder : relativePlaceholders) {
if (text.contains(relativePlaceholder.getTextPlaceholder())) {
text = text.replace(
relativePlaceholder.getTextPlaceholder(),
relativePlaceholder.getReplacement(player));
}
}
}
final Object originalCustomNameNMSObject = metadataHelper.getCustomNameNMSObject(customNameWatchableObject);
if (originalCustomNameNMSObject == null) {
return false;
}
Object replacedCustomNameNMSObject = originalCustomNameNMSObject;
for (RelativePlaceholder relativePlaceholder : relativePlaceholders) {
replacedCustomNameNMSObject = nmsManager.getCustomNameEditor().replaceCustomName(
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;
return text;
}
}