fixed duplicated display villagers when entities were not loaded yet

This commit is contained in:
MineTec committed 2026-10-15 22:55:54 +02:00
1 parent b8ba85f8c5
commit a91f550153
2 files changed
+30 -5

No files matched your search

@@ -20,6 +20,7 @@ import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.World;
import org.bukkit.attribute.Attribute;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Villager;
import org.bukkit.event.Listener;
@@ -104,6 +105,7 @@ public class Arena extends Appliance {
else Main.logger().warning("Arena: pads are not set up, fights cannot be started");
this.syncRegions().forEach(region -> this.worlds.keepLoaded(region, Main.instance()));
this.removeStrayVillagers();
this.placeStarterPlatform();
this.syncAll();
@@ -178,6 +180,25 @@ public class Arena extends Appliance {
.forEach(this::toTribune);
}
/**
* Removes old copies of the return villager, for example after its position was changed in the config by hand.
*/
private void removeStrayVillagers() {
Region region = this.settings.syncRegion();
if(region == null || this.villager == null) return;
UUID current = this.villager.getUniqueId();
for(int cx = region.minChunkX(); cx <= region.maxChunkX(); cx++) {
for(int cz = region.minChunkZ(); cz <= region.maxChunkZ(); cz++) {
for(Entity entity : this.worlds.play().getChunkAt(cx, cz).getEntities()) {
if(!(entity instanceof Villager) || entity.getUniqueId().equals(current)) continue;
if(!entity.getScoreboardTags().contains(Displays.MANAGED_TAG)) continue;
entity.remove();
Main.logger().info("Arena: removed a stray return villager at " + entity.getLocation().toVector());
}
}
}
}
/**
* A fresh, empty world gets a small platform at the tribune spawn, so the first visitors do not fall into the void.
*/