From 507fdc1ba59bcb283f6b937708ac572cb08b7a2c Mon Sep 17 00:00:00 2001 From: jottyfan Date: Sun, 13 Feb 2022 20:01:58 +0100 Subject: [PATCH] repaired drills --- .../blockentity/DrillBlockEntity.java | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/main/java/de/jottyfan/minecraft/quickiefabric/blockentity/DrillBlockEntity.java diff --git a/src/main/java/de/jottyfan/minecraft/quickiefabric/blockentity/DrillBlockEntity.java b/src/main/java/de/jottyfan/minecraft/quickiefabric/blockentity/DrillBlockEntity.java new file mode 100644 index 0000000..fd6cc6f --- /dev/null +++ b/src/main/java/de/jottyfan/minecraft/quickiefabric/blockentity/DrillBlockEntity.java @@ -0,0 +1,61 @@ +package de.jottyfan.minecraft.quickiefabric.blockentity; + +import de.jottyfan.minecraft.quickiefabric.blocks.QuickieBlocks; +import net.minecraft.block.BlockState; +import net.minecraft.block.Blocks; +import net.minecraft.block.entity.BlockEntity; +import net.minecraft.block.entity.BlockEntityType; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; + +public abstract class DrillBlockEntity extends BlockEntity { + + private Integer drillstep; + private final Integer maxDrillStep; + + public DrillBlockEntity(BlockEntityType type, BlockPos pos, BlockState state, Integer maxDrillStep) { + super(type, pos, state); + this.maxDrillStep = maxDrillStep; + } + + protected static final void moveBlockWithEntity(BlockPos from, BlockPos to, World world) { + BlockEntity be = world.getBlockEntity(from); + BlockState bs = world.getBlockState(from); + if (be != null) { + world.setBlockState(from, Blocks.AIR.getDefaultState()); + world.setBlockState(to, bs); + world.removeBlockEntity(from); + world.addBlockEntity(be); + } + } + + protected static final void drill(BlockPos pos, BlockPos to, World world) { + if (!world.getBlockState(to).isOf(Blocks.BEDROCK) && !world.getBlockState(to).isOf(QuickieBlocks.DRILLSTOP)) { + world.breakBlock(to, true); + if (pos.down() != to) { // no need for the falling one + moveBlockWithEntity(pos, to, world); + } + } + } + + public static void tick(World world, BlockPos pos, BlockState state, DrillBlockEntity be, Integer maxDrillStep, BlockPos drillPosition) { + if (be.getDrillstep() < 1) { + be.setDrillstep(maxDrillStep); + drill(pos, drillPosition, world); + } else { + be.doDrillstep(); + } + } + + public void doDrillstep() { + setDrillstep(getDrillstep() - 1); + } + + public Integer getDrillstep() { + return drillstep == null ? maxDrillStep : drillstep; + } + + public void setDrillstep(Integer drillstep) { + this.drillstep = drillstep; + } +}