Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6886417
commit d769e4a
Showing
9 changed files
with
237 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
84 changes: 84 additions & 0 deletions
84
api/src/main/java/us/ajg0702/queue/api/queueholders/QueueHolder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} |
44 changes: 44 additions & 0 deletions
44
api/src/main/java/us/ajg0702/queue/api/queueholders/QueueHolderRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
common/src/main/java/us/ajg0702/queue/common/DefaultQueueHolder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package us.ajg0702.queue.common; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import us.ajg0702.queue.api.players.QueuePlayer; | ||
import us.ajg0702.queue.api.queueholders.QueueHolder; | ||
import us.ajg0702.queue.api.queues.QueueServer; | ||
|
||
import java.util.List; | ||
import java.util.UUID; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
|
||
public class DefaultQueueHolder extends QueueHolder { | ||
|
||
List<QueuePlayer> queue = new CopyOnWriteArrayList<>(); | ||
|
||
public DefaultQueueHolder(QueueServer queueServer) { | ||
super(queueServer); | ||
} | ||
|
||
@Override | ||
public String getIdentifier() { | ||
return "default"; | ||
} | ||
|
||
@Override | ||
public void addPlayer(QueuePlayer player) { | ||
queue.add(player); | ||
} | ||
|
||
@Override | ||
public void addPlayer(QueuePlayer player, int position) { | ||
queue.add(position, player); | ||
} | ||
|
||
@Override | ||
public void removePlayer(QueuePlayer player) { | ||
queue.remove(player); | ||
} | ||
|
||
@Override | ||
public QueuePlayer findPlayer(UUID uuid) { | ||
for(QueuePlayer queuePlayer : queue) { | ||
if(queuePlayer.getUniqueId().toString().equals(uuid.toString())) { | ||
return queuePlayer; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public QueuePlayer findPlayer(String name) { | ||
for(QueuePlayer queuePlayer : queue) { | ||
if(queuePlayer.getName().equalsIgnoreCase(name)) { | ||
return queuePlayer; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public int getQueueSize() { | ||
return queue.size(); | ||
} | ||
|
||
@Override | ||
public List<QueuePlayer> getAllPlayers() { | ||
return ImmutableList.copyOf(queue); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.