Skip to content

Commit

Permalink
Add BoneMealSpreadable interface
Browse files Browse the repository at this point in the history
- Added BoneMealSpreadable and DynamicSaplingGenerator interfaces
  • Loading branch information
Hugman76 committed Jul 1, 2022
1 parent 8bd225e commit 1efbf6b
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 11 deletions.
48 changes: 48 additions & 0 deletions src/main/java/com/hugman/dawn/api/mixin/BoneMealMixin.java
Original file line number Diff line number Diff line change
@@ -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<ActionResult> cir) {
World world = context.getWorld();
BlockPos pos = context.getBlockPos();
ItemStack stack = context.getStack();

ArrayList<Block> 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));
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
22 changes: 22 additions & 0 deletions src/main/java/com/hugman/dawn/api/object/block/DawnRootsBlock.java
Original file line number Diff line number Diff line change
@@ -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<BlockState> canPlantOn;

public DawnRootsBlock(Settings settings, Predicate<BlockState> canPlantOn) {
super(settings);
this.canPlantOn = canPlantOn;
}

protected boolean canPlantOnTop(BlockState floor, BlockView world, BlockPos pos) {
return this.canPlantOn.test(floor);
}
}
18 changes: 7 additions & 11 deletions src/main/resources/dawn_api.mixins.json
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit 1efbf6b

Please # to comment.