Switch to vanilla's stillValid implementation
This commit is contained in:
parent
7c1f40031b
commit
00e2e2bd2d
|
@ -5,9 +5,9 @@
|
|||
package dan200.computercraft.shared.common;
|
||||
|
||||
import dan200.computercraft.shared.container.BasicContainer;
|
||||
import dan200.computercraft.shared.util.BlockEntityHelpers;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.world.Container;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.level.block.entity.BaseContainerBlockEntity;
|
||||
import net.minecraft.world.level.block.entity.BlockEntity;
|
||||
|
@ -29,6 +29,6 @@ protected final Component getDefaultName() {
|
|||
|
||||
@Override
|
||||
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.nbt.CompoundTag;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
|
||||
import net.minecraft.world.Container;
|
||||
import net.minecraft.world.LockCode;
|
||||
import net.minecraft.world.MenuProvider;
|
||||
import net.minecraft.world.Nameable;
|
||||
|
@ -75,13 +76,13 @@ public void setRemoved() {
|
|||
unload();
|
||||
}
|
||||
|
||||
protected double getInteractRange() {
|
||||
return BlockEntityHelpers.DEFAULT_INTERACT_RANGE;
|
||||
protected int getInteractRange() {
|
||||
return Container.DEFAULT_DISTANCE_LIMIT;
|
||||
}
|
||||
|
||||
public boolean isUsable(Player player) {
|
||||
return BaseContainerBlockEntity.canUnlock(player, lockCode, getDisplayName())
|
||||
&& BlockEntityHelpers.isUsable(this, player, getInteractRange());
|
||||
&& Container.stillValidBlockEntity(this, player, getInteractRange());
|
||||
}
|
||||
|
||||
protected void serverTick() {
|
||||
|
|
|
@ -26,6 +26,7 @@
|
|||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.world.Container;
|
||||
import net.minecraft.world.ContainerHelper;
|
||||
import net.minecraft.world.entity.player.Inventory;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
|
@ -88,8 +89,8 @@ protected void unload() {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected double getInteractRange() {
|
||||
return 12.0;
|
||||
protected int getInteractRange() {
|
||||
return Container.DEFAULT_DISTANCE_LIMIT + 4;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -4,24 +4,14 @@
|
|||
|
||||
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.entity.BlockEntity;
|
||||
import net.minecraft.world.level.block.entity.BlockEntityTicker;
|
||||
import net.minecraft.world.level.block.entity.BlockEntityType;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
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() {
|
||||
}
|
||||
|
||||
|
@ -33,27 +23,6 @@ public static <E extends BlockEntity, A extends BlockEntity> BlockEntityTicker<A
|
|||
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.
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue