Skip to content

Commit

Permalink
physics entity optimizations
Browse files Browse the repository at this point in the history
- cache tracked values
- disable many behaviors while locked
- remove direction syncing packet spam
  • Loading branch information
TropheusJ committed Aug 7, 2023
1 parent 180c429 commit e740d48
Showing 1 changed file with 32 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ public CorePhysicsEntity(EntityType<? extends PathfinderMob> type, Level world)

private final Vec3 offsetHeight = new Vec3(0, this.getBbHeight() / 2, 0);

private boolean locked = false;
private boolean onButton = false;
private Optional<UUID> holder = Optional.empty();

@Override
public boolean canBeCollidedWith() {
return canUsePortals;
Expand Down Expand Up @@ -187,24 +191,36 @@ protected void defineSynchedData() {
this.getEntityData().define(LOCKED, false);
}

@Override
public void onSyncedDataUpdated(EntityDataAccessor<?> key) {
super.onSyncedDataUpdated(key);
if (LOCKED.equals(key)) {
this.locked = entityData.get(LOCKED);
} else if (ON_BUTTON.equals(key)) {
this.onButton = entityData.get(ON_BUTTON);
} else if (HOLDER_UUID.equals(key)) {
this.holder = entityData.get(HOLDER_UUID);
}
}

public Optional<UUID> getHolderUUID() {
return getEntityData().get(HOLDER_UUID);
return holder;
}

public void setHolderUUID(Optional<UUID> uuid) {
this.getEntityData().set(HOLDER_UUID, uuid);
entityData.set(HOLDER_UUID, uuid);
}

public boolean isOnButton() {
return getEntityData().get(ON_BUTTON);
return onButton;
}

public void setOnButton(boolean on) {
getEntityData().set(ON_BUTTON, on);
entityData.set(ON_BUTTON, on);
}

public boolean isLocked() {
return entityData.get(LOCKED);
return locked;
}

public void setRotYaw(float yaw) {
Expand All @@ -219,20 +235,21 @@ public boolean canChangeDimensions() {
@Override
public void tick() {
super.tick();
final boolean isBeingHeld = getHolderUUID().isPresent() && !fizzling;
UUID holder = this.holder.orElse(null);
final boolean isBeingHeld = holder != null && !fizzling;
timeSinceLastSound++;
this.hasImpulse = true;
canUsePortals = getHolderUUID().isEmpty();
Vec3 rotatedOffset = RotationUtil.vecPlayerToWorld(offsetHeight, GravityChangerAPI.getGravityDirection(this));
canUsePortals = holder == null;
this.lastPos = this.position();
this.setDiscardFriction(!this.onGround() && !((EntityExt) this).isInFunnel());
if (isBeingHeld) {
Player player = (Player) ((LevelExt) level()).getEntityByUuid(getHolderUUID().get());
Player player = (Player) ((LevelExt) level()).getEntityByUuid(holder);
if (player != null && player.isAlive()) {
Vec3 eyes = player.getEyePosition(0);
double distance = 1.5;
canUsePortals = false;
Vec3 rotation = this.getPlayerRotationVector(player.getXRot(), player.getYRot());
Vec3 rotatedOffset = RotationUtil.vecPlayerToWorld(offsetHeight, GravityChangerAPI.getGravityDirection(this));
Vec3 target = eyes.add(
(rotation.x * distance) - rotatedOffset.x,
(rotation.y * distance) - rotatedOffset.y,
Expand Down Expand Up @@ -301,16 +318,12 @@ public void tick() {
}
}
}
if (getHolderUUID().isEmpty() && !level().isClientSide) {
//noinspection DataFlowIssue
level().getServer().getPlayerList().broadcast(
null, getX(), getY(), getZ(), 64,
level().dimension(),
new ClientboundRotateHeadPacket(this, (byte)Mth.floor(getYHeadRot() * 256f / 360f))
);
}
if (!RayonIntegration.INSTANCE.isPresent()) {
setXRot(0f);
}

@Override
public void aiStep() {
if (!isLocked()) {
super.aiStep();
}
}

Expand Down

0 comments on commit e740d48

Please sign in to comment.