Jump to content

Is there a way to make a tools harvest speed as the average of the surrounding blocks?


NindyBun

Recommended Posts

I have a tool that mines a 3x3 area with worldIn.destroyBlock() but the problem I have is how do I make the tool's mining speed equal to the average mining speed of the total surrounding blocks? I'm currently using public boolean onBlockDestroyed()

package com.nindybun.miningtool.objects.items;

import net.minecraft.block.BlockState;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.LivingEntity;
import net.minecraft.item.IItemTier;
import net.minecraft.item.ItemStack;
import net.minecraft.item.PickaxeItem;
import net.minecraft.util.math.*;
import net.minecraft.util.math.shapes.VoxelShapes;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.world.World;
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) {
        double a = entityLiving.getPosX();
        double b = entityLiving.getPosY() + (double) entityLiving.getEyeHeight();
        double c = entityLiving.getPosZ();
        Vec3d vec3start = new Vec3d(a, b, c);

        float f = entityLiving.rotationPitch;
        float f1 = entityLiving.rotationYaw;
        float f2 = MathHelper.cos(-f1 * 0.017453292F - (float) Math.PI);
        float f3 = MathHelper.sin(-f1 * 0.017453292F - (float) Math.PI);
        float f4 = -MathHelper.cos(-f * 0.017453292F);
        float f5 = MathHelper.sin(-f * 0.017453292F);
        float f6 = f3 * f4;
        float f7 = f2 * f4;
        double d3 = 5.0D;
        Vec3d vec3end = vec3start.add((double) f6 * d3, (double) f5 * d3, (double) f7 * d3);
        RayTraceResult face = worldIn.rayTraceBlocks(vec3start, vec3end, pos, VoxelShapes.fullCube(), state);
        if (!entityLiving.isSneaking()) {
            if (face.getHitVec().getY() == pos.getY() + 1 || face.getHitVec().getY() == pos.getY()) {
                //Top Face or Bottom Face
                for (int x = -1; x < 2; x++) {
                    for (int z = -1; z < 2; z++) {
                        if (worldIn.getBlockState(pos.add(x, 0, z)).getBlockHardness(worldIn, pos.add(x, 0, z)) <= 0 || pos.add(x, 0, z) == pos) {
                        } else {
                            worldIn.destroyBlock(pos.add(x, 0, z), true);
                            stack.setDamage(stack.getDamage() + 1);
                        }
                    }
                }
            }
            if (face.getHitVec().getZ() == pos.getZ() || face.getHitVec().getZ() == pos.getZ() + 1) {
                //North Face or South Face
                for (int x = -1; x < 2; x++) {
                    for (int y = -1; y < 2; y++) {
                        if (worldIn.getBlockState(pos.add(x, y, 0)).getBlockHardness(worldIn, pos.add(x, y, 0)) <= 0 || pos.add(x, y, 0) == pos) {
                        } else {
                            worldIn.destroyBlock(pos.add(x, y, 0), true);
                            stack.setDamage(stack.getDamage() + 1);
                        }
                    }
                }
            }
            if (face.getHitVec().getX() == pos.getX() || face.getHitVec().getX() == pos.getX() + 1) {
                //West Face or east Face
                for (int y = -1; y < 2; y++) {
                    for (int z = -1; z < 2; z++) {
                        if (worldIn.getBlockState(pos.add(0, y, z)).getBlockHardness(worldIn, pos.add(0, y, z)) <= 0 || pos.add(0, y, z) == pos) {
                        } else {
                            worldIn.destroyBlock(pos.add(0, y, z), true);
                            stack.setDamage(stack.getDamage() + 1);
                        }
                    }
                }
            }
        }

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

}

 

Link to comment
Share on other sites

On 5/10/2020 at 3:05 AM, diesieben07 said:

You have to use the PlayerEvent.BreakSpeed event for this, as the normal hook (Item#getDestroySpeed) does not provide the world position.

Note however that that event may still fire with an invalid position (read the comments in the source code), so you have to handle that.

I tried using this but it's not being used.

@SubscribeEvent(priority = EventPriority.HIGHEST)
    public void onBlockStartBreak(PlayerEvent.BreakSpeed event){
        LOGGER.info(event.getOriginalSpeed());
    }

 

Link to comment
Share on other sites

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.