From 1efbf6b373ca410c57f2bb88a7d3940e73e4d9c7 Mon Sep 17 00:00:00 2001 From: Hugman Date: Fri, 1 Jul 2022 15:53:17 +0200 Subject: [PATCH] Add BoneMealSpreadable interface - Added BoneMealSpreadable and DynamicSaplingGenerator interfaces --- .../hugman/dawn/api/mixin/BoneMealMixin.java | 48 +++++++++++++++++++ .../api/object/block/BoneMealSpreadable.java | 12 +++++ .../dawn/api/object/block/DawnRootsBlock.java | 22 +++++++++ src/main/resources/dawn_api.mixins.json | 18 +++---- 4 files changed, 89 insertions(+), 11 deletions(-) create mode 100644 src/main/java/com/hugman/dawn/api/mixin/BoneMealMixin.java create mode 100644 src/main/java/com/hugman/dawn/api/object/block/BoneMealSpreadable.java create mode 100644 src/main/java/com/hugman/dawn/api/object/block/DawnRootsBlock.java diff --git a/src/main/java/com/hugman/dawn/api/mixin/BoneMealMixin.java b/src/main/java/com/hugman/dawn/api/mixin/BoneMealMixin.java new file mode 100644 index 0000000..127e9e2 --- /dev/null +++ b/src/main/java/com/hugman/dawn/api/mixin/BoneMealMixin.java @@ -0,0 +1,48 @@ +package com.hugman.dawn.api.mixin; + +import com.hugman.dawn.api.object.block.BoneMealSpreadable; +import net.minecraft.block.Block; +import net.minecraft.item.BoneMealItem; +import net.minecraft.item.ItemStack; +import net.minecraft.item.ItemUsageContext; +import net.minecraft.util.ActionResult; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; +import net.minecraft.world.WorldEvents; +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 java.util.ArrayList; + +@Mixin(BoneMealItem.class) +public class BoneMealMixin +{ + @Inject(method = "useOnBlock", at = @At("HEAD"), cancellable = true) + public void promenade$useOnBlock(ItemUsageContext context, CallbackInfoReturnable cir) { + World world = context.getWorld(); + BlockPos pos = context.getBlockPos(); + ItemStack stack = context.getStack(); + + ArrayList potentialBlocks = new ArrayList<>(); + for(BlockPos pos2 : BlockPos.iterate(pos.add(-1, -1, -1), pos.add(1, 1, 1))) { + Block targetBlock = world.getBlockState(pos2).getBlock(); + if(targetBlock instanceof BoneMealSpreadable spreadable) { + if(spreadable.canSpreadAt(world, pos)) { + potentialBlocks.add(targetBlock); + } + } + } + + if(!potentialBlocks.isEmpty()) { + if (!world.isClient()) { + Block block = potentialBlocks.get(world.random.nextInt(potentialBlocks.size())); + world.setBlockState(pos, block.getDefaultState(), Block.NOTIFY_ALL); + stack.decrement(1); + world.syncWorldEvent(WorldEvents.BONE_MEAL_USED, pos, 0); + } + cir.setReturnValue(ActionResult.success(world.isClient)); + } + } +} diff --git a/src/main/java/com/hugman/dawn/api/object/block/BoneMealSpreadable.java b/src/main/java/com/hugman/dawn/api/object/block/BoneMealSpreadable.java new file mode 100644 index 0000000..16cec1e --- /dev/null +++ b/src/main/java/com/hugman/dawn/api/object/block/BoneMealSpreadable.java @@ -0,0 +1,12 @@ +package com.hugman.dawn.api.object.block; + +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.BlockView; + +/** + * Interface used for blocks that can be spread with bone meal. + */ +public interface BoneMealSpreadable +{ + boolean canSpreadAt(BlockView world, BlockPos pos); +} diff --git a/src/main/java/com/hugman/dawn/api/object/block/DawnRootsBlock.java b/src/main/java/com/hugman/dawn/api/object/block/DawnRootsBlock.java new file mode 100644 index 0000000..4f3830b --- /dev/null +++ b/src/main/java/com/hugman/dawn/api/object/block/DawnRootsBlock.java @@ -0,0 +1,22 @@ +package com.hugman.dawn.api.object.block; + +import net.minecraft.block.BlockState; +import net.minecraft.block.RootsBlock; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.BlockView; + +import java.util.function.Predicate; + +public class DawnRootsBlock extends RootsBlock +{ + private final Predicate canPlantOn; + + public DawnRootsBlock(Settings settings, Predicate canPlantOn) { + super(settings); + this.canPlantOn = canPlantOn; + } + + protected boolean canPlantOnTop(BlockState floor, BlockView world, BlockPos pos) { + return this.canPlantOn.test(floor); + } +} diff --git a/src/main/resources/dawn_api.mixins.json b/src/main/resources/dawn_api.mixins.json index 1913ad9..fed1b69 100644 --- a/src/main/resources/dawn_api.mixins.json +++ b/src/main/resources/dawn_api.mixins.json @@ -1,13 +1,9 @@ { - "required": true, - "package": "com.hugman.dawn.api.mixin", - "compatibilityLevel": "JAVA_17", - "mixins": [ - "DefaultedRegistryMixin", - "HungerManagerMixin", - "ItemAccessor" - ], - "injectors": { - "defaultRequire": 1 - } + "required": true, + "package": "com.hugman.dawn.api.mixin", + "compatibilityLevel": "JAVA_17", + "mixins": ["BoneMealMixin", "DefaultedRegistryMixin", "HungerManagerMixin", "ItemAccessor"], + "injectors": { + "defaultRequire": 1 + } }