1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-10-24 18:37:38 +00:00

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
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.turtle.TurtleUtil;
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 @@ public final class TurtlePlayer {
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) {