Skip to content

Commit 38bfc71

Browse files
committed
fix: remove overhead via updateBaseEntity0
1 parent d4efb0c commit 38bfc71

File tree

16 files changed

+31
-53
lines changed

16 files changed

+31
-53
lines changed

api/src/main/java/kr/toxicity/model/api/nms/ModelDisplay.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ public interface ModelDisplay extends Identifiable {
5151
void rotate(@NotNull ModelRotation rotation, @NotNull PacketBundler bundler);
5252

5353
/**
54-
* Synchronizes this display with a source entity's data.
54+
* Synchronizes the potion effect (glowing, etc.) from the base entity to this display.
5555
*
5656
* @param entity the source entity
57-
* @since 1.15.2
57+
* @since 2.2.0
5858
*/
59-
void syncEntity(@NotNull BaseEntity entity);
59+
void syncPotionEffect(@NotNull BaseEntity entity);
6060

6161
/**
6262
* Synchronizes this display's position with a location.

api/src/main/java/kr/toxicity/model/api/tracker/EntityTracker.java

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import kr.toxicity.model.api.platform.PlatformPlayer;
2323
import kr.toxicity.model.api.util.EventUtil;
2424
import kr.toxicity.model.api.util.FunctionUtil;
25+
import kr.toxicity.model.api.util.MathUtil;
2526
import kr.toxicity.model.api.util.function.BonePredicate;
2627
import org.jetbrains.annotations.ApiStatus;
2728
import org.jetbrains.annotations.NotNull;
@@ -69,6 +70,8 @@ public class EntityTracker extends Tracker {
6970
private final EntityBodyRotator bodyRotator;
7071
private EntityHideOption hideOption = EntityHideOption.DEFAULT;
7172

73+
private volatile PlatformLocation location;
74+
7275
/**
7376
* Creates a new entity tracker.
7477
*
@@ -82,6 +85,7 @@ public class EntityTracker extends Tracker {
8285
public EntityTracker(@NotNull EntityTrackerRegistry registry, @NotNull RenderPipeline pipeline, @NotNull TrackerModifier modifier, @NotNull Consumer<EntityTracker> preUpdateConsumer) {
8386
super(pipeline, modifier);
8487
this.registry = registry;
88+
this.location = registry.entity().location();
8589
bodyRotator = new EntityBodyRotator(registry);
8690

8791
var entity = registry.entity();
@@ -99,7 +103,7 @@ public EntityTracker(@NotNull EntityTrackerRegistry registry, @NotNull RenderPip
99103
tick(((t, s) -> {
100104
var wPos = bone.hitBoxPosition(posCache);
101105
shadow.shadowRadius(scale.getAsFloat() * baseScale);
102-
shadow.syncEntity(entity);
106+
shadow.syncPotionEffect(entity);
103107
shadow.syncPosition(location().add(wPos.x, wPos.y, wPos.z));
104108
shadow.sendDirtyEntityData(s.getDataBundler());
105109
shadow.sendPosition(entity, s.getTickBundler());
@@ -141,7 +145,7 @@ public EntityTracker(@NotNull EntityTrackerRegistry registry, @NotNull RenderPip
141145
if (isClosed()) return;
142146
createHitBox(null, CREATE_HITBOX_PREDICATE);
143147
});
144-
tick((t, s) -> updateBaseEntity0());
148+
tick((t, s) -> updateLocation());
145149
tick((t, s) -> {
146150
if (damageTint.getAndDecrement() == 0) update(TrackerUpdateAction.previousTint());
147151
});
@@ -152,7 +156,7 @@ public EntityTracker(@NotNull EntityTrackerRegistry registry, @NotNull RenderPip
152156

153157
@Override
154158
public @NotNull ModelRotation rotation() {
155-
return registry.entity().dead() ? pipeline.getRotation() : super.rotation();
159+
return sourceEntity().dead() ? pipeline.getRotation() : super.rotation();
156160
}
157161

158162
/**
@@ -161,21 +165,22 @@ public EntityTracker(@NotNull EntityTrackerRegistry registry, @NotNull RenderPip
161165
* @since 1.15.2
162166
*/
163167
public void updateBaseEntity() {
168+
if (sourceEntity().dead() || isClosed()) return;
164169
BetterModel.platform().scheduler().asyncTaskLater(1, () -> {
165-
updateBaseEntity0();
170+
var entity = sourceEntity();
171+
pipeline.forEach(bone -> bone.applyAtDisplay(BonePredicate.TRUE, display -> display.syncPotionEffect(entity)));
172+
updateLocation();
166173
forceUpdate(true);
167174
});
168175
}
169176

170-
/**
171-
* Updates base entity's data to parent entity
172-
*/
173-
private void updateBaseEntity0() {
174-
var loc = location();
175-
displays().forEach(d -> {
176-
d.syncEntity(registry.entity());
177-
d.syncPosition(loc);
178-
});
177+
private void updateLocation() {
178+
var loc = sourceEntity().location();
179+
if (this.location.distanceSquared(loc) < MathUtil.VECTOR_COMPARISON_EPSILON_SQ) return;
180+
synchronized (this) {
181+
this.location = loc;
182+
}
183+
pipeline.forEach(bone -> bone.applyAtDisplay(BonePredicate.TRUE, display -> display.syncPosition(loc)));
179184
}
180185

181186
/**
@@ -245,7 +250,7 @@ public void damageTint() {
245250

246251
@Override
247252
public void despawn() {
248-
if (registry.entity().dead()) {
253+
if (sourceEntity().dead()) {
249254
close(CloseReason.DESPAWN);
250255
return;
251256
}
@@ -254,7 +259,7 @@ public void despawn() {
254259

255260
@Override
256261
public @NotNull PlatformLocation location() {
257-
return sourceEntity().location();
262+
return location;
258263
}
259264

260265
/**
@@ -283,7 +288,7 @@ public void cancelDamageTint() {
283288
*/
284289
@ApiStatus.Internal
285290
public void refresh() {
286-
updateBaseEntity0();
291+
updateLocation();
287292
registry.entity().platform().task(() -> createHitBox(null, HITBOX_REFRESH_PREDICATE));
288293
}
289294

nms/v1_21_R1/src/main/kotlin/kr/toxicity/model/bukkit/nms/v1_21_R1/HitBoxImpl.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,6 @@ internal class HitBoxImpl(
286286
BlockPos.betweenClosedStream(boundingBox).forEach {
287287
level().getBlockState(it).entityInside(level(), it, delegate)
288288
}
289-
updateInWaterStateAndDoFluidPushing()
290289
if (isInLava) delegate.lavaHurt()
291290
firstTick = false
292291
listener.sync(craftEntity)

nms/v1_21_R1/src/main/kotlin/kr/toxicity/model/bukkit/nms/v1_21_R1/ModelDisplayImpl.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ internal class ModelDisplayImpl(
4848
override fun id(): Int = display.id
4949
override fun uuid(): UUID = display.uuid
5050
override fun rotate(rotation: ModelRotation, bundler: PacketBundler) {
51-
if (!display.valid) return
5251
display.xRot = rotation.x
5352
display.yRot = rotation.y
5453
bundler += ClientboundMoveEntityPacket.Rot(
@@ -67,9 +66,7 @@ internal class ModelDisplayImpl(
6766
}
6867
}
6968

70-
override fun syncEntity(entity: BaseEntity) {
71-
display.valid = !entity.dead()
72-
display.onGround = entity.ground()
69+
override fun syncPotionEffect(entity: BaseEntity) {
7370
val beforeInvisible = display.isInvisible
7471
val afterInvisible = entity.invisible()
7572
entityDataLock.accessToLock {

nms/v1_21_R3/src/main/kotlin/kr/toxicity/model/bukkit/nms/v1_21_R3/HitBoxImpl.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,6 @@ internal class HitBoxImpl(
300300
BlockPos.betweenClosedStream(boundingBox).forEach {
301301
level().getBlockState(it).entityInside(level(), it, delegate)
302302
}
303-
updateInWaterStateAndDoFluidPushing()
304303
if (isInLava) delegate.lavaHurt()
305304
firstTick = false
306305
listener.sync(craftEntity)

nms/v1_21_R3/src/main/kotlin/kr/toxicity/model/bukkit/nms/v1_21_R3/ModelDisplayImpl.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ internal class ModelDisplayImpl(
4848
override fun id(): Int = display.id
4949
override fun uuid(): UUID = display.uuid
5050
override fun rotate(rotation: ModelRotation, bundler: PacketBundler) {
51-
if (!display.valid) return
5251
display.xRot = rotation.x
5352
display.yRot = rotation.y
5453
bundler += ClientboundMoveEntityPacket.Rot(
@@ -67,9 +66,7 @@ internal class ModelDisplayImpl(
6766
}
6867
}
6968

70-
override fun syncEntity(entity: BaseEntity) {
71-
display.valid = !entity.dead()
72-
display.onGround = entity.ground()
69+
override fun syncPotionEffect(entity: BaseEntity) {
7370
val beforeInvisible = display.isInvisible
7471
val afterInvisible = entity.invisible()
7572
entityDataLock.accessToLock {

nms/v1_21_R4/src/main/kotlin/kr/toxicity/model/bukkit/nms/v1_21_R4/HitBoxImpl.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,6 @@ internal class HitBoxImpl(
290290
level().getBlockState(pos).entityInside(level(), pos, delegate, applier)
291291
}
292292
applier.applyAndClear(delegate)
293-
updateInWaterStateAndDoFluidPushing()
294293
if (isInLava) delegate.lavaHurt()
295294
firstTick = false
296295
listener.sync(craftEntity)

nms/v1_21_R4/src/main/kotlin/kr/toxicity/model/bukkit/nms/v1_21_R4/ModelDisplayImpl.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ internal class ModelDisplayImpl(
4848
override fun id(): Int = display.id
4949
override fun uuid(): UUID = display.uuid
5050
override fun rotate(rotation: ModelRotation, bundler: PacketBundler) {
51-
if (!display.valid) return
5251
display.xRot = rotation.x
5352
display.yRot = rotation.y
5453
bundler += ClientboundMoveEntityPacket.Rot(
@@ -67,9 +66,7 @@ internal class ModelDisplayImpl(
6766
}
6867
}
6968

70-
override fun syncEntity(entity: BaseEntity) {
71-
display.valid = !entity.dead()
72-
display.onGround = entity.ground()
69+
override fun syncPotionEffect(entity: BaseEntity) {
7370
val beforeInvisible = display.isInvisible
7471
val afterInvisible = entity.invisible()
7572
entityDataLock.accessToLock {

nms/v1_21_R5/src/main/kotlin/kr/toxicity/model/bukkit/nms/v1_21_R5/HitBoxImpl.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,6 @@ internal class HitBoxImpl(
307307
true
308308
}
309309
applier.applyAndClear(delegate)
310-
updateInWaterStateAndDoFluidPushing()
311310
if (isInLava) delegate.lavaHurt()
312311
firstTick = false
313312
listener.sync(craftEntity)

nms/v1_21_R5/src/main/kotlin/kr/toxicity/model/bukkit/nms/v1_21_R5/ModelDisplayImpl.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ internal class ModelDisplayImpl(
4848
override fun id(): Int = display.id
4949
override fun uuid(): UUID = display.uuid
5050
override fun rotate(rotation: ModelRotation, bundler: PacketBundler) {
51-
if (!display.valid) return
5251
display.xRot = rotation.x
5352
display.yRot = rotation.y
5453
bundler += ClientboundMoveEntityPacket.Rot(
@@ -67,9 +66,7 @@ internal class ModelDisplayImpl(
6766
}
6867
}
6968

70-
override fun syncEntity(entity: BaseEntity) {
71-
display.valid = !entity.dead()
72-
display.onGround = entity.ground()
69+
override fun syncPotionEffect(entity: BaseEntity) {
7370
val beforeInvisible = display.isInvisible
7471
val afterInvisible = entity.invisible()
7572
entityDataLock.accessToLock {

0 commit comments

Comments
 (0)