ZNPCsPlus/src/main/java/lol/pyr/znpcsplus/npc/NPC.java

128 lines
3.0 KiB
Java
Raw Normal View History

2023-04-23 19:24:31 +00:00
package lol.pyr.znpcsplus.npc;
2023-04-24 18:10:55 +00:00
import lol.pyr.znpcsplus.entity.PacketEntity;
import lol.pyr.znpcsplus.entity.PacketLocation;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Player;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
2023-04-23 19:24:31 +00:00
public class NPC {
protected static final Set<NPC> _ALL_NPCS = new HashSet<>();
2023-04-24 18:10:55 +00:00
private final Set<Player> viewers = new HashSet<>();
private final String worldName;
private PacketEntity entity;
private PacketLocation location;
private NPCType type;
2023-04-24 21:31:48 +00:00
private final Map<NPCProperty<?>, Object> propertyMap = new HashMap<>();
2023-04-24 18:10:55 +00:00
public NPC(World world, NPCType type, PacketLocation location) {
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);
_ALL_NPCS.add(this);
2023-04-24 18:10:55 +00:00
}
public void setType(NPCType type) {
_hideAll();
this.type = type;
2023-04-24 21:31:48 +00:00
entity = new PacketEntity(this, type.getType(), entity.getLocation());
2023-04-24 18:10:55 +00:00
_showAll();
}
public NPCType getType() {
return type;
}
2023-04-24 19:58:14 +00:00
public PacketEntity getEntity() {
return entity;
}
2023-04-24 18:10:55 +00:00
public PacketLocation getLocation() {
return location;
}
public void setLocation(PacketLocation location) {
this.location = location;
entity.setLocation(location, viewers);
}
public World getWorld() {
return Bukkit.getWorld(worldName);
}
public void delete() {
_ALL_NPCS.remove(this);
2023-04-24 18:10:55 +00:00
_hideAll();
viewers.clear();
}
2023-04-24 19:58:14 +00:00
public void respawn() {
_hideAll();
_showAll();
}
public void respawn(Player player) {
_hide(player);
_show(player);
}
2023-04-24 18:10:55 +00:00
public void show(Player player) {
if (viewers.contains(player)) return;
_show(player);
viewers.add(player);
}
public void hide(Player player) {
if (!viewers.contains(player)) return;
_hide(player);
viewers.remove(player);
}
2023-04-24 19:58:14 +00:00
public boolean isShown(Player player) {
return viewers.contains(player);
}
private void _show(Player player) {
entity.spawn(player);
}
2023-04-24 18:10:55 +00:00
private void _hide(Player player) {
entity.despawn(player);
}
private void _hideAll() {
for (Player viewer : viewers) _hide(viewer);
}
private void _showAll() {
for (Player viewer : viewers) _show(viewer);
}
2023-04-24 19:58:14 +00:00
@SuppressWarnings("unchecked")
2023-04-24 21:31:48 +00:00
public <T> T getProperty(NPCProperty<T> key) {
return hasProperty(key) ? (T) propertyMap.get(key) : key.getDefaultValue();
2023-04-24 19:58:14 +00:00
}
2023-04-24 21:31:48 +00:00
public boolean hasProperty(NPCProperty<?> key) {
2023-04-24 19:58:14 +00:00
return propertyMap.containsKey(key);
2023-04-24 18:10:55 +00:00
}
2023-04-24 21:31:48 +00:00
public <T> void setProperty(NPCProperty<T> key, T value) {
if (value.equals(key.getDefaultValue())) removeProperty(key);
else propertyMap.put(key, value);
2023-04-24 18:10:55 +00:00
}
2023-04-24 21:31:48 +00:00
public void removeProperty(NPCProperty<?> key) {
2023-04-24 19:58:14 +00:00
propertyMap.remove(key);
2023-04-24 18:10:55 +00:00
}
2023-04-23 19:24:31 +00:00
}