Can't Get Delayed Tasks To Work!

Discussion in 'Spigot Plugin Development' started by NathanDoesMuch, Jul 3, 2015.

  1. I am trying to make a plugin where you can walk around and it makes the ground glowstone. Then it put back the original block back after 5 seconds. When I test the plugin I get no errors and when I walk around it sets the ground as glowstone but does not remove it after 5 seconds. So the problem is it's trying to get the orrigal block from a different method. Does anyone know how to fix this? If someone could please take the time to help that would be great!

    Code (Text):
    package me.Nathan_Playz.foxtokens;

    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    public class FoxTokens extends JavaPlugin implements Listener {

        public void onEnable()
        {
        Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }

        public void onDisable()
        {
           
        }
        @EventHandler
        public void onMove(PlayerMoveEvent e)
        {
            Player p = e.getPlayer();
            if (p.isOp()) {
            final Block block = p.getLocation().subtract(0, 1, 0).getBlock();
            final Material type = block.getType();
            if (block.getType() != Material.AIR) {
           
            block.setType(Material.GLOWSTONE);
           
                Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
                  public void run(){
                    block.setType(type);
                  }
                }, 100L);
            }
        }
        }
             }

     
     
  2. You have a bit of a flawed method. If a player walks on the same block more than once before it resets, then it'll never reset to the "original" block. The original block gets overriden to Glowstone if you walk on it twice before it resets.

    How to avoid this issue?
    Only set the block type if the player isn't standing on Glowstone.
     
  3. Also, move
    Code (Text):
    final Block block = p.getLocation().subtract(0, 1, 0).getBlock();
            final Material type = block.getType();
    to inside the if statement that checks to see if the player is not standing on air.
     
  4. An even safer approach would be to only send block change packets to the clients. This way, the blocks don't really change, they just appear to. So if the server crashes in the midst of it all, there won't be glowstone anomalies all over the place.
     
    • Agree Agree x 1
  5. Everything works now! Thanks guys! Fast thing, if you step over wool it changes but replaces it with a white wool? I know because it's not storing the Data Value but how do I fix this?