Skip to content

Commit f11b52a

Browse files
clienthaxdualspiral
authored andcommitted
Add pathfinding chunkload optimization
1 parent 85728f2 commit f11b52a

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-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
@@ -122,6 +122,13 @@ public class OptimizationCategory extends ConfigCategory {
122122
+ "is not able to fix them. This provides an option to suppress the exceptions printing out in the log.")
123123
private boolean disableFailingAdvancementDeserialization = true;
124124

125+
@Setting(value = "disable-pathfinding-chunk-loads", comment = "In vanilla, pathfinding may result in loading chunks.\n" +
126+
"You can disable that here, which may result in a\n" +
127+
"performance improvement. This may not work well\n" +
128+
"with mods."
129+
)
130+
private boolean disablePathFindingChunkLoads = false;
131+
125132
public OptimizationCategory() {
126133
try {
127134
// Enabled by default on SpongeVanilla, disabled by default on SpongeForge.
@@ -208,4 +215,8 @@ public boolean disableFailingAdvancementDeserialization() {
208215
return this.disableFailingAdvancementDeserialization;
209216
}
210217

218+
public boolean disablePathFindingChunkLoads() {
219+
return this.disablePathFindingChunkLoads;
220+
}
221+
211222
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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.pathfinding;
26+
27+
import net.minecraft.pathfinding.PathNavigate;
28+
import net.minecraft.util.math.BlockPos;
29+
import net.minecraft.world.World;
30+
import org.spongepowered.asm.mixin.Mixin;
31+
import org.spongepowered.asm.mixin.Shadow;
32+
import org.spongepowered.asm.mixin.injection.At;
33+
import org.spongepowered.asm.mixin.injection.Inject;
34+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
35+
36+
@Mixin(value = PathNavigate.class, priority = 1500)
37+
public class PathNavigateMixin_ChunkLoadOptimizations {
38+
39+
@Shadow protected World world;
40+
41+
/**
42+
* Check if a chunk is loaded before attempting to check the state of the block
43+
* return false if the chunk is not loaded
44+
*/
45+
@Inject(method = "canEntityStandOnPos", at = @At(value = "HEAD"), cancellable = true)
46+
private void canEntityStandOnPos(BlockPos pos, CallbackInfoReturnable<Boolean> cir) {
47+
if (!this.world.isBlockLoaded(pos)) {
48+
cir.setReturnValue(false);
49+
}
50+
}
51+
52+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ public void postApply(String targetClassName, ClassNode targetClass, String mixi
139139
OptimizationCategory::isUseActiveChunkForCollisions)
140140
.put("org.spongepowered.common.mixin.optimization.world.WorldServerMixin_UseActiveChunkForCollisions",
141141
OptimizationCategory::isUseActiveChunkForCollisions)
142+
.put("org.spongepowered.common.mixin.optimization.pathfinding.PathNavigateMixin_ChunkLoadOptimizations",
143+
OptimizationCategory::disablePathFindingChunkLoads)
142144
.build();
143145

144146
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"entity.item.EntityItemFrameMixin_MapOptimization",
1818
"item.ItemMapMixin_MapOptimization",
1919
"network.play.server.SPacketChunkDataMixin_Async_Lighting",
20+
"pathfinding.PathNavigateMixin_ChunkLoadOptimizations",
2021
"server.MinecraftServerMixin_MapOptimization",
2122
"tileentity.TileEntityHopperMixin_HopperOptimization",
2223
"tileentity.TileEntityMixin_HopperOptimization",

0 commit comments

Comments
 (0)