Problem with getting and setting direction of block

Discussion in 'Plugin Development' started by Jaboi313, Jul 6, 2022.

Thread Status:
Not open for further replies.
  1. Offline

    Jaboi313

    I am trying to make a log respawn in its original orientation so I came up with this :

    Code:
    public class LogRegenBlockEvent implements Listener {
    
        PrisonRp plugin;
    
        public LogRegenBlockEvent(PrisonRp listener){
            this.plugin = listener;
        }
    
        @EventHandler
        public void onBlockBreakEvent(BlockBreakEvent e) {
            Block block = e.getBlock();
            BlockData blockData = block.getBlockData();
            Material material = block.getType();
    
            if (material.equals(Material.OAK_LOG)) {
                block.breakNaturally();
                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                    @Override
                    public void run(){
                        block.setType(Material.BARRIER);
                    }
                }, 1L);
                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                    @Override
                    public void run(){
                        if(blockData instanceof Directional){
                            Directional directional = (Directional) blockData;
                            BlockFace facing = directional.getFacing();
                            directional.setFacing(facing);
                            block.setBlockData(directional);
                        }
                        block.setType(Material.OAK_LOG);
                    }
                }, 200L);
            }
    But specifically this part :

    Code:
    if (material.equals(Material.OAK_LOG)) {
                block.breakNaturally();
                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                    @Override
                    public void run(){
                        block.setType(Material.BARRIER);
                    }
                }, 1L);
                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                    @Override
                    public void run(){
                        if(blockData instanceof Directional){
                            Directional directional = (Directional) blockData;
                            BlockFace facing = directional.getFacing();
                            directional.setFacing(facing);
                            block.setBlockData(directional);
                        }
                        block.setType(Material.OAK_LOG);
                    }
                }, 200L);
            }
    And the respawning part works but the logs just respawn straight up like they would with no given direction
     
  2. Offline

    timtower Administrator Administrator Moderator

    @Jaboi313 Need to update the block as well, and set the type before the direction
     
  3. Offline

    Jaboi313

    Like before the if statement or inside it?

    And what do I use for updating the block, I found that people update a block nest to it?
     
    Last edited: Jul 7, 2022
  4. Offline

    timtower Administrator Administrator Moderator

    After setting the block data.
    And block.update() probably does the trick.
     
  5. Offline

    Jaboi313

    But .update does not exist.
     
  6. Offline

    timtower Administrator Administrator Moderator

    block.getState().update() then probably (been a while for me)
     
  7. Offline

    Jaboi313

    I have this now and now the log doesn't respawn at all.

    Code:
    if (material.equals(Material.OAK_LOG)) {
                block.breakNaturally();
                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                    @Override
                    public void run(){
                        block.setType(Material.BARRIER);
                    }
                }, 1L);
                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                    @Override
                    public void run(){
                        if(blockData instanceof Directional){
                            Directional directional = (Directional) blockData;
                            BlockFace facing = directional.getFacing();
                            directional.setFacing(facing);
                            block.setBlockData(directional);
                            block.setType(Material.OAK_LOG);
                            block.getState().update();
    
                        }
                    }
                }, 200L);
            }
     
  8. Offline

    timtower Administrator Administrator Moderator

    @Jaboi313 Try posting the entire method.
    Somewhere you have "blockData", I don't know where it comes from.
    material needs to be checked with ==, not equals.
    I told you to set the type and THEN set the direction.
     
  9. Offline

    Jaboi313

    Does the type setting go in the if statement that checks if blockdata is instance of directional or before?
    And btw the material checking works fine with .equals

    Code:
    @EventHandler
        public void onBlockBreakEvent(BlockBreakEvent e) {
            Block block = e.getBlock();
            BlockData blockData = block.getBlockData();
            Material material = block.getType();
    
            if (material.equals(Material.OAK_LOG)) {
                block.breakNaturally();
                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                    @Override
                    public void run(){
                        block.setType(Material.BARRIER);
                    }
                }, 1L);
                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                    @Override
                    public void run(){
                        if(blockData instanceof Directional){
                            block.setType(Material.OAK_LOG);
                            Directional directional = (Directional) blockData;
                            BlockFace facing = directional.getFacing();
                            directional.setFacing(facing);
                            block.setBlockData(directional);
                            block.getState().update();
    
                        }
                    }
                }, 200L);
            }
     
  10. Offline

    timtower Administrator Administrator Moderator

    @Jaboi313 Have you tried to add debug statements to your code? To see where it goes wrong?
     
  11. Offline

    Jaboi313

    Yes I have tried it now and it seems that the whole directional part just gets skipped, like the whole if statement.
     
Thread Status:
Not open for further replies.

Share This Page