From aa8b626cd390947a5f4dcd45a1f17d8bd16e0415 Mon Sep 17 00:00:00 2001 From: Ivan Kh <47220198+Ivan-Khar@users.noreply.github.com> Date: Sun, 3 Dec 2023 22:25:36 +0300 Subject: [PATCH] added pearl cooldown --- .../aquatic/block/GiantClamBlock.kt | 51 ++++++++++++++----- .../block/entity/GiantClamBlockEntity.kt | 28 +++++++++- 2 files changed, 65 insertions(+), 14 deletions(-) diff --git a/src/main/kotlin/dev/hybridlabs/aquatic/block/GiantClamBlock.kt b/src/main/kotlin/dev/hybridlabs/aquatic/block/GiantClamBlock.kt index 2a97d2c60..3b9eb8eeb 100644 --- a/src/main/kotlin/dev/hybridlabs/aquatic/block/GiantClamBlock.kt +++ b/src/main/kotlin/dev/hybridlabs/aquatic/block/GiantClamBlock.kt @@ -1,9 +1,12 @@ package dev.hybridlabs.aquatic.block import dev.hybridlabs.aquatic.block.entity.GiantClamBlockEntity +import dev.hybridlabs.aquatic.block.entity.HybridAquaticBlockEntityTypes import dev.hybridlabs.aquatic.item.HybridAquaticItems import net.minecraft.block.* import net.minecraft.block.entity.BlockEntity +import net.minecraft.block.entity.BlockEntityTicker +import net.minecraft.block.entity.BlockEntityType import net.minecraft.entity.player.PlayerEntity import net.minecraft.fluid.FluidState import net.minecraft.fluid.Fluids @@ -13,6 +16,7 @@ import net.minecraft.registry.tag.FluidTags import net.minecraft.sound.SoundCategory import net.minecraft.sound.SoundEvents import net.minecraft.state.StateManager +import net.minecraft.state.property.BooleanProperty import net.minecraft.state.property.Properties.WATERLOGGED import net.minecraft.util.ActionResult import net.minecraft.util.Hand @@ -24,10 +28,12 @@ import net.minecraft.world.BlockView import net.minecraft.world.World import net.minecraft.world.WorldAccess -@Suppress("DEPRECATION") +@Suppress("OVERRIDE_DEPRECATION") class GiantClamBlock(settings: Settings) : PlantBlock(settings), BlockEntityProvider, Waterloggable { init { - defaultState = stateManager.defaultState.with(WATERLOGGED, false) + defaultState = stateManager.defaultState + .with(WATERLOGGED, false) + .with(CLAM_HAS_PEARL, true) } override fun canPlantOnTop(floor: BlockState, world: BlockView, pos: BlockPos): Boolean { @@ -82,7 +88,9 @@ class GiantClamBlock(settings: Settings) : PlantBlock(settings), BlockEntityProv } override fun appendProperties(builder: StateManager.Builder) { - builder.add(WATERLOGGED) + builder + .add(WATERLOGGED) + .add(CLAM_HAS_PEARL) } override fun onUse( @@ -94,21 +102,38 @@ class GiantClamBlock(settings: Settings) : PlantBlock(settings), BlockEntityProv hit: BlockHitResult? ): ActionResult? { if (hand == Hand.MAIN_HAND) { - 1 + world.random.nextInt(5) - dropStack(world, pos, ItemStack(HybridAquaticItems.PEARL, 1)) - world.playSound( - null, - pos, - SoundEvents.ENTITY_SHULKER_CLOSE, - SoundCategory.BLOCKS, - 1.0f, - 0.8f + world.random.nextFloat() * 0.4f - ) + val blockEntity = world.getBlockEntity(pos) + if (blockEntity is GiantClamBlockEntity && blockEntity.pearlCooldown == 0) { + blockEntity.pearlCooldown = world.random.nextBetween(1200, 6000) + + dropStack(world, pos, ItemStack(HybridAquaticItems.PEARL, 1)) + world.playSound( + null, + pos, + SoundEvents.ENTITY_SHULKER_CLOSE, + SoundCategory.BLOCKS, + 1.0f, + 0.8f + world.random.nextFloat() * 0.4f + ) + } } return super.onUse(state, world, pos, player, hand, hit) } + override fun getTicker( + world: World, + state: BlockState, + type: BlockEntityType + ): BlockEntityTicker? { + return if(world.isClient) { + null + } else { + BlockWithEntity.checkType(type, HybridAquaticBlockEntityTypes.GIANT_CLAM, GiantClamBlockEntity::tick) + } + } + companion object { + val CLAM_HAS_PEARL: BooleanProperty = BooleanProperty.of("clam_has_pearl") private val SHAPE = createCuboidShape(2.0, 0.0, 2.0, 14.0, 8.0, 14.0) private val COLLISION_SHAPE = createCuboidShape(2.0, 0.0, 2.0, 14.0, 8.0, 14.0) } diff --git a/src/main/kotlin/dev/hybridlabs/aquatic/block/entity/GiantClamBlockEntity.kt b/src/main/kotlin/dev/hybridlabs/aquatic/block/entity/GiantClamBlockEntity.kt index dbba40567..78d65db5e 100644 --- a/src/main/kotlin/dev/hybridlabs/aquatic/block/entity/GiantClamBlockEntity.kt +++ b/src/main/kotlin/dev/hybridlabs/aquatic/block/entity/GiantClamBlockEntity.kt @@ -1,10 +1,12 @@ package dev.hybridlabs.aquatic.block.entity +import dev.hybridlabs.aquatic.block.GiantClamBlock import net.minecraft.block.BlockState import net.minecraft.block.entity.BlockEntity import net.minecraft.nbt.NbtCompound import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket import net.minecraft.util.math.BlockPos +import net.minecraft.world.World import software.bernie.geckolib.core.animatable.GeoAnimatable import software.bernie.geckolib.core.animatable.instance.AnimatableInstanceCache import software.bernie.geckolib.core.animation.* @@ -14,10 +16,13 @@ import software.bernie.geckolib.util.RenderUtils class GiantClamBlockEntity(pos: BlockPos, state: BlockState) : BlockEntity(HybridAquaticBlockEntityTypes.GIANT_CLAM, pos, state), GeoAnimatable { private val factory = GeckoLibUtil.createInstanceCache(this) + var pearlCooldown: Int = 0 private fun predicate(event: AnimationState): PlayState where E : BlockEntity?, E : GeoAnimatable { return if (world != null) { - event.controller.setAnimation(RawAnimation.begin().then("open", Animation.LoopType.LOOP)) + if(world!!.getBlockState(pos).get(GiantClamBlock.CLAM_HAS_PEARL)) event.controller.setAnimation(OPEN_ANIMATION) + else event.controller.setAnimation(CLOSED_ANIMATION) + PlayState.CONTINUE } else { PlayState.STOP @@ -36,6 +41,16 @@ class GiantClamBlockEntity(pos: BlockPos, state: BlockState) : BlockEntity(Hybri return RenderUtils.getCurrentTick() } + override fun readNbt(nbt: NbtCompound) { + pearlCooldown = nbt.getInt(PEARL_COOLDOWN_NBT_KEY) + super.readNbt(nbt) + } + + override fun writeNbt(nbt: NbtCompound) { + nbt.putInt(PEARL_COOLDOWN_NBT_KEY, pearlCooldown) + super.writeNbt(nbt) + } + override fun toInitialChunkDataNbt(): NbtCompound { return createNbt() } @@ -43,4 +58,15 @@ class GiantClamBlockEntity(pos: BlockPos, state: BlockState) : BlockEntity(Hybri override fun toUpdatePacket(): BlockEntityUpdateS2CPacket { return BlockEntityUpdateS2CPacket.create(this) } + + companion object { + const val PEARL_COOLDOWN_NBT_KEY: String = "pearl_cooldown" + val OPEN_ANIMATION: RawAnimation = RawAnimation.begin().then("open", Animation.LoopType.LOOP) + val CLOSED_ANIMATION: RawAnimation = RawAnimation.begin().then("closed", Animation.LoopType.LOOP) + + fun tick(world: World, pos: BlockPos, state: BlockState, blockEntity: GiantClamBlockEntity) { + if(blockEntity.pearlCooldown > 0) blockEntity.pearlCooldown-- + world.setBlockState(pos, state.with(GiantClamBlock.CLAM_HAS_PEARL, blockEntity.pearlCooldown == 0)) + } + } }