Skip to content

Commit

Permalink
component logger and kick notification
Browse files Browse the repository at this point in the history
  • Loading branch information
bierdosenhalter committed Oct 7, 2024
1 parent bdb9fbf commit 67382a9
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 56 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
plugin_version=3.0.3-INDEV
plugin_version=3.0.4-INDEV
velocity_api_version=3.3.0-SNAPSHOT
minecraft_version=1.20.6
2 changes: 1 addition & 1 deletion run/proxy/velocity.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ player-info-forwarding-mode = "MODERN"
# The file is expected to be UTF-8 encoded and not empty.
forwarding-secret-file = "forwarding.secret"

# Announce whether or not your server supports Forge. If you run a modded server, we
# Announce whether your server supports Forge. If you run a modded server, we
# suggest turning this on.
#
# If your network runs one modpack consistently, consider using ping-passthrough = "mods"
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/org/zeroBzeroT/anarchyqueue/Components.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.zeroBzeroT.anarchyqueue;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;

/**
* helper method because the MiniMessage syntax is too verbose
*/
public final class Components {
public static Component mm(String miniMessageString) { // mm, short for MiniMessage
return MiniMessage.miniMessage().deserialize(miniMessageString);
}
}
18 changes: 9 additions & 9 deletions src/main/java/org/zeroBzeroT/anarchyqueue/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,30 @@
import com.velocitypowered.api.plugin.Plugin;
import com.velocitypowered.api.plugin.annotation.DataDirectory;
import com.velocitypowered.api.proxy.ProxyServer;
import org.slf4j.Logger;
import net.kyori.adventure.text.logger.slf4j.ComponentLogger;

import java.nio.file.Path;

@Plugin(
id = "anarchyqueue",
name = "AnarchyQueue",
version = BuildConstants.VERSION,
description = "velocity queue system for anarchy servers",
url = "https://github.com/zeroBzeroT/AnarchyQueue",
authors = {"bierdosenhalter", "nothub"}
id = "anarchyqueue",
name = "AnarchyQueue",
version = BuildConstants.VERSION,
description = "velocity queue system for anarchy servers",
url = "https://github.com/zeroBzeroT/AnarchyQueue",
authors = {"bierdosenhalter", "nothub"}
)

public class Main {
private static Main instance;

public final Logger log;
public final ComponentLogger log;

private final ProxyServer server;

private final Path dataDir;

@Inject
public Main(ProxyServer server, CommandManager commandManager, Logger logger, @DataDirectory final Path dataDir) {
public Main(ProxyServer server, CommandManager commandManager, ComponentLogger logger, @DataDirectory final Path dataDir) {
this.server = server;
this.log = logger;
this.dataDir = dataDir;
Expand Down
89 changes: 44 additions & 45 deletions src/main/java/org/zeroBzeroT/anarchyqueue/Queue.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,38 @@
package org.zeroBzeroT.anarchyqueue;

import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.player.KickedFromServerEvent;
import com.velocitypowered.api.event.player.ServerConnectedEvent;
import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.text.Component;
import org.slf4j.Logger;
import net.kyori.adventure.text.logger.slf4j.ComponentLogger;

import java.time.Duration;
import java.time.Instant;
import java.util.List;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Semaphore;

import static org.zeroBzeroT.anarchyqueue.Components.mm;

// velocity api event docs:
// https://jd.papermc.io/velocity/3.3.0/com/velocitypowered/api/event/package-summary.html

public class Queue {
private final Logger log;
private final ComponentLogger log;

private final ProxyServer proxyServer;

private final Semaphore queueSemaphore = new Semaphore(1);

/**
* We don't use ConcurrentLinkedQueue for this because we want index-based access to players.
*/
private final List<QueuedPlayer> queuedPlayers = new CopyOnWriteArrayList<>();
private final Deque<QueuedPlayer> queuedPlayers = new LinkedList<>();

/**
* Initializes a queue
* Initializes the queue.
*/
public Queue(ProxyServer proxyServer) {
this.log = Main.getInstance().log;
Expand All @@ -48,24 +47,23 @@ public Queue(ProxyServer proxyServer) {
}

/**
* This event is called once a connection to a server is fully operational.
* Add a Player to the queue if they join the queue server.
* This event is fired once the player has successfully connected to the
* target server and the connection to the previous server has been de-established.
*/
@Subscribe
public void onServerConnectedEvent(ServerConnectedEvent e) {
if (!e.getServer().getServerInfo().getName().equals(Config.serverQueue))
public void onServerConnected(ServerConnectedEvent event) {
if (!event.getServer().getServerInfo().getName().equals(Config.serverQueue))
return;

var queuedPlayer = new QueuedPlayer(e.getPlayer(), System.currentTimeMillis());

if (queuedPlayers.contains(queuedPlayer))
// stop adding the player several times
if (queuedPlayers.stream().anyMatch(p -> p.player() == event.getPlayer()))
return;

// Add Player to queue
try {
queueSemaphore.acquire();
queuedPlayers.add(queuedPlayer);
log.info("\u00A7f" + e.getPlayer().getUsername() + "\u00A73 was added to the §dplayer queue\u00A73. Queue count is " + queuedPlayers.size() + ".");
queuedPlayers.add(new QueuedPlayer(event.getPlayer(), System.currentTimeMillis()));
log.info(mm("<white>" + event.getPlayer().getUsername() + "<dark_aqua> was added to the <light_purple>queue<dark_aqua>. Queue count is " + queuedPlayers.size() + "."));
} catch (InterruptedException e1) {
e1.printStackTrace();
} finally {
Expand All @@ -74,9 +72,21 @@ public void onServerConnectedEvent(ServerConnectedEvent e) {
}

/**
* Try to connect one player to the server
* Fired when a player is kicked from a server.
*/
@Subscribe
public void onKickedFromServer(KickedFromServerEvent event) {
log.info(mm("<white>" + event.getPlayer().getUsername() + "<dark_aqua> was kicked from <light_purple>" + event.getServer().getServerInfo().getName() + "<dark_aqua> for <light_purple>").append(event.getServerKickReason().isPresent() ? event.getServerKickReason().get() : mm("<empty>")).append(mm("<dark_aqua>.")));
}

/**
* Try to connect one player to the server.
*/
public void flushQueue() {
// Ignore if queue is empty
if (queuedPlayers.isEmpty())
return;

// check queue server reachability
final RegisteredServer serverQueue;

Expand All @@ -87,22 +97,16 @@ public void flushQueue() {
return;
}

// Ignore if queue is empty
if (queuedPlayers.isEmpty())
return;

// check the main server reachability
final RegisteredServer serverMain;

try {
serverMain = getServer(Config.serverMain);
} catch (ServerNotReachableException e) {
if (Instant.now().getEpochSecond() % 10 == 0) {
serverQueue.getPlayersConnected().forEach(queuedPlayer ->
queuedPlayer.sendMessage(Identity.nil(), Component.text(
Config.messageOffline
)));
serverQueue.getPlayersConnected().forEach(queuedPlayer -> queuedPlayer.sendMessage(Component.text(Config.messageOffline)));
}

return;
}

Expand Down Expand Up @@ -145,21 +149,21 @@ public void flushQueue() {
serverQueue.getPlayersConnected().stream()
.filter(p -> p.getUniqueId().equals(uuid))
.findAny().ifPresentOrElse(p -> {
p.sendMessage(Identity.nil(), Component.text(Config.messageConnecting));
p.sendMessage(Component.text(Config.messageConnecting));
try {
if (p.createConnectionRequest(serverMain).connect().get().isSuccessful()) {
queuedPlayers.removeFirst();
log.info("\u00A7f" + p.getUsername() + "\u00A73 connected to server §b" + serverMain.getServerInfo().getName() + "\u00A73. Queue count is " + serverQueue.getPlayersConnected().size() + ". Main count is " + (serverMain.getPlayersConnected().size()) + " of " + Config.maxPlayers + ".");
log.info(mm("<white>" + p.getUsername() + "<dark_aqua> connected to server <aqua>" + serverMain.getServerInfo().getName() + "<dark_aqua>. Queue count is " + serverQueue.getPlayersConnected().size() + ". Main count is " + (serverMain.getPlayersConnected().size()) + " of " + Config.maxPlayers + "."));
}
} catch (InterruptedException | ExecutionException e) {
log.error("\u00A7f" + p.getUsername() + "s\u00A7c connection to server §b" + Config.serverMain + "\u00A7c failed: " + e.getMessage());
log.error(mm("<white>" + p.getUsername() + "s<red> connection to server <aqua>" + Config.serverMain + "<red> failed: " + e.getMessage()));
// FIXME: requeue
queuedPlayers.removeFirst();
queuedPlayers.add(new QueuedPlayer(finalCurrPlayer.player(), System.currentTimeMillis()));
}
},
() -> {
log.error("\u00A7f" + finalCurrPlayer.player().getUsername() + "s\u00A7c connection to server §b" + Config.serverMain + "\u00A7c failed: player is not connected to " + serverQueue.getServerInfo().getName());
log.error(mm("<white>" + finalCurrPlayer.player().getUsername() + "s<red> connection to server <aqua>" + Config.serverMain + "<red> failed: player is not connected to " + serverQueue.getServerInfo().getName()));
queuedPlayers.removeFirst();
}
);
Expand All @@ -171,25 +175,20 @@ public void flushQueue() {
}

private void sendInfo(RegisteredServer serverQueue, boolean full) {
for (int i = 0; i < queuedPlayers.size(); i++) {
queuedPlayers
.get(i)
.player()
.sendMessage(Identity.nil(), Component.text(
Config.messagePosition + (i + 1) + "/" + queuedPlayers.size()
));
int i = 1;

for (QueuedPlayer player : queuedPlayers) {
player.player().sendMessage(Component.text(Config.messagePosition + (i + 1) + "/" + queuedPlayers.size()));
i++;
}

if (full) {
serverQueue.getPlayersConnected().forEach(queuedPlayer ->
queuedPlayer.sendMessage(Identity.nil(), Component.text(
Config.messageFull
)));
serverQueue.getPlayersConnected().forEach(queuedPlayer -> queuedPlayer.sendMessage(Component.text(Config.messageFull)));
}
}

private RegisteredServer getServer(String name) throws ServerNotReachableException {
// get server configured in velocity.toml by name
// Get server configured in velocity.toml by name
Optional<RegisteredServer> serverOpt = proxyServer.getServer(name);

if (serverOpt.isEmpty()) {
Expand All @@ -198,7 +197,7 @@ private RegisteredServer getServer(String name) throws ServerNotReachableExcepti

final RegisteredServer server = serverOpt.get();

// test server availability by pinging
// Test server availability by pinging
try {
server.ping().get();
} catch (InterruptedException | ExecutionException e) {
Expand Down

0 comments on commit 67382a9

Please sign in to comment.