This commit is contained in:
T00thpick1 2013-03-11 00:15:57 -04:00 committed by NuclearW
parent b700a99c52
commit d59954d0f1
5 changed files with 68 additions and 38 deletions

View File

@ -129,10 +129,9 @@ public class McrankCommand implements CommandExecutor {
}
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...");
}
}

View File

@ -126,14 +126,12 @@ public class MctopCommand implements CommandExecutor {
// 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...");
}
}

View File

@ -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<Queueable> queue;
private boolean running;
import org.bukkit.scheduler.BukkitScheduler;
import com.gmail.nossr50.mcMMO;
public class AsyncQueueManager {
private List<Queue> queues;
protected LinkedBlockingQueue<Queueable> queue;;
public AsyncQueueManager(BukkitScheduler scheduler, int number) {
this.queues = new ArrayList<Queue>();
for (int i = 1; i <= number; i++) {
Queue queue = new Queue();
scheduler.runTaskAsynchronously(mcMMO.p, queue);
this.queues.add(queue);
}
public AsyncQueueManager() {
this.queue = new LinkedBlockingQueue<Queueable>();
this.running = true;
}
@Override
public void run() {
while(running) {
try {
queue.take().run();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
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;
@ -48,16 +45,28 @@ public class AsyncQueueManager implements Runnable {
if (obj instanceof Queueable) {
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;
}
}
}

View File

@ -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;
}
}

View File

@ -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