9
0
mirror of https://github.com/LeavesMC/Leaves.git synced 2025-12-19 14:59:32 +00:00
Files
LeavesMC/patches/server/0117-Renewable-coral.patch
2024-08-14 17:07:19 +08:00

149 lines
8.0 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 7701683000132479d8c8eaa77ac3f93e41a960be..3bd4308bb2b8cce5091486f5fc57fbc7cb24b202 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 org.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 org.leavesmc.leaves.LeavesConfig.renewableCoral == org.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 4c4aa2a63be84ff614a3dc0db2864266755545a2..208685f28509f92483f1efc89302e7a9ce6838c6 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 org.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 {
protected VoxelShape getShape(BlockState state, BlockGetter world, BlockPos pos, CollisionContext context) {
return CoralPlantBlock.SHAPE;
}
+
+ // Leaves start - renewable coral
+ @Override
+ public boolean isEnabled() {
+ return org.leavesmc.leaves.LeavesConfig.renewableCoral == org.leavesmc.leaves.LeavesConfig.RenewableCoralType.EXPANDED
+ || org.leavesmc.leaves.LeavesConfig.renewableCoral == org.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 7bc5ff8eb1174834dcc27363af4a5cef19017b3d..790686428b01127d0f94e044f0ec4bff4127f79e 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
@@ -31,7 +31,7 @@ public abstract class CoralFeature extends Feature<NoneFeatureConfiguration> {
return !optional.isEmpty() && this.placeFeature(worldGenLevel, randomSource, blockPos, optional.get().defaultBlockState());
}
- protected abstract boolean placeFeature(LevelAccessor world, RandomSource random, BlockPos pos, BlockState state);
+ public abstract boolean placeFeature(LevelAccessor world, RandomSource random, BlockPos pos, BlockState state); // Leaves - protected -> public
protected boolean placeCoralBlock(LevelAccessor world, RandomSource random, BlockPos pos, BlockState state) {
BlockPos blockPos = pos.above();
diff --git a/src/main/java/org/leavesmc/leaves/util/FertilizableCoral.java b/src/main/java/org/leavesmc/leaves/util/FertilizableCoral.java
new file mode 100644
index 0000000000000000000000000000000000000000..f1cad30bd4c937b137305e790e1d372a123170a6
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/util/FertilizableCoral.java
@@ -0,0 +1,72 @@
+package org.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.placeFeature(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);
+ }
+ }
+ }
+ }
+}