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

135 lines
3.8 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-05-04 07:25:05 +00:00
import lol.pyr.znpcsplus.api.npc.Npc;
2023-05-08 11:17:25 +00:00
import lol.pyr.znpcsplus.entity.EntityPropertyImpl;
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-05-04 07:25:05 +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;
2023-05-11 04:40:33 +00:00
import org.bukkit.Location;
2023-04-24 18:10:55 +00:00
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 07:25:05 +00:00
public class NpcImpl extends Viewable implements Npc {
2023-04-24 18:10:55 +00:00
private final String worldName;
private PacketEntity entity;
2023-04-26 23:58:09 +00:00
private ZLocation location;
2023-05-08 11:17:25 +00:00
private NpcTypeImpl type;
2023-05-04 04:07:07 +00:00
private final HologramImpl hologram;
2023-04-24 18:10:55 +00:00
2023-05-08 11:17:25 +00:00
private final Map<EntityPropertyImpl<?>, Object> propertyMap = new HashMap<>();
2023-05-04 07:25:05 +00:00
private final Set<NpcAction> actions = new HashSet<>();
2023-04-24 18:10:55 +00:00
2023-05-08 11:17:25 +00:00
protected NpcImpl(World world, NpcTypeImpl type, ZLocation location) {
this(world.getName(), type, location);
}
public NpcImpl(String world, NpcTypeImpl type, ZLocation location) {
this.worldName = world;
2023-04-24 18:10:55 +00:00
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
}
2023-05-08 11:17:25 +00:00
public void setType(NpcTypeImpl 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
}
2023-05-08 11:17:25 +00:00
public NpcTypeImpl getType() {
2023-04-24 18:10:55 +00:00
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-05-11 04:40:33 +00:00
public Location getBukkitLocation() {
return location.toBukkitLocation(getWorld());
}
2023-04-26 23:58:09 +00:00
public void setLocation(ZLocation location) {
2023-04-24 18:10:55 +00:00
this.location = location;
2023-05-11 04:40:33 +00:00
entity.setLocation(location, getViewers());
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-05-08 11:17:25 +00:00
public String getWorldName() {
return 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() {
2023-05-11 04:40:33 +00:00
for (Player viewer : getViewers()) 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-05-08 11:17:25 +00:00
return hasProperty(key) ? (T) propertyMap.get((EntityPropertyImpl<?>) 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-05-08 11:17:25 +00:00
return propertyMap.containsKey((EntityPropertyImpl<?>) key);
2023-04-24 18:10:55 +00:00
}
2023-05-08 11:17:25 +00:00
public <T> void setProperty(EntityPropertyImpl<T> key, T value) {
2023-04-24 21:31:48 +00:00
if (value.equals(key.getDefaultValue())) removeProperty(key);
2023-05-11 04:40:33 +00:00
else propertyMap.put(key, value);
_refreshMeta();
}
@SuppressWarnings("unchecked")
public <T> void UNSAFE_setProperty(EntityPropertyImpl<?> property, Object value) {
setProperty((EntityPropertyImpl<T>) property, (T) value);
2023-04-24 18:10:55 +00:00
}
2023-05-08 11:17:25 +00:00
public void removeProperty(EntityPropertyImpl<?> 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
2023-05-08 11:17:25 +00:00
public Set<EntityPropertyImpl<?>> getAppliedProperties() {
return Collections.unmodifiableSet(propertyMap.keySet());
}
2023-05-04 07:25:05 +00:00
public Collection<NpcAction> getActions() {
2023-04-25 17:14:15 +00:00
return Collections.unmodifiableSet(actions);
}
2023-05-04 07:25:05 +00:00
public void addAction(NpcAction action) {
2023-04-25 17:14:15 +00:00
actions.add(action);
}
2023-04-23 19:24:31 +00:00
}