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

74 lines
2.2 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-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;
2023-04-26 23:58:09 +00:00
import lol.pyr.znpcsplus.util.ZLocation;
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 {
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-04-26 23:58:09 +00:00
private ZLocation location;
2023-04-24 16:12:50 +00:00
2023-04-26 23:58:09 +00:00
public PacketEntity(PropertyHolder properties, EntityType type, ZLocation location) {
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-04-26 23:58:09 +00:00
public ZLocation 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-04-26 23:58:09 +00:00
public void setLocation(ZLocation location, Collection<Player> viewers) {
2023-04-24 16:12:50 +00:00
this.location = location;
for (Player viewer : viewers) PacketFactory.get().teleportEntity(viewer, this);
}
public void spawn(Player player) {
2023-04-25 23:31:49 +00:00
if (type == EntityTypes.PLAYER) PacketFactory.get().spawnPlayer(player, this, properties);
else PacketFactory.get().spawnEntity(player, this, properties);
2023-04-24 16:12:50 +00:00
}
public void despawn(Player player) {
2023-04-25 23:31:49 +00:00
PacketFactory.get().destroyEntity(player, this, properties);
}
public void refreshMeta(Player player) {
PacketFactory.get().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 (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
}