Skip to content

Commit ba6f8b6

Browse files
authored
use ModifyReturnValue to optimise some mixins (#1415)
Mostly inconsequential except for some heavy-use methods such as in WorldBorder or ServerLevel/ClientLevel
1 parent bf9713a commit ba6f8b6

11 files changed

Lines changed: 94 additions & 102 deletions

File tree

common/src/main/java/dev/ryanhcode/sable/mixin/camera/camera_rotation/EntityMixin.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dev.ryanhcode.sable.mixin.camera.camera_rotation;
22

3+
import com.llamalad7.mixinextras.injector.ModifyReturnValue;
34
import dev.ryanhcode.sable.companion.math.JOMLConversion;
45
import dev.ryanhcode.sable.companion.math.Pose3dc;
56
import dev.ryanhcode.sable.mixinhelpers.camera.camera_rotation.EntitySubLevelRotationHelper;
@@ -12,8 +13,6 @@
1213
import org.spongepowered.asm.mixin.Mixin;
1314
import org.spongepowered.asm.mixin.Shadow;
1415
import org.spongepowered.asm.mixin.injection.At;
15-
import org.spongepowered.asm.mixin.injection.Inject;
16-
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
1716

1817
import java.util.function.Function;
1918

@@ -22,8 +21,8 @@ public abstract class EntityMixin {
2221

2322
@Shadow private Level level;
2423

25-
@Inject(method = "calculateViewVector", at = @At("RETURN"), cancellable = true)
26-
public void sable$calculateViewVector(final float f, final float g, final CallbackInfoReturnable<Vec3> cir) {
24+
@ModifyReturnValue(method = "calculateViewVector", at = @At("RETURN"))
25+
public Vec3 sable$calculateViewVector(final Vec3 original) {
2726
final Function<SubLevel, Pose3dc> provider;
2827

2928
if (this.level instanceof final LevelPoseProviderExtension levelPoseProvider) {
@@ -35,9 +34,10 @@ public abstract class EntityMixin {
3534
final Quaterniond orientation = EntitySubLevelRotationHelper.getEntityOrientation((Entity) (Object) this, provider, 0.0f, EntitySubLevelRotationHelper.Type.CAMERA);
3635

3736
if (orientation != null) {
38-
final Vec3 viewVector = cir.getReturnValue();
39-
cir.setReturnValue(JOMLConversion.toMojang(orientation.transform(JOMLConversion.toJOML(viewVector))));
37+
return JOMLConversion.toMojang(orientation.transform(JOMLConversion.toJOML(original)));
4038
}
39+
40+
return original;
4141
}
4242

4343
}
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
package dev.ryanhcode.sable.mixin.entity.entity_aabb_lookup;
22

3+
import com.llamalad7.mixinextras.injector.ModifyReturnValue;
34
import dev.ryanhcode.sable.util.SubLevelInclusiveLevelEntityGetter;
45
import net.minecraft.client.multiplayer.ClientLevel;
5-
import net.minecraft.server.level.ServerLevel;
66
import net.minecraft.world.entity.Entity;
77
import net.minecraft.world.level.Level;
88
import net.minecraft.world.level.entity.LevelEntityGetter;
99
import org.spongepowered.asm.mixin.Mixin;
1010
import org.spongepowered.asm.mixin.injection.At;
11-
import org.spongepowered.asm.mixin.injection.Inject;
12-
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
1311

1412
/**
1513
* Wraps the client and server level {@link net.minecraft.world.level.entity.LevelEntityGetterAdapter} in a {@link SubLevelInclusiveLevelEntityGetter}
1614
*/
1715
@Mixin(ClientLevel.class)
1816
public class ClientLevelMixin {
1917

20-
@Inject(method = "getEntities()Lnet/minecraft/world/level/entity/LevelEntityGetter;", at = @At("RETURN"), cancellable = true)
21-
private void sable$postGetEntities(final CallbackInfoReturnable<LevelEntityGetter<Entity>> cir) {
22-
cir.setReturnValue(new SubLevelInclusiveLevelEntityGetter<>((Level) (Object) this, cir.getReturnValue()));
18+
@ModifyReturnValue(method = "getEntities()Lnet/minecraft/world/level/entity/LevelEntityGetter;", at = @At("RETURN"))
19+
private LevelEntityGetter<Entity> sable$postGetEntities(final LevelEntityGetter<Entity> original) {
20+
return new SubLevelInclusiveLevelEntityGetter<>((Level) (Object) this, original);
2321
}
2422
}
Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
11
package dev.ryanhcode.sable.mixin.entity.entity_aabb_lookup;
22

3+
import com.llamalad7.mixinextras.injector.ModifyReturnValue;
34
import dev.ryanhcode.sable.util.SubLevelInclusiveLevelEntityGetter;
4-
import net.minecraft.client.multiplayer.ClientLevel;
55
import net.minecraft.server.level.ServerLevel;
66
import net.minecraft.world.entity.Entity;
77
import net.minecraft.world.level.Level;
88
import net.minecraft.world.level.entity.LevelEntityGetter;
99
import org.spongepowered.asm.mixin.Mixin;
10-
import org.spongepowered.asm.mixin.Pseudo;
1110
import org.spongepowered.asm.mixin.injection.At;
12-
import org.spongepowered.asm.mixin.injection.Inject;
13-
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
1411

1512
/**
1613
* Wraps the client and server level {@link net.minecraft.world.level.entity.LevelEntityGetterAdapter} in a {@link SubLevelInclusiveLevelEntityGetter}
1714
*/
1815
@Mixin(ServerLevel.class)
1916
public class ServerLevelMixin {
2017

21-
@Inject(method = "getEntities()Lnet/minecraft/world/level/entity/LevelEntityGetter;", at = @At("RETURN"), cancellable = true)
22-
private void sable$postGetEntities(final CallbackInfoReturnable<LevelEntityGetter<Entity>> cir) {
23-
cir.setReturnValue(new SubLevelInclusiveLevelEntityGetter<>((Level) (Object) this, cir.getReturnValue()));
18+
@ModifyReturnValue(method = "getEntities()Lnet/minecraft/world/level/entity/LevelEntityGetter;", at = @At("RETURN"))
19+
private LevelEntityGetter<Entity> sable$postGetEntities(final LevelEntityGetter<Entity> original) {
20+
return new SubLevelInclusiveLevelEntityGetter<>((Level) (Object) this, original);
2421
}
2522
}

common/src/main/java/dev/ryanhcode/sable/mixin/entity/entity_kicking/BlockMixin.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package dev.ryanhcode.sable.mixin.entity.entity_kicking;
22

3+
import com.llamalad7.mixinextras.sugar.Local;
34
import dev.ryanhcode.sable.Sable;
4-
import dev.ryanhcode.sable.api.SubLevelHelper;
55
import dev.ryanhcode.sable.sublevel.SubLevel;
66
import net.minecraft.core.BlockPos;
77
import net.minecraft.world.entity.item.ItemEntity;
@@ -14,7 +14,6 @@
1414
import org.spongepowered.asm.mixin.injection.At;
1515
import org.spongepowered.asm.mixin.injection.Inject;
1616
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
17-
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
1817

1918
import java.util.function.Supplier;
2019

@@ -25,8 +24,8 @@ public abstract class BlockMixin {
2524
private static void popResource(final Level arg, final Supplier<ItemEntity> supplier, final ItemStack arg2) {
2625
}
2726

28-
@Inject(method = "popResource(Lnet/minecraft/world/level/Level;Lnet/minecraft/core/BlockPos;Lnet/minecraft/world/item/ItemStack;)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/block/Block;popResource(Lnet/minecraft/world/level/Level;Ljava/util/function/Supplier;Lnet/minecraft/world/item/ItemStack;)V", shift = At.Shift.BEFORE), locals = LocalCapture.CAPTURE_FAILHARD, cancellable = true)
29-
private static void sable$popResourceFromFace(final Level level, final BlockPos blockPos, final ItemStack itemStack, final CallbackInfo ci, final double yOffset, final double x, final double y, final double z) {
27+
@Inject(method = "popResource(Lnet/minecraft/world/level/Level;Lnet/minecraft/core/BlockPos;Lnet/minecraft/world/item/ItemStack;)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/block/Block;popResource(Lnet/minecraft/world/level/Level;Ljava/util/function/Supplier;Lnet/minecraft/world/item/ItemStack;)V", shift = At.Shift.BEFORE), cancellable = true)
28+
private static void sable$popResourceFromFace(final Level level, final BlockPos blockPos, final ItemStack itemStack, final CallbackInfo ci, @Local(ordinal = 1) final double x, @Local(ordinal = 2) final double y, @Local(ordinal = 3) final double z) {
3029
final SubLevel subLevel = Sable.HELPER.getContaining(level, blockPos);
3130

3231
if (subLevel != null) {

common/src/main/java/dev/ryanhcode/sable/mixin/entity/entity_pathfinding/PathMixin.java

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package dev.ryanhcode.sable.mixin.entity.entity_pathfinding;
22

3+
import com.llamalad7.mixinextras.injector.ModifyReturnValue;
34
import dev.ryanhcode.sable.Sable;
4-
import dev.ryanhcode.sable.api.SubLevelHelper;
55
import dev.ryanhcode.sable.mixinterface.entity.pathfinding.PathExtension;
66
import dev.ryanhcode.sable.sublevel.SubLevel;
77
import net.minecraft.core.BlockPos;
@@ -12,8 +12,6 @@
1212
import org.spongepowered.asm.mixin.Mixin;
1313
import org.spongepowered.asm.mixin.Unique;
1414
import org.spongepowered.asm.mixin.injection.At;
15-
import org.spongepowered.asm.mixin.injection.Inject;
16-
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
1715

1816
@Mixin(Path.class)
1917
public class PathMixin implements PathExtension {
@@ -24,48 +22,42 @@ public class PathMixin implements PathExtension {
2422
@Unique
2523
private boolean sable$project;
2624

27-
@Inject(method = "getNextEntityPos", at = @At("RETURN"), cancellable = true)
28-
private void sable$getNextEntityPos(final Entity entity, final CallbackInfoReturnable<Vec3> cir) {
25+
@ModifyReturnValue(method = "getNextEntityPos", at = @At("RETURN"))
26+
private Vec3 sable$getNextEntityPos(final Vec3 original, final Entity entity) {
2927
if (!this.sable$project) {
30-
return;
28+
return original;
3129
}
3230

33-
cir.setReturnValue(Sable.HELPER.projectOutOfSubLevel(entity.level(), cir.getReturnValue()));
31+
return Sable.HELPER.projectOutOfSubLevel(entity.level(), original);
3432
}
3533

36-
@Inject(method = "getNextNodePos", at = @At("RETURN"), cancellable = true)
37-
private void sable$getNextNodePos(final CallbackInfoReturnable<BlockPos> cir) {
34+
@ModifyReturnValue(method = "getNextNodePos", at = @At("RETURN"))
35+
private BlockPos sable$getNextNodePos(final BlockPos original) {
3836
if (!this.sable$project) {
39-
return;
37+
return original;
4038
}
4139

42-
final BlockPos blockPos = cir.getReturnValue();
43-
44-
45-
final SubLevel subLevel = Sable.HELPER.getContaining(this.sable$level, blockPos);
40+
final SubLevel subLevel = Sable.HELPER.getContaining(this.sable$level, original);
4641
if (subLevel == null) {
47-
return;
42+
return original;
4843
}
4944

50-
final BlockPos global = BlockPos.containing(subLevel.logicalPose().transformPosition(blockPos.getCenter()));
51-
cir.setReturnValue(global);
45+
final BlockPos global = BlockPos.containing(subLevel.logicalPose().transformPosition(original.getCenter()));
46+
return global;
5247
}
5348

54-
@Inject(method = "getNodePos", at = @At("RETURN"), cancellable = true)
55-
private void sable$getNodePos(final int i, final CallbackInfoReturnable<BlockPos> cir) {
49+
@ModifyReturnValue(method = "getNodePos", at = @At("RETURN"))
50+
private BlockPos sable$getNodePos(final BlockPos original) {
5651
if (!this.sable$project) {
57-
return;
52+
return original;
5853
}
5954

60-
final BlockPos blockPos = cir.getReturnValue();
61-
62-
final SubLevel subLevel = Sable.HELPER.getContaining(this.sable$level, blockPos);
55+
final SubLevel subLevel = Sable.HELPER.getContaining(this.sable$level, original);
6356
if (subLevel == null) {
64-
return;
57+
return original;
6558
}
6659

67-
final BlockPos global = BlockPos.containing(subLevel.logicalPose().transformPosition(blockPos.getCenter()));
68-
cir.setReturnValue(global);
60+
return BlockPos.containing(subLevel.logicalPose().transformPosition(original.getCenter()));
6961
}
7062

7163
@Override

common/src/main/java/dev/ryanhcode/sable/mixin/options/OptionsScreenMixin.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dev.ryanhcode.sable.mixin.options;
22

3+
import com.llamalad7.mixinextras.injector.ModifyReturnValue;
34
import dev.ryanhcode.sable.config.SubLevelSettingsScreen;
45
import net.minecraft.client.Options;
56
import net.minecraft.client.gui.components.Button;
@@ -12,8 +13,6 @@
1213
import org.spongepowered.asm.mixin.Mixin;
1314
import org.spongepowered.asm.mixin.Shadow;
1415
import org.spongepowered.asm.mixin.injection.At;
15-
import org.spongepowered.asm.mixin.injection.Inject;
16-
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
1716

1817
/**
1918
* Adds a button to access the sable menu on integrated servers to the {@link OptionsScreen}
@@ -27,10 +26,10 @@ protected OptionsScreenMixin(final Component component) {
2726
super(component);
2827
}
2928

30-
@Inject(method = "createOnlineButton", at = @At("RETURN"), cancellable = true)
31-
public void sable$createSableButton(final CallbackInfoReturnable<LayoutElement> cir) {
29+
@ModifyReturnValue(method = "createOnlineButton", at = @At("RETURN"))
30+
public LayoutElement sable$createSableButton(LayoutElement original) {
3231
if (this.minecraft.level == null || !this.minecraft.hasSingleplayerServer()) {
33-
return;
32+
return original;
3433
}
3534

3635
final LinearLayout layout = LinearLayout.vertical();
@@ -39,10 +38,10 @@ protected OptionsScreenMixin(final Component component) {
3938
this.minecraft.setScreen(new SubLevelSettingsScreen(this, this.options, SubLevelSettingsScreen.TITLE));
4039
}).pos(0, 30).size(150, 20).build();
4140

42-
layout.addChild(cir.getReturnValue());
41+
layout.addChild(original);
4342
layout.spacing(5);
4443
layout.addChild(sableButton);
45-
cir.setReturnValue(layout);
44+
return layout;
4645
}
4746

4847

common/src/main/java/dev/ryanhcode/sable/mixin/plot/ChunkMapMixin.java

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

3+
import com.llamalad7.mixinextras.injector.ModifyReturnValue;
34
import dev.ryanhcode.sable.api.sublevel.SubLevelContainer;
45
import dev.ryanhcode.sable.sublevel.ServerSubLevel;
56
import dev.ryanhcode.sable.sublevel.plot.LevelPlot;
@@ -33,6 +34,7 @@ public class ChunkMapMixin {
3334
@Inject(method = "getPlayers", at = @At("HEAD"), cancellable = true)
3435
private void sable$getPlayers(final ChunkPos chunkPos, final boolean bl, final CallbackInfoReturnable<List<ServerPlayer>> cir) {
3536
final SubLevelContainer container = SubLevelContainer.getContainer(this.level);
37+
assert container != null;
3638

3739
if (container.inBounds(chunkPos)) {
3840
final List<ServerPlayer> players = container.getPlayersTracking(chunkPos);
@@ -56,15 +58,18 @@ public class ChunkMapMixin {
5658
return !updatingChunkMap.values().stream().anyMatch(chunkHolder -> !(chunkHolder instanceof PlotChunkHolder));
5759
}
5860

59-
@Inject(method = "isChunkTracked", at = @At(value = "HEAD"), cancellable = true)
60-
private void sable$isChunkTracked(final ServerPlayer serverPlayer, final int i, final int j, final CallbackInfoReturnable<Boolean> cir) {
61+
@ModifyReturnValue(method = "isChunkTracked", at = @At(value = "RETURN"))
62+
private boolean sable$isChunkTracked(boolean original, final ServerPlayer serverPlayer, final int x, final int z) {
6163
final SubLevelContainer container = SubLevelContainer.getContainer(this.level);
64+
assert container != null;
6265

63-
final LevelPlot plot = container.getPlot(new ChunkPos(i, j));
66+
final LevelPlot plot = container.getPlot(new ChunkPos(x, z));
6467
if (plot != null) {
6568
final ServerSubLevel subLevel = (ServerSubLevel) plot.getSubLevel();
66-
cir.setReturnValue(subLevel.getTrackingPlayers().contains(serverPlayer.getGameProfile().getId()));
69+
return subLevel.getTrackingPlayers().contains(serverPlayer.getGameProfile().getId());
6770
}
71+
72+
return original;
6873
}
6974

7075
@Inject(method = "anyPlayerCloseEnoughForSpawning", at = @At("HEAD"), cancellable = true)

common/src/main/java/dev/ryanhcode/sable/mixin/plot/ServerChunkCacheMixin.java

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

3+
import com.llamalad7.mixinextras.injector.ModifyReturnValue;
34
import com.mojang.datafixers.DataFixer;
45
import dev.ryanhcode.sable.api.sublevel.SubLevelContainer;
56
import dev.ryanhcode.sable.sublevel.plot.PlotChunkHolder;
@@ -84,14 +85,14 @@ private void getChunkFutureMainThread(final int x, final int z, final ChunkStatu
8485
}
8586
}
8687

87-
@Inject(method = "hasChunk", at = @At("HEAD"), cancellable = true)
88-
private void hasChunk(final int x, final int z, final CallbackInfoReturnable<Boolean> cir) {
88+
@ModifyReturnValue(method = "hasChunk", at = @At("RETURN"))
89+
private boolean hasChunk(final boolean original, final int x, final int z) {
8990
final SubLevelContainer container = this.sable$getPlotContainer();
9091
if (container.inBounds(x, z)) {
91-
final ChunkAccess chunk = container.getChunk(new ChunkPos(x, z));
92-
93-
cir.setReturnValue(chunk != null);
92+
return container.getChunk(new ChunkPos(x, z)) != null;
9493
}
94+
95+
return original;
9596
}
9697

9798

@@ -105,15 +106,15 @@ private void getChunkForLighting(final int x, final int z, final CallbackInfoRet
105106
}
106107
}
107108

108-
@Inject(method = "isPositionTicking", at = @At("HEAD"), cancellable = true)
109-
private void isPositionTicking(final long pos, final CallbackInfoReturnable<Boolean> cir) {
109+
@ModifyReturnValue(method = "isPositionTicking", at = @At("RETURN"))
110+
private boolean isPositionTicking(final boolean original, final long chunkPos) {
110111
final SubLevelContainer container = this.sable$getPlotContainer();
111-
if (container.inBounds(ChunkPos.getX(pos), ChunkPos.getZ(pos))) {
112-
final ChunkPos chunkPos = new ChunkPos(pos);
113-
final LevelChunk chunk = container.getChunk(chunkPos);
114-
115-
cir.setReturnValue(chunk != null);
112+
if (container.inBounds(ChunkPos.getX(chunkPos), ChunkPos.getZ(chunkPos))) {
113+
final LevelChunk chunk = container.getChunk(new ChunkPos(chunkPos));
114+
return chunk != null;
116115
}
116+
117+
return original;
117118
}
118119

119120
@Inject(method = "getFullChunk", at = @At("HEAD"), cancellable = true)

common/src/main/java/dev/ryanhcode/sable/mixin/plot/ServerLevelMixin.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dev.ryanhcode.sable.mixin.plot;
22

3+
import com.llamalad7.mixinextras.injector.ModifyReturnValue;
34
import dev.ryanhcode.sable.api.sublevel.ServerSubLevelContainer;
45
import dev.ryanhcode.sable.api.sublevel.SubLevelContainer;
56
import dev.ryanhcode.sable.mixinterface.plot.SubLevelContainerHolder;
@@ -25,7 +26,6 @@
2526
import org.spongepowered.asm.mixin.injection.At;
2627
import org.spongepowered.asm.mixin.injection.Inject;
2728
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
28-
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
2929

3030
import java.util.function.BooleanSupplier;
3131
import java.util.function.Supplier;
@@ -90,24 +90,28 @@ protected ServerLevelMixin(final WritableLevelData writableLevelData, final Reso
9090
}
9191
}
9292

93-
@Inject(method = "shouldTickBlocksAt", at = @At("HEAD"), cancellable = true)
94-
private void sable$shouldTickBlocksAt(final long l, final CallbackInfoReturnable<Boolean> cir) {
93+
@ModifyReturnValue(method = "shouldTickBlocksAt", at = @At("RETURN"))
94+
private boolean sable$shouldTickBlocksAt(final boolean original, final long chunkPos) {
9595
final SubLevelContainer plotContainer = SubLevelContainer.getContainer((ServerLevel) (Object) this);
9696
assert plotContainer != null;
9797

98-
if (plotContainer.getPlot(new ChunkPos(l)) != null) {
99-
cir.setReturnValue(true);
98+
if (plotContainer.getPlot(new ChunkPos(chunkPos)) != null) {
99+
return true;
100100
}
101+
102+
return original;
101103
}
102104

103-
@Inject(method = "isNaturalSpawningAllowed(Lnet/minecraft/world/level/ChunkPos;)Z", at = @At("HEAD"), cancellable = true)
104-
private void sable$isNaturalSpawningAllowed(final ChunkPos chunkPos, final CallbackInfoReturnable<Boolean> cir) {
105+
@ModifyReturnValue(method = "isNaturalSpawningAllowed(Lnet/minecraft/world/level/ChunkPos;)Z", at = @At("RETURN"))
106+
private boolean sable$isNaturalSpawningAllowed(boolean original, final ChunkPos chunkPos) {
105107
final SubLevelContainer plotContainer = SubLevelContainer.getContainer((ServerLevel) (Object) this);
106108
assert plotContainer != null;
107109

108110
if (plotContainer.getPlot(chunkPos) != null) {
109-
cir.setReturnValue(true);
111+
return true;
110112
}
113+
114+
return original;
111115
}
112116

113117
@Inject(method = "close", at = @At("TAIL"))

0 commit comments

Comments
 (0)