ZNPCsPlus/src/main/java/lol/pyr/znpcsplus/entity/PacketEntity.java

73 lines
2.0 KiB
Java
Raw Normal View History

2023-04-24 16:12:50 +00:00
package lol.pyr.znpcsplus.entity;
import com.github.retrooper.packetevents.protocol.entity.type.EntityType;
import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes;
2023-04-24 19:58:14 +00:00
import io.github.znetworkw.znpcservers.reflection.Reflections;
import io.github.znetworkw.znpcservers.utility.Utils;
2023-04-24 21:31:48 +00:00
import lol.pyr.znpcsplus.npc.NPC;
2023-04-24 16:12:50 +00:00
import lol.pyr.znpcsplus.packets.PacketFactory;
import org.bukkit.entity.Player;
import java.util.Set;
2023-04-24 18:10:55 +00:00
import java.util.UUID;
2023-04-24 16:12:50 +00:00
public class PacketEntity {
2023-04-24 21:31:48 +00:00
private final NPC owner;
2023-04-24 16:12:50 +00:00
private final int entityId;
2023-04-24 18:10:55 +00:00
private final UUID uuid;
2023-04-24 16:12:50 +00:00
private final EntityType type;
private PacketLocation location;
2023-04-24 21:31:48 +00:00
public PacketEntity(NPC owner, EntityType type, PacketLocation location) {
this.owner = owner;
2023-04-24 19:58:14 +00:00
this.entityId = reserveEntityID();
2023-04-24 18:10:55 +00:00
this.uuid = UUID.randomUUID();
2023-04-24 16:12:50 +00:00
this.type = type;
this.location = location;
}
public int getEntityId() {
return entityId;
}
public PacketLocation getLocation() {
return location;
}
2023-04-24 18:10:55 +00:00
public UUID getUuid() {
return uuid;
}
2023-04-24 16:12:50 +00:00
public EntityType getType() {
return type;
}
2023-04-24 21:31:48 +00:00
public NPC getOwner() {
return owner;
}
2023-04-24 16:12:50 +00:00
public void setLocation(PacketLocation location, Set<Player> viewers) {
this.location = location;
for (Player viewer : viewers) PacketFactory.get().teleportEntity(viewer, this);
}
public void spawn(Player player) {
2023-04-24 21:31:48 +00:00
if (type == EntityTypes.PLAYER) PacketFactory.get().spawnPlayer(player, this);
else PacketFactory.get().spawnEntity(player, this);
2023-04-24 16:12:50 +00:00
}
public void despawn(Player player) {
PacketFactory.get().destroyEntity(player, this);
}
2023-04-24 19:58:14 +00:00
private static int reserveEntityID() {
if (Utils.versionNewer(14)) return Reflections.ATOMIC_ENTITY_ID_FIELD.get().incrementAndGet();
else {
int id = Reflections.ENTITY_ID_MODIFIER.get();
Reflections.ENTITY_ID_MODIFIER.set(id + 1);
return id;
}
}
2023-04-24 16:12:50 +00:00
}