Skip to content

Commit

Permalink
Debloat entity constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed Nov 19, 2024
1 parent 463231f commit 2c7d48b
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@ public void onJoin(ClientboundLoginPacket packet) {

// Init client entity
clientEntity =
new ClientEntity(packet.getEntityId(), botProfile.getId(), connection, currentLevel());
new ClientEntity(connection, currentLevel(), botProfile);
clientEntity.entityId(packet.getEntityId());
clientEntity.showReducedDebug(packet.isReducedDebugInfo());
entityTrackerState.addEntity(clientEntity);
}
Expand Down Expand Up @@ -795,29 +796,19 @@ public void onBorderWarningBlocks(ClientboundSetBorderWarningDistancePacket pack
public void onEntitySpawn(ClientboundAddEntityPacket packet) {
var entityState =
new RawEntity(
packet.getEntityId(),
packet.getUuid(),
EntityType.REGISTRY.getById(packet.getType().ordinal()),
packet.getData(),
currentLevel(),
packet.getX(),
packet.getY(),
packet.getZ(),
packet.getYaw(),
packet.getPitch(),
packet.getHeadYaw(),
packet.getMotionX(),
packet.getMotionY(),
packet.getMotionZ());
currentLevel());
entityState.fromAddEntityPacket(packet);

entityTrackerState.addEntity(entityState);
}

@EventHandler
public void onExperienceOrbSpawn(ClientboundAddExperienceOrbPacket packet) {
var experienceOrbState =
new ExperienceOrbEntity(packet.getEntityId(), packet.getExp(), currentLevel(), packet.getX(), packet.getY(),
packet.getZ());
new ExperienceOrbEntity(currentLevel(), packet.getExp());
experienceOrbState.entityId(packet.getEntityId());
experienceOrbState.setPosition(packet.getX(), packet.getY(), packet.getZ());

entityTrackerState.addEntity(experienceOrbState);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,12 @@
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.geysermc.mcprotocollib.auth.GameProfile;
import org.geysermc.mcprotocollib.protocol.data.game.entity.EntityEvent;
import org.geysermc.mcprotocollib.protocol.data.game.entity.player.PlayerState;
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.level.ServerboundPlayerInputPacket;
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.player.*;

import java.util.UUID;

/**
* Represents the bot itself as an entity.
*/
Expand All @@ -43,6 +42,7 @@
public class ClientEntity extends Entity {
private final AbilitiesData abilitiesData = new AbilitiesData();
private final BotConnection connection;
private final GameProfile gameProfile;
private boolean showReducedDebug;
private int opPermissionLevel;
private double lastX = 0;
Expand All @@ -59,11 +59,11 @@ public class ClientEntity extends Entity {
private int positionReminder = 0;
private ServerboundPlayerInputPacket lastSentInput = new ServerboundPlayerInputPacket(false, false, false, false, false, false, false);

public ClientEntity(
int entityId, UUID uuid, BotConnection connection,
Level level) {
super(entityId, uuid, EntityType.PLAYER, level, 0, 0, 0, -180, 0, -180, 0, 0, 0);
public ClientEntity(BotConnection connection, Level level, GameProfile gameProfile) {
super(EntityType.PLAYER, level);
this.connection = connection;
this.gameProfile = gameProfile;
uuid(gameProfile.getId());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import org.cloudburstmc.math.vector.Vector3i;
import org.geysermc.mcprotocollib.protocol.data.game.entity.EntityEvent;
import org.geysermc.mcprotocollib.protocol.data.game.entity.RotationOrigin;
import org.geysermc.mcprotocollib.protocol.data.game.entity.object.ObjectData;
import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.entity.spawn.ClientboundAddEntityPacket;

import java.util.HashSet;
import java.util.Set;
Expand All @@ -49,10 +51,11 @@ public abstract class Entity {
private final EntityAttributeState attributeState = new EntityAttributeState();
private final EntityEffectState effectState = new EntityEffectState();
private final Set<TagKey<FluidType>> fluidOnEyes = new HashSet<>();
public float fallDistance;
private final int entityId;
private final UUID uuid;
private final EntityType entityType;
public float fallDistance;
protected UUID uuid;
protected ObjectData data;
private int entityId;
protected Level level;
protected double x;
protected double y;
Expand All @@ -72,24 +75,19 @@ public abstract class Entity {
protected boolean isInPowderSnow;
protected boolean wasInPowderSnow;

public Entity(int entityId, UUID uuid, EntityType entityType,
Level level,
double x, double y, double z,
float yRot, float xRot, float headYRot,
double motionX, double motionY, double motionZ) {
this.entityId = entityId;
this.uuid = uuid;
public Entity(EntityType entityType, Level level) {
this.entityType = entityType;
this.level = level;
this.x = x;
this.y = y;
this.z = z;
this.yRot = yRot;
this.xRot = xRot;
this.headYRot = headYRot;
this.motionX = motionX;
this.motionY = motionY;
this.motionZ = motionZ;
}

public void fromAddEntityPacket(ClientboundAddEntityPacket packet) {
entityId(packet.getEntityId());
uuid(packet.getUuid());
data(packet.getData());
setPosition(packet.getX(), packet.getY(), packet.getZ());
setHeadRotation(packet.getHeadYaw());
setRotation(packet.getYaw(), packet.getPitch());
setMotion(packet.getMotionX(), packet.getMotionY(), packet.getMotionZ());
}

public EntityMovement toMovement() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,13 @@
import lombok.EqualsAndHashCode;
import lombok.Getter;

import java.util.UUID;

@Getter
@EqualsAndHashCode(callSuper = true)
public class ExperienceOrbEntity extends Entity {
private final int expValue;

public ExperienceOrbEntity(int entityId, int expValue, Level level, double x, double y, double z) {
super(entityId, UUID.randomUUID(), EntityType.EXPERIENCE_ORB, level, x, y, z, 0, 0, 0, 0, 0, 0);
public ExperienceOrbEntity(Level level, int expValue) {
super(EntityType.EXPERIENCE_ORB, level);
this.expValue = expValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,12 @@
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.geysermc.mcprotocollib.protocol.data.game.entity.object.ObjectData;

import java.util.UUID;

@Getter
@Setter
@EqualsAndHashCode(callSuper = true)
public class RawEntity extends Entity {
private final ObjectData data;

public RawEntity(int entityId, UUID uuid, EntityType type, ObjectData data,
Level level,
double x, double y, double z,
float yRot, float xRot, float headYRot,
double motionX, double motionY, double motionZ) {
super(entityId, uuid, type, level, x, y, z, yRot, xRot, headYRot, motionX, motionY, motionZ);
this.data = data;
public RawEntity(EntityType type, Level level) {
super(type, level);
}
}

0 comments on commit 2c7d48b

Please sign in to comment.