fixed duplicated display villagers when entities were not loaded yet
This commit is contained in:
1 parent
b8ba85f8c5
commit
a91f550153
2 files changed
+30
-5
No files matched your search
@@ -8,6 +8,7 @@ import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.entity.Villager;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Consumer;
|
||||
@@ -19,11 +20,14 @@ public class DisplayVillager {
|
||||
public DisplayVillager(UUID uuid, Location location, Consumer<Villager> villagerCreator) {
|
||||
this.location = location;
|
||||
|
||||
try {
|
||||
ChunkUtils.loadChunkAtLocation(this.location);
|
||||
this.villager = (Villager) this.location.getWorld().getEntity(uuid);
|
||||
Objects.requireNonNull(this.villager);
|
||||
} catch(NullPointerException | IllegalArgumentException e) {
|
||||
// getEntities() waits for the chunk's entities; getEntity(uuid) misses them while they are still loading
|
||||
this.villager = Arrays.stream(ChunkUtils.loadChunkAtLocation(this.location).getEntities())
|
||||
.filter(entity -> entity.getUniqueId().equals(uuid))
|
||||
.filter(Villager.class::isInstance)
|
||||
.map(Villager.class::cast)
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
if(this.villager == null) {
|
||||
this.villager = this.getBaseVillager();
|
||||
villagerCreator.accept(this.villager);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user