Skip to content

Commit

Permalink
Only highlight blocks that will actually be broken with the drill, see
Browse files Browse the repository at this point in the history
  • Loading branch information
malte0811 committed Oct 28, 2023
1 parent 1b3a986 commit ebeecfd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Rotation;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.*;
import net.minecraft.world.phys.HitResult.Type;
import net.minecraft.world.phys.shapes.VoxelShape;
Expand All @@ -104,7 +105,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.IntPredicate;
import java.util.stream.IntStream;

import static blusunrize.immersiveengineering.ImmersiveEngineering.rl;
Expand Down Expand Up @@ -692,7 +692,7 @@ public void onMouseEvent(MouseScrollingEvent event)
@SubscribeEvent()
public void renderAdditionalBlockBounds(RenderHighlightEvent.Block event)
{
if(event.getTarget().getType()==Type.BLOCK&&event.getCamera().getEntity() instanceof LivingEntity player)
if(event.getTarget().getType()==Type.BLOCK&&event.getCamera().getEntity() instanceof LivingEntity living)
{
PoseStack transform = event.getPoseStack();
MultiBufferSource buffer = event.getMultiBufferSource();
Expand All @@ -703,8 +703,8 @@ public void renderAdditionalBlockBounds(RenderHighlightEvent.Block event)
transform.translate(-renderView.x, -renderView.y, -renderView.z);
transform.translate(pos.getX(), pos.getY(), pos.getZ());
float eps = 0.002F;
BlockEntity tile = player.level.getBlockEntity(rtr.getBlockPos());
ItemStack stack = player.getItemInHand(InteractionHand.MAIN_HAND);
BlockEntity tile = living.level.getBlockEntity(rtr.getBlockPos());
ItemStack stack = living.getItemInHand(InteractionHand.MAIN_HAND);

if(tile instanceof TurntableBlockEntity turntableTile&&Utils.isHammer(stack))
{
Expand All @@ -714,15 +714,15 @@ public void renderAdditionalBlockBounds(RenderHighlightEvent.Block event)
{
transform.pushPose();
transform.translate(0.5, 0.5, 0.5);
ClientUtils.toModelRotation(side).getRotation().push(transform);
transform.pushTransformation(ClientUtils.toModelRotation(side).getRotation());
transform.mulPose(new Quaternion(-90, 0, 0, true));
Rotation rotation = turntableTile.getRotationFromSide(side);
boolean cw180 = rotation==Rotation.CLOCKWISE_180;
double angle;
if(cw180)
angle = player.tickCount%40/20d;
angle = living.tickCount%40/20d;
else
angle = player.tickCount%80/40d;
angle = living.tickCount%80/40d;
double stepDistance = (cw180?2: 4)*Math.PI;
angle = -(angle-Math.sin(angle*stepDistance)/stepDistance)*Math.PI;
BlockOverlayUtils.drawCircularRotationArrows(buffer, transform, (float)angle, rotation==Rotation.COUNTERCLOCKWISE_90, cw180);
Expand All @@ -731,7 +731,7 @@ public void renderAdditionalBlockBounds(RenderHighlightEvent.Block event)
}
}

Level world = player.level;
Level world = living.level;
if(!stack.isEmpty()&&ConveyorHandler.isConveyorBlock(Block.byItem(stack.getItem()))&&rtr.getDirection().getAxis()==Axis.Y)
{
Direction side = rtr.getDirection();
Expand Down Expand Up @@ -773,15 +773,23 @@ public void renderAdditionalBlockBounds(RenderHighlightEvent.Block event)
}

transform.popPose();
if(!stack.isEmpty()&&stack.getItem() instanceof DrillItem&&
((DrillItem)stack.getItem()).isEffective(stack, world.getBlockState(rtr.getBlockPos())))
BlockState targetBlock = world.getBlockState(rtr.getBlockPos());
if(stack.getItem() instanceof DrillItem drillItem&&drillItem.isEffective(stack, targetBlock))
{
ItemStack head = ((DrillItem)stack.getItem()).getHead(stack);
if(!head.isEmpty()&&player instanceof Player&&!player.isShiftKeyDown()&&!DrillItem.isSingleBlockMode(stack))
ItemStack head = drillItem.getHead(stack);
if(!head.isEmpty()&&living instanceof Player player&&!living.isShiftKeyDown()&&!DrillItem.isSingleBlockMode(stack))
{
ImmutableList<BlockPos> blocks = ((IDrillHead)head.getItem()).getExtraBlocksDug(head, world,
(Player)player, event.getTarget());
BlockOverlayUtils.drawAdditionalBlockbreak(event, (Player)player, blocks);
ImmutableList<BlockPos> potentialBlocks = ((IDrillHead)head.getItem()).getExtraBlocksDug(
head, world, player, event.getTarget()
);
List<BlockPos> breakingBlocks = new ArrayList<>();
for(BlockPos candidate : potentialBlocks)
{
BlockState targetState = world.getBlockState(candidate);
if(drillItem.canBreakExtraBlock(world, candidate, targetState, player, stack, head))
breakingBlocks.add(candidate);
}
BlockOverlayUtils.drawAdditionalBlockbreak(event, player, breakingBlocks);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.event.entity.player.PlayerEvent.HarvestCheck;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus;
import net.minecraftforge.items.IItemHandler;
Expand Down Expand Up @@ -273,16 +272,13 @@ public float getDestroySpeed(ItemStack stack, BlockState state)
return super.getDestroySpeed(stack, state);
}

public boolean canBreakExtraBlock(Level world, Block block, BlockPos pos, BlockState state, Player player, ItemStack drill, ItemStack head, boolean inWorld)
public boolean canBreakExtraBlock(
Level world, BlockPos pos, BlockState state, Player player, ItemStack drill, ItemStack head
)
{
if(block.canHarvestBlock(state, world, pos, player)&&isEffective(drill, state)&&canToolBeUsed(drill))
{
if(inWorld)
return !((IDrillHead)head.getItem()).beforeBlockbreak(drill, head, player);
else
return true;
}
return false;
if(!state.canHarvestBlock(world, pos, player)||!isEffective(drill, state)||!canToolBeUsed(drill))
return false;
return !((IDrillHead)head.getItem()).beforeBlockbreak(drill, head, player);
}

@Override
Expand All @@ -309,7 +305,7 @@ public boolean onBlockStartBreak(ItemStack stack, BlockPos iPos, Player player)

if(!state.isAir()&&state.getDestroyProgress(player, world, pos)!=0)
{
if(!this.canBreakExtraBlock(world, block, pos, state, player, stack, head, true))
if(!this.canBreakExtraBlock(world, pos, state, player, stack, head))
continue;
int xpDropEvent = ForgeHooks.onBlockBreakEvent(world, ((ServerPlayer)player).gameMode.getGameModeForPlayer(), (ServerPlayer)player, pos);
if(xpDropEvent < 0)
Expand Down

0 comments on commit ebeecfd

Please sign in to comment.