Timed

Discussion in 'Spigot Plugin Development' started by Salman, May 12, 2017.

  1. Hello, i want to my players to place a cobweb and then after 5 seconds that cobweb will be removed by the server how to do that?
     
  2. Test for when a block is placed, see if it's a cobweb. Save the location, start a timer for 5 seconds and when it's over remove the block at the location
     
    • Funny Funny x 1
  3. Use BlockPlaceEvent, if it's a cobweb run a method that take a block as parameter. In that method start a delayed task that remove the block after 100 ticks.
     
    #3 GB_6, May 12, 2017
    Last edited: May 12, 2017
  4. And how shall i start a timer?
     
  5. Here is the method I use in a lil plugin:

    Code (Text):
        @EventHandler
        public void onLogin(PlayerLoginEvent event) {
            final Player player = event.getPlayer();
            new BukkitRunnable() {
                public void run() {
                    ruleBook(player);
                }
            }.runTaskLater(this, 20);
        }
     
    the 20 in runTaskLater is ticks, 20 ticks a second in optimal conditions

    BukkitRunnable
     
  6. You shouldn't run for each broken block a new task, its better to create 1 static task, where you only save the current ( current system time + 5000ms ) after breaking a block in a map and then you check if the timestamp has passed the current system time
     
  7. Why recreate what the server is doing already? Plus its spider webs not cobble so its not like there will be thousands of them.
     
  8. All the scheduler does is insert tasks into a queue to be executed anyhow, this really isn't a matter of clogging up an executor. If it took a long time to execute, then there'd be concern.
     
  9. So this does'nt work what to do?
    Code (Text):

        @EventHandler
        public void onLogin(PlayerLoginEvent event) {
            final Player player = event.getPlayer();
            new BukkitRunnable() {
                public void run() {
                    ruleBook(player);
                }
            }.runTaskLater(this, 20);
        }
     
     
  10. What does not work?
     
  11. It was an example from working code of a runnable inside a listener not spoon fed code. You will need to change the event type to whats called when blocks are placed. Instead of passing a player object, I would pass the block location. Before you blindly delete the block check it to make sure it exists and that its still a cobweb.
     
  12. What you do is stop blindly copy/pasting code and start learning: http://bukkit.gamepedia.com/Scheduler_Programming
     
  13. Yea you are right, but i thought if he had like 100 players doing 24/7 the same thing like placing a cobweb then there will be a huge amount of tasks running in the queue, ( even if its only a 5 sec delay )

    (Correct me if i'm wrong)
     
  14. They don't run until it's time for them to run. It's basically a SynchronousQueue<Runnable>, which just stores the tasks.