Solved Place block when destroy another block

Discussion in 'Spigot Plugin Development' started by SolitaSolaa, Jan 30, 2020.

  1. Hi, i'm trying to make a plugin, but there were complications :)

    When you destroy a block (for example obsidian) that gives the drop (the obsidian block), and that in the location of the block appears lava of imediate. I tried it as follows:

    Code (Java):
    public void onBreak(BlockBreakEvent e) {
        if (e.getBlock().getType().equals(Material.OBSIDIAN)) {
            e.getBlock().setType(Material.LAVA);
        }
    }
    Not works. Any can help me with any idea? Also I tryed to cancel the event. In that case the block changes the material, but don't drop anything.

    any response can help :giggle:
     
  2. The event is called prior to the block breaking, so if you set the block in the event handler itself, you will actually be breaking the block you set it to in-game. It looks like this:

    Start breaking block -> BlockBreakEvent called at the end -> Your listener runs, setting the new block type -> the server breaks the block, which you just set to LAVA

    To make sure you set the block AFTER the server actually breaks it, you need to use the Bukkit scheduler or schedule a BukkitRunnable to delay setting the block itself 1 tick.
     
    • Like Like x 1
  3. Show your full code please. Also, for ENUMs you should use == Material. From what I understand you're trying to replace any broken obsidian to lava, right?
     
  4. I'm trying to understand you
     
  5. You need to change to block to lava 10 ticks (for example) later than the block break event
     
  6. Code (Java):
    package com.SolitaSolaa.SkyBlock.Events;

    import org.bukkit.Material;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;

    public class Events implements Listener {

        @EventHandler
        public void onBreak(BlockBreakEvent e) {
         
            if (e.getBlock().getType().equals(Material.OBSIDIAN)) {
                e.getBlock().setType(Material.LAVA);
                 
            }
        }
     
    }
     
     
  7. Code (Text):
    @EventHandler
        public void onBreak(BlockBreakEvent e) {
            if (e.getBlock().getType() == Material.OBSIDIAN) {
                // Setup a bukkit runnable that will run e.getBlock().setType(Material.LAVA); about 10 ticks later
            }
        }
     
  8. Delay the setting of the block using the scheduler or a bukkitrunnable
     
  9. Any exemple of BukkitRunnable please?
     
  10. Code (Text):

    new BukkitRunnable() {
               @Override
               public void run() {
                   // What you want to schedule goes here
               }
               
           }.runTaskLater(yourplugininstance, timeinticks);