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

@ -123,16 +123,15 @@ public class McrankCommand implements CommandExecutor {
} }
private void sqlDisplay(CommandSender sender, String playerName) { private void sqlDisplay(CommandSender sender, String playerName) {
//TODO: Localize messages // TODO: Localize messages
if (mcMMO.queueManager.contains(sender.getName())) { if (mcMMO.queueManager.contains(sender.getName())) {
sender.sendMessage(ChatColor.RED + "Please wait for your previous command to process"); sender.sendMessage(ChatColor.RED + "Please wait for your previous command to process");
} }
if (!mcMMO.queueManager.queue(new McRankAsync(playerName, sender))) { 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 + "Queue is full");
sender.sendMessage(ChatColor.RED + "Unable to add to queue.");
} }
sender.sendMessage(ChatColor.YELLOW + "Calculating mcMMO rankings..."); sender.sendMessage(ChatColor.YELLOW + "Calculating rankings...");
} }
} }

View File

@ -123,17 +123,15 @@ public class MctopCommand implements CommandExecutor {
} }
private void sqlDisplay(int page, String query, CommandSender sender) { private void sqlDisplay(int page, String query, CommandSender sender) {
//TODO: Localize messages // TODO: Localize messages
if (mcMMO.queueManager.contains(sender.getName())) { if (mcMMO.queueManager.contains(sender.getName())) {
sender.sendMessage(ChatColor.RED + "Please wait for your previous command to process"); sender.sendMessage(ChatColor.RED + "Please wait for your previous command to process");
return;
} }
if (!mcMMO.queueManager.queue(new McTopAsync(page, query, sender))) { 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 + "Queue is full");
sender.sendMessage(ChatColor.RED + "Unable to add to queue.");
} }
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; package com.gmail.nossr50.database.queuemanager;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
public class AsyncQueueManager implements Runnable { import org.bukkit.scheduler.BukkitScheduler;
private LinkedBlockingQueue<Queueable> queue;
private boolean running;
public AsyncQueueManager() {
this.queue = new LinkedBlockingQueue<Queueable>();
this.running = true;
}
@Override import com.gmail.nossr50.mcMMO;
public void run() {
while(running) { public class AsyncQueueManager {
try {
queue.take().run(); private List<Queue> queues;
} catch (InterruptedException e) { protected LinkedBlockingQueue<Queueable> queue;;
e.printStackTrace();
} 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);
} }
this.queue = new LinkedBlockingQueue<Queueable>();
} }
public boolean queue(Queueable task) { public boolean queue(Queueable task) {
@ -30,12 +33,6 @@ public class AsyncQueueManager implements Runnable {
return queue.contains(new EqualString(player)); 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 class EqualString {
private String player; private String player;
@ -46,18 +43,30 @@ public class AsyncQueueManager implements Runnable {
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (obj instanceof Queueable) { if (obj instanceof Queueable) {
return ((Queueable)obj).getPlayer().equalsIgnoreCase(player); return ((Queueable) obj).getPlayer().equalsIgnoreCase(player);
} }
return false; 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 @Override
public void run() { } public void run() {
}
@Override @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); scheduler.scheduleSyncRepeatingTask(this, new BleedTimerTask(), 40, 40);
if (Config.getInstance().getUseMySQL()) { if (Config.getInstance().getUseMySQL()) {
queueManager = new AsyncQueueManager(); queueManager = new AsyncQueueManager(scheduler, Config.getInstance().getQueueNumber());
scheduler.runTaskAsynchronously(this, queueManager);
} }
// Old & Powerless User remover // Old & Powerless User remover