Skip to content

Commit

Permalink
Cache entity max tracking distance
Browse files Browse the repository at this point in the history
  • Loading branch information
DrexHD committed Jun 11, 2024
1 parent fc7f950 commit 26274f5
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lithium-mixin-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@ Uses custom hashset/list combination for faster mob spawn checks
(default: `true`)
Various entity optimizations

### `mixin.entity.cache_max_track_distance`
(default: `true`)
Caches the max tracking distance of entities with their passengers

### `mixin.entity.collisions`
(default: `true`)
Various entity collision optimizations
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package me.jellysquid.mods.lithium.common.entity;

public interface PassengerCachingEntity {

boolean lithium$isPassengerListDirty();

void lithium$setPassengerListDirty(boolean dirty);

void lithium$setPassengerListDirtyRecursive(boolean dirty);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package me.jellysquid.mods.lithium.mixin.entity.cache_max_track_distance;

import me.jellysquid.mods.lithium.common.entity.PassengerCachingEntity;
import net.minecraft.entity.Entity;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(Entity.class)
public abstract class EntityMixin implements PassengerCachingEntity {
@Shadow
private @Nullable Entity vehicle;
@Unique
private boolean isPassengerListDirty = true;

@Override
public boolean lithium$isPassengerListDirty() {
return this.isPassengerListDirty;
}

@Override
public void lithium$setPassengerListDirty(boolean dirty) {
this.isPassengerListDirty = dirty;
}

@Override
public void lithium$setPassengerListDirtyRecursive(boolean dirty) {
this.isPassengerListDirty = true;
if (this.vehicle != null) {
((PassengerCachingEntity) this.vehicle).lithium$setPassengerListDirtyRecursive(dirty);
}
}

@Inject(method = {"addPassenger", "removePassenger"}, at = @At("TAIL"))
private void lithium$markPassengerListDirty(Entity passenger, CallbackInfo ci) {
lithium$setPassengerListDirtyRecursive(true);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package me.jellysquid.mods.lithium.mixin.entity.cache_max_track_distance;

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import me.jellysquid.mods.lithium.common.entity.PassengerCachingEntity;
import net.minecraft.entity.Entity;
import net.minecraft.server.world.ServerChunkLoadingManager;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;

@Mixin(ServerChunkLoadingManager.EntityTracker.class)
public abstract class ServerChunkLoadingManager$EntityTrackerMixin {

@Shadow
@Final
public Entity entity;

@Unique
private int cachedMaxTrackingDistance;

@WrapOperation(method = "updateTrackedStatus(Lnet/minecraft/server/network/ServerPlayerEntity;)V",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/server/world/ServerChunkLoadingManager$EntityTracker;getMaxTrackDistance()I"
)
)
private int cacheMaxTrackDistance(ServerChunkLoadingManager.EntityTracker instance, Operation<Integer> original) {
if (((PassengerCachingEntity) this.entity).lithium$isPassengerListDirty()) {
this.cachedMaxTrackingDistance = original.call(instance);
((PassengerCachingEntity) this.entity).lithium$setPassengerListDirty(false);
}
return this.cachedMaxTrackingDistance;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@MixinConfigOption(description = "Caches the max tracking distance of entities with their passengers")
package me.jellysquid.mods.lithium.mixin.entity.cache_max_track_distance;

import net.caffeinemc.gradle.MixinConfigOption;
2 changes: 2 additions & 0 deletions src/main/resources/lithium.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@
"collections.goals.GoalSelectorMixin",
"collections.mob_spawning.PoolMixin",
"collections.mob_spawning.SpawnSettingsMixin",
"entity.cache_max_track_distance.EntityMixin",
"entity.cache_max_track_distance.ServerChunkLoadingManager$EntityTrackerMixin",
"entity.collisions.fluid.EntityMixin",
"entity.collisions.intersection.EntityViewMixin",
"entity.collisions.intersection.WorldMixin",
Expand Down

0 comments on commit 26274f5

Please sign in to comment.