Skip to content

Commit

Permalink
added pearl cooldown
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan-Khar committed Dec 3, 2023
1 parent d242424 commit aa8b626
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 14 deletions.
51 changes: 38 additions & 13 deletions src/main/kotlin/dev/hybridlabs/aquatic/block/GiantClamBlock.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -82,7 +88,9 @@ class GiantClamBlock(settings: Settings) : PlantBlock(settings), BlockEntityProv
}

override fun appendProperties(builder: StateManager.Builder<Block, BlockState>) {
builder.add(WATERLOGGED)
builder
.add(WATERLOGGED)
.add(CLAM_HAS_PEARL)
}

override fun onUse(
Expand All @@ -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 <T : BlockEntity> getTicker(
world: World,
state: BlockState,
type: BlockEntityType<T>
): BlockEntityTicker<T>? {
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)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.*
Expand All @@ -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 <E> predicate(event: AnimationState<E>): 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
Expand All @@ -36,11 +41,32 @@ 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()
}

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))
}
}
}

0 comments on commit aa8b626

Please sign in to comment.