Jump to content

(Minecraft 1.15.2 Forge) Is there a way to get the side of the block the player is looking at?


Recommended Posts

Posted (edited)

I'm trying to make a tool that mines a 3x3 area based on the side of the block the player broke. For example if I broke a block while having my cursor on one of the faces, then it would mine blocks around it on the same plane. So far I'm using the direction of the entity and pitch.

 

package com.nindybun.miningtool.objects.items;

import com.sun.org.apache.xerces.internal.xs.XSIDCDefinition;
import net.minecraft.block.BlockState;
import net.minecraft.block.DirectionalBlock;
import net.minecraft.client.Minecraft;
import net.minecraft.client.MinecraftGame;
import net.minecraft.client.renderer.FaceDirection;
import net.minecraft.client.renderer.Vector3d;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.IItemTier;
import net.minecraft.item.ItemStack;
import net.minecraft.item.PickaxeItem;
import net.minecraft.util.Direction;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.World;
import net.minecraftforge.client.event.ClientChatEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.annotation.Nullable;
import java.util.List;

public class TestDrill extends PickaxeItem {
    private static final Logger LOGGER = LogManager.getLogger();
    public TestDrill(IItemTier tier, int attackDamageIn, float attackSpeedIn, Properties builder) {
        super(tier, attackDamageIn, attackSpeedIn, builder);
    }

    @Override
    public void addInformation(ItemStack stack, @Nullable World worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) {
        tooltip.add(new StringTextComponent("This is a test drill."));
        super.addInformation(stack, worldIn, tooltip, flagIn);
    }

    @Override
    public boolean onBlockDestroyed(ItemStack stack, World worldIn, BlockState state, BlockPos pos, LivingEntity entityLiving) {
        float pitch = entityLiving.getPitch(0); //Vertical

        if (pitch < 0){
            pitch = -pitch;
        }
        if ((entityLiving.getHorizontalFacing() == Direction.NORTH || entityLiving.getHorizontalFacing() == Direction.SOUTH) & (pitch != 90)) {
            //mines North/South
            worldIn.destroyBlock(pos.add(0, 1, 0), true);
            worldIn.destroyBlock(pos.add(0, -1, 0), true);

            worldIn.destroyBlock(pos.add(1, 0, 0), true);
            worldIn.destroyBlock(pos.add(-1, 0, 0), true);

            worldIn.destroyBlock(pos.add(1, 1, 0), true);
            worldIn.destroyBlock(pos.add(-1, -1, 0), true);

            worldIn.destroyBlock(pos.add(1, -1, 0), true);
            worldIn.destroyBlock(pos.add(-1, 1, 0), true);
        } else if ((entityLiving.getHorizontalFacing() == Direction.WEST || entityLiving.getHorizontalFacing() == Direction.EAST) & (pitch != 90)){
            //Mines West/east
            worldIn.destroyBlock(pos.add(0, 1, 0), true);
            worldIn.destroyBlock(pos.add(0, -1, 0), true);

            worldIn.destroyBlock(pos.add(0, 0, 1), true);
            worldIn.destroyBlock(pos.add(0, 0, -1), true);

            worldIn.destroyBlock(pos.add(0, 1, 1), true);
            worldIn.destroyBlock(pos.add(0, -1, -1), true);

            worldIn.destroyBlock(pos.add(0, -1, 1), true);
            worldIn.destroyBlock(pos.add(0, 1, -1), true);
        } else if (pitch == 90){
            //Mines Up/Down
            worldIn.destroyBlock(pos.add(1, 0, 0), true);
            worldIn.destroyBlock(pos.add(-1, 0, 0), true);

            worldIn.destroyBlock(pos.add(0, 0, 1), true);
            worldIn.destroyBlock(pos.add(0, 0, -1), true);

            worldIn.destroyBlock(pos.add(1, 0, 1), true);
            worldIn.destroyBlock(pos.add(-1, 0, -1), true);

            worldIn.destroyBlock(pos.add(-1, 0, 1), true);
            worldIn.destroyBlock(pos.add(1, 0, -1), true);
        }




        return super.onBlockDestroyed(stack, worldIn, state, pos, entityLiving);
    }
}

 

Edited by NindyBun
Posted

Look at how vanilla uses Direction.getFacingDirections or use Direction.getFacingFromVector with the player's look vector.

I'm eager to learn and am prone to mistakes. Don't hesitate to tell me how I can improve.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.