mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-07-07 12:32:54 +00:00
Switch to vanilla's stillValid implementation
This commit is contained in:
parent
7c1f40031b
commit
00e2e2bd2d
@ -5,9 +5,9 @@
|
|||||||
package dan200.computercraft.shared.common;
|
package dan200.computercraft.shared.common;
|
||||||
|
|
||||||
import dan200.computercraft.shared.container.BasicContainer;
|
import dan200.computercraft.shared.container.BasicContainer;
|
||||||
import dan200.computercraft.shared.util.BlockEntityHelpers;
|
|
||||||
import net.minecraft.core.BlockPos;
|
import net.minecraft.core.BlockPos;
|
||||||
import net.minecraft.network.chat.Component;
|
import net.minecraft.network.chat.Component;
|
||||||
|
import net.minecraft.world.Container;
|
||||||
import net.minecraft.world.entity.player.Player;
|
import net.minecraft.world.entity.player.Player;
|
||||||
import net.minecraft.world.level.block.entity.BaseContainerBlockEntity;
|
import net.minecraft.world.level.block.entity.BaseContainerBlockEntity;
|
||||||
import net.minecraft.world.level.block.entity.BlockEntity;
|
import net.minecraft.world.level.block.entity.BlockEntity;
|
||||||
@ -29,6 +29,6 @@ public abstract class AbstractContainerBlockEntity extends BaseContainerBlockEnt
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean stillValid(Player player) {
|
public boolean stillValid(Player player) {
|
||||||
return BlockEntityHelpers.isUsable(this, player, BlockEntityHelpers.DEFAULT_INTERACT_RANGE);
|
return Container.stillValidBlockEntity(this, player);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,6 +24,7 @@ import net.minecraft.core.Direction;
|
|||||||
import net.minecraft.nbt.CompoundTag;
|
import net.minecraft.nbt.CompoundTag;
|
||||||
import net.minecraft.network.chat.Component;
|
import net.minecraft.network.chat.Component;
|
||||||
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
|
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
|
||||||
|
import net.minecraft.world.Container;
|
||||||
import net.minecraft.world.LockCode;
|
import net.minecraft.world.LockCode;
|
||||||
import net.minecraft.world.MenuProvider;
|
import net.minecraft.world.MenuProvider;
|
||||||
import net.minecraft.world.Nameable;
|
import net.minecraft.world.Nameable;
|
||||||
@ -75,13 +76,13 @@ public abstract class AbstractComputerBlockEntity extends BlockEntity implements
|
|||||||
unload();
|
unload();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected double getInteractRange() {
|
protected int getInteractRange() {
|
||||||
return BlockEntityHelpers.DEFAULT_INTERACT_RANGE;
|
return Container.DEFAULT_DISTANCE_LIMIT;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isUsable(Player player) {
|
public boolean isUsable(Player player) {
|
||||||
return BaseContainerBlockEntity.canUnlock(player, lockCode, getDisplayName())
|
return BaseContainerBlockEntity.canUnlock(player, lockCode, getDisplayName())
|
||||||
&& BlockEntityHelpers.isUsable(this, player, getInteractRange());
|
&& Container.stillValidBlockEntity(this, player, getInteractRange());
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void serverTick() {
|
protected void serverTick() {
|
||||||
|
@ -26,6 +26,7 @@ import net.minecraft.core.NonNullList;
|
|||||||
import net.minecraft.nbt.CompoundTag;
|
import net.minecraft.nbt.CompoundTag;
|
||||||
import net.minecraft.resources.ResourceLocation;
|
import net.minecraft.resources.ResourceLocation;
|
||||||
import net.minecraft.server.level.ServerLevel;
|
import net.minecraft.server.level.ServerLevel;
|
||||||
|
import net.minecraft.world.Container;
|
||||||
import net.minecraft.world.ContainerHelper;
|
import net.minecraft.world.ContainerHelper;
|
||||||
import net.minecraft.world.entity.player.Inventory;
|
import net.minecraft.world.entity.player.Inventory;
|
||||||
import net.minecraft.world.entity.player.Player;
|
import net.minecraft.world.entity.player.Player;
|
||||||
@ -88,8 +89,8 @@ public class TurtleBlockEntity extends AbstractComputerBlockEntity implements Ba
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected double getInteractRange() {
|
protected int getInteractRange() {
|
||||||
return 12.0;
|
return Container.DEFAULT_DISTANCE_LIMIT + 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -4,24 +4,14 @@
|
|||||||
|
|
||||||
package dan200.computercraft.shared.util;
|
package dan200.computercraft.shared.util;
|
||||||
|
|
||||||
import dan200.computercraft.shared.platform.PlatformHelper;
|
|
||||||
import net.minecraft.world.entity.player.Player;
|
|
||||||
import net.minecraft.world.level.block.Block;
|
import net.minecraft.world.level.block.Block;
|
||||||
import net.minecraft.world.level.block.entity.BlockEntity;
|
import net.minecraft.world.level.block.entity.BlockEntity;
|
||||||
import net.minecraft.world.level.block.entity.BlockEntityTicker;
|
import net.minecraft.world.level.block.entity.BlockEntityTicker;
|
||||||
import net.minecraft.world.level.block.entity.BlockEntityType;
|
import net.minecraft.world.level.block.entity.BlockEntityType;
|
||||||
import net.minecraft.world.phys.Vec3;
|
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
public final class BlockEntityHelpers {
|
public final class BlockEntityHelpers {
|
||||||
/**
|
|
||||||
* The maximum limit a player can be away from a block to still have its UI open.
|
|
||||||
*
|
|
||||||
* @see #isUsable(BlockEntity, Player, double)
|
|
||||||
*/
|
|
||||||
public static final double DEFAULT_INTERACT_RANGE = 8.0;
|
|
||||||
|
|
||||||
private BlockEntityHelpers() {
|
private BlockEntityHelpers() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -33,27 +23,6 @@ public final class BlockEntityHelpers {
|
|||||||
return actualType == expectedType ? (BlockEntityTicker<A>) ticker : null;
|
return actualType == expectedType ? (BlockEntityTicker<A>) ticker : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Determine if a block entity is "usable" by a player.
|
|
||||||
*
|
|
||||||
* @param blockEntity The current block entity.
|
|
||||||
* @param player The player who is trying to interact with the block.
|
|
||||||
* @param range The default distance the player can be away. This typically defaults to {@link #DEFAULT_INTERACT_RANGE},
|
|
||||||
* but a custom value may be used. If {@link PlatformHelper#getReachDistance(Player)} is larger,
|
|
||||||
* that will be used instead.
|
|
||||||
* @return Whether this block entity is usable.
|
|
||||||
*/
|
|
||||||
public static boolean isUsable(BlockEntity blockEntity, Player player, double range) {
|
|
||||||
var level = blockEntity.getLevel();
|
|
||||||
var pos = blockEntity.getBlockPos();
|
|
||||||
|
|
||||||
range = Math.max(range, PlatformHelper.get().getReachDistance(player));
|
|
||||||
|
|
||||||
return player.isAlive() && player.getCommandSenderWorld() == level &&
|
|
||||||
!blockEntity.isRemoved() && level.getBlockEntity(pos) == blockEntity &&
|
|
||||||
player.distanceToSqr(Vec3.atCenterOf(pos)) <= range * range;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update a block entity, marking it as changed and propagating changes to the client.
|
* Update a block entity, marking it as changed and propagating changes to the client.
|
||||||
*
|
*
|
||||||
|
Loading…
x
Reference in New Issue
Block a user