|
| 1 | +package com.mmodding.mmodding_lib.library.worldgen.features.builtin.differed; |
| 2 | + |
| 3 | +import com.mmodding.mmodding_lib.library.blocks.CustomInfluenceableBlock; |
| 4 | +import com.mojang.serialization.Codec; |
| 5 | +import com.mojang.serialization.codecs.RecordCodecBuilder; |
| 6 | +import net.minecraft.block.Block; |
| 7 | +import net.minecraft.block.BlockState; |
| 8 | +import net.minecraft.block.SnowyBlock; |
| 9 | +import net.minecraft.util.StringIdentifiable; |
| 10 | +import net.minecraft.util.math.BlockPos; |
| 11 | +import net.minecraft.util.math.Direction; |
| 12 | +import net.minecraft.world.Heightmap; |
| 13 | +import net.minecraft.world.StructureWorldAccess; |
| 14 | +import net.minecraft.world.biome.Biome; |
| 15 | +import net.minecraft.world.gen.feature.Feature; |
| 16 | +import net.minecraft.world.gen.feature.FeatureConfig; |
| 17 | +import net.minecraft.world.gen.feature.util.FeatureContext; |
| 18 | +import net.minecraft.world.gen.stateprovider.BlockStateProvider; |
| 19 | + |
| 20 | +public class DifferedFreezeTopLayerFeature extends Feature<DifferedFreezeTopLayerFeature.Config> { |
| 21 | + |
| 22 | + public DifferedFreezeTopLayerFeature(Codec<DifferedFreezeTopLayerFeature.Config> codec) { |
| 23 | + super(codec); |
| 24 | + } |
| 25 | + |
| 26 | + @Override |
| 27 | + public boolean place(FeatureContext<DifferedFreezeTopLayerFeature.Config> context) { |
| 28 | + StructureWorldAccess world = context.getWorld(); |
| 29 | + BlockPos blockPos = context.getOrigin(); |
| 30 | + BlockPos.Mutable snowLayerPos = new BlockPos.Mutable(); |
| 31 | + BlockPos.Mutable iceBlockPos = new BlockPos.Mutable(); |
| 32 | + |
| 33 | + for (int xOffset = 0; xOffset < 16; xOffset++) { |
| 34 | + for (int zOffset = 0; zOffset < 16; zOffset++) { |
| 35 | + int x = blockPos.getX() + xOffset; |
| 36 | + int z = blockPos.getZ() + zOffset; |
| 37 | + int y = world.getTopY(Heightmap.Type.MOTION_BLOCKING, x, z); |
| 38 | + snowLayerPos.set(x, y, z); |
| 39 | + iceBlockPos.set(snowLayerPos).move(Direction.DOWN, 1); |
| 40 | + Biome biome = world.getBiome(snowLayerPos).value(); |
| 41 | + if (biome.canSetIce(world, iceBlockPos, false)) { |
| 42 | + world.setBlockState( |
| 43 | + iceBlockPos, |
| 44 | + context.getConfig().iceBlock.getBlockState(context.getRandom(), iceBlockPos), |
| 45 | + Block.NOTIFY_LISTENERS |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + if (biome.canSetSnow(world, snowLayerPos)) { |
| 50 | + world.setBlockState(snowLayerPos, context.getConfig().snowLayer.getBlockState(context.getRandom(), snowLayerPos), Block.NOTIFY_LISTENERS); |
| 51 | + BlockState state = world.getBlockState(iceBlockPos); |
| 52 | + if (state.contains(SnowyBlock.SNOWY)) { |
| 53 | + world.setBlockState(iceBlockPos, state.with(SnowyBlock.SNOWY, Boolean.TRUE), Block.NOTIFY_LISTENERS); |
| 54 | + } |
| 55 | + this.updateInfluencableBlock(state, world, new BlockPos(iceBlockPos)); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + return true; |
| 61 | + } |
| 62 | + |
| 63 | + @SuppressWarnings("unchecked") |
| 64 | + private <E extends Enum<E> & StringIdentifiable> void updateInfluencableBlock(BlockState state, StructureWorldAccess world, BlockPos pos) { |
| 65 | + if (state.getBlock() instanceof CustomInfluenceableBlock<?>) { |
| 66 | + CustomInfluenceableBlock<E> influenceable = (CustomInfluenceableBlock<E>) state.getBlock(); |
| 67 | + world.setBlockState( |
| 68 | + pos, |
| 69 | + state.with( |
| 70 | + influenceable.getInfluenceProperty(), |
| 71 | + influenceable.getInfluence(world.getBlockState(new BlockPos(pos).up())) |
| 72 | + ), |
| 73 | + Block.NOTIFY_LISTENERS |
| 74 | + ); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + public static class Config implements FeatureConfig { |
| 79 | + |
| 80 | + public static final Codec<Config> CODEC = RecordCodecBuilder.create(instance -> instance.group( |
| 81 | + BlockStateProvider.TYPE_CODEC.fieldOf("ice_block").forGetter(config -> config.iceBlock), |
| 82 | + BlockStateProvider.TYPE_CODEC.fieldOf("snow_layer").forGetter(config -> config.snowLayer) |
| 83 | + ).apply(instance, Config::new)); |
| 84 | + |
| 85 | + private final BlockStateProvider iceBlock; |
| 86 | + private final BlockStateProvider snowLayer; |
| 87 | + |
| 88 | + public Config(BlockStateProvider iceBlock, BlockStateProvider snowLayer) { |
| 89 | + this.iceBlock = iceBlock; |
| 90 | + this.snowLayer = snowLayer; |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments