Refactored npc villager system
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user