Add QueueHolder API
This commit is contained in:
@@ -3,6 +3,7 @@ package us.ajg0702.queue.api;
|
||||
import us.ajg0702.queue.api.events.utils.EventReceiver;
|
||||
import us.ajg0702.queue.api.premium.Logic;
|
||||
import us.ajg0702.queue.api.premium.LogicGetter;
|
||||
import us.ajg0702.queue.api.queueholders.QueueHolderRegistry;
|
||||
import us.ajg0702.queue.api.spigot.AjQueueSpigotAPI;
|
||||
import us.ajg0702.queue.api.util.QueueLogger;
|
||||
import us.ajg0702.utils.common.Config;
|
||||
@@ -12,6 +13,8 @@ import java.util.concurrent.ExecutorService;
|
||||
|
||||
public abstract class AjQueueAPI {
|
||||
|
||||
public static QueueHolderRegistry queueHolderRegistry = new QueueHolderRegistry();
|
||||
|
||||
public static AjQueueAPI INSTANCE;
|
||||
public static AjQueueSpigotAPI SPIGOT_INSTANCE;
|
||||
|
||||
@@ -121,6 +124,10 @@ public abstract class AjQueueAPI {
|
||||
*/
|
||||
public abstract void shutdown();
|
||||
|
||||
public static QueueHolderRegistry getQueueHolderRegistry() {
|
||||
return queueHolderRegistry;
|
||||
}
|
||||
|
||||
public abstract <E> void listen(Class<E> event, EventReceiver<E> handler);
|
||||
|
||||
public abstract ExecutorService getServersUpdateExecutor();
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
package us.ajg0702.queue.api;
|
||||
|
||||
public interface QueueHolder {
|
||||
boolean isAvailable();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package us.ajg0702.queue.api.queueholders;
|
||||
|
||||
import us.ajg0702.queue.api.players.AdaptedPlayer;
|
||||
import us.ajg0702.queue.api.players.QueuePlayer;
|
||||
import us.ajg0702.queue.api.queues.QueueServer;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public abstract class QueueHolder {
|
||||
|
||||
private final QueueServer queueServer;
|
||||
|
||||
public QueueHolder(QueueServer queueServer) {
|
||||
this.queueServer = queueServer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the identifier of this QueueHolder
|
||||
* Used by the server owner in order to tell ajQueue to use this QueueHolder
|
||||
* @return a string that is very unlikely to be re-used by another QueueHolder
|
||||
*/
|
||||
public abstract String getIdentifier();
|
||||
|
||||
|
||||
/**
|
||||
* Adds a player to the end of the queue
|
||||
* NOTE: Do not manually call this! Use the QueueManager to add players to queues
|
||||
* @param player The QueuePlayer to add
|
||||
*/
|
||||
public abstract void addPlayer(QueuePlayer player);
|
||||
|
||||
/**
|
||||
* Adds a player to the specified position in the queue
|
||||
* NOTE: Do not manually call this! Use the QueueManager to add players to queues
|
||||
* @param player The QueuePlayer to add
|
||||
* @param position The position to add them to
|
||||
*/
|
||||
public abstract void addPlayer(QueuePlayer player, int position);
|
||||
|
||||
public void removePlayer(AdaptedPlayer player) {
|
||||
removePlayer(player.getUniqueId());
|
||||
}
|
||||
|
||||
public void removePlayer(UUID uuid) {
|
||||
QueuePlayer player = findPlayer(uuid);
|
||||
if(player == null) return;
|
||||
removePlayer(player);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a player from the queue
|
||||
* @param player The player to remove
|
||||
*/
|
||||
public abstract void removePlayer(QueuePlayer player);
|
||||
|
||||
/**
|
||||
* Finds the player with this uuid in this queue and returns the representative QueuePlayer
|
||||
* @return The QueuePlayer representing the player, null if not found
|
||||
*/
|
||||
public abstract QueuePlayer findPlayer(UUID uuid);
|
||||
|
||||
/**
|
||||
* Finds the player with this username in this queue and returns the representative QueuePlayer
|
||||
* @return The QueuePlayer representing the player, null if not found
|
||||
*/
|
||||
public abstract QueuePlayer findPlayer(String name);
|
||||
|
||||
public QueuePlayer findPlayer(AdaptedPlayer player) {
|
||||
return findPlayer(player.getUniqueId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the size of the queue
|
||||
* @return The number of players in the queue
|
||||
*/
|
||||
public abstract int getQueueSize();
|
||||
|
||||
/**
|
||||
* Get all players that are in the queue
|
||||
* @return a list of players in the queue
|
||||
*/
|
||||
public abstract List<QueuePlayer> getAllPlayers();
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package us.ajg0702.queue.api.queueholders;
|
||||
|
||||
import us.ajg0702.queue.api.AjQueueAPI;
|
||||
import us.ajg0702.queue.api.queues.QueueServer;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class QueueHolderRegistry {
|
||||
|
||||
private Map<String, Class<? extends QueueHolder>> holders = new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* Register a QueueHolder that can be used
|
||||
* @param holder The QueueHolder to register
|
||||
*/
|
||||
public void register(String identifier, Class<? extends QueueHolder> holder) {
|
||||
holders.put(identifier, holder);
|
||||
}
|
||||
|
||||
public QueueHolder getQueueHolder(QueueServer queueServer) {
|
||||
String queueHolderName = AjQueueAPI.getInstance().getConfig().getString("queue-holder");
|
||||
QueueHolder queueHolder = getQueueHolder(queueHolderName, queueServer);
|
||||
if(queueHolder == null) {
|
||||
AjQueueAPI.getInstance().getLogger().warn("Invalid queue-holder '" + queueHolderName + "'! Using the default one");
|
||||
return getQueueHolder("default", queueServer);
|
||||
}
|
||||
return queueHolder;
|
||||
}
|
||||
|
||||
public QueueHolder getQueueHolder(String identifier, QueueServer queueServer) {
|
||||
Class<? extends QueueHolder> holder = holders.get(identifier);
|
||||
if(holder == null) return null;
|
||||
try {
|
||||
return holder.getConstructor(QueueServer.class).newInstance(queueServer);
|
||||
} catch(NoSuchMethodException e) {
|
||||
throw new IllegalArgumentException("QueueHolder " + identifier + " is missing the required constructor!");
|
||||
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package us.ajg0702.queue.api.queues;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import us.ajg0702.queue.api.players.AdaptedPlayer;
|
||||
import us.ajg0702.queue.api.players.QueuePlayer;
|
||||
import us.ajg0702.queue.api.queueholders.QueueHolder;
|
||||
import us.ajg0702.queue.api.server.AdaptedServer;
|
||||
|
||||
import java.util.List;
|
||||
@@ -17,7 +18,9 @@ public interface QueueServer {
|
||||
/**
|
||||
* Get the players who are queued.
|
||||
* @return The players who are queued
|
||||
* @deprecated It is recommended to not use this method unless you absolutely have to. If you have to, use getQueueHolder().getAllPlayers()
|
||||
*/
|
||||
@Deprecated
|
||||
ImmutableList<QueuePlayer> getQueue();
|
||||
|
||||
/**
|
||||
@@ -219,6 +222,12 @@ public interface QueueServer {
|
||||
*/
|
||||
Balancer getBalancer();
|
||||
|
||||
/**
|
||||
* Gets the QueueHolder for this queue
|
||||
* @return the QueueHolder that holds the queue
|
||||
*/
|
||||
QueueHolder getQueueHolder();
|
||||
|
||||
|
||||
/**
|
||||
* elliot is bad
|
||||
|
||||
Reference in New Issue
Block a user