Move computer right click code to the block

Rather than handling right clicks within the block entity code, we now
handle it within the block. Turtles now handle the nametagging
behaviour themselves, rather than overriding canNameWithTag.
This commit is contained in:
Jonathan Coates 2024-04-17 15:00:43 +01:00
parent fabd77132d
commit 03bb279206
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
6 changed files with 41 additions and 46 deletions

View File

@ -59,7 +59,7 @@ checkstyle = "10.14.1"
curseForgeGradle = "1.0.14"
errorProne-core = "2.23.0"
errorProne-plugin = "3.1.0"
fabric-loom = "1.5.7"
fabric-loom = "1.6.7"
forgeGradle = "6.0.20"
githubRelease = "2.5.2"
gradleVersions = "0.50.0"

View File

@ -8,6 +8,7 @@
import dan200.computercraft.api.ComputerCraftAPI;
import dan200.computercraft.shared.common.IBundledRedstoneBlock;
import dan200.computercraft.shared.computer.items.IComputerItem;
import dan200.computercraft.shared.network.container.ComputerContainerData;
import dan200.computercraft.shared.platform.RegistryEntry;
import dan200.computercraft.shared.util.BlockEntityHelpers;
import net.minecraft.core.BlockPos;
@ -158,8 +159,19 @@ public void setPlacedBy(Level world, BlockPos pos, BlockState state, @Nullable L
@Override
@Deprecated
public final InteractionResult use(BlockState state, Level world, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
return world.getBlockEntity(pos) instanceof AbstractComputerBlockEntity computer ? computer.use(player, hand) : InteractionResult.PASS;
public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
if (!player.isCrouching() && level.getBlockEntity(pos) instanceof AbstractComputerBlockEntity computer) {
// Regular right click to activate computer
if (!level.isClientSide && computer.isUsable(player)) {
var serverComputer = computer.createServerComputer();
serverComputer.turnOn();
new ComputerContainerData(serverComputer, getItem(computer)).open(player, computer);
}
return InteractionResult.sidedSuccess(level.isClientSide);
}
return super.use(state, level, pos, player, hand, hit);
}
@Override

View File

@ -13,7 +13,6 @@
import dan200.computercraft.shared.computer.core.ComputerState;
import dan200.computercraft.shared.computer.core.ServerComputer;
import dan200.computercraft.shared.computer.core.ServerContext;
import dan200.computercraft.shared.network.container.ComputerContainerData;
import dan200.computercraft.shared.platform.ComponentAccess;
import dan200.computercraft.shared.platform.PlatformHelper;
import dan200.computercraft.shared.util.BlockEntityHelpers;
@ -25,10 +24,10 @@
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.world.*;
import net.minecraft.world.LockCode;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.Nameable;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.block.entity.BaseContainerBlockEntity;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
@ -76,10 +75,6 @@ public void setRemoved() {
unload();
}
protected boolean canNameWithTag(Player player) {
return false;
}
protected double getInteractRange() {
return BlockEntityHelpers.DEFAULT_INTERACT_RANGE;
}
@ -89,31 +84,6 @@ public boolean isUsable(Player player) {
&& BlockEntityHelpers.isUsable(this, player, getInteractRange());
}
public InteractionResult use(Player player, InteractionHand hand) {
var currentItem = player.getItemInHand(hand);
if (!currentItem.isEmpty() && currentItem.getItem() == Items.NAME_TAG && canNameWithTag(player) && currentItem.hasCustomHoverName()) {
// Label to rename computer
if (!getLevel().isClientSide) {
setLabel(currentItem.getHoverName().getString());
currentItem.shrink(1);
}
return InteractionResult.sidedSuccess(getLevel().isClientSide);
} else if (!player.isCrouching()) {
// Regular right click to activate computer
if (!getLevel().isClientSide && isUsable(player)) {
var computer = createServerComputer();
computer.turnOn();
var stack = getBlockState().getBlock() instanceof AbstractComputerBlock<?>
? ((AbstractComputerBlock<?>) getBlockState().getBlock()).getItem(this)
: ItemStack.EMPTY;
new ComputerContainerData(computer, stack).open(player, this);
}
return InteractionResult.sidedSuccess(getLevel().isClientSide);
}
return InteractionResult.PASS;
}
protected void serverTick() {
if (getLevel().isClientSide) return;
if (computerID < 0 && !startOn) return; // Don't tick if we don't need a computer!
@ -343,7 +313,7 @@ public final void setComputerID(int id) {
if (getLevel().isClientSide || computerID == id) return;
computerID = id;
setChanged();
BlockEntityHelpers.updateBlock(this);
}
@Override
@ -353,7 +323,7 @@ public final void setLabel(@Nullable String label) {
this.label = label;
var computer = getServerComputer();
if (computer != null) computer.setLabel(label);
setChanged();
BlockEntityHelpers.updateBlock(this);
}
@Override

View File

@ -18,10 +18,13 @@
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.Containers;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.projectile.AbstractHurtingProjectile;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Explosion;
@ -38,6 +41,7 @@
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.DirectionProperty;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.Shapes;
@ -155,6 +159,22 @@ public void setPlacedBy(Level world, BlockPos pos, BlockState state, @Nullable L
}
}
@Override
@Deprecated
public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
var currentItem = player.getItemInHand(hand);
if (currentItem.getItem() == Items.NAME_TAG && currentItem.hasCustomHoverName() && level.getBlockEntity(pos) instanceof AbstractComputerBlockEntity computer) {
// Label to rename computer
if (!level.isClientSide) {
computer.setLabel(currentItem.getHoverName().getString());
currentItem.shrink(1);
}
return InteractionResult.sidedSuccess(level.isClientSide);
}
return super.use(state, level, pos, player, hand, hit);
}
@ForgeOverride
public float getExplosionResistance(BlockState state, BlockGetter world, BlockPos pos, Explosion explosion) {
var exploder = explosion.getDirectSourceEntity();

View File

@ -87,11 +87,6 @@ protected void unload() {
if (!hasMoved()) super.unload();
}
@Override
protected boolean canNameWithTag(Player player) {
return true;
}
@Override
protected double getInteractRange() {
return 12.0;

View File

@ -17,7 +17,6 @@
import net.minecraft.core.Direction
import net.minecraft.gametest.framework.GameTest
import net.minecraft.gametest.framework.GameTestHelper
import net.minecraft.world.InteractionHand
import net.minecraft.world.item.ItemStack
import net.minecraft.world.item.Items
import net.minecraft.world.level.block.Blocks
@ -140,8 +139,7 @@ fun Open_on_client(context: GameTestHelper) = context.sequence {
// Teleport the player to the computer and then open it.
thenExecute {
context.positionAt(BlockPos(2, 2, 1))
val computer = context.getBlockEntity(BlockPos(2, 2, 2), ModRegistry.BlockEntities.COMPUTER_ADVANCED.get())
computer.use(context.level.randomPlayer!!, InteractionHand.MAIN_HAND)
context.useBlock(BlockPos(2, 2, 2), context.level.randomPlayer!!)
}
// Assert the terminal is synced to the client.
thenIdle(2)