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

120 lines
2.9 KiB
Java
Raw Normal View History

2023-04-23 19:24:31 +00:00
package lol.pyr.znpcsplus.npc;
2023-04-24 19:58:14 +00:00
import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes;
2023-04-24 18:10:55 +00:00
import lol.pyr.znpcsplus.entity.PacketEntity;
import lol.pyr.znpcsplus.entity.PacketLocation;
2023-04-24 19:58:14 +00:00
import lol.pyr.znpcsplus.entity.PacketPlayer;
2023-04-24 18:10:55 +00:00
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 {
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 19:58:14 +00:00
private final Map<NPCPropertyKey<?>, 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 19:58:14 +00:00
entity = new PacketEntity(type.getType(), location);
2023-04-24 18:10:55 +00:00
}
public void setType(NPCType type) {
_hideAll();
this.type = type;
2023-04-24 19:58:14 +00:00
entity = type.getType() == EntityTypes.PLAYER ? new PacketPlayer(entity.getLocation()) : new PacketEntity(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() {
_hideAll();
viewers.clear();
}
2023-04-24 19:58:14 +00:00
public void respawn() {
_hideAll();
_showAll();
}
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")
public <T> T getProperty(NPCPropertyKey<T> key) {
return (T) propertyMap.get(key);
}
public boolean hasProperty(NPCPropertyKey<?> key) {
return propertyMap.containsKey(key);
2023-04-24 18:10:55 +00:00
}
2023-04-24 19:58:14 +00:00
public <T> void setProperty(NPCPropertyKey<T> key, T value) {
propertyMap.put(key, value);
key.update(this, value);
2023-04-24 18:10:55 +00:00
}
2023-04-24 19:58:14 +00:00
public void removeProperty(NPCPropertyKey<?> key) {
propertyMap.remove(key);
2023-04-24 18:10:55 +00:00
}
2023-04-23 19:24:31 +00:00
}