Solved Stuck

Discussion in 'Spigot Plugin Development' started by chochoco4777, Jul 31, 2021.

  1. Lots of weird stuff going on here, but looking at your other plugins I'm guessing this too was created in VisualBukkit. The root of your issue probably has something to do with these couple lines here:
    Code (Java):
    YamlConfiguration.loadConfiguration(new File("config.yml")).set("10000", event.getBlock().getState());
    I'm not sure exactly what this line is supposed to do, but it can't work. Using YamlConfiguration#set() in this way will only change the copy of your config.yml in memory, and not write it to disk. The YamlConfiguration then immediately goes out of scope and your changes in memory are lost. Instead, use something like PluginMain.getInstance().getConfig().set(...) and then PluginMain.getInstance().saveConfig() to actually write the changes to disk.
    Code (Java):
    event.getBlock().setType(event.getBlock().getType());
    This line won't do what you're hoping either, because when you have an instance of Block (event.getBlock()), using getType() on it will go and get the material the block is in the world at the moment the code is run. To resolve this, you'll probably want to grab the material the block was immediately when the event happens, so outside of your runnables.
     
    • Like Like x 1
  2. Hello! It also might be me sometimes I will go in the source code and change a few thing and RE-PACKGE with IntelJ so it might be me
     
  3. I have made progress I have gotten it to drop the block on the floor and turn to bedrock! I still don't fully understand how to get the material of the block the moment my code runs :)
     
  4. Hello! I don't know what to do I wan't all block that are broken to rebecome that block after 100 ticks (This code may be weird)


    Code (Java):

       @EventHandler(priority = EventPriority.HIGHEST)
        public void onBlockBreak(BlockBreakEvent event) {
            Material blockType = event.getBlock().getType();
            BukkitRunnable replaceBlockRunnable = new BukkitRunnable() {
                @Override
                public void run() {
                    event.getBlock().setType(blockType);
                }
            };
            new BukkitRunnable() {
                @Override
                public void run() {
                    event.getBlock().setType(Material.BEDROCK);
                    replaceBlockRunnable.runTaskLater(BlockRegeneration.getInstance(), 100l);
                }
            }.runTaskLater(BlockRegeneration.getInstance(), 1);
        }
    }
     
     
    #5 Eman49, Jul 31, 2021
    Last edited: Aug 1, 2021
  5. You need to store the previous material type of the block in a variable to use later before you set its type to bedrock. When you call block.setType(block.getType) you are just setting the material to bedrock again, as that is what you set it to before the runnable executes. event.getBlock() does not return an image of the state of the block when the event happened, you get the current block as it appears on the server.
     
  6. Firstly, some issues with your code in general that can be improved on.
    - Why are you writing out all the package names every time rather than just importing? It makes it look so unreadable if you do that without purpose.
    - Why have a try and catch for no reason.
    - Why are you breaking a block after it was broken?
    - Why does your method have a random number after it. Choose a better name for it, rather than adding a 1.
    - Why are you delaying for a double that you immediately cast to a long? You can simply write 10

    All you have to do is the following:

    Listen to on break
    - Save the block data and material.
    - new Scheduler to run 200 (In ticks, not seconds. 20ticks = 1 second)
    - Set the location block back to the material and apply the block data.
     
    • Like Like x 2
  7. I know How ticks work... I just did that to test if it works and no Im not breaking the block after it's broken
     
  8. What issue are you having then?
     
  9. You are listening to block break event. If a block is broken you get the block and break it

    event.getBlock().breakNaturally();

    Overall, clean up your code and remove the unnecessary stuff. If you do not optimize and clean your code, it is going to get a lot harder to read and waste time in the long run. In addition, code like that makes you look like a beginner/noob.

    Follow what I said above, and your code should work out fine.

    P.S. Also use better forum names. "Stuck" is not proper at all.
     
    • Agree Agree x 1
  10. Well, here's how I'd write it:
    Code (Java):
    @EventHandler(priority = EventPriority.HIGHEST)
        public void onBlockBreak(BlockBreakEvent event) {
           Material blockType = event.getBlock().getType(); // grab the block type outside the runnable
           // personally I put this in a variable because that way you're not dealing with so many nested brackets but it's just preference
           BukkitRunnable replaceBlockRunnable = new BukkitRunnable() {
               @Override
                public void run() {
                    event.getBlock().setType(blockType);
                }
            };
           new BukkitRunnable() {
               @Override
               public void run() {
                   event.getBlock().setType(Material.BEDROCK);
                   replaceBlockRunnable.runTaskLater(PluginMain.getInstance(), 100l);
               }
           }.runTaskLater(PluginMain.getInstance(), 1);
        }
    Here's why:
    Under no circumstances is event.getBlock() going to be null, it's tagged with @NotNull in the javadocs. It also seems kinda pointless to make sure a Block is a Block, so I've completely removed this if statement.
    You've not cancelled the event, so the block will be broken at the end of the event. No need to do it manually.
    Catching Exception is almost always either bad or useless, depending on how you use it. Here it's useless, most points where Bukkit executes plugin code, including runnables, are already wrapped in try-catch blocks.
     
    #11 Datatags, Jul 31, 2021
    Last edited: Jul 31, 2021
    • Like Like x 1
  11. Thank you :)
     
  12. It's better than it was
     
  13. So are you experiencing an issue with the code currently in your original post? Or looking for code review? I've just loaded it into a test plugin and it worked perfectly.

    Edit: While we're here, here's an even better way of accomplishing this.
    Code (Java):
    @EventHandler(priority = EventPriority.HIGHEST)
        public void onBlockBreak(BlockBreakEvent event) {
            BlockState state = event.getBlock().getState();
            event.setCancelled(true);
            event.getBlock().setType(Material.BEDROCK);
            new BukkitRunnable() {
                @Override
                public void run() {
                    state.update(true);
                }
            }.runTaskLater(BlockRegeneration.getInstance(), 100l);
        }
    This way, if, for example, a furnace is broken, it will put the items back in the furnace when it resets the block.
     
    #14 Datatags, Aug 1, 2021
    Last edited: Aug 1, 2021
    • Winner Winner x 2