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

79 lines
2.4 KiB
Java
Raw Normal View History

2023-04-24 16:12:50 +00:00
package lol.pyr.znpcsplus.entity;
import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.manager.server.ServerVersion;
2023-04-24 16:12:50 +00:00
import com.github.retrooper.packetevents.protocol.entity.type.EntityType;
import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes;
2023-04-26 23:59:39 +00:00
import lol.pyr.znpcsplus.api.entity.PropertyHolder;
2023-04-24 16:12:50 +00:00
import lol.pyr.znpcsplus.packets.PacketFactory;
import lol.pyr.znpcsplus.reflection.Reflections;
2023-05-21 12:45:43 +00:00
import lol.pyr.znpcsplus.util.NpcLocation;
2023-04-24 16:12:50 +00:00
import org.bukkit.entity.Player;
2023-04-25 23:31:49 +00:00
import java.util.Collection;
2023-04-24 18:10:55 +00:00
import java.util.UUID;
2023-04-24 16:12:50 +00:00
public class PacketEntity {
private final PacketFactory packetFactory;
2023-04-25 23:31:49 +00:00
private final PropertyHolder properties;
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;
2023-05-21 12:45:43 +00:00
private NpcLocation location;
2023-04-24 16:12:50 +00:00
2023-05-21 12:45:43 +00:00
public PacketEntity(PacketFactory packetFactory, PropertyHolder properties, EntityType type, NpcLocation location) {
this.packetFactory = packetFactory;
2023-04-25 23:31:49 +00:00
this.properties = properties;
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;
}
2023-05-21 12:45:43 +00:00
public NpcLocation getLocation() {
2023-04-24 16:12:50 +00:00
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-05-21 12:45:43 +00:00
public void setLocation(NpcLocation location, Collection<Player> viewers) {
2023-04-24 16:12:50 +00:00
this.location = location;
for (Player viewer : viewers) packetFactory.teleportEntity(viewer, this);
2023-04-24 16:12:50 +00:00
}
public void spawn(Player player) {
if (type == EntityTypes.PLAYER) packetFactory.spawnPlayer(player, this, properties);
else packetFactory.spawnEntity(player, this, properties);
2023-04-24 16:12:50 +00:00
}
public void despawn(Player player) {
packetFactory.destroyEntity(player, this, properties);
2023-04-25 23:31:49 +00:00
}
public void refreshMeta(Player player) {
packetFactory.sendAllMetadata(player, this, properties);
2023-04-24 16:12:50 +00:00
}
2023-04-24 19:58:14 +00:00
private static int reserveEntityID() {
if (PacketEvents.getAPI().getServerManager().getVersion().isNewerThanOrEquals(ServerVersion.V_1_14)) {
return Reflections.ATOMIC_ENTITY_ID_FIELD.get().incrementAndGet();
} else {
2023-04-24 19:58:14 +00:00
int id = Reflections.ENTITY_ID_MODIFIER.get();
Reflections.ENTITY_ID_MODIFIER.set(id + 1);
return id;
}
}
2023-04-24 16:12:50 +00:00
}