Skip to content

Commit 9d778ab

Browse files
committed
Changed to wrap only ServerInfo containing .viaversion. identifier with HostnameBungeeServerInfo
1 parent 8c5daf7 commit 9d778ab

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

src/main/java/com/kamesuta/bungeeviaproxy/BungeeViaProxy.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.kamesuta.bungeeviaproxy;
22

3-
import net.md_5.bungee.BungeeServerInfo;
43
import net.md_5.bungee.api.ProxyServer;
54
import net.md_5.bungee.api.config.ServerInfo;
65
import net.md_5.bungee.api.connection.PendingConnection;
@@ -30,20 +29,22 @@ public void onEnable() {
3029
// This results in the error "You are already connected to this server!".
3130
// To avoid this, we use unresolved InetSocketAddress objects so that different hostnames are treated as distinct servers.
3231
ProxyServer.getInstance().getServers().replaceAll((name, serverInfo) -> {
33-
// if the serverInfo is already a HostnameBungeeServerInfo, return it as is
34-
if (serverInfo instanceof HostnameBungeeServerInfo) return serverInfo;
35-
// if the serverInfo is not a BungeeServerInfo, return it as is
36-
if (!(serverInfo instanceof BungeeServerInfo)) return serverInfo;
32+
// only address containing ".viaproxy."
33+
if (!serverInfo.getSocketAddress().toString().contains(IDENTIFIER)) return serverInfo;
3734
// wrap the BungeeServerInfo to HostnameBungeeServerInfo
38-
return new HostnameBungeeServerInfo(
39-
serverInfo.getName(),
40-
serverInfo.getSocketAddress(),
41-
serverInfo.getMotd(),
42-
serverInfo.isRestricted()
43-
);
35+
return HostnameBungeeServerInfo.wrap(serverInfo);
4436
});
4537
}
4638

39+
@Override
40+
public void onDisable() {
41+
// Plugin shutdown logic
42+
ProxyServer.getInstance().getPluginManager().unregisterListener(this);
43+
44+
// Restore the original BungeeServerInfo
45+
ProxyServer.getInstance().getServers().replaceAll((name, serverInfo) -> HostnameBungeeServerInfo.unwrap(serverInfo));
46+
}
47+
4748
@EventHandler
4849
public void onServerConnect(ServerConnectEvent event) {
4950
// Get the target server address

src/main/java/com/kamesuta/bungeeviaproxy/HostnameBungeeServerInfo.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,30 @@
99

1010
public class HostnameBungeeServerInfo extends BungeeServerInfo {
1111

12-
public HostnameBungeeServerInfo(String name, SocketAddress socketAddress, String motd, boolean restricted) {
13-
super(name, socketAddress, motd, restricted);
12+
public HostnameBungeeServerInfo(String name, SocketAddress address, String motd, boolean restricted) {
13+
super(name, address, motd, restricted);
14+
}
15+
16+
public static ServerInfo wrap(ServerInfo serverInfo) {
17+
if (serverInfo instanceof HostnameBungeeServerInfo) return serverInfo;
18+
19+
return new HostnameBungeeServerInfo(
20+
serverInfo.getName(),
21+
serverInfo.getSocketAddress(),
22+
serverInfo.getMotd(),
23+
serverInfo.isRestricted()
24+
);
25+
}
26+
27+
public static ServerInfo unwrap(ServerInfo serverInfo) {
28+
if (!(serverInfo instanceof HostnameBungeeServerInfo)) return serverInfo;
29+
30+
return new BungeeServerInfo(
31+
serverInfo.getName(),
32+
serverInfo.getSocketAddress(),
33+
serverInfo.getMotd(),
34+
serverInfo.isRestricted()
35+
);
1436
}
1537

1638
@Override

0 commit comments

Comments
 (0)