Skip to content

Commit

Permalink
Reset tablist, bossbar, title & Customizable serializer & Title & Don…
Browse files Browse the repository at this point in the history
…'t send empty messages

Merge pull request #5 from UserNugget/clear
  • Loading branch information
SkyWatcher2019 authored Jan 23, 2023
2 parents d01d4a3 + 0428865 commit b621ce7
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 10 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ repositories {

dependencies {
compileOnly 'com.velocitypowered:velocity-api:3.1.2-SNAPSHOT'
compileOnly 'com.velocitypowered:velocity-proxy:3.1.2-SNAPSHOT'
compileOnly 'io.netty:netty-handler:4.1.86.Final'
annotationProcessor 'com.velocitypowered:velocity-api:3.1.2-SNAPSHOT'
implementation "net.elytrium.commons:config:1.2.1"
implementation "net.elytrium.commons:kyori:1.2.1"
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/ru/skywatcher_2019/limboreconnect/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,23 @@
package ru.skywatcher_2019.limboreconnect;

import net.elytrium.commons.config.YamlConfig;
import net.elytrium.commons.kyori.serialization.Serializers;

public class Config extends YamlConfig {

@Ignore
public static final Config IMP = new Config();

@Comment({
"Available serializers:",
"LEGACY_AMPERSAND - \"&c&lExample &c&9Text\".",
"LEGACY_SECTION - \"§c§lExample §c§9Text\".",
"MINIMESSAGE - \"<bold><red>Example</red> <blue>Text</blue></bold>\". (https://webui.adventure.kyori.net/)",
"GSON - \"[{\"text\":\"Example\",\"bold\":true,\"color\":\"red\"},{\"text\":\" \",\"bold\":true},{\"text\":\"Text\",\"bold\":true,\"color\":\"blue\"}]\". (https://minecraft.tools/en/json_text.php/)",
"GSON_COLOR_DOWNSAMPLING - Same as GSON, but uses downsampling."
})
public Serializers SERIALIZER = Serializers.MINIMESSAGE;

@Comment("Send player to the limbo, if disconnect reason contains this text (using regex)")
public String RESTART_MESSAGE = "((?i)^(server closed|multiplayer\\.disconnect\\.server_shutdown|server is restarting))+$";

Expand All @@ -32,12 +43,35 @@ public class Config extends YamlConfig {
@Comment("Server status check timeout in milliseconds")
public long PING_TIMEOUT = 500;

@Create
public TITLE TITLE;

public static class TITLE {
@Comment(value = "time in ticks", at = Comment.At.SAME_LINE)
public int FADE_IN = 10;
@Comment(value = "time in ticks", at = Comment.At.SAME_LINE)
public int FADE_OUT = 20;
}

@Create
public MESSAGES MESSAGES;

@Comment("Empty messages will not be sent")
public static class MESSAGES {

public String SERVER_OFFLINE = "<red>Server is restarting, please wait...";
public String CONNECTING = "<aqua>Connecting to the server...";

@Create
public TITLE TITLE;

public static class TITLE {

public String OFFLINE_TITLE = "";
public String OFFLINE_SUBTITLE = "<red>Server is restarting, please wait...";

public String CONNECTING_TITLE = "";
public String CONNECTING_SUBTITLE = "<aqua>Connecting to the server...";
}
}
}
66 changes: 56 additions & 10 deletions src/main/java/ru/skywatcher_2019/limboreconnect/LimboReconnect.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,26 @@
import com.velocitypowered.api.scheduler.ScheduledTask;
import java.io.File;
import java.nio.file.Path;
import java.time.Duration;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import net.elytrium.commons.kyori.serialization.Serializers;
import com.velocitypowered.proxy.connection.MinecraftConnection;
import com.velocitypowered.proxy.connection.client.ConnectedPlayer;
import com.velocitypowered.proxy.connection.client.ClientPlaySessionHandler;
import com.velocitypowered.proxy.protocol.packet.BossBar;
import net.elytrium.limboapi.api.Limbo;
import net.elytrium.limboapi.api.LimboFactory;
import net.elytrium.limboapi.api.chunk.Dimension;
import net.elytrium.limboapi.api.chunk.VirtualWorld;
import net.elytrium.limboapi.api.player.LimboPlayer;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.ComponentSerializer;
import net.kyori.adventure.title.Title;
import org.slf4j.Logger;
import ru.skywatcher_2019.limboreconnect.handler.ReconnectHandler;
import ru.skywatcher_2019.limboreconnect.listener.ReconnectListener;
Expand All @@ -66,6 +72,10 @@ public class LimboReconnect {
private ScheduledTask limboTask;
private Component offlineServerMessage;
private Component connectingMessage;
private Component offlineTitleMessage;
private Component offlineSubtitleMessage;
private Component connectingTitleMessage;
private Component connectingSubtitleMessage;

@Inject
public LimboReconnect(Logger logger, ProxyServer server, @DataDirectory Path dataDirectory) {
Expand Down Expand Up @@ -95,19 +105,17 @@ public void onProxyInitialization(ProxyInitializeEvent event) {
public void reload() {
Config.IMP.reload(this.configFile);

ComponentSerializer<Component, Component, String> serializer = Serializers.MINIMESSAGE.getSerializer();
if (serializer == null) {
LOGGER.warn("The specified serializer could not be founded, using default. (LEGACY_AMPERSAND)");
setSerializer(Serializers.LEGACY_AMPERSAND.getSerializer());
} else {
setSerializer(serializer);
}
setSerializer(Config.IMP.SERIALIZER.getSerializer());

VirtualWorld world = this.factory.createVirtualWorld(Dimension.OVERWORLD, 0, 100, 0, (float) 90, (float) 0.0);
this.limbo = this.factory.createLimbo(world).setName("LimboReconnect").setWorldTime(6000);

this.offlineServerMessage = SERIALIZER.deserialize(Config.IMP.MESSAGES.SERVER_OFFLINE);
this.connectingMessage = SERIALIZER.deserialize(Config.IMP.MESSAGES.CONNECTING);
this.offlineTitleMessage = SERIALIZER.deserialize(Config.IMP.MESSAGES.TITLE.OFFLINE_TITLE);
this.offlineSubtitleMessage = SERIALIZER.deserialize(Config.IMP.MESSAGES.TITLE.OFFLINE_SUBTITLE);
this.connectingTitleMessage = SERIALIZER.deserialize(Config.IMP.MESSAGES.TITLE.CONNECTING_TITLE);
this.connectingSubtitleMessage = SERIALIZER.deserialize(Config.IMP.MESSAGES.TITLE.CONNECTING_SUBTITLE);

this.server.getEventManager().register(this, new ReconnectListener(this));

Expand All @@ -119,6 +127,31 @@ private ProxyServer getServer() {
}

public void addPlayer(Player player, RegisteredServer server) {
ConnectedPlayer connectedPlayer = (ConnectedPlayer) player;
MinecraftConnection connection = connectedPlayer.getConnection();
if (connection.getSessionHandler() instanceof ClientPlaySessionHandler) {
ClientPlaySessionHandler sessionHandler = (ClientPlaySessionHandler) connection.getSessionHandler();
for (UUID bossBar : sessionHandler.getServerBossBars()) {
BossBar deletePacket = new BossBar();
deletePacket.setUuid(bossBar);
deletePacket.setAction(BossBar.REMOVE);
connectedPlayer.getConnection().delayedWrite(deletePacket);
}
sessionHandler.getServerBossBars().clear();
}

connectedPlayer.getTabList().clearAll();

player.showTitle(Title.title(
this.offlineTitleMessage,
this.offlineSubtitleMessage,
Title.Times.times(
Duration.ofMillis(Config.IMP.TITLE.FADE_IN * 50L),
Duration.ofDays(32),
Duration.ofMillis(Config.IMP.TITLE.FADE_OUT * 50L)
)
));

this.limbo.spawnPlayer(player, new ReconnectHandler(this, server));
}

Expand All @@ -134,10 +167,23 @@ private void startTask() {
try {
server.ping().get(Config.IMP.PING_TIMEOUT, TimeUnit.MILLISECONDS);
} catch (CompletionException | InterruptedException | ExecutionException | TimeoutException e) {
player.getProxyPlayer().sendMessage(this.offlineServerMessage);
if (!Config.IMP.MESSAGES.SERVER_OFFLINE.isEmpty()) {
player.getProxyPlayer().sendMessage(this.offlineServerMessage);
}
return;
}
player.getProxyPlayer().sendMessage(this.connectingMessage);
if (!Config.IMP.MESSAGES.CONNECTING.isEmpty()) {
player.getProxyPlayer().sendMessage(this.connectingMessage);
}
player.getProxyPlayer().showTitle(Title.title(
this.connectingTitleMessage,
this.connectingSubtitleMessage,
Title.Times.times(
Duration.ofMillis(Config.IMP.TITLE.FADE_IN * 50L),
Duration.ofDays(32),
Duration.ofMillis(Config.IMP.TITLE.FADE_OUT * 50L)
)
));
player.disconnect(server);
});
}).repeat(Config.IMP.CHECK_INTERVAL, TimeUnit.MILLISECONDS).schedule();
Expand Down

0 comments on commit b621ce7

Please sign in to comment.