1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-11-09 18:03:08 +00:00

Limit turtle's reach distance in Item.use

When a turtle attempts to place a block, it does so by searching for
nearby blocks and attempting to place the item against that block.

This has slightly strange behaviour when working with "placable"
non-block items though (such as buckets or boats). In this case, we call
Item.use, which doesn't take in the position of the block we're placing
against. Instead these items do their own ray trace, using the default
reach distance.

If the block we're trying to place against is non-solid, the ray trace
will go straight through it and continue (up to the maximum of 5
blocks), allowing placing the item much further away.

Our fix here is to override the default reach distance of our fake
players, limiting it to 2. This is easy on Forge (it has built-in
support), and requires a mixin on Fabric.

Closes #1497.
This commit is contained in:
Jonathan Coates
2023-06-24 17:09:34 +01:00
parent c3bdb0440e
commit 672c2cf029
7 changed files with 245 additions and 2 deletions

View File

@@ -0,0 +1,32 @@
// SPDX-FileCopyrightText: 2023 The CC: Tweaked Developers
//
// SPDX-License-Identifier: MPL-2.0
package dan200.computercraft.mixin;
import dan200.computercraft.shared.platform.FakePlayer;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.ClipContext;
import net.minecraft.world.level.Level;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.Constant;
import org.spongepowered.asm.mixin.injection.ModifyConstant;
@Mixin(Item.class)
class ItemMixin {
/**
* Replace the reach distance in {@link Item#getPlayerPOVHitResult(Level, Player, ClipContext.Fluid)}.
*
* @param reach The original reach distance.
* @param level The current level.
* @param player The current player.
* @return The new reach distance.
* @see FakePlayer#getBlockReach()
*/
@ModifyConstant(method = "getPlayerPOVHitResult", constant = @Constant(doubleValue = 5))
@SuppressWarnings("UnusedMethod")
private static double getReachDistance(double reach, Level level, Player player) {
return player instanceof FakePlayer fp ? fp.getBlockReach() : reach;
}
}

View File

@@ -11,7 +11,9 @@ import net.minecraft.world.entity.EntityDimensions;
import net.minecraft.world.entity.Pose;
import net.minecraft.world.entity.player.Player;
final class FakePlayer extends net.fabricmc.fabric.api.entity.FakePlayer {
import static dan200.computercraft.shared.platform.FakePlayerConstants.MAX_REACH;
public final class FakePlayer extends net.fabricmc.fabric.api.entity.FakePlayer {
private FakePlayer(ServerLevel serverLevel, GameProfile gameProfile) {
super(serverLevel, gameProfile);
}
@@ -33,4 +35,8 @@ final class FakePlayer extends net.fabricmc.fabric.api.entity.FakePlayer {
public float getStandingEyeHeight(Pose pose, EntityDimensions dimensions) {
return 0;
}
public double getBlockReach() {
return MAX_REACH;
}
}

View File

@@ -12,6 +12,7 @@
"EntityMixin",
"ExplosionDamageCalculatorMixin",
"ItemEntityMixin",
"ItemMixin",
"ServerLevelMixin",
"ShapedRecipeMixin",
"TagEntryAccessor",