9
0
mirror of https://github.com/LeavesMC/Leaves.git synced 2025-12-23 00:49:23 +00:00
Files
LeavesMC/patches/server/0132-Renewable-coral.patch
2024-01-23 01:29:48 +08:00

170 lines
9.2 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: violetc <58360096+s-yh-china@users.noreply.github.com>
Date: Tue, 23 Jan 2024 01:08:41 +0800
Subject: [PATCH] Renewable coral
diff --git a/src/main/java/net/minecraft/world/level/block/CoralFanBlock.java b/src/main/java/net/minecraft/world/level/block/CoralFanBlock.java
index c13e8bf5d6dbe9af6c7f391eeac976575f75301f..c2c8527fabd5bf683076792d3b86c8a8e4449fe9 100644
--- a/src/main/java/net/minecraft/world/level/block/CoralFanBlock.java
+++ b/src/main/java/net/minecraft/world/level/block/CoralFanBlock.java
@@ -13,7 +13,7 @@ import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.Fluid;
import net.minecraft.world.level.material.Fluids;
-public class CoralFanBlock extends BaseCoralFanBlock {
+public class CoralFanBlock extends BaseCoralFanBlock implements top.leavesmc.leaves.util.FertilizableCoral { // Leaves - renewable coral
public static final MapCodec<CoralFanBlock> CODEC = RecordCodecBuilder.mapCodec((instance) -> {
return instance.group(CoralBlock.DEAD_CORAL_FIELD.forGetter((blockcoralfan) -> {
@@ -63,4 +63,11 @@ public class CoralFanBlock extends BaseCoralFanBlock {
return super.updateShape(state, direction, neighborState, world, pos, neighborPos);
}
}
+
+ // Leaves start - renewable coral
+ @Override
+ public boolean isEnabled() {
+ return top.leavesmc.leaves.LeavesConfig.renewableCoral == top.leavesmc.leaves.LeavesConfig.RenewableCoralType.EXPANDED;
+ }
+ // Leaves end - renewable coral
}
diff --git a/src/main/java/net/minecraft/world/level/block/CoralPlantBlock.java b/src/main/java/net/minecraft/world/level/block/CoralPlantBlock.java
index b461508e9efcea07acc621507b4c27ce45307714..1473aa6fee5342398edb9e2ac57301ff9962a0fe 100644
--- a/src/main/java/net/minecraft/world/level/block/CoralPlantBlock.java
+++ b/src/main/java/net/minecraft/world/level/block/CoralPlantBlock.java
@@ -16,7 +16,7 @@ import net.minecraft.world.level.material.Fluids;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
-public class CoralPlantBlock extends BaseCoralPlantTypeBlock {
+public class CoralPlantBlock extends BaseCoralPlantTypeBlock implements top.leavesmc.leaves.util.FertilizableCoral { // Leaves - renewable coral
public static final MapCodec<CoralPlantBlock> CODEC = RecordCodecBuilder.mapCodec((instance) -> {
return instance.group(CoralBlock.DEAD_CORAL_FIELD.forGetter((blockcoralplant) -> {
@@ -73,4 +73,12 @@ public class CoralPlantBlock extends BaseCoralPlantTypeBlock {
public VoxelShape getShape(BlockState state, BlockGetter world, BlockPos pos, CollisionContext context) {
return CoralPlantBlock.SHAPE;
}
+
+ // Leaves start - renewable coral
+ @Override
+ public boolean isEnabled() {
+ return top.leavesmc.leaves.LeavesConfig.renewableCoral == top.leavesmc.leaves.LeavesConfig.RenewableCoralType.EXPANDED
+ || top.leavesmc.leaves.LeavesConfig.renewableCoral == top.leavesmc.leaves.LeavesConfig.RenewableCoralType.TRUE;
+ }
+ // Leaves end - renewable coral
}
diff --git a/src/main/java/net/minecraft/world/level/levelgen/feature/CoralFeature.java b/src/main/java/net/minecraft/world/level/levelgen/feature/CoralFeature.java
index 4b9fc715e8055e4593faba7514e0827e585d2a56..66a963c4750210ce1d90ad5a1a5926664e8e0436 100644
--- a/src/main/java/net/minecraft/world/level/levelgen/feature/CoralFeature.java
+++ b/src/main/java/net/minecraft/world/level/levelgen/feature/CoralFeature.java
@@ -33,6 +33,12 @@ public abstract class CoralFeature extends Feature<NoneFeatureConfiguration> {
return optional.isEmpty() ? false : this.placeFeature(worldGenLevel, randomSource, blockPos, optional.get().defaultBlockState());
}
+ // Leaves start - renewable coral
+ public boolean growSpecific(LevelAccessor world, RandomSource random, BlockPos pos, BlockState state) {
+ return placeFeature(world, random, pos, state);
+ }
+ // Leaves end - renewable coral
+
protected abstract boolean placeFeature(LevelAccessor world, RandomSource random, BlockPos pos, BlockState state);
protected boolean placeCoralBlock(LevelAccessor world, RandomSource random, BlockPos pos, BlockState state) {
@@ -57,12 +63,12 @@ public abstract class CoralFeature extends Feature<NoneFeatureConfiguration> {
BuiltInRegistries.BLOCK.getTag(BlockTags.WALL_CORALS).flatMap((blocks) -> {
return blocks.getRandomElement(random);
}).map(Holder::value).ifPresent((block) -> {
- BlockState blockState = block.defaultBlockState();
- if (blockState.hasProperty(BaseCoralWallFanBlock.FACING)) {
- blockState = blockState.setValue(BaseCoralWallFanBlock.FACING, direction);
+ BlockState blockState1 = block.defaultBlockState();
+ if (blockState1.hasProperty(BaseCoralWallFanBlock.FACING)) {
+ blockState1 = blockState1.setValue(BaseCoralWallFanBlock.FACING, direction);
}
- world.setBlock(blockPos2, blockState, 2);
+ world.setBlock(blockPos2, blockState1, 2);
});
}
}
diff --git a/src/main/java/top/leavesmc/leaves/util/FertilizableCoral.java b/src/main/java/top/leavesmc/leaves/util/FertilizableCoral.java
new file mode 100644
index 0000000000000000000000000000000000000000..575fdfd268626e89111c04a81a133d69c8adf931
--- /dev/null
+++ b/src/main/java/top/leavesmc/leaves/util/FertilizableCoral.java
@@ -0,0 +1,72 @@
+package top.leavesmc.leaves.util;
+
+import net.minecraft.core.BlockPos;
+import net.minecraft.core.Holder;
+import net.minecraft.core.HolderSet;
+import net.minecraft.core.registries.Registries;
+import net.minecraft.server.level.ServerLevel;
+import net.minecraft.tags.BlockTags;
+import net.minecraft.tags.FluidTags;
+import net.minecraft.util.RandomSource;
+import net.minecraft.world.level.Level;
+import net.minecraft.world.level.LevelReader;
+import net.minecraft.world.level.block.BaseCoralPlantTypeBlock;
+import net.minecraft.world.level.block.Block;
+import net.minecraft.world.level.block.Blocks;
+import net.minecraft.world.level.block.BonemealableBlock;
+import net.minecraft.world.level.block.state.BlockState;
+import net.minecraft.world.level.levelgen.feature.CoralClawFeature;
+import net.minecraft.world.level.levelgen.feature.CoralFeature;
+import net.minecraft.world.level.levelgen.feature.CoralMushroomFeature;
+import net.minecraft.world.level.levelgen.feature.CoralTreeFeature;
+import net.minecraft.world.level.levelgen.feature.configurations.NoneFeatureConfiguration;
+import net.minecraft.world.level.material.MapColor;
+import org.jetbrains.annotations.NotNull;
+
+// Powered by fabric-carpet/src/main/java/carpet/helpers/FertilizableCoral.java
+public interface FertilizableCoral extends BonemealableBlock {
+
+ boolean isEnabled();
+
+ @Override
+ default boolean isValidBonemealTarget(@NotNull LevelReader world, @NotNull BlockPos pos, @NotNull BlockState state) {
+ return isEnabled() && state.getValue(BaseCoralPlantTypeBlock.WATERLOGGED) && world.getFluidState(pos.above()).is(FluidTags.WATER);
+ }
+
+ @Override
+ default boolean isBonemealSuccess(@NotNull Level world, RandomSource random, @NotNull BlockPos pos, @NotNull BlockState state) {
+ return random.nextFloat() < 0.15D;
+ }
+
+ @Override
+ default void performBonemeal(@NotNull ServerLevel worldIn, RandomSource random, @NotNull BlockPos pos, @NotNull BlockState blockUnder) {
+ int variant = random.nextInt(3);
+ CoralFeature coral = switch (variant) {
+ case 0 -> new CoralClawFeature(NoneFeatureConfiguration.CODEC);
+ case 1 -> new CoralTreeFeature(NoneFeatureConfiguration.CODEC);
+ default -> new CoralMushroomFeature(NoneFeatureConfiguration.CODEC);
+ };
+
+ MapColor color = blockUnder.getMapColor(worldIn, pos);
+ BlockState properBlock = blockUnder;
+ HolderSet.Named<Block> coralBlocks = worldIn.registryAccess().registryOrThrow(Registries.BLOCK).getTag(BlockTags.CORAL_BLOCKS).orElseThrow();
+ for (Holder<Block> block : coralBlocks) {
+ properBlock = block.value().defaultBlockState();
+ if (properBlock.getMapColor(worldIn, pos) == color) {
+ break;
+ }
+ }
+ worldIn.setBlock(pos, Blocks.WATER.defaultBlockState(), Block.UPDATE_NONE);
+
+ if (!coral.growSpecific(worldIn, random, pos, properBlock)) {
+ worldIn.setBlock(pos, blockUnder, 3);
+ } else {
+ if (worldIn.random.nextInt(10) == 0) {
+ BlockPos randomPos = pos.offset(worldIn.random.nextInt(16) - 8, worldIn.random.nextInt(8), worldIn.random.nextInt(16) - 8);
+ if (coralBlocks.contains(worldIn.getBlockState(randomPos).getBlockHolder())) {
+ worldIn.setBlock(randomPos, Blocks.WET_SPONGE.defaultBlockState(), Block.UPDATE_ALL);
+ }
+ }
+ }
+ }
+}