Refactored npc villager system
This commit is contained in:
parent
ad074616a9
commit
4e29e3b7fe
@ -7,7 +7,7 @@ import eu.mhsl.craftattack.spawn.config.ConfigUtil;
|
||||
import eu.mhsl.craftattack.spawn.config.Configuration;
|
||||
import eu.mhsl.craftattack.spawn.appliances.worldmuseum.command.MoveWorldMuseumVillagerCommand;
|
||||
import eu.mhsl.craftattack.spawn.appliances.worldmuseum.listener.PlayerEntityInteractListener;
|
||||
import eu.mhsl.craftattack.spawn.util.ChunkUtils;
|
||||
import eu.mhsl.craftattack.spawn.util.DisplayVillager;
|
||||
import eu.mhsl.craftattack.spawn.util.PluginMessage;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
@ -22,7 +22,7 @@ import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
|
||||
public class WorldMuseum extends Appliance {
|
||||
public Villager villager;
|
||||
public DisplayVillager villager;
|
||||
|
||||
public WorldMuseum() {
|
||||
super("worldMuseum");
|
||||
@ -30,21 +30,19 @@ public class WorldMuseum extends Appliance {
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
// sadly, already existing entities can only be found when chunks are loaded, so to reuse the villager, the chunk has to be loaded
|
||||
// https://www.spigotmc.org/threads/getting-entity-problem.332246/
|
||||
|
||||
Location location = ConfigUtil.Position.paseLocation(Objects.requireNonNull(localConfig().getConfigurationSection("villagerLocation")));
|
||||
this.villager = new DisplayVillager(
|
||||
UUID.fromString(localConfig().getString("uuid", UUID.randomUUID().toString())),
|
||||
location,
|
||||
villager -> {
|
||||
localConfig().set("uuid", villager.getUniqueId().toString());
|
||||
Configuration.saveChanges();
|
||||
|
||||
try {
|
||||
UUID villagerUuid = UUID.fromString(Objects.requireNonNull(localConfig().getString("uuid")));
|
||||
ChunkUtils.loadChunkAtLocation(location);
|
||||
this.villager = (Villager) Objects.requireNonNull(Bukkit.getWorld("world")).getEntity(villagerUuid);
|
||||
Objects.requireNonNull(this.villager);
|
||||
} catch (NullPointerException | IllegalArgumentException e) {
|
||||
this.villager = createVillagerEntity();
|
||||
villager.customName(Component.text("Weltenansicht").color(NamedTextColor.GOLD));
|
||||
villager.setProfession(Villager.Profession.LIBRARIAN);
|
||||
villager.setVillagerType(Villager.Type.SAVANNA);
|
||||
}
|
||||
|
||||
this.villager.teleport(location);
|
||||
);
|
||||
}
|
||||
public void updateVillagerPosition(Location location) {
|
||||
ConfigUtil.Position.writeLocation(
|
||||
@ -53,7 +51,7 @@ public class WorldMuseum extends Appliance {
|
||||
);
|
||||
Configuration.saveChanges();
|
||||
|
||||
this.villager.teleport(location);
|
||||
this.villager.getVillager().teleport(location);
|
||||
}
|
||||
|
||||
public void handleVillagerInteraction(Player player) {
|
||||
@ -61,25 +59,6 @@ public class WorldMuseum extends Appliance {
|
||||
PluginMessage.connect(player, localConfig().getString("connect-server-name"));
|
||||
}
|
||||
|
||||
private Villager createVillagerEntity() {
|
||||
Location villagerLocation = ConfigUtil.Position.paseLocation(Objects.requireNonNull(localConfig().getConfigurationSection("villagerLocation")));
|
||||
Villager villager = (Villager) villagerLocation.getWorld().spawnEntity(villagerLocation, EntityType.VILLAGER);
|
||||
|
||||
localConfig().set("uuid", villager.getUniqueId().toString());
|
||||
Configuration.saveChanges();
|
||||
|
||||
villager.setInvulnerable(true);
|
||||
villager.setPersistent(true);
|
||||
villager.setGravity(false);
|
||||
villager.setAI(false);
|
||||
villager.setCollidable(false);
|
||||
villager.setCustomNameVisible(true);
|
||||
villager.customName(Component.text("Weltenansicht").color(NamedTextColor.GOLD));
|
||||
villager.setProfession(Villager.Profession.LIBRARIAN);
|
||||
villager.setVillagerType(Villager.Type.SAVANNA);
|
||||
return villager;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @NotNull List<ApplianceCommand<?>> commands() {
|
||||
return List.of(new MoveWorldMuseumVillagerCommand());
|
||||
|
@ -10,7 +10,7 @@ public class InventoryOpenListener extends ApplianceListener<WorldMuseum> {
|
||||
@EventHandler
|
||||
public void onInventoryOpen(InventoryOpenEvent event) {
|
||||
if(event.getInventory().getHolder() instanceof Villager villager) {
|
||||
event.setCancelled(villager.getUniqueId().equals(getAppliance().villager.getUniqueId()));
|
||||
event.setCancelled(villager.getUniqueId().equals(getAppliance().villager.getVillager().getUniqueId()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ import org.bukkit.event.player.PlayerInteractAtEntityEvent;
|
||||
public class PlayerEntityInteractListener extends ApplianceListener<WorldMuseum> {
|
||||
@EventHandler
|
||||
public void onInteract(PlayerInteractAtEntityEvent event) {
|
||||
if (!event.getRightClicked().getUniqueId().equals(getAppliance().villager.getUniqueId())) return;
|
||||
if (!event.getRightClicked().getUniqueId().equals(getAppliance().villager.getVillager().getUniqueId())) return;
|
||||
|
||||
event.setCancelled(true);
|
||||
getAppliance().handleVillagerInteraction(event.getPlayer());
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user