Skip to content

Commit

Permalink
fix: worm count per block
Browse files Browse the repository at this point in the history
  • Loading branch information
andantet committed Nov 19, 2024
1 parent d6ec36c commit e18c1b7
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
"min_inclusive": 2
}
},
"max_worms": 4,
"min_worms": 1,
"spread_radius": 5,
"vent_block": {
"type": "minecraft:simple_state_provider",
Expand Down Expand Up @@ -44,6 +42,13 @@
"min_inclusive": 4
}
},
"worm_count_per_block": {
"type": "minecraft:uniform",
"value": {
"max_inclusive": 4,
"min_inclusive": 1
}
},
"worm_spread_radius": {
"type": "minecraft:uniform",
"value": {
Expand Down
16 changes: 10 additions & 6 deletions src/main/kotlin/dev/hybridlabs/aquatic/block/TubeWormBlock.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.hybridlabs.aquatic.block

import com.mojang.serialization.Codec
import net.minecraft.block.*
import net.minecraft.entity.ai.pathing.NavigationType
import net.minecraft.fluid.FluidState
Expand All @@ -13,6 +14,7 @@ import net.minecraft.state.property.IntProperty
import net.minecraft.state.property.Properties
import net.minecraft.util.math.BlockPos
import net.minecraft.util.math.Direction
import net.minecraft.util.math.intprovider.IntProvider
import net.minecraft.util.math.random.Random
import net.minecraft.util.shape.VoxelShape
import net.minecraft.world.BlockView
Expand All @@ -24,24 +26,26 @@ import org.jetbrains.annotations.Nullable
@Suppress("DEPRECATION", "SameParameterValue")
class TubeWormBlock(settings: Settings) : PlantBlock(settings), Fertilizable, Waterloggable {
companion object {
const val MAX_WORMS = 4
val WORMS: IntProperty = IntProperty.of("worms", 1, MAX_WORMS)
val WORMS: IntProperty = IntProperty.of("worms", 1, 4)
val WATERLOGGED: BooleanProperty = Properties.WATERLOGGED

val WORM_COUNT_CODEC: Codec<IntProvider> = IntProvider.createValidatingCodec(WORMS.min, WORMS.max)

private val ONE_WORM_SHAPE: VoxelShape = createCuboidShape(6.0, 0.0, 6.0, 10.0, 8.0, 10.0)
private val TWO_WORMS_SHAPE: VoxelShape = createCuboidShape(3.0, 0.0, 3.0, 13.0, 8.0, 13.0)
private val THREE_WORMS_SHAPE: VoxelShape = createCuboidShape(2.0, 0.0, 2.0, 14.0, 8.0, 14.0)
private val FOUR_WORMS_SHAPE: VoxelShape = createCuboidShape(2.0, 0.0, 2.0, 14.0, 10.0, 14.0)
}

init {
defaultState = stateManager.defaultState.with(WORMS, 1).with(WATERLOGGED, true)
defaultState = stateManager.defaultState.with(WORMS, WORMS.min).with(WATERLOGGED, true)
}

@Nullable
override fun getPlacementState(ctx: ItemPlacementContext): BlockState? {
val blockState = ctx.world.getBlockState(ctx.blockPos)
return if (blockState.isOf(this)) {
blockState.with(WORMS, (blockState[WORMS]!! + 1).coerceAtMost(MAX_WORMS))
blockState.with(WORMS, (blockState[WORMS] + 1).coerceAtMost(WORMS.max))
} else {
val fluidState = ctx.world.getFluidState(ctx.blockPos)
val isWaterlogged = fluidState.fluid == Fluids.WATER
Expand Down Expand Up @@ -75,7 +79,7 @@ class TubeWormBlock(settings: Settings) : PlantBlock(settings), Fertilizable, Wa
override fun canReplace(state: BlockState, context: ItemPlacementContext): Boolean {
return !context.shouldCancelInteraction() &&
context.stack.isOf(asItem()) &&
state[WORMS]!! < MAX_WORMS || super.canReplace(state, context)
state[WORMS] < WORMS.max || super.canReplace(state, context)
}

override fun getOutlineShape(
Expand Down Expand Up @@ -124,4 +128,4 @@ class TubeWormBlock(settings: Settings) : PlantBlock(settings), Fertilizable, Wa
private fun isDry(state: BlockState): Boolean {
return !state[WATERLOGGED]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package dev.hybridlabs.aquatic.data.server

import dev.hybridlabs.aquatic.block.HybridAquaticBlocks
import dev.hybridlabs.aquatic.block.TubeWormBlock
import dev.hybridlabs.aquatic.tag.HybridAquaticBlockTags
import dev.hybridlabs.aquatic.world.gen.feature.*
import net.fabricmc.fabric.api.datagen.v1.FabricDataOutput
Expand Down Expand Up @@ -94,8 +95,7 @@ class ConfiguredFeatureProvider(output: FabricDataOutput, registriesFuture: Comp
ConstantIntProvider.create(5),
UniformIntProvider.create(4, 8),
UniformIntProvider.create(2, 8), // Worm spread radius
1,
4
UniformIntProvider.create(TubeWormBlock.WORMS.min, TubeWormBlock.WORMS.max),
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package dev.hybridlabs.aquatic.world.gen.feature

import com.mojang.serialization.Codec
import dev.hybridlabs.aquatic.block.HydrothermalVentBlock
import dev.hybridlabs.aquatic.block.TubeWormBlock
import net.minecraft.block.Block
import net.minecraft.block.BlockState
import net.minecraft.block.enums.Thickness
import net.minecraft.state.property.Properties
import net.minecraft.util.math.BlockPos
import net.minecraft.util.math.Direction
import net.minecraft.util.math.intprovider.IntProvider
import net.minecraft.util.math.random.Random
import net.minecraft.world.Heightmap
import net.minecraft.world.WorldAccess
Expand All @@ -25,7 +27,7 @@ class VentPatchFeature(codec: Codec<VentPatchFeatureConfig>) : Feature<VentPatch
val origin = context.origin
val random = context.random

val (baseProvider, ventProvider, wormProvider, countProvider, radiusProvider, wormCountProvider, wormRadiusProvider) = context.config
val (baseProvider, ventProvider, wormProvider, countProvider, radiusProvider, wormCountProvider, wormRadiusProvider, wormCountPerBlockProvider) = context.config

val count = countProvider.get(random)
repeat(count) {
Expand All @@ -43,7 +45,7 @@ class VentPatchFeature(codec: Codec<VentPatchFeatureConfig>) : Feature<VentPatch
if (generateSingleVent(world, candidateTopPos, random, heightMultiplier, baseProvider, ventProvider)) {
val wormCount = wormCountProvider.get(random)
val wormRadius = wormRadiusProvider.get(random)
generateTubeWormPatch(world, candidateTopPos, random, wormCount, wormRadius, wormProvider)
generateTubeWormPatch(world, candidateTopPos, random, wormCount, wormCountPerBlockProvider, wormRadius, wormProvider)
generated = true
}
}
Expand Down Expand Up @@ -122,6 +124,7 @@ class VentPatchFeature(codec: Codec<VentPatchFeatureConfig>) : Feature<VentPatch
pos: BlockPos,
random: Random,
count: Int,
wormCountProvider: IntProvider,
wormRadius: Int,
stateProvider: BlockStateProvider
): Boolean {
Expand All @@ -142,7 +145,8 @@ class VentPatchFeature(codec: Codec<VentPatchFeatureConfig>) : Feature<VentPatch

val state = stateProvider.get(random, tubeWormPos)
if (isValidPosition(world, tubeWormPos, state)) {
world.setBlockState(tubeWormPos, state, Block.NOTIFY_LISTENERS)
val wormCount = wormCountProvider.get(random)
world.setBlockState(tubeWormPos, state.with(TubeWormBlock.WORMS, wormCount), Block.NOTIFY_LISTENERS)
placed++
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ data class VentPatchFeatureConfig(
val spreadRadius: IntProvider,
val wormCount: IntProvider,
val wormSpreadRadius: IntProvider,
val minWorms: Int,
val maxWorms: Int,
val wormCountPerBlock: IntProvider,
) : FeatureConfig {
companion object {
val CODEC: Codec<VentPatchFeatureConfig> = RecordCodecBuilder.create { instance ->
Expand All @@ -28,8 +27,7 @@ data class VentPatchFeatureConfig(
IntProvider.POSITIVE_CODEC.fieldOf("spread_radius").forGetter(VentPatchFeatureConfig::spreadRadius),
IntProvider.POSITIVE_CODEC.fieldOf("worm_count").forGetter(VentPatchFeatureConfig::wormCount),
IntProvider.POSITIVE_CODEC.fieldOf("worm_spread_radius").forGetter(VentPatchFeatureConfig::wormSpreadRadius),
Codec.intRange(1, TubeWormBlock.MAX_WORMS).fieldOf("min_worms").forGetter(VentPatchFeatureConfig::minWorms),
Codec.intRange(1, TubeWormBlock.MAX_WORMS).fieldOf("max_worms").forGetter(VentPatchFeatureConfig::maxWorms),
TubeWormBlock.WORM_COUNT_CODEC.fieldOf("worm_count_per_block").forGetter(VentPatchFeatureConfig::wormCountPerBlock),
).apply(instance, ::VentPatchFeatureConfig)
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/hybrid-aquatic.accesswidener
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ accessible class net/minecraft/client/render/BackgroundRenderer$StatusEffectFogM
accessible class net/minecraft/client/render/BackgroundRenderer$BlindnessFogModifier
accessible class net/minecraft/client/render/BackgroundRenderer$DarknessFogModifier
accessible method net/minecraft/block/PointedDripstoneBlock getThickness (Lnet/minecraft/world/WorldView;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/util/math/Direction;Z)Lnet/minecraft/block/enums/Thickness;
accessible field net/minecraft/state/property/IntProperty min I
accessible field net/minecraft/state/property/IntProperty max I

0 comments on commit e18c1b7

Please sign in to comment.