Skip to content

Commit 30031d9

Browse files
committed
Snowy Leaves now melt under light
1 parent e5c4550 commit 30031d9

3 files changed

Lines changed: 32 additions & 11 deletions

File tree

src/main/java/fr/hugman/promenade/block/SnowyLeavesBlock.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
package fr.hugman.promenade.block;
22

3+
import fr.hugman.promenade.registry.PromenadeRegistryKeys;
4+
import fr.hugman.promenade.tag.PromenadeBiomeTags;
35
import net.minecraft.block.Block;
46
import net.minecraft.block.BlockState;
57
import net.minecraft.block.LeavesBlock;
68
import net.minecraft.item.ItemPlacementContext;
9+
import net.minecraft.server.world.ServerWorld;
710
import net.minecraft.state.StateManager;
811
import net.minecraft.state.property.BooleanProperty;
912
import net.minecraft.state.property.Properties;
13+
import net.minecraft.state.property.Property;
1014
import net.minecraft.util.math.BlockPos;
1115
import net.minecraft.util.math.Direction;
1216
import net.minecraft.util.math.random.Random;
17+
import net.minecraft.world.LightType;
1318
import net.minecraft.world.WorldView;
1419
import net.minecraft.world.tick.ScheduledTickView;
1520

@@ -35,6 +40,29 @@ public BlockState getPlacementState(ItemPlacementContext context) {
3540
return state.with(BOTTOM, !isSnow(stateBelow));
3641
}
3742

43+
@Override
44+
protected boolean hasRandomTicks(BlockState state) {
45+
return true;
46+
}
47+
48+
@Override
49+
protected void randomTick(BlockState state, ServerWorld world, BlockPos pos, Random random) {
50+
super.randomTick(state, world, pos, random);
51+
52+
if (world.getLightLevel(LightType.BLOCK, pos) > 11 && !world.getBiome(pos).isIn(PromenadeBiomeTags.CAN_FREEZE_DURING_SNOWFALL)) {
53+
var normalLeaves = world.getRegistryManager().getOrThrow(PromenadeRegistryKeys.SNOWY_BLOCK_TRANSFORMATION).stream().filter(
54+
entry -> entry.snowyBlock().value() == state.getBlock()
55+
).findFirst().map(sbt -> sbt.baseBlock().value()).orElse(null);
56+
57+
var newLeavesState = normalLeaves.getDefaultState();
58+
// copy properties of snowy block
59+
for (Property property : state.getProperties()) {
60+
newLeavesState = newLeavesState.contains(property) ? newLeavesState.with(property, state.get(property)) : newLeavesState;
61+
}
62+
world.setBlockState(pos, newLeavesState);
63+
}
64+
}
65+
3866
@Override
3967
protected BlockState getStateForNeighborUpdate(BlockState state, WorldView world, ScheduledTickView tickView, BlockPos pos, Direction direction, BlockPos neighborPos, BlockState neighborState, Random random) {
4068
BlockState stateBelow = world.getBlockState(pos.down());

src/main/java/fr/hugman/promenade/block/snowy/SnowyBlockTransformation.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,11 @@
44
import com.mojang.serialization.codecs.RecordCodecBuilder;
55
import net.minecraft.block.Block;
66
import net.minecraft.registry.Registries;
7-
import net.minecraft.registry.RegistryCodecs;
8-
import net.minecraft.registry.RegistryKeys;
97
import net.minecraft.registry.entry.RegistryEntry;
10-
import net.minecraft.registry.entry.RegistryEntryList;
118

12-
public record SnowyBlockTransformation(RegistryEntryList<Block> baseBlocks, RegistryEntry<Block> snowyBlock) {
9+
public record SnowyBlockTransformation(RegistryEntry<Block> baseBlock, RegistryEntry<Block> snowyBlock) {
1310
public static final Codec<SnowyBlockTransformation> CODEC = RecordCodecBuilder.create(instance -> instance.group(
14-
RegistryCodecs.entryList(RegistryKeys.BLOCK).fieldOf("base_blocks").forGetter(SnowyBlockTransformation::baseBlocks),
11+
Registries.BLOCK.getEntryCodec().fieldOf("base_block").forGetter(SnowyBlockTransformation::baseBlock),
1512
Registries.BLOCK.getEntryCodec().fieldOf("snowy_block").forGetter(SnowyBlockTransformation::snowyBlock)
1613
).apply(instance, SnowyBlockTransformation::new));
17-
18-
public SnowyBlockTransformation(RegistryEntry<Block> baseBlock, RegistryEntry<Block> snowyBlock) {
19-
this(RegistryEntryList.of(baseBlock), snowyBlock);
20-
}
2114
}

src/main/java/fr/hugman/promenade/mixin/SnowBlockMixin.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class SnowBlockMixin {
4040
}
4141
var downState = world.getBlockState(downPos.get());
4242
var snowyLeaves = world.getRegistryManager().getOrThrow(PromenadeRegistryKeys.SNOWY_BLOCK_TRANSFORMATION).stream().filter(
43-
entry -> entry.baseBlocks().contains(downState.getBlock().getRegistryEntry())
43+
entry -> entry.baseBlock().matches(downState.getBlock().getRegistryEntry())
4444
).findFirst().map(sbt -> sbt.snowyBlock().value()).orElse(null);
4545

4646
if(snowyLeaves != null) {
@@ -74,7 +74,7 @@ private Optional<BlockPos> findNextBottomSnowyBlock(ServerWorld world, BlockPos
7474

7575
var block = blockState.getBlock();
7676
return world.getRegistryManager().getOrThrow(PromenadeRegistryKeys.SNOWY_BLOCK_TRANSFORMATION).stream().anyMatch(
77-
entry -> entry.baseBlocks().contains(block.getRegistryEntry())
77+
entry -> entry.baseBlock().matches(block.getRegistryEntry())
7878
) ? Optional.of(mutable) : Optional.empty();
7979
}
8080

0 commit comments

Comments
 (0)