SHARE
    TWEET
    ferrybig

    MainThreadPlanner.java - a class designed for Bukkit progra.

    Jul 27th, 2014
    648
    0
    Never
    Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
    Java 4.73 KB | None | 0 0
    1. package sander;
    2. import java.util.ArrayList;
    3. import static java.util.Collections.addAll;
    4. import java.util.List;
    5. import static org.bukkit.Bukkit.getScheduler;
    6. import org.bukkit.plugin.Plugin;
    7. public class MainThreadPlanner<T> {
    8. private AccumulativeRunnable<T> doProcess;
    9. private final AccumulativeRunnable<Runnable> doSubmit;
    10. protected final Plugin plugin;
    11. private final Processor<T> processor;
    12. public MainThreadPlanner(Plugin plugin,Processor<T> processor) {
    13. this.doSubmit = new DoSubmitAccumulativeRunnable(plugin);
    14. this.plugin = plugin;
    15. this.processor = processor;
    16. }
    17. private void process(List<T> chunks) {
    18. this.processor.process(chunks);
    19. }
    20. /**
    21. * Sends data chunks to the {@link #process} method. This method is to be
    22. * used from inside the {@code doInBackground} method to deliver
    23. * intermediate results for processing on the <i>Bukkit Main Server
    24. * Thread</i> inside the {@code process} method.
    25. *
    26. * <p>
    27. * Because the {@code process} method is invoked asynchronously on the
    28. * <i>Bukkit Main Server Thread</i>
    29. * multiple invocations to the {@code publish} method might occur before the
    30. * {@code process} method is executed. For performance purposes all these
    31. * invocations are coalesced into one invocation with concatenated
    32. * arguments.
    33. *
    34. * <p>
    35. * For example:
    36. *
    37. * <pre>
    38. * publish(&quot;1&quot;);
    39. * publish(&quot;2&quot;, &quot;3&quot;);
    40. * publish(&quot;4&quot;, &quot;5&quot;, &quot;6&quot;);
    41. * </pre>
    42. *
    43. * might result in:
    44. *
    45. * <pre>
    46. * process(&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;, &quot;5&quot;, &quot;6&quot;)
    47. * </pre>
    48. *
    49. *
    50. * @param chunks intermediate results to process
    51. *
    52. * @see #process
    53. *
    54. */
    55. public final void publish(T... chunks) {
    56. synchronized (this) {
    57. if (this.doProcess == null) {
    58. this.doProcess = new AccumulativeRunnable<T>() {
    59. @Override
    60. public void run(List<T> args) {
    61. MainThreadPlanner.this.process(args);
    62. }
    63. @Override
    64. protected void submit() {
    65. MainThreadPlanner.this.doSubmit.add(this);
    66. }
    67. };
    68. }
    69. }
    70. this.doProcess.add(chunks);
    71. }
    72. private static abstract class AccumulativeRunnable<T extends Object> implements Runnable {
    73. private List<T> arguments = null;
    74. protected abstract void run(List<T> paramList);
    75. @Override
    76. public final void run() {
    77. this.run(this.flush());
    78. }
    79. public final synchronized void add(T... toAdd) {
    80. boolean mustSubmit = false;
    81. if (this.arguments == null) {
    82. mustSubmit = true;
    83. this.arguments = new ArrayList<>();
    84. }
    85. addAll(this.arguments, toAdd);
    86. if (mustSubmit) {
    87. this.submit();
    88. }
    89. }
    90. abstract protected void submit();
    91. private synchronized List<T> flush() {
    92. List<T> localList = this.arguments;
    93. this.arguments = null;
    94. return localList;
    95. }
    96. }
    97. public interface Processor<T> {
    98. public void process(List<T> process);
    99. }
    100. private class DoSubmitAccumulativeRunnable extends AccumulativeRunnable<Runnable> implements Runnable {
    101. /**
    102. * Time in ticks between 2 invokings of the schedular
    103. */
    104. private final static int DELAY = 1;
    105. /**
    106. * The plugin, used to schedule tasks
    107. */
    108. private final Plugin plugin;
    109. public DoSubmitAccumulativeRunnable(Plugin plugin) {
    110. this.plugin = plugin;
    111. }
    112. @Override
    113. protected void run(List<Runnable> args) {
    114. for (Runnable runnable : args) {
    115. runnable.run();
    116. }
    117. }
    118. @Override
    119. protected void submit() {
    120. if (this.plugin.isEnabled()) {
    121. getScheduler().scheduleSyncDelayedTask(this.plugin, this, DELAY);
    122. } else {
    123. // What do do, scheduling an task would throw an IllegalArgumentException, and enablking this plugin for a little time may cause bugs inside the plugin?
    124. // Mayby use reflection to access the enabled field of JavaPlugin, but this wont work whit plugins that dont exend that class
    125. throw new IllegalStateException("Plugin not enabled, unable to schadule task");
    126. }
    127. }
    128. }
    129. }
    Advertisement
    Add Comment
    Please, Sign In to add comment
    Public Pastes
    We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
    Not a member of Pastebin yet?
    Sign Up, it unlocks many cool features!

    AltStyle によって変換されたページ (->オリジナル) /