From 4454296eebcfee9a017157b93ae6f7b04a851849 Mon Sep 17 00:00:00 2001 From: ceze88 Date: Wed, 7 Feb 2024 16:03:11 +0100 Subject: [PATCH] Fix nametag splitting --- .../stackable/entity/EntityStackImpl.java | 32 ++++++++++++++++--- .../tasks/StackingTask.java | 26 +++++++++++++++ 2 files changed, 53 insertions(+), 5 deletions(-) diff --git a/UltimateStacker-Plugin/src/main/java/com.craftaro.ultimatestacker/stackable/entity/EntityStackImpl.java b/UltimateStacker-Plugin/src/main/java/com.craftaro.ultimatestacker/stackable/entity/EntityStackImpl.java index f1d7ef5..f54c84f 100644 --- a/UltimateStacker-Plugin/src/main/java/com.craftaro.ultimatestacker/stackable/entity/EntityStackImpl.java +++ b/UltimateStacker-Plugin/src/main/java/com.craftaro.ultimatestacker/stackable/entity/EntityStackImpl.java @@ -252,14 +252,35 @@ public class EntityStackImpl implements EntityStack { @Override public synchronized void releaseHost() { + //Remove the metadata from the entity if it's the last one +// if (getAmount() == 1) { +// if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_14)) { +// PersistentDataContainer container = hostEntity.getPersistentDataContainer(); +// container.remove(STACKED_ENTITY_KEY); +// } else { +// hostEntity.removeMetadata("US_AMOUNT", plugin); +// } +// hostEntity.setCustomName(null); +// hostEntity.setCustomNameVisible(false); +// return; +// } + LivingEntity oldHost = hostEntity; - LivingEntity entity = takeOneAndSpawnEntity(hostEntity.getLocation()); - if (getAmount() >= 0) { - plugin.getEntityStackManager().updateStack(oldHost, entity); - updateNameTag(); + if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_14)) { + PersistentDataContainer container = oldHost.getPersistentDataContainer(); + container.remove(STACKED_ENTITY_KEY); + //Add new entry that the entity was split by player, not to stack it anymore + container.set(new NamespacedKey(plugin, "US_SPLIT_PLAYER"), PersistentDataType.BYTE, (byte) 1); } else { - destroy(); + oldHost.removeMetadata("US_AMOUNT", plugin); + //Add new entry that the entity was split by player, not to stack it anymore + oldHost.setMetadata("US_SPLIT_PLAYER", new FixedMetadataValue(plugin, true)); } + + //Summon a new entity and update the stack and remove the metadata from the old entity + this.hostEntity = takeOneAndSpawnEntity(hostEntity.getLocation()); + setAmount(amount-1); + updateNameTag(); } @Override @@ -273,6 +294,7 @@ public class EntityStackImpl implements EntityStack { if (hostEntity == null) { return; } + hostEntity.setCustomNameVisible(!Settings.HOLOGRAMS_ON_LOOK_ENTITY.getBoolean()); hostEntity.setCustomName(Methods.compileEntityName(hostEntity, getAmount())); } diff --git a/UltimateStacker-Plugin/src/main/java/com.craftaro.ultimatestacker/tasks/StackingTask.java b/UltimateStacker-Plugin/src/main/java/com.craftaro.ultimatestacker/tasks/StackingTask.java index 34d2f8e..717d17a 100644 --- a/UltimateStacker-Plugin/src/main/java/com.craftaro.ultimatestacker/tasks/StackingTask.java +++ b/UltimateStacker-Plugin/src/main/java/com.craftaro.ultimatestacker/tasks/StackingTask.java @@ -14,6 +14,7 @@ import com.craftaro.ultimatestacker.utils.CachedChunk; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; +import org.bukkit.NamespacedKey; import org.bukkit.World; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.AbstractHorse; @@ -43,6 +44,8 @@ import org.bukkit.entity.Villager; import org.bukkit.entity.Wolf; import org.bukkit.entity.Zombie; import org.bukkit.inventory.EntityEquipment; +import org.bukkit.persistence.PersistentDataContainer; +import org.bukkit.persistence.PersistentDataType; import java.util.ArrayList; import java.util.Collections; @@ -92,9 +95,11 @@ public class StackingTask extends TimerTask { onlyStackOnSurface = Settings.ONLY_STACK_ON_SURFACE.getBoolean(); private final Set loadedWorlds; + private final NamespacedKey playerSplitKey; public StackingTask(UltimateStacker plugin) { this.plugin = plugin; + playerSplitKey = new NamespacedKey(plugin, "US_SPLIT_PLAYER"); this.stackManager = plugin.getEntityStackManager(); // Add loaded worlds. loadedWorlds = new HashSet<>(); @@ -182,6 +187,7 @@ public class StackingTask extends TimerTask { private boolean isEntityNotStackable(LivingEntity entity) { if (isMaxStack(entity)) return true; + if (isPersistentSplit(entity)) return true; // Make sure we have the correct entity type and that it is valid. if (!entity.isValid() @@ -255,6 +261,10 @@ public class StackingTask extends TimerTask { // Loop through our similar stackable entities. for (LivingEntity friendlyEntity : stackableFriends) { + if (isPersistentSplit(friendlyEntity)) { + continue; + } + // Process similar entities. EntityStack friendStack = stackManager.getStackedEntity(friendlyEntity); int amount = friendStack != null ? friendStack.getAmount() : 1; @@ -665,6 +675,22 @@ public class StackingTask extends TimerTask { return stack.getAmount() >= getEntityMaxStackSize(livingEntity); } + private boolean isPersistentSplit(LivingEntity entity) { + if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_14)) { + PersistentDataContainer container = entity.getPersistentDataContainer(); + if (container.has(playerSplitKey, PersistentDataType.BYTE)) { + processed.add(entity.getUniqueId()); + return true; + } + } else { + if (entity.hasMetadata("US_SPLIT_PLAYER")) { + processed.add(entity.getUniqueId()); + return true; + } + } + return false; + } + public boolean canFly(LivingEntity entity) { switch (entity.getType()) { case GHAST: