1.13.2 Get the xp that a block would drop if broken with a tool

Discussion in 'Spigot Plugin Development' started by Nemo_64, Feb 8, 2020.

  1. Block has a method called getDrops(ItemStack) that gives the items that a block would drop if broken with the given item stack, is there anything like this for xp?
    Also, i'm having a problem because getDrops(ItemStack) doesn't seem to respect the enchantments of the tools, how can I fix it?
     
    • Like Like x 1
  2. But i'm not using a block break event, i'm using a player interact event
     
  3. I think the only way is as Bjorno said. Use something like this... I don't know if it's a double or an integer, in doubt try both. I don't even know if it can be put in a method with a return, but you can try the same. Since I don't know what happens if the block doesn't drop experience. For security, add a check of not null
    Code (Java):
    @EventHandler
    public double breakEvent(BlockBreakEvent event){

         return event.getExpToDrop();

    }
     
     
    #4 Obsidi4nPvP, Feb 8, 2020
    Last edited: Feb 8, 2020
    • Funny Funny x 3
  4. Please don't comment on threads when you have no idea what you're doing. Event listeners do not work that way. Please learn about the api before giving code to other people.
     
    • Agree Agree x 3
    • Like Like x 1
  5. To get this to a solution: You have to use NMS to get the exp below is an example code you should use Reflections to be version independent.

    Code (Text):

            Method getNMSBlockMethod = CraftBlock.class.getDeclaredMethod("getNMSBlock");
            getNMSBlockMethod.setAccessible(true);
            net.minecraft.server.v1_8_R3.Block nmsBlock = (net.minecraft.server.v1_8_R3.Block) getNMSBlockMethod.invoke(block);
            int xp = nmsBlock.getExpDrop(((CraftWorld)block.getWorld()).getHandle(), nmsBlock.getBlockData(), bonusLevel);
    - 'block' is a normal bukkit Block.
    - 'bonusLevel' is the luck enchantment level of the pickaxe the breaker uses.
     
    • Like Like x 1
  6. but this code wont work on 1.13.2 reason : net.minecraft.server.v1_8_R3.Block its for 1.8
     
  7. Then use the 1.13 NMS import?
     
  8. Exactly