Refactored npc villager system

This commit is contained in:
2023-11-11 00:24:54 +01:00
parent ad074616a9
commit 4e29e3b7fe
4 changed files with 62 additions and 37 deletions

View File

@@ -0,0 +1,46 @@
package eu.mhsl.craftattack.spawn.util;
import org.bukkit.Location;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Villager;
import java.util.Objects;
import java.util.UUID;
import java.util.function.Consumer;
public class DisplayVillager {
private final Location location;
private Villager villager;
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) {
this.villager = getBaseVillager();
villagerCreator.accept(this.villager);
}
this.villager.teleport(this.location);
}
public Villager getVillager() {
return villager;
}
private Villager getBaseVillager() {
Villager villager = (Villager) this.location.getWorld().spawnEntity(this.location, EntityType.VILLAGER);
villager.setRemoveWhenFarAway(false);
villager.setInvulnerable(true);
villager.setPersistent(true);
villager.setGravity(false);
villager.setAI(false);
villager.setCollidable(false);
villager.setCustomNameVisible(true);
return villager;
}
}