From 03bb279206bdf2ee3f18ee771007ded8786bdeae Mon Sep 17 00:00:00 2001 From: Jonathan Coates Date: Wed, 17 Apr 2024 15:00:43 +0100 Subject: [PATCH] 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. --- gradle/libs.versions.toml | 2 +- .../blocks/AbstractComputerBlock.java | 16 +++++++- .../blocks/AbstractComputerBlockEntity.java | 40 +++---------------- .../shared/turtle/blocks/TurtleBlock.java | 20 ++++++++++ .../turtle/blocks/TurtleBlockEntity.java | 5 --- .../computercraft/gametest/Computer_Test.kt | 4 +- 6 files changed, 41 insertions(+), 46 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 5179d43b5..7d39dcb80 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -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" diff --git a/projects/common/src/main/java/dan200/computercraft/shared/computer/blocks/AbstractComputerBlock.java b/projects/common/src/main/java/dan200/computercraft/shared/computer/blocks/AbstractComputerBlock.java index a5f6eb34d..46af2e6ff 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/computer/blocks/AbstractComputerBlock.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/computer/blocks/AbstractComputerBlock.java @@ -8,6 +8,7 @@ import dan200.computercraft.annotations.ForgeOverride; 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 abstract class 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 abstract class AbstractComputerBlockEntity extends BlockEntity implements if (getLevel().isClientSide || computerID == id) return; computerID = id; - setChanged(); + BlockEntityHelpers.updateBlock(this); } @Override @@ -353,7 +323,7 @@ public abstract class AbstractComputerBlockEntity extends BlockEntity implements this.label = label; var computer = getServerComputer(); if (computer != null) computer.setLabel(label); - setChanged(); + BlockEntityHelpers.updateBlock(this); } @Override diff --git a/projects/common/src/main/java/dan200/computercraft/shared/turtle/blocks/TurtleBlock.java b/projects/common/src/main/java/dan200/computercraft/shared/turtle/blocks/TurtleBlock.java index e2b01fa73..4694a8183 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/turtle/blocks/TurtleBlock.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/turtle/blocks/TurtleBlock.java @@ -18,10 +18,13 @@ import dan200.computercraft.shared.util.WaterloggableHelpers; 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.StateDefinition; 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 class TurtleBlock extends AbstractComputerBlock implem } } + @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(); diff --git a/projects/common/src/main/java/dan200/computercraft/shared/turtle/blocks/TurtleBlockEntity.java b/projects/common/src/main/java/dan200/computercraft/shared/turtle/blocks/TurtleBlockEntity.java index 3160aac02..2fc0c225d 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/turtle/blocks/TurtleBlockEntity.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/turtle/blocks/TurtleBlockEntity.java @@ -87,11 +87,6 @@ public class TurtleBlockEntity extends AbstractComputerBlockEntity implements Ba if (!hasMoved()) super.unload(); } - @Override - protected boolean canNameWithTag(Player player) { - return true; - } - @Override protected double getInteractRange() { return 12.0; diff --git a/projects/common/src/testMod/kotlin/dan200/computercraft/gametest/Computer_Test.kt b/projects/common/src/testMod/kotlin/dan200/computercraft/gametest/Computer_Test.kt index 517580964..a4ebf599e 100644 --- a/projects/common/src/testMod/kotlin/dan200/computercraft/gametest/Computer_Test.kt +++ b/projects/common/src/testMod/kotlin/dan200/computercraft/gametest/Computer_Test.kt @@ -17,7 +17,6 @@ import net.minecraft.core.BlockPos 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 @@ class Computer_Test { // 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)