Skip to content

Commit 838111e

Browse files
clienthaxdualspiral
authored andcommitted
Add raytracing chunkload optimization
1 parent 28443ae commit 838111e

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed

src/main/java/org/spongepowered/common/config/category/OptimizationCategory.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,13 @@ public class OptimizationCategory extends ConfigCategory {
129129
)
130130
private boolean disablePathFindingChunkLoads = false;
131131

132+
@Setting(value = "disable-raytracing-chunk-loads", comment = "In vanilla, ray tracing may result in loading chunks.\n" +
133+
"You can disable that here, which may result in a\n" +
134+
"performance improvement. This may not work well\n" +
135+
"with mods."
136+
)
137+
private boolean disableRayTracingChunkLoads = false;
138+
132139
public OptimizationCategory() {
133140
try {
134141
// Enabled by default on SpongeVanilla, disabled by default on SpongeForge.
@@ -219,4 +226,8 @@ public boolean disablePathFindingChunkLoads() {
219226
return this.disablePathFindingChunkLoads;
220227
}
221228

229+
public boolean isDisableRayTracingChunkLoads() {
230+
return this.disableRayTracingChunkLoads;
231+
}
232+
222233
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* This file is part of Sponge, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) SpongePowered <https://www.spongepowered.org>
5+
* Copyright (c) contributors
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package org.spongepowered.common.mixin.optimization.world;
26+
27+
import net.minecraft.block.Block;
28+
import net.minecraft.block.state.IBlockState;
29+
import net.minecraft.util.EnumFacing;
30+
import net.minecraft.util.math.BlockPos;
31+
import net.minecraft.util.math.RayTraceResult;
32+
import net.minecraft.util.math.Vec3d;
33+
import net.minecraft.world.World;
34+
import org.spongepowered.asm.mixin.Mixin;
35+
import org.spongepowered.asm.mixin.Shadow;
36+
import org.spongepowered.asm.mixin.injection.At;
37+
import org.spongepowered.asm.mixin.injection.Inject;
38+
import org.spongepowered.asm.mixin.injection.Surrogate;
39+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
40+
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
41+
42+
@Mixin(value = World.class, priority = 1500)
43+
public abstract class WorldMixin_RayTraceChunkLoadOptimizations {
44+
45+
@Shadow public abstract boolean isBlockLoaded(BlockPos pos);
46+
47+
/**
48+
* Check if a chunk is loaded before attempting to check the state of the block
49+
* return null if the chunk is not loaded
50+
* Based on https://github.com/PaperMC/Paper/blob/ver/1.12.2/Spigot-Server-Patches/0369-Prevent-rayTrace-from-loading-chunks.patch
51+
*/
52+
@Inject(method = "rayTraceBlocks(Lnet/minecraft/util/math/Vec3d;Lnet/minecraft/util/math/Vec3d;ZZZ)Lnet/minecraft/util/math/RayTraceResult;",
53+
at = @At(
54+
value = "INVOKE",
55+
target = "Lnet/minecraft/world/World;getBlockState(Lnet/minecraft/util/math/BlockPos;)Lnet/minecraft/block/state/IBlockState;"
56+
), locals = LocalCapture.CAPTURE_FAILHARD, cancellable = true
57+
)
58+
private void checkChunkLoaded(Vec3d vec31, Vec3d vec32, boolean arg2, boolean arg3, boolean arg4,
59+
CallbackInfoReturnable<RayTraceResult> cir, int i, int j, int k, int l, int i1, int j1,
60+
BlockPos blockpos) {
61+
if (!this.isBlockLoaded(blockpos)) {
62+
cir.setReturnValue(null);
63+
}
64+
}
65+
66+
@Surrogate
67+
private void checkChunkLoaded(Vec3d vec31, Vec3d vec32, boolean stopOnLiquid, boolean ignoreBlockWithoutBoundingBox,
68+
boolean returnLastUncollidableBlock, CallbackInfoReturnable<RayTraceResult> cir, int i,
69+
int j, int k, int l, int i1, int j1, BlockPos blockpos, IBlockState iblockstate, Block block,
70+
RayTraceResult raytraceresult2, int k1, boolean flag2, boolean flag, boolean flag1, double d0,
71+
double d1, double d2, double d3, double d4, double d5, double d6, double d7, double d8, EnumFacing enumfacing) {
72+
if (!this.isBlockLoaded(blockpos)) {
73+
cir.setReturnValue(null);
74+
}
75+
}
76+
77+
}

src/main/java/org/spongepowered/common/mixin/plugin/OptimizationPlugin.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ public void postApply(String targetClassName, ClassNode targetClass, String mixi
135135
OptimizationCategory::isOptimizeHoppers)
136136
.put("org.spongepowered.common.mixin.optimization.entity.EntityMixin_UseActiveChunkForCollisions",
137137
OptimizationCategory::isUseActiveChunkForCollisions)
138+
.put("org.spongepowered.common.mixin.optimization.world.WorldMixin_RayTraceChunkLoadOptimizations",
139+
OptimizationCategory::isDisableRayTracingChunkLoads)
138140
.put("org.spongepowered.common.mixin.optimization.world.WorldMixin_UseActiveChunkForCollisions",
139141
OptimizationCategory::isUseActiveChunkForCollisions)
140142
.put("org.spongepowered.common.mixin.optimization.world.WorldServerMixin_UseActiveChunkForCollisions",

src/main/resources/mixins.common.optimization.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"server.MinecraftServerMixin_MapOptimization",
2222
"tileentity.TileEntityHopperMixin_HopperOptimization",
2323
"tileentity.TileEntityMixin_HopperOptimization",
24+
"world.WorldMixin_RayTraceChunkLoadOptimizations",
2425
"world.WorldMixin_UseActiveChunkForCollisions",
2526
"world.WorldServerMixin_Async_Lighting",
2627
"world.WorldServerMixin_UseActiveChunkForCollisions",

0 commit comments

Comments
 (0)