Is block destructible with specific tool?

Hey everyone!

Just trying to get a little bit of help with a query that I have. So, I started developing a plugin just for personal use and now I am stuck. What I want is when a block is interacted with via a right-click to find out if it is the right tool for destroying that block. For example a pickaxe for stone, hatchet for wood, shovel for sand. How can I check that?

I am using Sponge Vanilla and API 7.0.

Here is what I have thus far for my interact method, I just need to figure out what tool is right for the block:

@Listener
public void OnPlayerSecondaryInteracted(InteractBlockEvent.Secondary.MainHand event, @First Player p){
    Optional<ItemStack> itemMainHand = p.getItemInHand(HandTypes.MAIN_HAND);
    String itemName = itemMainHand.toString().toLowerCase();

    logger.info("SECONDARY INTERACTION");
    logger.info(itemName);

    if(itemName.contains("pickaxe")){

    } else if(itemName.contains("hatchet")){

    }
}

Sponge does expose the list of block types that an item can destroy with the following code (based on your own code)

@Listener
public void onPlayerSecondaryInteracted(InteractBlockEvent.Secondary.MainHand event, @First Player player){
    Optional<ItemStack> opItemMainHand = player.getItemInHand(HandTypes.MAIN_HAND);
    if(!opItemMainHand.isPresent()){
       return;
    }
    ItemStack itemMainHand = opItemMainHand.get();
    Set<BlockType> destructableTypes = itemMainHand.get(Keys.BREAKABLE_BLOCK_TYPES);
    BlockSnapshot snapshot = event.getTargetBlock();
    if(destructableTypes.contains(snapshot.getType())){
       //is the correct tool
    }else{
       //not the correct tool
    }
}

That looks great thank you so much. I will have a go at that in a moment!

1 Like

I return with bad news! Getting the destructible blocks is not returning anything. (Well, an empty optional, as an optional is what the IDE demanded). Is there anything that stands out to you as incorrect?

[22:28:41] [Server thread/INFO] [minerpg]: PRIMARY INTERACTION
[22:28:41] [Server thread/INFO] [minerpg]: Optional[1xitem.shovelWood@0]
[22:28:41] [Server thread/INFO] [minerpg]: Optional.empty

Thanks in advance!

@Listener
public void onPlayerPrimaryInteracted(InteractBlockEvent.Primary.MainHand event, @First Player p){
    logger.info("PRIMARY INTERACTION");

    Optional<ItemStack> opItemMainHand = p.getItemInHand(HandTypes.MAIN_HAND);

    logger.info(opItemMainHand.toString());

    if(opItemMainHand.isPresent() == false) {
        return;
    }

    ItemStack itemMainHand = opItemMainHand.get();
    // String itemName = itemMainHand.toString().toLowerCase();

    Optional<Set<BlockType>> opDestructibles = itemMainHand.get(Keys.BREAKABLE_BLOCK_TYPES);

    logger.info(opDestructibles.toString());

    if(opDestructibles.isPresent() == false) {
        return;
    }

    Set<BlockType> destructibles = opDestructibles.get();

    BlockSnapshot block = event.getTargetBlock();

    if(destructibles.contains(block)){
        logger.info("CORRECT TOOL FOR THE JOB!");
    }
}

Thats strange. The only things I can think of is that the key either doesn’t do what I thought it did or its not implemented yet.

When I get some time ill do some digging

1 Like

Just checked, its not implemented.

Put up a “issue” on the spongeAPI issues page on GitHub stating that the key is not implemented

1 Like

Done! https://github.com/SpongePowered/SpongeAPI/issues/2095