From f96d923b2aa4de88f93fea5cee7131e8e47edda6 Mon Sep 17 00:00:00 2001 From: Jonathan Coates Date: Sat, 29 May 2021 14:52:04 +0100 Subject: [PATCH] Allow cleaning dyed turtles in a cauldron Fixes #771. This does not allow clearing pocket computers or disks right now - neither normally responds very well to water! --- .../shared/turtle/items/ItemTurtle.java | 30 ++++ .../dan200/computercraft/ingame/TurtleTest.kt | 14 +- .../computercraft/lua/rom/autorun/cctest.lua | 8 + .../computers/computer/12/startup.lua | 10 ++ src/test/server-files/computers/ids.json | 2 +- .../turtle_test.cleaned_with_cauldrons.snbt | 163 ++++++++++++++++++ 6 files changed, 222 insertions(+), 5 deletions(-) create mode 100644 src/test/server-files/computers/computer/12/startup.lua create mode 100644 src/test/server-files/structures/turtle_test.cleaned_with_cauldrons.snbt diff --git a/src/main/java/dan200/computercraft/shared/turtle/items/ItemTurtle.java b/src/main/java/dan200/computercraft/shared/turtle/items/ItemTurtle.java index 24be2a9f3..003516cd4 100644 --- a/src/main/java/dan200/computercraft/shared/turtle/items/ItemTurtle.java +++ b/src/main/java/dan200/computercraft/shared/turtle/items/ItemTurtle.java @@ -13,14 +13,21 @@ import dan200.computercraft.shared.computer.core.ComputerFamily; import dan200.computercraft.shared.computer.items.ItemComputerBase; import dan200.computercraft.shared.turtle.blocks.BlockTurtle; +import net.minecraft.block.BlockState; +import net.minecraft.block.Blocks; +import net.minecraft.block.CauldronBlock; import net.minecraft.item.ItemGroup; import net.minecraft.item.ItemStack; +import net.minecraft.item.ItemUseContext; import net.minecraft.nbt.CompoundNBT; +import net.minecraft.util.ActionResultType; import net.minecraft.util.NonNullList; import net.minecraft.util.ResourceLocation; +import net.minecraft.util.math.BlockPos; import net.minecraft.util.text.ITextComponent; import net.minecraft.util.text.StringTextComponent; import net.minecraft.util.text.TranslationTextComponent; +import net.minecraft.world.World; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -161,4 +168,27 @@ public int getFuelLevel( @Nonnull ItemStack stack ) CompoundNBT tag = stack.getTag(); return tag != null && tag.contains( NBT_FUEL ) ? tag.getInt( NBT_FUEL ) : 0; } + + @Override + public ActionResultType onItemUseFirst( ItemStack stack, ItemUseContext context ) + { + if( context.isSecondaryUseActive() || getColour( stack ) == -1 ) return ActionResultType.PASS; + + World level = context.getLevel(); + BlockPos pos = context.getClickedPos(); + + BlockState blockState = level.getBlockState( pos ); + if( blockState.getBlock() != Blocks.CAULDRON ) return ActionResultType.PASS; + + int waterLevel = blockState.getValue( CauldronBlock.LEVEL ); + if( waterLevel <= 0 ) return ActionResultType.PASS; + + if( !level.isClientSide ) + { + ((CauldronBlock) blockState.getBlock()).setWaterLevel( level, pos, blockState, waterLevel - 1 ); + IColouredItem.setColourBasic( stack, -1 ); + } + + return ActionResultType.SUCCESS; + } } diff --git a/src/test/java/dan200/computercraft/ingame/TurtleTest.kt b/src/test/java/dan200/computercraft/ingame/TurtleTest.kt index 4c793a763..3672d9b89 100644 --- a/src/test/java/dan200/computercraft/ingame/TurtleTest.kt +++ b/src/test/java/dan200/computercraft/ingame/TurtleTest.kt @@ -33,7 +33,7 @@ class TurtleTest { suspend fun `Place waterlogged`(context: TestContext) = context.checkComputerOk(7) /** - * Checks turtles can place when waterlogged. + * Checks turtles can pick up lava * * @see [#297](https://github.com/SquidDev-CC/CC-Tweaked/issues/297) */ @@ -41,7 +41,7 @@ class TurtleTest { suspend fun `Gather lava`(context: TestContext) = context.checkComputerOk(8) /** - * Checks turtles can place when waterlogged. + * Checks turtles can hoe dirt. * * @see [#258](https://github.com/SquidDev-CC/CC-Tweaked/issues/258) */ @@ -57,9 +57,15 @@ class TurtleTest { suspend fun `Place monitor`(context: TestContext) = context.checkComputerOk(10) /** - * Checks computers can place into compostors. These are non-typical inventories, so - * worth ensuring. + * Checks turtles can place into compostors. These are non-typical inventories, so + * worth testing. */ @GameTest suspend fun `Use compostors`(context: TestContext) = context.checkComputerOk(11) + + /** + * Checks turtles can be cleaned in cauldrons. + */ + @GameTest + suspend fun `Cleaned with cauldrons`(context: TestContext) = context.checkComputerOk(12) } diff --git a/src/test/resources/data/computercraft/lua/rom/autorun/cctest.lua b/src/test/resources/data/computercraft/lua/rom/autorun/cctest.lua index 1b4f6d778..ecf36c4e9 100644 --- a/src/test/resources/data/computercraft/lua/rom/autorun/cctest.lua +++ b/src/test/resources/data/computercraft/lua/rom/autorun/cctest.lua @@ -15,3 +15,11 @@ function test.eq(expected, actual, msg) if msg then message = ("%s - %s"):format(msg, message) end test.fail(message) end + +function test.neq(expected, actual, msg) + if expected ~= actual then return end + + local message = ("Assertion failed:\nExpected something different to %s"):format(expected) + if msg then message = ("%s - %s"):format(msg, message) end + test.fail(message) +end diff --git a/src/test/server-files/computers/computer/12/startup.lua b/src/test/server-files/computers/computer/12/startup.lua new file mode 100644 index 000000000..d84bc4bdc --- /dev/null +++ b/src/test/server-files/computers/computer/12/startup.lua @@ -0,0 +1,10 @@ +local old_details = turtle.getItemDetail(1, true) + +test.assert(turtle.place(), "Dyed turtle") + +local new_details = turtle.getItemDetail(1, true) +test.eq("computercraft:turtle_normal", new_details.name, "Still a turtle") +test.neq(old_details.nbt, new_details.nbt, "Colour has changed") + +test.ok() + diff --git a/src/test/server-files/computers/ids.json b/src/test/server-files/computers/ids.json index a90f89af9..6b4a94cbe 100644 --- a/src/test/server-files/computers/ids.json +++ b/src/test/server-files/computers/ids.json @@ -1,3 +1,3 @@ { - "computer": 11 + "computer": 12 } \ No newline at end of file diff --git a/src/test/server-files/structures/turtle_test.cleaned_with_cauldrons.snbt b/src/test/server-files/structures/turtle_test.cleaned_with_cauldrons.snbt new file mode 100644 index 000000000..9b0808cb7 --- /dev/null +++ b/src/test/server-files/structures/turtle_test.cleaned_with_cauldrons.snbt @@ -0,0 +1,163 @@ +{ + size: [3, 3, 3], + entities: [], + blocks: [ + { + pos: [0, 0, 0], + state: 0 + }, + { + pos: [1, 0, 0], + state: 0 + }, + { + pos: [2, 0, 0], + state: 0 + }, + { + pos: [0, 0, 1], + state: 0 + }, + { + pos: [1, 0, 1], + state: 0 + }, + { + pos: [2, 0, 1], + state: 0 + }, + { + pos: [0, 0, 2], + state: 0 + }, + { + pos: [1, 0, 2], + state: 0 + }, + { + pos: [2, 0, 2], + state: 0 + }, + { + nbt: { + Owner: { + UpperId: 4039158846114182220L, + LowerId: -6876936588741668278L, + Name: "Dev" + }, + Fuel: 0, + Label: "Clean turtle", + Slot: 0, + Items: [ + { + Slot: 0b, + id: "computercraft:turtle_normal", + Count: 1b, + tag: { + display: { + Name: '{"text":"Clean turtle"}' + }, + Color: 13388876, + ComputerId: 12 + } + } + ], + id: "computercraft:turtle_normal", + ComputerId: 12, + On: 1b + }, + pos: [1, 1, 0], + state: 1 + }, + { + pos: [0, 1, 0], + state: 2 + }, + { + pos: [2, 1, 0], + state: 2 + }, + { + pos: [0, 2, 0], + state: 2 + }, + { + pos: [1, 2, 0], + state: 2 + }, + { + pos: [2, 2, 0], + state: 2 + }, + { + pos: [0, 1, 1], + state: 2 + }, + { + pos: [1, 1, 1], + state: 3 + }, + { + pos: [2, 1, 1], + state: 2 + }, + { + pos: [0, 2, 1], + state: 2 + }, + { + pos: [1, 2, 1], + state: 2 + }, + { + pos: [2, 2, 1], + state: 2 + }, + { + pos: [0, 1, 2], + state: 2 + }, + { + pos: [1, 1, 2], + state: 2 + }, + { + pos: [2, 1, 2], + state: 2 + }, + { + pos: [0, 2, 2], + state: 2 + }, + { + pos: [1, 2, 2], + state: 2 + }, + { + pos: [2, 2, 2], + state: 2 + } + ], + palette: [ + { + Name: "minecraft:polished_andesite" + }, + { + Properties: { + waterlogged: "false", + facing: "south" + }, + Name: "computercraft:turtle_normal" + }, + { + Name: "minecraft:air" + }, + { + Properties: { + level: "3" + }, + Name: "minecraft:cauldron" + } + ], + DataVersion: 2230 +}