1.14.4 BlockBreakEvent glitches out walking

Discussion in 'Spigot Plugin Development' started by Joshuak52, Jan 14, 2020.

  1. this.plugin.getBlocks().containsKey()
    If this is not a Hash based collection i got bad news for you...

    Show the MinePickup.class pls

    Also you dont need to parse the inventory for empty slots by yourself.
    Inventory#addItem(ItemStack...) returns a Map<ItemStack, Integer> of every item that did not fit.
    You can just do
    ItemStack item;
    Consumer<ItemStack> overheadConsumer;

    player.getInventory().addItem(item).values().forEach(left -> player.getLocation().getWorld().dropItemNaturally(player.getLocation, left));

    I wrote this to get the drops of a Block with any item respecting enchantments etc:
    Code (Java):

    public class BlockBreak_1_14 implements BreakEvaluator{

        public List<ItemStack> evaluate(World world, ItemStack breakItem, BlockState blockToBreak) {
            WorldServer ws = ((CraftWorld) world).getHandle();
            IBlockData data = ((CraftBlockState)blockToBreak).getHandle();
            BlockPosition position = ((CraftBlockState)blockToBreak).getBlock().getPosition();
            LootTableInfo.Builder info = new LootTableInfo.Builder(ws)
                    .set(LootContextParameters.POSITION, position)
                    .set(LootContextParameters.BLOCK_STATE, data)
                    .setOptional(LootContextParameters.BLOCK_ENTITY, ws.getTileEntity(position))
                    .setOptional(LootContextParameters.TOOL, CraftItemStack.asNMSCopy(breakItem));
            return data.a(info).stream().map(CraftItemStack::asBukkitCopy).collect(Collectors.toList());

    You also don't really need the worldguard hook. Just check if the event is cancelled.
    Otherwise you will duplicate blocks from other plugins that cancel block break.
    I know of some plugins that use diamond or emerald blocks. This could destroy a whole servers economy by itself.

    Also what is up with the whole
    this.plugin.getBlocks().put(player.getUniqueId(), Double.valueOf(((Double)this.plugin.getBlocks().get(player.getUniqueId())).doubleValue() + 1.0D));
    stuff? Incrementing should be done with a long or int.

    You need to organize your code more.
    This stuff can be done in 6 lines including all checks.
    Create new classes that have isolated tasks.
    Create a BlockCounter class for example that has the purpose of counting up.
    #2 7smile7, Jan 14, 2020
    Last edited: Jan 14, 2020