Skip to content

Commit

Permalink
fixed unsuccessful connections to the server that led to the queue bl…
Browse files Browse the repository at this point in the history
…ocking and added titles
  • Loading branch information
bierdosenhalter committed Nov 12, 2024
1 parent d03c05b commit 1924266
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 23 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ tasks {
}
}

tasks.register('runServerMain', runServer.getClass()) {
tasks.register('runServerMain', runServer.getClass() as Class<Task>) {
minecraftVersion("${project.minecraft_version}")
systemProperty('com.mojang.eula.agree', 'true')
systemProperty('Paper.skipServerPropertiesComments', 'true')
runDirectory = layout.projectDirectory.dir('run').dir('main')
dependsOn('build')
}

tasks.register('runServerQueue', runServer.getClass()) {
tasks.register('runServerQueue', runServer.getClass() as Class<Task>) {
minecraftVersion("${project.minecraft_version}")
systemProperty('com.mojang.eula.agree', 'true')
systemProperty('Paper.skipServerPropertiesComments', 'true')
Expand Down
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.5-INDEV
plugin_version=3.0.6-INDEV
velocity_api_version=3.3.0-SNAPSHOT
minecraft_version=1.20.6
3 changes: 3 additions & 0 deletions src/main/java/org/zeroBzeroT/anarchyqueue/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public class Config {

public static int waitOnKick = 16;

public static boolean sendTitle = true;

/**
* Load the config from the plugin data folder
*
Expand Down Expand Up @@ -65,5 +67,6 @@ static void loadConfig(Path path) throws IOException {
messageOffline = toml.getString("message-offline", "Server is currently down!");
kick = toml.getBoolean("kick", true);
waitOnKick = toml.getLong("wait-on-kick", 16L).intValue();
sendTitle = toml.getBoolean("send-title", true);
}
}
93 changes: 73 additions & 20 deletions src/main/java/org/zeroBzeroT/anarchyqueue/Queue.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.velocitypowered.api.proxy.server.RegisteredServer;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.logger.slf4j.ComponentLogger;
import net.kyori.adventure.title.Title;

import java.time.Duration;
import java.time.Instant;
Expand All @@ -19,6 +20,8 @@

// velocity api event docs:
// https://jd.papermc.io/velocity/3.3.0/com/velocitypowered/api/event/package-summary.html
// TODO: direct connection to the main server if the queue is empty
// TODO: replace player objects with uuids

public class Queue {
private final ComponentLogger log;
Expand Down Expand Up @@ -99,18 +102,7 @@ public void onKickedFromServer(KickedFromServerEvent event) {
Player player = event.getPlayer();
Component reason = event.getServerKickReason().isPresent() ? event.getServerKickReason().get() : mm("Kicked without a reason.");

// - kicking is not enabled
if (!Config.kick) {
// save the disconnection time
kickedPlayers.put(player, Instant.now().getEpochSecond());

// send message
player.sendMessage(mm("<gold>You were sent back to the queue for: <red>").append(reason).append(mm("<reset>")));
log.info(mm("<white>" + player.getUsername() + "<dark_aqua> was sent back to server <yellow>" + Config.queue + "<dark_aqua> after a disconnection (\"").append(reason).append(mm("<dark_aqua>\"). Kicked count is " + kickedPlayers.size() + ".")));
} else {
// set the disconnect reason from the target server (not the bungee message)
player.disconnect(reason);
}
KickOrRequeue(player, reason);
} catch (InterruptedException e1) {
e1.printStackTrace();
} finally {
Expand All @@ -119,6 +111,30 @@ public void onKickedFromServer(KickedFromServerEvent event) {
}
}

/**
* Kick a player if kicking is allowed in the config
*
* @param player Player to kick
* @param reason kicking reason from the target server
*/
private void KickOrRequeue(Player player, Component reason) {
// is kicking enabled?
if (!Config.kick) {
// save the disconnection time
kickedPlayers.put(player, Instant.now().getEpochSecond());

// send message
player.sendMessage(mm("<gold>You were sent back to the queue for: <red>").append(reason).append(mm("<reset>")));
log.info(mm("<white>" + player.getUsername() + "<dark_aqua> was sent back to server <yellow>" + Config.queue + "<dark_aqua> after a disconnection (\"").append(reason).append(mm("<dark_aqua>\"). Kicked count is " + kickedPlayers.size() + ".")));
} else {
playerQueue.remove(player);

// set the disconnect reason from the parameter (not the velocity message)
player.disconnect(reason);
log.info(mm("<white>" + player.getUsername() + "<dark_aqua> was kicked from the server (\"").append(reason).append(mm("<dark_aqua>\"). Queue count is " + playerQueue.size() + ".")));
}
}

/**
* Try to connect one player to the server.
*/
Expand Down Expand Up @@ -166,6 +182,7 @@ public void flushQueue() {
break;
}

// no player to connect to the main server
if (currPlayer == null) {
mutex.release();
return;
Expand All @@ -178,23 +195,29 @@ public void flushQueue() {
serverQueue.getPlayersConnected().stream()
.filter(p -> p.getUniqueId().equals(uuid))
.findAny().ifPresentOrElse(p -> {
// TODO: direct connection to the main server if the queue is empty
p.sendMessage(mm(Config.messageConnecting));
try {
if (p.createConnectionRequest(serverMain).connect().get().isSuccessful()) {
var conReq = p.createConnectionRequest(serverMain).connect().get();

if (conReq.isSuccessful()) {
playerQueue.removeFirst();
// Clear the title
if (Config.sendTitle) {
sendTitle(finalCurrPlayer, mm(" "), mm(" "), 0, 0, 0);
}
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 + "."));
} else {
var reason = conReq.getReasonComponent().isPresent() ? conReq.getReasonComponent().get() : mm("Connection to the main server failed.");
KickOrRequeue(finalCurrPlayer, reason);
}
} catch (InterruptedException | ExecutionException e) {
log.error(mm("<white>" + p.getUsername() + "s<red> connection to server <aqua>" + Config.target + "<red> failed: " + e.getMessage()));
// count that as a kick ;)
log.error(mm("<white>" + p.getUsername() + "s<red> connection to server <aqua>" + Config.target + "<red> failed with an exception: " + e.getMessage()));
// server down?
kickedPlayers.put(finalCurrPlayer, Instant.now().getEpochSecond());
}
},
() -> {
log.error(mm("<white>" + finalCurrPlayer.getUsername() + "s<red> connection to server <aqua>" + Config.target + "<red> failed: player is not connected to " + serverQueue.getServerInfo().getName()));
playerQueue.removeFirst();
}
// player is in the queue, but not connected to the queue server
playerQueue::removeFirst
);
} catch (InterruptedException e) {
e.printStackTrace();
Expand All @@ -211,6 +234,13 @@ public void sendUpdate() {

for (Player player : playerQueue) {
player.sendMessage(mm(Config.messagePosition.replaceAll("%position%", Integer.toString(i)).replaceAll("%size%", Integer.toString(playerQueue.size()))));

// Title in the center of the screen
if (Config.sendTitle) {
// 10 Seconds + 1 s buffer
sendTitle(player, mm(" "), mm(Config.messagePosition.replaceAll("%position%", Integer.toString(i)) + "<reset>"), 0, 20 * 11, 0);
}

i++;
}
}
Expand All @@ -237,4 +267,27 @@ private RegisteredServer getServer(String name) throws ServerNotReachableExcepti

return server;
}

/**
* Sends a title and a subtitle message to the player.
*
* @param title Title text
* @param subtitle Subtitle text
* @param fadeIn Time in ticks for titles to fade in. Defaults to 10.
* @param stay Time in ticks for titles to stay. Defaults to 70.
* @param fadeOut Time in ticks for titles to fade out. Defaults to 20.
*/
public void sendTitle(Player player, Component title, Component subtitle, int fadeIn, int stay, int fadeOut) {
try {
Title bungeeTitle = Title.title(
title,
subtitle,
Title.Times.times(Duration.ofMillis(fadeIn * 20L),
Duration.ofMillis(stay * 20L),
Duration.ofMillis(fadeOut * 20L)));
player.showTitle(bungeeTitle);
} catch (Exception e) {
log.error(mm("<red>Could not send title to <white>" + player.getUsername() + "<red>: " + e.getMessage()));
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ message-full = "<red>Server is currently full!<reset>"
message-offline = "<red>Server is currently down!<reset>"
kick = true
wait-on-kick = 16
send-title = true

0 comments on commit 1924266

Please sign in to comment.