-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added BoneMealSpreadable and DynamicSaplingGenerator interfaces
- Loading branch information
Showing
4 changed files
with
89 additions
and
11 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
src/main/java/com/hugman/dawn/api/mixin/BoneMealMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/hugman/dawn/api/object/block/BoneMealSpreadable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
22
src/main/java/com/hugman/dawn/api/object/block/DawnRootsBlock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |