Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions api/src/main/java/kr/toxicity/model/api/nms/HitBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,19 @@ default void listener(@NotNull Function<HitBoxListener.Builder, HitBoxListener.B
*/
@NotNull RenderedBone positionSource();

/**
* Synchronizes the position of this hitbox with its source entity.
* <p>
* This method ensures that the hitbox position follows its model even when
* it is outside the server's simulation distance. It should be called
* periodically from the entity tracker's tick system.
* </p>
*
* @since 3.3.2
*/
@ApiStatus.Internal
void syncPosition();

/**
* Returns the entity tracker registry for this hitbox's source entity.
*
Expand Down
15 changes: 11 additions & 4 deletions api/src/main/java/kr/toxicity/model/api/tracker/EntityTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@
import org.jetbrains.annotations.Nullable;
import org.joml.Quaternionf;

import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
Expand Down Expand Up @@ -146,6 +143,16 @@ public EntityTracker(@NotNull EntityTrackerRegistry registry, @NotNull RenderPip
if (isClosed()) return;
createHitBox(null, CREATE_HITBOX_PREDICATE);
});
tick((_, _) -> {
var hbs = new ArrayList<>(registry.hitBoxCache.values());
if (!hbs.isEmpty()) {
registry.entity().platform().task(() -> {
for (var hb : hbs) {
hb.syncPosition();
}
});
}
});
tick((_, _) -> updateLocation());
tick((_, _) -> {
if (damageTint.getAndDecrement() == 0) update(TrackerUpdateAction.previousTint());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ private void initialLoad() {
}

private void refreshPlayer() {
if (entity.dead()) return;
entity.trackedBy()
.map(p -> BetterModel.player(p.uuid()).orElse(null))
.filter(Objects::nonNull)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,25 @@ internal class HitBoxImpl(
override fun relativePosition(): Vector3f = delegate.position().run {
bone.hitBoxPosition(posCache).add(x.toFloat(), y.toFloat(), z.toFloat())
}

override fun syncPosition() {
yRot = bone.rotation().y
yHeadRot = yRot
yBodyRot = yRot
val pos = relativePosition()
val minusHeight = source.minY * bone.hitBoxScale()
setPos(
pos.x.toDouble(),
pos.y.toDouble() + minusHeight,
pos.z.toDouble()
)
interaction.setPos(
pos.x.toDouble(),
pos.y.toDouble() + minusHeight,
pos.z.toDouble()
)
}

override fun listener(): HitBoxListener = listener
override fun listener(listener: HitBoxListener) {
this.listener = listener
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,25 @@ internal class HitBoxImpl(
override fun relativePosition(): Vector3f = delegate.position().run {
bone.hitBoxPosition(posCache).add(x.toFloat(), y.toFloat(), z.toFloat())
}

override fun syncPosition() {
yRot = bone.rotation().y
yHeadRot = yRot
yBodyRot = yRot
val pos = relativePosition()
val minusHeight = source.minY * bone.hitBoxScale()
setPos(
pos.x.toDouble(),
pos.y.toDouble() + minusHeight,
pos.z.toDouble()
)
interaction.setPos(
pos.x.toDouble(),
pos.y.toDouble() + minusHeight,
pos.z.toDouble()
)
}

override fun listener(): HitBoxListener = listener
override fun listener(listener: HitBoxListener) {
this.listener = listener
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,25 @@ internal class HitBoxImpl(
override fun relativePosition(): Vector3f = delegate.position().run {
bone.hitBoxPosition(posCache).add(x.toFloat(), y.toFloat(), z.toFloat())
}

override fun syncPosition() {
yRot = bone.rotation().y
yHeadRot = yRot
yBodyRot = yRot
val pos = relativePosition()
val minusHeight = source.minY * bone.hitBoxScale()
setPos(
pos.x.toDouble(),
pos.y.toDouble() + minusHeight,
pos.z.toDouble()
)
interaction.setPos(
pos.x.toDouble(),
pos.y.toDouble() + minusHeight,
pos.z.toDouble()
)
}

override fun listener(): HitBoxListener = listener
override fun listener(listener: HitBoxListener) {
this.listener = listener
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,25 @@ internal class HitBoxImpl(
override fun relativePosition(): Vector3f = delegate.position().run {
bone.hitBoxPosition(posCache).add(x.toFloat(), y.toFloat(), z.toFloat())
}

override fun syncPosition() {
yRot = bone.rotation().y
yHeadRot = yRot
yBodyRot = yRot
val pos = relativePosition()
val minusHeight = source.minY * bone.hitBoxScale()
setPos(
pos.x.toDouble(),
pos.y.toDouble() + minusHeight,
pos.z.toDouble()
)
interaction.setPos(
pos.x.toDouble(),
pos.y.toDouble() + minusHeight,
pos.z.toDouble()
)
}

override fun listener(): HitBoxListener = listener
override fun listener(listener: HitBoxListener) {
this.listener = listener
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,25 @@ internal class HitBoxImpl(
override fun relativePosition(): Vector3f = delegate.position().run {
bone.hitBoxPosition(posCache).add(x.toFloat(), y.toFloat(), z.toFloat())
}

override fun syncPosition() {
yRot = bone.rotation().y
yHeadRot = yRot
yBodyRot = yRot
val pos = relativePosition()
val minusHeight = source.minY * bone.hitBoxScale()
setPos(
pos.x.toDouble(),
pos.y.toDouble() + minusHeight,
pos.z.toDouble()
)
interaction.setPos(
pos.x.toDouble(),
pos.y.toDouble() + minusHeight,
pos.z.toDouble()
)
}

override fun listener(): HitBoxListener = listener
override fun listener(listener: HitBoxListener) {
this.listener = listener
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,25 @@ internal class HitBoxImpl(
override fun relativePosition(): Vector3f = delegate.position().run {
bone.hitBoxPosition(posCache).add(x.toFloat(), y.toFloat(), z.toFloat())
}

override fun syncPosition() {
yRot = bone.rotation().y
yHeadRot = yRot
yBodyRot = yRot
val pos = relativePosition()
val minusHeight = source.minY * bone.hitBoxScale()
setPos(
pos.x.toDouble(),
pos.y.toDouble() + minusHeight,
pos.z.toDouble()
)
interaction.setPos(
pos.x.toDouble(),
pos.y.toDouble() + minusHeight,
pos.z.toDouble()
)
}

override fun listener(): HitBoxListener = listener
override fun listener(listener: HitBoxListener) {
this.listener = listener
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,19 @@ class HitBoxEntityImpl(
)
}

override fun syncPosition() {
yRot = bone.rotation().y
yHeadRot = yRot
yBodyRot = yRot
val pos = relativePosition()
val minusHeight = source.minY * bone.hitBoxScale()
val targetX = pos.x.toDouble()
val targetY = pos.y.toDouble() + minusHeight
val targetZ = pos.z.toDouble()
setPos(targetX, targetY, targetZ)
interaction.setPos(targetX, targetY, targetZ)
}

override fun listener(): HitBoxListener = listener

override fun listener(listener: HitBoxListener) {
Expand Down