From 0a2625a2a22ae3b461121db11c346cb5c7839925 Mon Sep 17 00:00:00 2001 From: FaeWulfs Date: Sun, 10 Nov 2024 09:36:41 +0700 Subject: [PATCH] + Add: Add custom BonemealItem interface for better compatibility and qol. --- .../diversity/event/useBonemealOnCoral.java | 140 ------------------ .../event/useBonemealOnSmallFlower.java | 108 -------------- .../diversity/inter/ICustomBonemealable.java | 18 +++ .../bonemealCoral/BoneMealItemMixin.java | 131 ---------------- .../mixin/bonemealCoral/CoralFanMixin.java | 99 +++++++++++++ .../mixin/bonemealCoral/CoralMixin.java | 99 +++++++++++++ .../bonemealSmallFlower/FlowerBlockMixin.java | 66 +++++++++ .../CustomBonemealable/BonemealItemMixin.java | 47 ++++++ .../src/main/resources/diversity.mixins.json | 5 +- .../java/xyz/faewulf/diversity/Diversity.java | 2 - .../event_handler/useBonemealOnCoral.java | 9 -- .../useBonemealOnSmallFlower.java | 9 -- .../event_handler/useBonemealOnCoral.java | 18 --- .../useBonemealOnSmallFlower.java | 18 --- 14 files changed, 333 insertions(+), 436 deletions(-) delete mode 100644 common/src/main/java/xyz/faewulf/diversity/event/useBonemealOnSmallFlower.java create mode 100644 common/src/main/java/xyz/faewulf/diversity/inter/ICustomBonemealable.java delete mode 100644 common/src/main/java/xyz/faewulf/diversity/mixin/bonemealCoral/BoneMealItemMixin.java create mode 100644 common/src/main/java/xyz/faewulf/diversity/mixin/bonemealCoral/CoralFanMixin.java create mode 100644 common/src/main/java/xyz/faewulf/diversity/mixin/bonemealCoral/CoralMixin.java create mode 100644 common/src/main/java/xyz/faewulf/diversity/mixin/bonemealSmallFlower/FlowerBlockMixin.java create mode 100644 common/src/main/java/xyz/faewulf/diversity/mixin/core/CustomBonemealable/BonemealItemMixin.java delete mode 100644 fabric/src/main/java/xyz/faewulf/diversity/event_handler/useBonemealOnCoral.java delete mode 100644 fabric/src/main/java/xyz/faewulf/diversity/event_handler/useBonemealOnSmallFlower.java delete mode 100644 neoforge/src/main/java/xyz/faewulf/diversity/event_handler/useBonemealOnCoral.java delete mode 100644 neoforge/src/main/java/xyz/faewulf/diversity/event_handler/useBonemealOnSmallFlower.java diff --git a/common/src/main/java/xyz/faewulf/diversity/event/useBonemealOnCoral.java b/common/src/main/java/xyz/faewulf/diversity/event/useBonemealOnCoral.java index 414531e..e69de29 100644 --- a/common/src/main/java/xyz/faewulf/diversity/event/useBonemealOnCoral.java +++ b/common/src/main/java/xyz/faewulf/diversity/event/useBonemealOnCoral.java @@ -1,140 +0,0 @@ -package xyz.faewulf.diversity.event; - -import net.minecraft.core.BlockPos; -import net.minecraft.core.Holder; -import net.minecraft.core.HolderSet; -import net.minecraft.core.particles.ParticleTypes; -import net.minecraft.core.registries.Registries; -import net.minecraft.server.level.ServerLevel; -import net.minecraft.sounds.SoundEvents; -import net.minecraft.sounds.SoundSource; -import net.minecraft.tags.BiomeTags; -import net.minecraft.tags.BlockTags; -import net.minecraft.tags.FluidTags; -import net.minecraft.world.InteractionHand; -import net.minecraft.world.InteractionResult; -import net.minecraft.world.entity.player.Player; -import net.minecraft.world.item.Item; -import net.minecraft.world.item.Items; -import net.minecraft.world.level.Level; -import net.minecraft.world.level.biome.Biome; -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.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 net.minecraft.world.phys.BlockHitResult; -import xyz.faewulf.diversity.mixin.bonemealCoral.CoralFeatureInvoker; -import xyz.faewulf.diversity.util.compare; -import xyz.faewulf.diversity.util.config.ModConfigs; - -import java.util.Optional; - -import static net.minecraft.world.level.block.Block.UPDATE_ALL; - -public class useBonemealOnCoral { - public static InteractionResult run(Level world, Player player, InteractionHand hand, BlockHitResult hitResult) { - - if (!ModConfigs.bonemeal_coral_fan) - return InteractionResult.PASS; - - if (world.isClientSide) - return InteractionResult.PASS; - - - Item item = player.getItemInHand(hand).getItem(); - - if (item == Items.BONE_MEAL) { - - BlockState block = world.getBlockState(hitResult.getBlockPos()); - BlockPos pos = hitResult.getBlockPos(); - - Holder currentBiome = world.getBiome(pos); - - //check valid criteria - if (block.is(BlockTags.CORALS) - && !compare.isHasTag(block.getBlock(), "diversity:bonemeal_blacklist") - && block.getValue(BaseCoralPlantTypeBlock.WATERLOGGED) - && world.getFluidState(pos.above()).is(FluidTags.WATER) - && currentBiome.is(BiomeTags.PRODUCES_CORALS_FROM_BONEMEAL) - ) { - - if (world.random.nextFloat() >= 0.15D) { - player.swing(hand, true); - - if (world instanceof ServerLevel serverLevel) { - serverLevel.playSound(null, pos, SoundEvents.BONE_MEAL_USE, SoundSource.PLAYERS, 1.0F, (1.0F + world.getRandom().nextFloat() * 0.2F) * 0.7F); - serverLevel.sendParticles(ParticleTypes.HAPPY_VILLAGER, // Bonemeal-like particles - pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, // Position (above the block) - 7, // Number of particles - 0.3, 0.3, 0.3, // Particle spread on X, Y, Z axes - 0.1 // Particle speed - ); - } - - return InteractionResult.PASS; - } - - - //perform bonmeal action - - CoralFeature coral = switch (world.random.nextInt(3)) { - case 0 -> new CoralTreeFeature(NoneFeatureConfiguration.CODEC); - case 1 -> new CoralClawFeature(NoneFeatureConfiguration.CODEC); - default -> new CoralMushroomFeature(NoneFeatureConfiguration.CODEC); - }; - - //get corresponding block based on the fan, use color map... - MapColor color = block.getMapColor(world, pos); - BlockState coralBlockMatchedColor = block; - //get all coral blocks - Optional> coralBlocksRegistry = world.registryAccess().registryOrThrow(Registries.BLOCK).getTag(BlockTags.CORAL_BLOCKS); - HolderSet.Named coralBlocks; - - //extra check - if (coralBlocksRegistry.isPresent()) - coralBlocks = coralBlocksRegistry.get(); - else - return InteractionResult.PASS; - - //then find using color - for (Holder coral_block : coralBlocks) { - coralBlockMatchedColor = coral_block.value().defaultBlockState(); - if (coralBlockMatchedColor.getMapColor(world, pos) == color) { - break; - } - } - - world.setBlock(pos, Blocks.WATER.defaultBlockState(), Block.UPDATE_NONE); - - if (!((CoralFeatureInvoker) coral).generateCoral(world, world.random, pos, coralBlockMatchedColor)) { - world.setBlock(pos, block, UPDATE_ALL); - } - - - //handle perform action/effect - player.swing(hand, true); - - if (world instanceof ServerLevel serverLevel) { - serverLevel.playSound(null, pos, SoundEvents.BONE_MEAL_USE, SoundSource.PLAYERS, 1.0F, (1.0F + world.getRandom().nextFloat() * 0.2F) * 0.7F); - serverLevel.sendParticles(ParticleTypes.HAPPY_VILLAGER, // Bonemeal-like particles - pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, // Position (above the block) - 7, // Number of particles - 0.3, 0.3, 0.3, // Particle spread on X, Y, Z axes - 0.1 // Particle speed - ); - } - - player.getItemInHand(hand).consume(1, player); - return InteractionResult.CONSUME; - } - } - - return InteractionResult.PASS; - } -} diff --git a/common/src/main/java/xyz/faewulf/diversity/event/useBonemealOnSmallFlower.java b/common/src/main/java/xyz/faewulf/diversity/event/useBonemealOnSmallFlower.java deleted file mode 100644 index e6a8e12..0000000 --- a/common/src/main/java/xyz/faewulf/diversity/event/useBonemealOnSmallFlower.java +++ /dev/null @@ -1,108 +0,0 @@ -package xyz.faewulf.diversity.event; - -import net.minecraft.core.BlockPos; -import net.minecraft.core.particles.ParticleTypes; -import net.minecraft.server.level.ServerLevel; -import net.minecraft.sounds.SoundEvents; -import net.minecraft.sounds.SoundSource; -import net.minecraft.tags.BlockTags; -import net.minecraft.world.InteractionHand; -import net.minecraft.world.InteractionResult; -import net.minecraft.world.entity.player.Player; -import net.minecraft.world.item.Item; -import net.minecraft.world.item.Items; -import net.minecraft.world.level.Level; -import net.minecraft.world.level.block.Block; -import net.minecraft.world.level.block.Blocks; -import net.minecraft.world.level.block.state.BlockState; -import net.minecraft.world.phys.BlockHitResult; -import xyz.faewulf.diversity.util.compare; -import xyz.faewulf.diversity.util.config.ModConfigs; - -public class useBonemealOnSmallFlower { - public static InteractionResult run(Level world, Player player, InteractionHand hand, BlockHitResult hitResult) { - - if (!ModConfigs.bonemeal_small_flower) - return InteractionResult.PASS; - - if (world.isClientSide) - return InteractionResult.PASS; - - Item item = player.getItemInHand(hand).getItem(); - - if (item == Items.BONE_MEAL) { - - BlockState block = world.getBlockState(hitResult.getBlockPos()); - BlockPos pos = hitResult.getBlockPos(); - - //check valid tag - if (block.is(BlockTags.SMALL_FLOWERS) - && !compare.isHasTag(block.getBlock(), "diversity:bonemeal_blacklist") - && compare.isHasTag(world.getBlockState(pos.below()).getBlock(), "diversity:rich_soil") - ) { - - //chance to fail bonemeal - if (world.random.nextFloat() >= 0.5D) { - player.swing(hand, true); - - if (world instanceof ServerLevel serverLevel) { - serverLevel.playSound(null, pos, SoundEvents.BONE_MEAL_USE, SoundSource.PLAYERS, 1.0F, (1.0F + world.getRandom().nextFloat() * 0.2F) * 0.7F); - serverLevel.sendParticles(ParticleTypes.HAPPY_VILLAGER, // Bonemeal-like particles - pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, // Position (above the block) - 7, // Number of particles - 0.3, 0.3, 0.3, // Particle spread on X, Y, Z axes - 0.1 // Particle speed - ); - } - - return InteractionResult.PASS; - } - - //perform bonmeal action - int j = 1; - int l = 0; - int m = pos.getX() - 1; - int n = 0; - - for (int o = 0; o < 3; ++o) { - for (int p = 0; p < j; ++p) { - int q = 2 + pos.getY() - 1; - - for (int r = q - 2; r < q; ++r) { - BlockPos blockPos = new BlockPos(m + o, r, pos.getZ() - n + p); - if (!blockPos.equals(pos) && world.random.nextInt(12) == 0 && world.getBlockState(blockPos).is(Blocks.AIR)) { - BlockState blockState = world.getBlockState(blockPos.below()); - if (compare.isHasTag(blockState.getBlock(), "diversity:rich_soil")) { - world.setBlock(blockPos, block, Block.UPDATE_ALL); - } - } - } - } - if (l < 2) { - j += 2; - ++n; - } else { - j -= 2; - --n; - } - ++l; - } - - player.swing(hand, true); - if (world instanceof ServerLevel serverLevel) { - serverLevel.sendParticles(ParticleTypes.HAPPY_VILLAGER, // Bonemeal-like particles - pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, // Position (above the block) - 7, // Number of particles - 0.3, 0.3, 0.3, // Particle spread on X, Y, Z axes - 0.1 // Particle speed - ); - serverLevel.playSound(null, pos, SoundEvents.BONE_MEAL_USE, SoundSource.PLAYERS, 1.0F, (1.0F + world.getRandom().nextFloat() * 0.2F) * 0.7F); - } - player.getItemInHand(hand).consume(1, player); - return InteractionResult.CONSUME; - } - } - - return InteractionResult.PASS; - } -} diff --git a/common/src/main/java/xyz/faewulf/diversity/inter/ICustomBonemealable.java b/common/src/main/java/xyz/faewulf/diversity/inter/ICustomBonemealable.java new file mode 100644 index 0000000..b6f5906 --- /dev/null +++ b/common/src/main/java/xyz/faewulf/diversity/inter/ICustomBonemealable.java @@ -0,0 +1,18 @@ +package xyz.faewulf.diversity.inter; + +import net.minecraft.core.BlockPos; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.util.RandomSource; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.LevelReader; +import net.minecraft.world.level.block.state.BlockState; + +public interface ICustomBonemealable { + default boolean Diversity$isValidBonemealTarget(LevelReader level, BlockPos pos, BlockState state) { + return true; + } + + boolean Diversity$isBonemealSuccess(Level level, RandomSource random, BlockPos pos, BlockState state); + + void Diversity$performBonemeal(ServerLevel level, RandomSource random, BlockPos pos, BlockState state); +} diff --git a/common/src/main/java/xyz/faewulf/diversity/mixin/bonemealCoral/BoneMealItemMixin.java b/common/src/main/java/xyz/faewulf/diversity/mixin/bonemealCoral/BoneMealItemMixin.java deleted file mode 100644 index c76ca7f..0000000 --- a/common/src/main/java/xyz/faewulf/diversity/mixin/bonemealCoral/BoneMealItemMixin.java +++ /dev/null @@ -1,131 +0,0 @@ -package xyz.faewulf.diversity.mixin.bonemealCoral; - -import net.minecraft.core.BlockPos; -import net.minecraft.core.Holder; -import net.minecraft.core.HolderSet; -import net.minecraft.core.particles.ParticleTypes; -import net.minecraft.core.registries.Registries; -import net.minecraft.server.level.ServerLevel; -import net.minecraft.sounds.SoundEvents; -import net.minecraft.sounds.SoundSource; -import net.minecraft.tags.BiomeTags; -import net.minecraft.tags.BlockTags; -import net.minecraft.tags.FluidTags; -import net.minecraft.world.InteractionResult; -import net.minecraft.world.item.BoneMealItem; -import net.minecraft.world.item.ItemStack; -import net.minecraft.world.level.Level; -import net.minecraft.world.level.biome.Biome; -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.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.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; -import xyz.faewulf.diversity.util.compare; -import xyz.faewulf.diversity.util.config.ModConfigs; - -import java.util.Optional; - -import static net.minecraft.world.level.block.Block.UPDATE_ALL; - -@Mixin(BoneMealItem.class) -public class BoneMealItemMixin { - - @Inject(method = "growCrop", at = @At(value = "HEAD"), cancellable = true) - private static void growPlantInject(ItemStack stack, Level level, BlockPos pos, CallbackInfoReturnable cir) { - - if (!ModConfigs.bonemeal_coral_fan) - return; - - BlockState block = level.getBlockState(pos); - Holder currentBiome = level.getBiome(pos); - - if (block.is(BlockTags.CORALS) - && !compare.isHasTag(block.getBlock(), "diversity:bonemeal_blacklist") - && block.getValue(BaseCoralPlantTypeBlock.WATERLOGGED) - && level.getFluidState(pos.above()).is(FluidTags.WATER) - && currentBiome.is(BiomeTags.PRODUCES_CORALS_FROM_BONEMEAL) - ) { - if (level.random.nextFloat() >= 0.15D) { - - if (level instanceof ServerLevel serverLevel) { - serverLevel.playSound(null, pos, SoundEvents.BONE_MEAL_USE, SoundSource.BLOCKS, 1.0F, (1.0F + level.getRandom().nextFloat() * 0.2F) * 0.7F); - serverLevel.sendParticles(ParticleTypes.HAPPY_VILLAGER, // Bonemeal-like particles - pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, // Position (above the block) - 7, // Number of particles - 0.3, 0.3, 0.3, // Particle spread on X, Y, Z axes - 0.1 // Particle speed - ); - } - - stack.shrink(1); - cir.setReturnValue(true); - cir.cancel(); - return; - } - - - //perform bonmeal action - - CoralFeature coral = switch (level.random.nextInt(3)) { - case 0 -> new CoralTreeFeature(NoneFeatureConfiguration.CODEC); - case 1 -> new CoralClawFeature(NoneFeatureConfiguration.CODEC); - default -> new CoralMushroomFeature(NoneFeatureConfiguration.CODEC); - }; - - //get corresponding block based on the fan, use color map... - MapColor color = block.getMapColor(level, pos); - BlockState coralBlockMatchedColor = block; - //get all coral blocks - Optional> coralBlocksRegistry = level.registryAccess().lookupOrThrow(Registries.BLOCK).get(BlockTags.CORAL_BLOCKS); - HolderSet.Named coralBlocks; - - //extra check - if (coralBlocksRegistry.isPresent()) - coralBlocks = coralBlocksRegistry.get(); - else { - cir.setReturnValue(false); - cir.cancel(); - return; - } - - //then find using color - for (Holder coral_block : coralBlocks) { - coralBlockMatchedColor = coral_block.value().defaultBlockState(); - if (coralBlockMatchedColor.getMapColor(level, pos) == color) { - break; - } - } - - level.setBlock(pos, Blocks.WATER.defaultBlockState(), Block.UPDATE_NONE); - - if (!((CoralFeatureInvoker) coral).generateCoral(level, level.random, pos, coralBlockMatchedColor)) { - level.setBlock(pos, block, UPDATE_ALL); - } - - - if (level instanceof ServerLevel serverLevel) { - serverLevel.playSound(null, pos, SoundEvents.BONE_MEAL_USE, SoundSource.PLAYERS, 1.0F, (1.0F + serverLevel.getRandom().nextFloat() * 0.2F) * 0.7F); - serverLevel.sendParticles(ParticleTypes.HAPPY_VILLAGER, // Bonemeal-like particles - pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, // Position (above the block) - 7, // Number of particles - 0.3, 0.3, 0.3, // Particle spread on X, Y, Z axes - 0.1 // Particle speed - ); - } - - stack.shrink(1); - cir.setReturnValue(true); - cir.cancel(); - } - } -} diff --git a/common/src/main/java/xyz/faewulf/diversity/mixin/bonemealCoral/CoralFanMixin.java b/common/src/main/java/xyz/faewulf/diversity/mixin/bonemealCoral/CoralFanMixin.java new file mode 100644 index 0000000..003d244 --- /dev/null +++ b/common/src/main/java/xyz/faewulf/diversity/mixin/bonemealCoral/CoralFanMixin.java @@ -0,0 +1,99 @@ +package xyz.faewulf.diversity.mixin.bonemealCoral; + +import net.minecraft.core.BlockPos; +import net.minecraft.core.Holder; +import net.minecraft.core.HolderSet; +import net.minecraft.core.particles.ParticleTypes; +import net.minecraft.core.registries.Registries; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.tags.BiomeTags; +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.biome.Biome; +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.CoralFanBlock; +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.spongepowered.asm.mixin.Mixin; +import xyz.faewulf.diversity.inter.ICustomBonemealable; +import xyz.faewulf.diversity.util.compare; + +import java.util.Optional; + +import static net.minecraft.world.level.block.Block.UPDATE_ALL; + +@Mixin(CoralFanBlock.class) +public class CoralFanMixin implements ICustomBonemealable { + + @Override + public boolean Diversity$isValidBonemealTarget(LevelReader level, BlockPos pos, BlockState state) { + Holder currentBiome = level.getBiome(pos); + return + !compare.isHasTag(state.getBlock(), "diversity:bonemeal_blacklist") + && state.getValue(BaseCoralPlantTypeBlock.WATERLOGGED) + && level.getFluidState(pos.above()).is(FluidTags.WATER) + && currentBiome.is(BiomeTags.PRODUCES_CORALS_FROM_BONEMEAL); + } + + @Override + public boolean Diversity$isBonemealSuccess(Level level, RandomSource random, BlockPos pos, BlockState state) { + return level.random.nextFloat() < 0.12D; + } + + @Override + public void Diversity$performBonemeal(ServerLevel level, RandomSource random, BlockPos pos, BlockState state) { + CoralFeature coral = switch (level.random.nextInt(3)) { + case 0 -> new CoralTreeFeature(NoneFeatureConfiguration.CODEC); + case 1 -> new CoralClawFeature(NoneFeatureConfiguration.CODEC); + default -> new CoralMushroomFeature(NoneFeatureConfiguration.CODEC); + }; + + //get corresponding block based on the fan, use color map... + MapColor color = state.getMapColor(level, pos); + BlockState coralBlockMatchedColor = state; + //get all coral blocks + Optional> coralBlocksRegistry = level.registryAccess().lookupOrThrow(Registries.BLOCK).get(BlockTags.CORAL_BLOCKS); + HolderSet.Named coralBlocks; + + //extra check + if (coralBlocksRegistry.isPresent()) + coralBlocks = coralBlocksRegistry.get(); + else { + return; + } + + //then find using color + for (Holder coral_block : coralBlocks) { + coralBlockMatchedColor = coral_block.value().defaultBlockState(); + if (coralBlockMatchedColor.getMapColor(level, pos) == color) { + break; + } + } + + level.setBlock(pos, Blocks.WATER.defaultBlockState(), Block.UPDATE_NONE); + + if (!((CoralFeatureInvoker) coral).generateCoral(level, level.random, pos, coralBlockMatchedColor)) { + level.setBlock(pos, state, UPDATE_ALL); + } + + + if (level instanceof ServerLevel serverLevel) { + serverLevel.sendParticles(ParticleTypes.HAPPY_VILLAGER, // Bonemeal-like particles + pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, // Position (above the block) + 7, // Number of particles + 0.3, 0.3, 0.3, // Particle spread on X, Y, Z axes + 0.1 // Particle speed + ); + } + } +} diff --git a/common/src/main/java/xyz/faewulf/diversity/mixin/bonemealCoral/CoralMixin.java b/common/src/main/java/xyz/faewulf/diversity/mixin/bonemealCoral/CoralMixin.java new file mode 100644 index 0000000..a0fab2c --- /dev/null +++ b/common/src/main/java/xyz/faewulf/diversity/mixin/bonemealCoral/CoralMixin.java @@ -0,0 +1,99 @@ +package xyz.faewulf.diversity.mixin.bonemealCoral; + +import net.minecraft.core.BlockPos; +import net.minecraft.core.Holder; +import net.minecraft.core.HolderSet; +import net.minecraft.core.particles.ParticleTypes; +import net.minecraft.core.registries.Registries; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.tags.BiomeTags; +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.biome.Biome; +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.CoralPlantBlock; +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.spongepowered.asm.mixin.Mixin; +import xyz.faewulf.diversity.inter.ICustomBonemealable; +import xyz.faewulf.diversity.util.compare; + +import java.util.Optional; + +import static net.minecraft.world.level.block.Block.UPDATE_ALL; + +@Mixin(CoralPlantBlock.class) +public class CoralMixin implements ICustomBonemealable { + + @Override + public boolean Diversity$isValidBonemealTarget(LevelReader level, BlockPos pos, BlockState state) { + Holder currentBiome = level.getBiome(pos); + return + !compare.isHasTag(state.getBlock(), "diversity:bonemeal_blacklist") + && state.getValue(BaseCoralPlantTypeBlock.WATERLOGGED) + && level.getFluidState(pos.above()).is(FluidTags.WATER) + && currentBiome.is(BiomeTags.PRODUCES_CORALS_FROM_BONEMEAL); + } + + @Override + public boolean Diversity$isBonemealSuccess(Level level, RandomSource random, BlockPos pos, BlockState state) { + return level.random.nextFloat() < 0.12D; + } + + @Override + public void Diversity$performBonemeal(ServerLevel level, RandomSource random, BlockPos pos, BlockState state) { + CoralFeature coral = switch (level.random.nextInt(3)) { + case 0 -> new CoralTreeFeature(NoneFeatureConfiguration.CODEC); + case 1 -> new CoralClawFeature(NoneFeatureConfiguration.CODEC); + default -> new CoralMushroomFeature(NoneFeatureConfiguration.CODEC); + }; + + //get corresponding block based on the fan, use color map... + MapColor color = state.getMapColor(level, pos); + BlockState coralBlockMatchedColor = state; + //get all coral blocks + Optional> coralBlocksRegistry = level.registryAccess().lookupOrThrow(Registries.BLOCK).get(BlockTags.CORAL_BLOCKS); + HolderSet.Named coralBlocks; + + //extra check + if (coralBlocksRegistry.isPresent()) + coralBlocks = coralBlocksRegistry.get(); + else { + return; + } + + //then find using color + for (Holder coral_block : coralBlocks) { + coralBlockMatchedColor = coral_block.value().defaultBlockState(); + if (coralBlockMatchedColor.getMapColor(level, pos) == color) { + break; + } + } + + level.setBlock(pos, Blocks.WATER.defaultBlockState(), Block.UPDATE_NONE); + + if (!((CoralFeatureInvoker) coral).generateCoral(level, level.random, pos, coralBlockMatchedColor)) { + level.setBlock(pos, state, UPDATE_ALL); + } + + if (level instanceof ServerLevel serverLevel) { + serverLevel.sendParticles(ParticleTypes.HAPPY_VILLAGER, // Bonemeal-like particles + pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, // Position (above the block) + 7, // Number of particles + 0.3, 0.3, 0.3, // Particle spread on X, Y, Z axes + 0.1 // Particle speed + ); + } + } + +} diff --git a/common/src/main/java/xyz/faewulf/diversity/mixin/bonemealSmallFlower/FlowerBlockMixin.java b/common/src/main/java/xyz/faewulf/diversity/mixin/bonemealSmallFlower/FlowerBlockMixin.java new file mode 100644 index 0000000..c224522 --- /dev/null +++ b/common/src/main/java/xyz/faewulf/diversity/mixin/bonemealSmallFlower/FlowerBlockMixin.java @@ -0,0 +1,66 @@ +package xyz.faewulf.diversity.mixin.bonemealSmallFlower; + +import net.minecraft.core.BlockPos; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.tags.BlockTags; +import net.minecraft.util.RandomSource; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.LevelReader; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.Blocks; +import net.minecraft.world.level.block.FlowerBlock; +import net.minecraft.world.level.block.state.BlockState; +import org.spongepowered.asm.mixin.Mixin; +import xyz.faewulf.diversity.inter.ICustomBonemealable; +import xyz.faewulf.diversity.util.compare; + +@Mixin(FlowerBlock.class) +public class FlowerBlockMixin implements ICustomBonemealable { + @Override + public boolean Diversity$isValidBonemealTarget(LevelReader level, BlockPos pos, BlockState state) { + //small flower + //not in blacklist tag + //standing on block with rich_soil tag + return state.is(BlockTags.SMALL_FLOWERS) + && !compare.isHasTag(state.getBlock(), "diversity:bonemeal_blacklist") + && compare.isHasTag(level.getBlockState(pos.below()).getBlock(), "diversity:rich_soil"); + } + + @Override + public boolean Diversity$isBonemealSuccess(Level level, RandomSource random, BlockPos pos, BlockState state) { + return random.nextFloat() < 0.5D; + } + + @Override + public void Diversity$performBonemeal(ServerLevel level, RandomSource random, BlockPos pos, BlockState state) { + + int j = 1; + int l = 0; + int m = pos.getX() - 1; + int n = 0; + + for (int o = 0; o < 3; ++o) { + for (int p = 0; p < j; ++p) { + int q = 2 + pos.getY() - 1; + + for (int r = q - 2; r < q; ++r) { + BlockPos blockPos = new BlockPos(m + o, r, pos.getZ() - n + p); + if (!blockPos.equals(pos) && random.nextInt(12) == 0 && level.getBlockState(blockPos).is(Blocks.AIR)) { + BlockState blockState = level.getBlockState(blockPos.below()); + if (compare.isHasTag(blockState.getBlock(), "diversity:rich_soil")) { + level.setBlock(blockPos, state, Block.UPDATE_ALL); + } + } + } + } + if (l < 2) { + j += 2; + ++n; + } else { + j -= 2; + --n; + } + ++l; + } + } +} diff --git a/common/src/main/java/xyz/faewulf/diversity/mixin/core/CustomBonemealable/BonemealItemMixin.java b/common/src/main/java/xyz/faewulf/diversity/mixin/core/CustomBonemealable/BonemealItemMixin.java new file mode 100644 index 0000000..9723e8e --- /dev/null +++ b/common/src/main/java/xyz/faewulf/diversity/mixin/core/CustomBonemealable/BonemealItemMixin.java @@ -0,0 +1,47 @@ +package xyz.faewulf.diversity.mixin.core.CustomBonemealable; + +import net.minecraft.core.BlockPos; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.world.InteractionResult; +import net.minecraft.world.item.BoneMealItem; +import net.minecraft.world.item.Item; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.context.UseOnContext; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.state.BlockState; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; +import xyz.faewulf.diversity.inter.ICustomBonemealable; + +@Mixin(BoneMealItem.class) +public abstract class BonemealItemMixin extends Item { + public BonemealItemMixin(Properties properties) { + super(properties); + } + + @Inject(method = "growCrop", at = @At(value = "HEAD"), cancellable = true) + private static void growCropInject(ItemStack stack, Level level, BlockPos pos, CallbackInfoReturnable cir) { + BlockState blockstate = level.getBlockState(pos); + if (blockstate.getBlock() instanceof ICustomBonemealable iCustomBonemealable && iCustomBonemealable.Diversity$isValidBonemealTarget(level, pos, blockstate)) { + if (level instanceof ServerLevel) { + if (iCustomBonemealable.Diversity$isBonemealSuccess(level, level.random, pos, blockstate)) { + iCustomBonemealable.Diversity$performBonemeal((ServerLevel) level, level.random, pos, blockstate); + } + + stack.shrink(1); + } + + cir.setReturnValue(true); + cir.cancel(); + } + } + + //server side swing + @Inject(method = "useOn", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/item/context/UseOnContext;getPlayer()Lnet/minecraft/world/entity/player/Player;", ordinal = 0)) + private void useOnInject(UseOnContext context, CallbackInfoReturnable cir) { + if (context.getPlayer() != null) + context.getPlayer().swing(context.getHand(), true); + } +} diff --git a/common/src/main/resources/diversity.mixins.json b/common/src/main/resources/diversity.mixins.json index e39b7be..3d14efb 100644 --- a/common/src/main/resources/diversity.mixins.json +++ b/common/src/main/resources/diversity.mixins.json @@ -21,9 +21,11 @@ "bannerTrophy.GiveGiftsToHeroTaskMixin", "bannerTrophy.WardenSuperClassMixin", "bannerTrophy.WitherEntityMixin", - "bonemealCoral.BoneMealItemMixin", + "bonemealCoral.CoralFanMixin", "bonemealCoral.CoralFeatureInvoker", + "bonemealCoral.CoralMixin", "bonemealSmallFlower.BoneMealItemMixin", + "bonemealSmallFlower.FlowerBlockMixin", "buildingBundle.BlockItemInvoker", "buildingBundle.BlockItemMixin", "buildingBundle.BuilderMixin", @@ -33,6 +35,7 @@ "clickThrough.ItemFrame", "clickThrough.WallSign", "clockHUD.ItemMixin", + "core.CustomBonemealable.BonemealItemMixin", "core.invoker.ItemEntityInvoker", "core.PseudoBlockEntity.ServerEntityMixin", "core.PseudoBlockEntity.TextDisplayMixin", diff --git a/fabric/src/main/java/xyz/faewulf/diversity/Diversity.java b/fabric/src/main/java/xyz/faewulf/diversity/Diversity.java index 0af5d7e..4b5b6e3 100644 --- a/fabric/src/main/java/xyz/faewulf/diversity/Diversity.java +++ b/fabric/src/main/java/xyz/faewulf/diversity/Diversity.java @@ -42,8 +42,6 @@ private void loadEvent() { putItemIntoBrushableBlocks.register(); useClockOnBlock.register(); useShearOnBlock.register(); - useBonemealOnSmallFlower.register(); - useBonemealOnCoral.register(); }); } diff --git a/fabric/src/main/java/xyz/faewulf/diversity/event_handler/useBonemealOnCoral.java b/fabric/src/main/java/xyz/faewulf/diversity/event_handler/useBonemealOnCoral.java deleted file mode 100644 index 6c383a1..0000000 --- a/fabric/src/main/java/xyz/faewulf/diversity/event_handler/useBonemealOnCoral.java +++ /dev/null @@ -1,9 +0,0 @@ -package xyz.faewulf.diversity.event_handler; - -import net.fabricmc.fabric.api.event.player.UseBlockCallback; - -public class useBonemealOnCoral { - public static void register() { - UseBlockCallback.EVENT.register((player, world, hand, hitResult) -> xyz.faewulf.diversity.event.useBonemealOnCoral.run(world, player, hand, hitResult)); - } -} diff --git a/fabric/src/main/java/xyz/faewulf/diversity/event_handler/useBonemealOnSmallFlower.java b/fabric/src/main/java/xyz/faewulf/diversity/event_handler/useBonemealOnSmallFlower.java deleted file mode 100644 index 37d006c..0000000 --- a/fabric/src/main/java/xyz/faewulf/diversity/event_handler/useBonemealOnSmallFlower.java +++ /dev/null @@ -1,9 +0,0 @@ -package xyz.faewulf.diversity.event_handler; - -import net.fabricmc.fabric.api.event.player.UseBlockCallback; - -public class useBonemealOnSmallFlower { - public static void register() { - UseBlockCallback.EVENT.register((player, world, hand, hitResult) -> xyz.faewulf.diversity.event.useBonemealOnSmallFlower.run(world, player, hand, hitResult)); - } -} diff --git a/neoforge/src/main/java/xyz/faewulf/diversity/event_handler/useBonemealOnCoral.java b/neoforge/src/main/java/xyz/faewulf/diversity/event_handler/useBonemealOnCoral.java deleted file mode 100644 index 8ecd73b..0000000 --- a/neoforge/src/main/java/xyz/faewulf/diversity/event_handler/useBonemealOnCoral.java +++ /dev/null @@ -1,18 +0,0 @@ -package xyz.faewulf.diversity.event_handler; - -import net.minecraft.world.InteractionResult; -import net.neoforged.bus.api.SubscribeEvent; -import net.neoforged.fml.common.EventBusSubscriber; -import net.neoforged.neoforge.event.entity.player.PlayerInteractEvent; -import xyz.faewulf.diversity.Constants; - -@EventBusSubscriber(modid = Constants.MOD_ID) -public class useBonemealOnCoral { - @SubscribeEvent - public static void onUseItemOnBlock(PlayerInteractEvent.RightClickBlock event) { - InteractionResult interactionResult = xyz.faewulf.diversity.event.useBonemealOnCoral.run(event.getLevel(), event.getEntity(), event.getHand(), event.getHitVec()); - - if (interactionResult.consumesAction()) - event.setCanceled(true); - } -} \ No newline at end of file diff --git a/neoforge/src/main/java/xyz/faewulf/diversity/event_handler/useBonemealOnSmallFlower.java b/neoforge/src/main/java/xyz/faewulf/diversity/event_handler/useBonemealOnSmallFlower.java deleted file mode 100644 index 6c5cb20..0000000 --- a/neoforge/src/main/java/xyz/faewulf/diversity/event_handler/useBonemealOnSmallFlower.java +++ /dev/null @@ -1,18 +0,0 @@ -package xyz.faewulf.diversity.event_handler; - -import net.minecraft.world.InteractionResult; -import net.neoforged.bus.api.SubscribeEvent; -import net.neoforged.fml.common.EventBusSubscriber; -import net.neoforged.neoforge.event.entity.player.PlayerInteractEvent; -import xyz.faewulf.diversity.Constants; - -@EventBusSubscriber(modid = Constants.MOD_ID) -public class useBonemealOnSmallFlower { - @SubscribeEvent - public static void onUseItemOnBlock(PlayerInteractEvent.RightClickBlock event) { - InteractionResult interactionResult = xyz.faewulf.diversity.event.useBonemealOnSmallFlower.run(event.getLevel(), event.getEntity(), event.getHand(), event.getHitVec()); - - if (interactionResult.consumesAction()) - event.setCanceled(true); - } -} \ No newline at end of file