Executing tasks when a plugin is disabled

Discussion in 'Plugin Development' started by Drkmaster83, Jan 25, 2014.

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

    Drkmaster83

    Is there a way to get all pending tasks and execute them with no delay before the plugin disables?

    My reason for this is that I have a plugin that spawns items, and I have them being removed after 15 ticks, but if the plugin disables, the task that does this is cancelled. Is there any way to remove them after its task has been disabled? (The task is executed in a method, and the method is used in an external class).

    As a possible solution to this as well, is there any way to make an item remove itself in 15 ticks other than a task, but perhaps through natural item despawning?
     
  2. Offline

    xTrollxDudex

  3. Offline

    Drkmaster83

    I'd rather not remove them at all if it means having to use a separate plugin instance.
     
  4. Offline

    1Rogue

    Use ScheduledExecutorService yourself, forgoing bukkit's scheduler. A copy of the class I use for this is below:

    Show Spoiler
    Code:java
    1. /*
    2.  * Copyright (C) 2013 Spencer Alderman
    3.  *
    4.  * This program is free software: you can redistribute it and/or modify
    5.  * it under the terms of the GNU General Public License as published by
    6.  * the Free Software Foundation, either version 3 of the License, or
    7.  * (at your option) any later version.
    8.  *
    9.  * This program is distributed in the hope that it will be useful,
    10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12.  * GNU General Public License for more details.
    13.  *
    14.  * You should have received a copy of the GNU General Public License
    15.  * along with this program. If not, see <[url]http://www.gnu.org/licenses/>[/url].
    16.  */
    17. package com.rogue.plugintemplate.scheduler;
    18.  
    19. import com.rogue.plugintemplate.PluginTemplate;
    20. import java.util.ArrayList;
    21. import java.util.List;
    22. import java.util.concurrent.Callable;
    23. import java.util.concurrent.Executors;
    24. import java.util.concurrent.ScheduledExecutorService;
    25. import java.util.concurrent.ScheduledFuture;
    26. import java.util.concurrent.TimeUnit;
    27.  
    28. /**
    29.  * Handles and schedules tasks for the plugin in a JVM-accurate time-base, as
    30.  * opposed to depending on any other build's time system which will lag with
    31.  * ticks
    32.  *
    33.  * @since 1.0.0
    34.  * @author 1Rogue
    35.  * @version 1.0.0
    36.  */
    37. public class ExecScheduler {
    38.  
    39. private final PluginTemplate plugin;
    40. private final ScheduledExecutorService es;
    41. private final List<ScheduledFuture<?>> executives = new ArrayList();
    42.  
    43. /**
    44.   * {@link ExecScheduler} constructor
    45.   *
    46.   * @since 1.0.0
    47.   * @version 1.0.0
    48.   *
    49.   * @param plugin Main {@link PluginTemplate} instance
    50.   */
    51. public ExecScheduler(PluginTemplate plugin) {
    52. this.plugin = plugin;
    53. this.es = Executors.newScheduledThreadPool(10);
    54. }
    55.  
    56. /**
    57.   * Runs a repeating asynchronous task under {@link PluginTemplate}
    58.   *
    59.   * @since 1.0.0
    60.   * @version 1.0.0
    61.   *
    62.   * @param r The runnable to execute
    63.   * @param startAfter Time (in seconds) to wait before execution
    64.   * @param delay Time (in seconds) between execution to wait
    65.   */
    66. public void runAsyncTaskRepeat(Runnable r, long startAfter, long delay) {
    67. this.executives.add(this.es.scheduleWithFixedDelay(r, startAfter, delay, TimeUnit.SECONDS));
    68. }
    69.  
    70. /**
    71.   * Runs a single asynchronous task under {@link PluginTemplate}
    72.   *
    73.   * @since 1.0.0
    74.   * @version 1.0.0
    75.   *
    76.   * @param r The runnable to execute
    77.   * @param delay Time (in seconds) to wait before execution
    78.   */
    79. public void runAsyncTask(Runnable r, long delay) {
    80. this.executives.add(this.es.schedule(r, delay, TimeUnit.SECONDS));
    81. }
    82.  
    83. /**
    84.   * Runs a {@link Callable}
    85.   *
    86.   * @since 1.0.0
    87.   * @version 1.0.0
    88.   *
    89.   * @param c The callable to execute
    90.   * @param delay Time (in seconds) to wait before execution
    91.   */
    92. public void runCallable(Callable<?> c, long delay) {
    93. this.es.schedule(c, delay, TimeUnit.SECONDS);
    94. }
    95.  
    96. /**
    97.   * Cancels all running tasks/threads under {@link PluginTemplate}
    98.   *
    99.   * @since 1.0.0
    100.   * @version 1.0.0
    101.   */
    102. public void cancelAllTasks() {
    103. for (ScheduledFuture<?> s : this.executives) {
    104. s.cancel(false);
    105. }
    106. }
    107.  
    108. }
    109.  


    You could potentially edit this to create a wrapper for "cancelable" tasks, and then you would simply make the items non-cancelable.
     
Thread Status:
Not open for further replies.

Share This Page