Update turtle reach limits to 1.20.5

We can replace our mixins with vanilla's built-in attributes.
This commit is contained in:
Jonathan Coates 2024-04-26 21:15:06 +01:00
parent a3b07909b0
commit b9a002586c
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
5 changed files with 17 additions and 85 deletions

View File

@ -1,31 +0,0 @@
// SPDX-FileCopyrightText: 2023 The CC: Tweaked Developers
//
// SPDX-License-Identifier: MPL-2.0
package dan200.computercraft.shared.platform;
import com.mojang.authlib.GameProfile;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.item.ItemStack;
/**
* Shared constants for {@linkplain PlatformHelper#createFakePlayer(ServerLevel, GameProfile) fake player}
* implementations.
*
* @see net.minecraft.server.level.ServerPlayer
* @see net.minecraft.world.entity.player.Player
*/
final class FakePlayerConstants {
private FakePlayerConstants() {
}
/**
* The maximum distance this player can reach.
* <p>
* This is used in the override of {@link net.minecraft.world.entity.player.Player#mayUseItemAt(BlockPos, Direction, ItemStack)},
* to prevent the fake player reaching more than 2 blocks away.
*/
static final double MAX_REACH = 2;
}

View File

@ -11,8 +11,11 @@
import dan200.computercraft.shared.util.DirectionUtil;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.Holder;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.ai.attributes.Attribute;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.item.ItemStack;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -28,6 +31,11 @@ public final class TurtlePlayer {
"[ComputerCraft]"
);
/**
* The maximum distance this player can reach.
*/
private static final double MAX_REACH = 2;
private final ServerPlayer player;
private TurtlePlayer(ServerPlayer player) {
@ -87,6 +95,15 @@ private void setState(ITurtleAccess turtle) {
setRotation(turtle.getDirection().toYRot(), 0);
player.getInventory().clearContent();
// Prevent the turtle reaching too far.
trySetAttribute(Attributes.BLOCK_INTERACTION_RANGE, MAX_REACH);
trySetAttribute(Attributes.ENTITY_INTERACTION_RANGE, MAX_REACH);
}
private void trySetAttribute(Holder<Attribute> attribute, double value) {
var instance = player.getAttribute(attribute);
if (instance != null) instance.setBaseValue(value);
}
public void setPosition(ITurtleAccess turtle, BlockPos position, Direction direction) {

View File

@ -1,46 +0,0 @@
// 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 net.minecraft.world.phys.BlockHitResult;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
@Mixin(Item.class)
class ItemMixin {
/**
* Replace the reach distance in {@link Item#getPlayerPOVHitResult(Level, Player, ClipContext.Fluid)}.
*
* @param level The current level.
* @param player The current player.
* @param fluidMode The current clip-context fluid mode.
* @param cir Callback info to store the new reach distance.
* @see FakePlayer#getBlockReach()
*/
@Inject(method = "getPlayerPOVHitResult", at = @At("HEAD"), cancellable = true)
@SuppressWarnings("UnusedMethod")
private static void getReachDistance(Level level, Player player, ClipContext.Fluid fluidMode, CallbackInfoReturnable<BlockHitResult> cir) {
// It would theoretically be cleaner to use @ModifyConstant here, but as it's treated as a @Redirect, it doesn't
// compose with other mods. Instead, we replace the method when working with our fake player.
if (player instanceof FakePlayer fp) cir.setReturnValue(getHitResult(level, fp, fluidMode));
}
@Unique
private static BlockHitResult getHitResult(Level level, FakePlayer player, ClipContext.Fluid fluidMode) {
var start = player.getEyePosition();
var reach = player.getBlockReach();
var direction = player.getViewVector(1.0f);
var end = start.add(direction.x() * reach, direction.y() * reach, direction.z() * reach);
return level.clip(new ClipContext(start, end, ClipContext.Block.OUTLINE, fluidMode, player));
}
}

View File

@ -12,8 +12,6 @@
import net.minecraft.world.entity.Pose;
import net.minecraft.world.entity.player.Player;
import static dan200.computercraft.shared.platform.FakePlayerConstants.MAX_REACH;
public final class FakePlayer extends net.fabricmc.fabric.api.entity.FakePlayer {
private static final EntityDimensions DIMENSIONS = EntityDimensions.fixed(0, 0);
@ -40,11 +38,6 @@ public EntityDimensions getDefaultDimensions(Pose pose) {
return DIMENSIONS;
}
public double getBlockReach() {
// TODO: Replace with normal reach attribute
return MAX_REACH;
}
@Override
public boolean broadcastToPlayer(ServerPlayer player) {
return false;

View File

@ -11,7 +11,6 @@
"EntityMixin",
"ExplosionDamageCalculatorMixin",
"ItemEntityMixin",
"ItemMixin",
"PlayerChunkSenderMixin",
"ServerLevelMixin",
"TagEntryAccessor",