From d59954d0f129f75163aa129942b28fdb9a51a5f7 Mon Sep 17 00:00:00 2001 From: T00thpick1 Date: Mon, 11 Mar 2013 00:15:57 -0400 Subject: [PATCH] Redesign --- .../commands/player/McrankCommand.java | 7 +-- .../nossr50/commands/player/MctopCommand.java | 8 +-- .../queuemanager/AsyncQueueManager.java | 63 +++++++++++-------- .../nossr50/database/queuemanager/Queue.java | 25 ++++++++ src/main/java/com/gmail/nossr50/mcMMO.java | 3 +- 5 files changed, 68 insertions(+), 38 deletions(-) create mode 100644 src/main/java/com/gmail/nossr50/database/queuemanager/Queue.java diff --git a/src/main/java/com/gmail/nossr50/commands/player/McrankCommand.java b/src/main/java/com/gmail/nossr50/commands/player/McrankCommand.java index 76d55dbde..ec6023fd1 100644 --- a/src/main/java/com/gmail/nossr50/commands/player/McrankCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/player/McrankCommand.java @@ -123,16 +123,15 @@ public class McrankCommand implements CommandExecutor { } private void sqlDisplay(CommandSender sender, String playerName) { - //TODO: Localize messages + // TODO: Localize messages if (mcMMO.queueManager.contains(sender.getName())) { sender.sendMessage(ChatColor.RED + "Please wait for your previous command to process"); } if (!mcMMO.queueManager.queue(new McRankAsync(playerName, sender))) { - // This will only run if for some reason it is unable to add to the queue - sender.sendMessage(ChatColor.RED + "Unable to add to queue."); + sender.sendMessage(ChatColor.RED + "Queue is full"); } - sender.sendMessage(ChatColor.YELLOW + "Calculating mcMMO rankings..."); + sender.sendMessage(ChatColor.YELLOW + "Calculating rankings..."); } } diff --git a/src/main/java/com/gmail/nossr50/commands/player/MctopCommand.java b/src/main/java/com/gmail/nossr50/commands/player/MctopCommand.java index 86ae7ddd2..3f4f336bc 100644 --- a/src/main/java/com/gmail/nossr50/commands/player/MctopCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/player/MctopCommand.java @@ -123,17 +123,15 @@ public class MctopCommand implements CommandExecutor { } private void sqlDisplay(int page, String query, CommandSender sender) { - //TODO: Localize messages + // TODO: Localize messages if (mcMMO.queueManager.contains(sender.getName())) { sender.sendMessage(ChatColor.RED + "Please wait for your previous command to process"); - return; } if (!mcMMO.queueManager.queue(new McTopAsync(page, query, sender))) { - // This will only run if for some reason it is unable to add to the queue - sender.sendMessage(ChatColor.RED + "Unable to add to queue."); + sender.sendMessage(ChatColor.RED + "Queue is full"); } - sender.sendMessage(ChatColor.YELLOW + "Calculating mcMMO rankings..."); + sender.sendMessage(ChatColor.YELLOW + "Calculating leaderboards..."); } } diff --git a/src/main/java/com/gmail/nossr50/database/queuemanager/AsyncQueueManager.java b/src/main/java/com/gmail/nossr50/database/queuemanager/AsyncQueueManager.java index 56f26174c..1565a49a6 100644 --- a/src/main/java/com/gmail/nossr50/database/queuemanager/AsyncQueueManager.java +++ b/src/main/java/com/gmail/nossr50/database/queuemanager/AsyncQueueManager.java @@ -1,25 +1,28 @@ package com.gmail.nossr50.database.queuemanager; +import java.util.ArrayList; +import java.util.List; import java.util.concurrent.LinkedBlockingQueue; -public class AsyncQueueManager implements Runnable { - private LinkedBlockingQueue queue; - private boolean running; - - public AsyncQueueManager() { - this.queue = new LinkedBlockingQueue(); - this.running = true; - } +import org.bukkit.scheduler.BukkitScheduler; - @Override - public void run() { - while(running) { - try { - queue.take().run(); - } catch (InterruptedException e) { - e.printStackTrace(); - } +import com.gmail.nossr50.mcMMO; + +public class AsyncQueueManager { + + private List queues; + protected LinkedBlockingQueue queue;; + + public AsyncQueueManager(BukkitScheduler scheduler, int number) { + this.queues = new ArrayList(); + + for (int i = 1; i <= number; i++) { + Queue queue = new Queue(); + scheduler.runTaskAsynchronously(mcMMO.p, queue); + this.queues.add(queue); } + + this.queue = new LinkedBlockingQueue(); } public boolean queue(Queueable task) { @@ -30,12 +33,6 @@ public class AsyncQueueManager implements Runnable { return queue.contains(new EqualString(player)); } - public void disable() { - running = false; - // Throw one more Queueable into queue to unblock take() - queue.add(new EndThread()); - } - private class EqualString { private String player; @@ -46,18 +43,30 @@ public class AsyncQueueManager implements Runnable { @Override public boolean equals(Object obj) { if (obj instanceof Queueable) { - return ((Queueable)obj).getPlayer().equalsIgnoreCase(player); + return ((Queueable) obj).getPlayer().equalsIgnoreCase(player); } - return false; } } - private class EndThread implements Queueable { + public void disable() { + for (Queue queueThread : queues) { + queueThread.kill(); + } + + for (int i = 0; i < queues.size(); i++) { + queue.offer(new KillQueue()); + } + } + + public class KillQueue implements Queueable { @Override - public void run() { } + public void run() { + } @Override - public String getPlayer() { return null; } + public String getPlayer() { + return null; + } } } diff --git a/src/main/java/com/gmail/nossr50/database/queuemanager/Queue.java b/src/main/java/com/gmail/nossr50/database/queuemanager/Queue.java new file mode 100644 index 000000000..ab3200608 --- /dev/null +++ b/src/main/java/com/gmail/nossr50/database/queuemanager/Queue.java @@ -0,0 +1,25 @@ +package com.gmail.nossr50.database.queuemanager; + +import com.gmail.nossr50.mcMMO; + +public class Queue implements Runnable { + private boolean running; + + public Queue() { + this.running = true; + } + + public void run() { + while (running) { + try { + mcMMO.queueManager.queue.take().run(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + + public void kill() { + this.running = false; + } +} diff --git a/src/main/java/com/gmail/nossr50/mcMMO.java b/src/main/java/com/gmail/nossr50/mcMMO.java index 3095b458a..b5cb02010 100644 --- a/src/main/java/com/gmail/nossr50/mcMMO.java +++ b/src/main/java/com/gmail/nossr50/mcMMO.java @@ -464,8 +464,7 @@ public class mcMMO extends JavaPlugin { scheduler.scheduleSyncRepeatingTask(this, new BleedTimerTask(), 40, 40); if (Config.getInstance().getUseMySQL()) { - queueManager = new AsyncQueueManager(); - scheduler.runTaskAsynchronously(this, queueManager); + queueManager = new AsyncQueueManager(scheduler, Config.getInstance().getQueueNumber()); } // Old & Powerless User remover