Skip to content

Commit

Permalink
Small refactoring to the EntityTracker
Browse files Browse the repository at this point in the history
  • Loading branch information
Bram1903 committed Mar 1, 2025
1 parent e288524 commit 50dc6ef
Showing 1 changed file with 12 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes;
import com.github.retrooper.packetevents.protocol.packettype.PacketType;
import com.github.retrooper.packetevents.protocol.packettype.PacketTypeCommon;
import com.github.retrooper.packetevents.protocol.player.User;
import com.github.retrooper.packetevents.wrapper.play.server.*;

/**
Expand Down Expand Up @@ -63,7 +62,7 @@ public void onPacketSend(PacketSendEvent event) {
} else if (PacketType.Play.Server.SPAWN_PLAYER == type) {
handleSpawnPlayer(new WrapperPlayServerSpawnPlayer(event));
} else if (PacketType.Play.Server.ENTITY_METADATA == type) {
handleEntityMetadata(new WrapperPlayServerEntityMetadata(event), event.getUser(), settings);
handleEntityMetadata(new WrapperPlayServerEntityMetadata(event), settings);
} else if (PacketType.Play.Server.DESTROY_ENTITIES == type) {
handleDestroyEntities(new WrapperPlayServerDestroyEntities(event));
} else if (PacketType.Play.Server.RESPAWN == type) {
Expand All @@ -77,44 +76,31 @@ public void onPacketSend(PacketSendEvent event) {

private void handleSpawnLivingEntity(WrapperPlayServerSpawnLivingEntity packet, Settings settings) {
EntityType entityType = packet.getEntityType();

if (settings.getEntityData().isPlayersOnly()) {
if (!EntityTypes.isTypeInstanceOf(entityType, EntityTypes.PLAYER)) return;
}

int entityId = packet.getEntityId();

CachedEntity entityData = createLivingEntity(entityType);
entityCache.addLivingEntity(entityId, entityData);
if (settings.getEntityData().isPlayersOnly() && !EntityTypes.isTypeInstanceOf(entityType, EntityTypes.PLAYER)) return;
spawnEntity(packet.getEntityId(), entityType);
}

private void handleSpawnEntity(WrapperPlayServerSpawnEntity packet, Settings settings) {
EntityType entityType = packet.getEntityType();
if (!EntityTypes.isTypeInstanceOf(entityType, EntityTypes.LIVINGENTITY)) return;
if (settings.getEntityData().isPlayersOnly() && !EntityTypes.isTypeInstanceOf(entityType, EntityTypes.PLAYER)) return;

if (EntityTypes.isTypeInstanceOf(entityType, EntityTypes.LIVINGENTITY)) {
if (settings.getEntityData().isPlayersOnly()) {
if (!EntityTypes.isTypeInstanceOf(entityType, EntityTypes.PLAYER)) return;
}

int entityId = packet.getEntityId();

CachedEntity entityData = createLivingEntity(entityType);
entityCache.addLivingEntity(entityId, entityData);
}
spawnEntity(packet.getEntityId(), entityType);
}

private void handleSpawnPlayer(WrapperPlayServerSpawnPlayer packet) {
CachedEntity livingEntityData = new CachedEntity();
livingEntityData.setEntityType(EntityTypes.PLAYER);
spawnEntity(packet.getEntityId(), EntityTypes.PLAYER);
}

entityCache.addLivingEntity(packet.getEntityId(), livingEntityData);
private void spawnEntity(int entityId, EntityType entityType) {
CachedEntity entityData = createLivingEntity(entityType);
entityCache.addLivingEntity(entityId, entityData);
}

private void handleEntityMetadata(WrapperPlayServerEntityMetadata packet, User user, Settings settings) {
private void handleEntityMetadata(WrapperPlayServerEntityMetadata packet, Settings settings) {
if (settings.getEntityData().isPlayersOnly()) return;

int entityId = packet.getEntityId();

CachedEntity entityData = entityCache.getCachedEntity(entityId).orElse(null);
if (entityData == null) return;

Expand All @@ -141,15 +127,13 @@ private void handleConfigurationStart() {

private CachedEntity createLivingEntity(EntityType entityType) {
CachedEntity entityData;

if (EntityTypes.isTypeInstanceOf(entityType, EntityTypes.WOLF)) {
entityData = new WolfEntity();
} else if (RidableEntities.isRideable(entityType)) {
entityData = new RidableEntity();
} else {
entityData = new CachedEntity();
}

entityData.setEntityType(entityType);
return entityData;
}
Expand Down

0 comments on commit 50dc6ef

Please sign in to comment.