Skip to content

Commit e70862d

Browse files
committed
Optimize sub-level climbing mixin
1 parent 02b8a9f commit e70862d

3 files changed

Lines changed: 37 additions & 7 deletions

File tree

common/src/main/java/dev/ryanhcode/sable/mixin/climbing_sub_levels/LivingEntityMixin.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package dev.ryanhcode.sable.mixin.climbing_sub_levels;
22

3+
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
4+
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
5+
import com.llamalad7.mixinextras.sugar.Share;
6+
import com.llamalad7.mixinextras.sugar.ref.LocalRef;
37
import dev.ryanhcode.sable.Sable;
4-
import dev.ryanhcode.sable.api.SubLevelHelper;
58
import dev.ryanhcode.sable.companion.math.BoundingBox3d;
69
import dev.ryanhcode.sable.companion.math.JOMLConversion;
10+
import dev.ryanhcode.sable.mixinterface.entity.entity_sublevel_collision.EntityMovementExtension;
711
import dev.ryanhcode.sable.platform.SablePlatform;
812
import dev.ryanhcode.sable.sublevel.SubLevel;
913
import net.minecraft.core.BlockPos;
@@ -30,12 +34,13 @@ public LivingEntityMixin(final EntityType<?> entityType, final Level level) {
3034
}
3135

3236
@Redirect(method = "onClimbable", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/LivingEntity;blockPosition()Lnet/minecraft/core/BlockPos;"))
33-
private BlockPos sable$redirectPos(final LivingEntity instance) {
37+
private BlockPos sable$redirectPos(final LivingEntity instance, @Share("subLevelBlockState") final LocalRef<BlockState> subLevelBlockState) {
3438
final Level level = this.level();
35-
final BlockPos defaultPos = instance.blockPosition();
36-
3739
final LivingEntity self = (LivingEntity) (Object) this;
38-
final BlockState defaultState = level.getBlockState(defaultPos);
40+
41+
final BlockPos defaultPos = ((EntityMovementExtension) this).sable$getInBlockStatePos();
42+
final BlockState defaultState = this.getInBlockState();
43+
3944
if (defaultState.is(BlockTags.CLIMBABLE) && SablePlatform.INSTANCE.isBlockstateLadder(defaultState, level, defaultPos, self)) {
4045
return defaultPos;
4146
}
@@ -48,10 +53,18 @@ public LivingEntityMixin(final EntityType<?> entityType, final Level level) {
4853
final BlockState state = level.getBlockState(pos);
4954

5055
if (state.is(BlockTags.CLIMBABLE) && SablePlatform.INSTANCE.isBlockstateLadder(state, level, pos, self)) {
56+
subLevelBlockState.set(state);
5157
return pos.immutable();
5258
}
5359
}
5460

5561
return defaultPos;
5662
}
63+
64+
65+
@WrapOperation(method = "onClimbable", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/LivingEntity;getInBlockState()Lnet/minecraft/world/level/block/state/BlockState;"))
66+
private BlockState getInBlockState(final LivingEntity instance, final Operation<BlockState> original, @Share("subLevelBlockState") final LocalRef<BlockState> subLevelBlockState) {
67+
final BlockState state = subLevelBlockState.get();
68+
return state != null ? state : original.call(instance);
69+
}
5770
}

common/src/main/java/dev/ryanhcode/sable/mixin/entity/entity_sublevel_collision/EntityMixin.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
55
import dev.ryanhcode.sable.ActiveSableCompanion;
66
import dev.ryanhcode.sable.Sable;
7-
import dev.ryanhcode.sable.api.SubLevelHelper;
8-
import dev.ryanhcode.sable.api.entity.EntitySubLevelUtil;
97
import dev.ryanhcode.sable.companion.math.BoundingBox3d;
108
import dev.ryanhcode.sable.companion.math.JOMLConversion;
119
import dev.ryanhcode.sable.index.SableTags;
@@ -79,6 +77,9 @@ public abstract class EntityMixin implements EntityMovementExtension {
7977
@Shadow
8078
protected abstract boolean isHorizontalCollisionMinor(Vec3 arg);
8179

80+
@Unique
81+
private BlockPos sable$inBlockStatePos = BlockPos.ZERO;
82+
8283
// @Unique
8384
// private Vector3d sable$trackStartUpDirection = null;
8485
//
@@ -247,6 +248,14 @@ public void updateEntityAfterFallOn(final Block instance, final BlockGetter arg,
247248
}
248249
}
249250

251+
/**
252+
* @return the position that the state returned by getInBlockState was gotten from
253+
*/
254+
@Override
255+
public BlockPos sable$getInBlockStatePos() {
256+
return this.sable$inBlockStatePos;
257+
}
258+
250259
/**
251260
* @author RyanH
252261
* @reason Take into account sub-levels
@@ -257,6 +266,7 @@ public BlockState getInBlockState() {
257266

258267
if (this.inBlockState == null || this.sable$trackingSubLevel != null) {
259268
this.inBlockState = level.getBlockState(this.blockPosition);
269+
this.sable$inBlockStatePos = this.blockPosition;
260270

261271
final Iterable<SubLevel> intersecting = Sable.HELPER.getAllIntersecting(this.level, new BoundingBox3d(this.blockPosition));
262272

@@ -265,6 +275,7 @@ public BlockState getInBlockState() {
265275
final SubLevel subLevel = iter.next();
266276
final BlockPos localBlockPos = BlockPos.containing(subLevel.logicalPose().transformPositionInverse(this.position.add(0.0, 0.001, 0.0)));
267277
this.inBlockState = level.getBlockState(localBlockPos);
278+
this.sable$inBlockStatePos = localBlockPos;
268279
}
269280
}
270281

common/src/main/java/dev/ryanhcode/sable/mixinterface/entity/entity_sublevel_collision/EntityMovementExtension.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import dev.ryanhcode.sable.sublevel.SubLevel;
44
import dev.ryanhcode.sable.sublevel.entity_collision.SubLevelEntityCollision;
5+
import net.minecraft.core.BlockPos;
56
import net.minecraft.world.phys.Vec3;
67
import org.jetbrains.annotations.ApiStatus;
78

@@ -23,4 +24,9 @@ public interface EntityMovementExtension {
2324
void sable$setTrackingSubLevel(SubLevel subLevel);
2425

2526
void sable$setLastTrackingSubLevelID(UUID uuid);
27+
28+
/**
29+
* @return the position that the state returned by getInBlockState was gotten from
30+
*/
31+
BlockPos sable$getInBlockStatePos();
2632
}

0 commit comments

Comments
 (0)