Fix nametag splitting

This commit is contained in:
ceze88 2024-02-07 16:03:11 +01:00
parent 21614461f9
commit 4454296eeb
2 changed files with 53 additions and 5 deletions

View File

@ -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()));
}

View File

@ -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<SWorld> 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: