ZNPCsPlus/plugin/src/main/java/lol/pyr/znpcsplus/npc/NPCImpl.java

114 lines
3.1 KiB
Java
Raw Normal View History

2023-04-23 19:24:31 +00:00
package lol.pyr.znpcsplus.npc;
2023-04-26 23:59:39 +00:00
import lol.pyr.znpcsplus.api.entity.EntityProperty;
2023-04-26 23:58:09 +00:00
import lol.pyr.znpcsplus.api.npc.NPCType;
2023-04-24 18:10:55 +00:00
import lol.pyr.znpcsplus.entity.PacketEntity;
2023-05-04 04:07:07 +00:00
import lol.pyr.znpcsplus.hologram.HologramImpl;
2023-04-25 17:14:15 +00:00
import lol.pyr.znpcsplus.interaction.NPCAction;
2023-04-25 23:31:49 +00:00
import lol.pyr.znpcsplus.util.Viewable;
2023-04-26 23:58:09 +00:00
import lol.pyr.znpcsplus.util.ZLocation;
2023-04-24 18:10:55 +00:00
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Player;
2023-04-25 17:14:15 +00:00
import java.util.*;
2023-04-24 18:10:55 +00:00
2023-05-04 04:07:07 +00:00
public class NPCImpl extends Viewable implements lol.pyr.znpcsplus.api.npc.NPC {
2023-04-24 18:10:55 +00:00
private final Set<Player> viewers = new HashSet<>();
private final String worldName;
private PacketEntity entity;
2023-04-26 23:58:09 +00:00
private ZLocation location;
2023-04-24 18:10:55 +00:00
private NPCType type;
2023-05-04 04:07:07 +00:00
private final HologramImpl hologram;
2023-04-24 18:10:55 +00:00
2023-04-25 23:31:49 +00:00
private final Map<EntityProperty<?>, Object> propertyMap = new HashMap<>();
2023-04-25 17:14:15 +00:00
private final Set<NPCAction> actions = new HashSet<>();
2023-04-24 18:10:55 +00:00
2023-05-04 04:07:07 +00:00
protected NPCImpl(World world, NPCType type, ZLocation location) {
2023-04-24 18:10:55 +00:00
this.worldName = world.getName();
this.type = type;
this.location = location;
2023-04-24 21:31:48 +00:00
entity = new PacketEntity(this, type.getType(), location);
2023-05-04 04:07:07 +00:00
hologram = new HologramImpl(location.withY(location.getY() + type.getHologramOffset()));
2023-04-24 18:10:55 +00:00
}
public void setType(NPCType type) {
2023-04-25 23:31:49 +00:00
UNSAFE_hideAll();
2023-04-24 18:10:55 +00:00
this.type = type;
2023-04-24 21:31:48 +00:00
entity = new PacketEntity(this, type.getType(), entity.getLocation());
2023-04-25 23:31:49 +00:00
UNSAFE_showAll();
2023-04-24 18:10:55 +00:00
}
public NPCType getType() {
return type;
}
2023-04-24 19:58:14 +00:00
public PacketEntity getEntity() {
return entity;
}
2023-04-26 23:58:09 +00:00
public ZLocation getLocation() {
2023-04-24 18:10:55 +00:00
return location;
}
2023-04-26 23:58:09 +00:00
public void setLocation(ZLocation location) {
2023-04-24 18:10:55 +00:00
this.location = location;
entity.setLocation(location, viewers);
2023-04-26 17:50:40 +00:00
hologram.setLocation(location.withY(location.getY() + type.getHologramOffset()));
}
2023-05-04 04:07:07 +00:00
public HologramImpl getHologram() {
2023-04-26 17:50:40 +00:00
return hologram;
2023-04-24 18:10:55 +00:00
}
public World getWorld() {
return Bukkit.getWorld(worldName);
}
2023-04-25 23:31:49 +00:00
@Override
protected void _show(Player player) {
2023-04-24 19:58:14 +00:00
entity.spawn(player);
2023-04-26 17:50:40 +00:00
hologram.show(player);
2023-04-24 19:58:14 +00:00
}
2023-04-25 23:31:49 +00:00
@Override
protected void _hide(Player player) {
2023-04-24 18:10:55 +00:00
entity.despawn(player);
2023-04-26 17:50:40 +00:00
hologram.hide(player);
2023-04-24 18:10:55 +00:00
}
2023-04-25 23:31:49 +00:00
private void _refreshMeta() {
for (Player viewer : viewers) entity.refreshMeta(viewer);
2023-04-24 18:10:55 +00:00
}
2023-04-24 19:58:14 +00:00
@SuppressWarnings("unchecked")
2023-04-25 23:31:49 +00:00
public <T> T getProperty(EntityProperty<T> key) {
2023-04-24 21:31:48 +00:00
return hasProperty(key) ? (T) propertyMap.get(key) : key.getDefaultValue();
2023-04-24 19:58:14 +00:00
}
2023-04-25 23:31:49 +00:00
public boolean hasProperty(EntityProperty<?> key) {
2023-04-24 19:58:14 +00:00
return propertyMap.containsKey(key);
2023-04-24 18:10:55 +00:00
}
2023-04-25 23:31:49 +00:00
public <T> void setProperty(EntityProperty<T> key, T value) {
2023-04-24 21:31:48 +00:00
if (value.equals(key.getDefaultValue())) removeProperty(key);
2023-04-25 17:49:27 +00:00
else {
propertyMap.put(key, value);
2023-04-25 23:31:49 +00:00
_refreshMeta();
2023-04-25 17:49:27 +00:00
}
2023-04-24 18:10:55 +00:00
}
2023-04-25 23:31:49 +00:00
public void removeProperty(EntityProperty<?> key) {
2023-04-24 19:58:14 +00:00
propertyMap.remove(key);
2023-04-25 23:31:49 +00:00
_refreshMeta();
2023-04-24 18:10:55 +00:00
}
2023-04-25 17:14:15 +00:00
public Collection<NPCAction> getActions() {
return Collections.unmodifiableSet(actions);
}
public void addAction(NPCAction action) {
actions.add(action);
}
2023-04-23 19:24:31 +00:00
}