From 65398db5afa323cff6c6045b1aa570f5fa5479f4 Mon Sep 17 00:00:00 2001 From: Sollace Date: Tue, 8 Oct 2024 15:10:54 +0100 Subject: [PATCH] You can now soak clouds using bottles of water --- .../block/cloud/NaturalCloudBlock.java | 2 +- .../block/cloud/PoreousCloudBlock.java | 11 +++++++ .../block/cloud/PoreousCloudStairsBlock.java | 12 +++++++ .../unicopia/block/cloud/Soakable.java | 32 +++++++++++++------ 4 files changed, 47 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/minelittlepony/unicopia/block/cloud/NaturalCloudBlock.java b/src/main/java/com/minelittlepony/unicopia/block/cloud/NaturalCloudBlock.java index c8bea7bf..17c1203c 100644 --- a/src/main/java/com/minelittlepony/unicopia/block/cloud/NaturalCloudBlock.java +++ b/src/main/java/com/minelittlepony/unicopia/block/cloud/NaturalCloudBlock.java @@ -59,6 +59,6 @@ public class NaturalCloudBlock extends PoreousCloudBlock { return ItemActionResult.SUCCESS; } - return ItemActionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION; + return super.onUseWithItem(stack, state, world, pos, player, hand, hit); } } diff --git a/src/main/java/com/minelittlepony/unicopia/block/cloud/PoreousCloudBlock.java b/src/main/java/com/minelittlepony/unicopia/block/cloud/PoreousCloudBlock.java index 12e312ce..3601425a 100644 --- a/src/main/java/com/minelittlepony/unicopia/block/cloud/PoreousCloudBlock.java +++ b/src/main/java/com/minelittlepony/unicopia/block/cloud/PoreousCloudBlock.java @@ -12,9 +12,15 @@ import com.mojang.serialization.codecs.RecordCodecBuilder; import net.minecraft.block.BedBlock; import net.minecraft.block.BlockState; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.ItemStack; import net.minecraft.server.world.ServerWorld; +import net.minecraft.util.Hand; +import net.minecraft.util.ItemActionResult; +import net.minecraft.util.hit.BlockHitResult; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.random.Random; +import net.minecraft.world.World; public class PoreousCloudBlock extends CloudBlock implements Soakable { private static final MapCodec CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group( @@ -36,6 +42,11 @@ public class PoreousCloudBlock extends CloudBlock implements Soakable { return CODEC; } + @Override + protected ItemActionResult onUseWithItem(ItemStack stack, BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { + return Soakable.tryDepositMoisture(stack, state, world, pos, player, hand, hit); + } + @Nullable @Override public BlockState getStateWithMoisture(BlockState state, int moisture) { diff --git a/src/main/java/com/minelittlepony/unicopia/block/cloud/PoreousCloudStairsBlock.java b/src/main/java/com/minelittlepony/unicopia/block/cloud/PoreousCloudStairsBlock.java index 63434c5e..37b1f505 100644 --- a/src/main/java/com/minelittlepony/unicopia/block/cloud/PoreousCloudStairsBlock.java +++ b/src/main/java/com/minelittlepony/unicopia/block/cloud/PoreousCloudStairsBlock.java @@ -11,6 +11,13 @@ import com.mojang.serialization.codecs.RecordCodecBuilder; import net.minecraft.block.BlockState; import net.minecraft.block.StairsBlock; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.ItemStack; +import net.minecraft.util.Hand; +import net.minecraft.util.ItemActionResult; +import net.minecraft.util.hit.BlockHitResult; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; public class PoreousCloudStairsBlock extends CloudStairsBlock implements Soakable { private static final MapCodec CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group( @@ -31,6 +38,11 @@ public class PoreousCloudStairsBlock extends CloudStairsBlock implements Soakabl return CODEC; } + @Override + protected ItemActionResult onUseWithItem(ItemStack stack, BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { + return Soakable.tryDepositMoisture(stack, state, world, pos, player, hand, hit); + } + @Nullable @Override public BlockState getStateWithMoisture(BlockState state, int moisture) { diff --git a/src/main/java/com/minelittlepony/unicopia/block/cloud/Soakable.java b/src/main/java/com/minelittlepony/unicopia/block/cloud/Soakable.java index 6baed1fd..b137a6c7 100644 --- a/src/main/java/com/minelittlepony/unicopia/block/cloud/Soakable.java +++ b/src/main/java/com/minelittlepony/unicopia/block/cloud/Soakable.java @@ -8,10 +8,14 @@ import com.mojang.serialization.Codec; import net.minecraft.block.Block; import net.minecraft.block.BlockState; +import net.minecraft.component.DataComponentTypes; +import net.minecraft.component.type.PotionContentsComponent; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.ItemStack; +import net.minecraft.item.ItemUsage; import net.minecraft.item.Items; import net.minecraft.particle.ParticleTypes; +import net.minecraft.potion.Potions; import net.minecraft.registry.Registries; import net.minecraft.server.world.ServerWorld; import net.minecraft.sound.SoundCategory; @@ -53,15 +57,8 @@ public interface Soakable { static ItemActionResult tryCollectMoisture(ItemStack stack, BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { if (state.getBlock() instanceof Soakable soakable) { - if (stack.getItem() == Items.GLASS_BOTTLE) { - if (!player.isCreative()) { - stack.split(1); - } - if (stack.isEmpty()) { - player.setStackInHand(hand, Items.POTION.getDefaultStack()); - } else { - player.giveItemStack(Items.POTION.getDefaultStack()); - } + if (stack.isOf(Items.GLASS_BOTTLE)) { + player.setStackInHand(hand, ItemUsage.exchangeStack(stack, player, Items.POTION.getDefaultStack(), false)); world.playSound(player, player.getX(), player.getY(), player.getZ(), USounds.Vanilla.ITEM_BOTTLE_FILL, SoundCategory.NEUTRAL, 1, 1); world.emitGameEvent(player, GameEvent.FLUID_PICKUP, pos); updateMoisture(soakable, state, world, pos, soakable.getMoisture(state) - 1); @@ -70,6 +67,23 @@ public interface Soakable { } } + return tryDepositMoisture(stack, state, world, pos, player, hand, hit); + } + + static ItemActionResult tryDepositMoisture(ItemStack stack, BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { + if (state.getBlock() instanceof Soakable soakable) { + if (soakable.getMoisture(state) < 7 + && stack.isOf(Items.POTION) + && stack.getOrDefault(DataComponentTypes.POTION_CONTENTS, PotionContentsComponent.DEFAULT).matches(Potions.WATER)) { + player.setStackInHand(hand, ItemUsage.exchangeStack(stack, player, Items.GLASS_BOTTLE.getDefaultStack(), false)); + world.playSound(player, player.getX(), player.getY(), player.getZ(), USounds.Vanilla.ITEM_BUCKET_EMPTY, SoundCategory.NEUTRAL, 1, 1); + world.emitGameEvent(player, GameEvent.FLUID_PLACE, pos); + updateMoisture(soakable, state, world, pos, soakable.getMoisture(state) + 1); + + return ItemActionResult.SUCCESS; + } + } + return ItemActionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION; }