Solved Place block and then remove it after x seconds

Discussion in 'Plugin Development' started by Mrwinkles, Nov 3, 2012.

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

    Mrwinkles

    Hello!

    Can someone please help me figure this one out?

    example:

    Player places block x after y seconds remove x block from the map.

    Thanks.
     
  2. Offline

    Unscrewed

    Place the block location and type in a hashmap, start a timer, remove the block (get location and type from hashmap) once the timer runs out.

    Or did you want some code? :p

    EDIT: I guess you could use the scheduler too.
     
  3. Offline

    ferrybig

    dont use a timer as it will cause bugs whit bukkit (but like al threading isues, its unknown when (but it will be worrse)) use an sync task whit the schedular instead
     
  4. Offline

    fireblast709

    Example code (if you want). Replace the PluginInstance with an actual instance of your plugin, and timeinseconds with an amount x seconds:
    Code:java
    1. @EventHandler
    2. public void onPlace(BlockPlaceEvent event)
    3. {
    4. final Player p = event.getPlayer();
    5. final Block b = event.getBlock();
    6. if(!p.hasPermission("can.I.Build.Here")) // Or whatever
    7. {
    8. Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable()
    9. {
    10. @Override
    11. public void run()
    12. {
    13. b.setType(Material.AIR);
    14. // Optional: give item back
    15. p.getInventory().addItem(new ItemStack(b.getType(), 1));
    16. // Optional: send message
    17. p.sendMessage("You cannot build here");
    18.  
    19. }
    20. }, timeinseconds*20L);
    21. }
    22. }


    [Edit] You can also use b.breakNaturally();
     
  5. Offline

    Xenon117

    I would use the shedular as well, but not with a hashmap.
    I would create a new class that implements a runnable interface. The constructor should take a location.
    In the run() method the block should be replaced with air.
    HashMaps with actual Locations and Worlds etc. could result in memory leaks.
     
  6. Offline

    fireblast709

    Xenon117 or just be lazy like me ;3
     
  7. Offline

    ferrybig

    its better if you first give the item back to the player, and then remove the block, as your setting the block to air, and then giving air to the player, :p
     
  8. Offline

    fireblast709

    ferrybig mmh true, a fail on my side xD
     
  9. Offline

    Mrwinkles

    Nvm figured it all out thank you!
     
  10. Offline

    fireblast709

    You would need an instance of the main class:
    Code:java
    1. Bukkit.getScheduler().scheduleSyncDelayedTask(Plugin instanceOfMainClass, new Runnable()
    2. {
    3. @Override
    4. public void run()
    5. {
    6. // Whatever you want
    7. }
    8. }, delayInSeconds*20L);
     
Thread Status:
Not open for further replies.

Share This Page