|
| 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 | +} |
0 commit comments