Solved listen when cobble/obsidian is generated

Discussion in 'Spigot Plugin Development' started by Plochem, Jul 2, 2019.

  1. Is BlockFormEvent not the right event to listen to?
     
  2. drives_a_ford

    Moderator

    If it's not getting called, you've probably forgot to register the listener.
     
  3. Post main and where the event is
     
  4. Code (Text):
    // this is in the class extending javaplugin

    Bukkit.getServer().getPlugin!anager().registerEvents(new OreGen(), this);

    // OreGen class
    public class OreGen implements Listener{

    @EventHandler
    public void onForm(BlockFormEvent e){


    }

    }

     
    This is what I did (not copy and pasted since I’m not on my computer)
     
  5. Looks fine, were you using #getBlock() or #getNewState() as getBlock returns the prior state, and getNewState returns BlockState of the formed material

    Code (Text):

    @EventHandler
    public void onBlockForm(BlockFormEvent event) {
        System.out.println(event.getNewState().getType().toString());
    }
     
    Will return OBSIDIAN or COBBLESTONE when formed
     
  6. drives_a_ford

    Moderator

    All I can tell you is that the event does get called when obsidian or cobblestone is formed.
    From NMS BlockFluids:
    Code (Java):

        public static boolean handleBlockFormEvent(World world, BlockPosition pos, IBlockData block) {
            return handleBlockFormEvent(world, pos, block, 3);
        }
        public boolean a(World world, BlockPosition blockposition, IBlockData iblockdata) {
            if (this.b.a(TagsFluid.LAVA)) {
                boolean flag = false;
                EnumDirection[] aenumdirection = EnumDirection.values();
                int i = aenumdirection.length;

                for (int j = 0; j < i; ++j) {
                    EnumDirection enumdirection = aenumdirection[j];

                    if (enumdirection != EnumDirection.DOWN && world.getFluid(blockposition.shift(enumdirection)).a(TagsFluid.WATER)) {
                        flag = true;
                        break;
                    }
                }

                if (flag) {
                    Fluid fluid = world.getFluid(blockposition);

                    if (fluid.isSource()) {
                        // CraftBukkit start
                        if (org.bukkit.craftbukkit.event.CraftEventFactory.handleBlockFormEvent(world, blockposition, Blocks.OBSIDIAN.getBlockData())) {
                            this.fizz(world, blockposition);
                        }
                        // CraftBukkit end
                        return false;
                    }

                    if (fluid.getHeight(world, blockposition) >= 0.44444445F) {
                        // CraftBukkit start
                        if (org.bukkit.craftbukkit.event.CraftEventFactory.handleBlockFormEvent(world, blockposition, Blocks.COBBLESTONE.getBlockData())) {
                            this.fizz(world, blockposition);
                        }
                        // CraftBukkit end
                        return false;
                    }
                }
            }

            return true;
        }
     
    And from CraftEventFactory:
    Code (Java):

        public static boolean handleBlockFormEvent(World world, BlockPosition pos, IBlockData block, @Nullable Entity entity) {
            return handleBlockFormEvent(world, pos, block, 3, entity);
        }

        public static boolean handleBlockFormEvent(World world, BlockPosition pos, IBlockData block, int flag) {
            return handleBlockFormEvent(world, pos, block, flag, null);
        }

        public static boolean handleBlockFormEvent(World world, BlockPosition pos, IBlockData block, int flag, @Nullable Entity entity) {
            CraftBlockState blockState = CraftBlockState.getBlockState(world, pos, flag);
            blockState.setData(block);

            BlockFormEvent event = (entity == null) ? new BlockFormEvent(blockState.getBlock(), blockState) : new EntityBlockFormEvent(entity.getBukkitEntity(), blockState.getBlock(), blockState);
            world.getServer().getPluginManager().callEvent(event);

            if (!event.isCancelled()) {
                blockState.update(true);
            }

            return !event.isCancelled();
        }
     
     
  7. A simple debug statement doesn’t even show in console
     
  8. That event only is called with water, lava, and a teleporting dragon egg according to the javadoc. You would still need to use the BlockFormEvent.
     
  9. Well that event doesn't fire while BlockFromToEvent does.

    EDIT: i found a solution on stackoverflow using the BlockFromToEvent. It works now.
     
    #14 Plochem, Jul 2, 2019
    Last edited: Jul 2, 2019