Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

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

Featured Replies

Posted

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);
    }

}

 

  • Author
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());
    }

 

  • Author
@Mod.EventBusSubscriber(modid = MiningTool.MOD_ID, bus = Mod.EventBusSubscriber.Bus.MOD)

With this?

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.