|
1 | 1 | package com.jaimemartz.playerbalancer.section; |
2 | 2 |
|
| 3 | +import com.google.common.io.ByteArrayDataInput; |
| 4 | +import com.google.common.io.ByteStreams; |
| 5 | +import com.jaimemartz.playerbalancer.PlayerBalancer; |
| 6 | +import com.jaimemartz.playerbalancer.connection.ConnectionIntent; |
| 7 | +import com.jaimemartz.playerbalancer.listeners.PluginMessageListener; |
3 | 8 | import com.jaimemartz.playerbalancer.settings.props.features.BalancerProps; |
4 | 9 | import net.md_5.bungee.BungeeServerInfo; |
5 | 10 | import net.md_5.bungee.api.Callback; |
|
8 | 13 | import net.md_5.bungee.api.chat.TextComponent; |
9 | 14 | import net.md_5.bungee.api.config.ServerInfo; |
10 | 15 | import net.md_5.bungee.api.connection.ProxiedPlayer; |
| 16 | +import net.md_5.bungee.api.connection.Server; |
| 17 | +import net.md_5.bungee.api.event.PluginMessageEvent; |
| 18 | +import net.md_5.bungee.api.plugin.Listener; |
| 19 | +import net.md_5.bungee.event.EventHandler; |
| 20 | +import net.md_5.bungee.event.EventPriority; |
11 | 21 |
|
12 | 22 | import java.net.InetSocketAddress; |
13 | 23 | import java.util.Collection; |
14 | 24 | import java.util.Collections; |
15 | 25 | import java.util.stream.Collectors; |
16 | 26 |
|
17 | | -public class SectionServer extends BungeeServerInfo { |
| 27 | +public class SectionServer implements Listener { |
| 28 | + private final PlayerBalancer plugin; |
18 | 29 | private final BalancerProps props; |
19 | 30 | private final ServerSection section; |
20 | 31 |
|
21 | | - public SectionServer(BalancerProps props, ServerSection section) { |
22 | | - super( |
23 | | - "@" + section.getProps().getServerName(), |
24 | | - new InetSocketAddress("0.0.0.0", (int) Math.floor(Math.random() * (0xFFFF + 1))), |
25 | | - "Section server of " + section.getName(), |
26 | | - false |
27 | | - ); |
28 | | - |
| 32 | + public SectionServer(PlayerBalancer plugin, BalancerProps props, ServerSection section) { |
| 33 | + this.plugin = plugin; |
29 | 34 | this.props = props; |
30 | 35 | this.section = section; |
31 | 36 | } |
32 | 37 |
|
33 | | - @Override |
34 | | - public Collection<ProxiedPlayer> getPlayers() { |
35 | | - if (props.isShowPlayers()) { |
36 | | - return section.getServers().stream() |
37 | | - .map(ServerInfo::getPlayers) |
38 | | - .flatMap(Collection::stream) |
39 | | - .collect(Collectors.toList()); |
40 | | - } else return Collections.emptyList(); |
41 | | - } |
42 | | - |
43 | | - @Override |
44 | | - public void sendData(String channel, byte[] data) { |
45 | | - this.sendData(channel, data, true); |
46 | | - } |
47 | | - |
48 | | - @Override |
49 | | - public boolean sendData(String channel, byte[] data, boolean queue) { |
50 | | - //Nothing to do |
51 | | - return true; |
52 | | - } |
53 | | - |
54 | | - @Override |
55 | | - public void ping(Callback<ServerPing> callback) { |
56 | | - this.ping(callback, ProxyServer.getInstance().getProtocolVersion()); |
57 | | - } |
58 | | - |
59 | | - @Override |
60 | | - public void ping(Callback<ServerPing> callback, int protocolVersion) { |
61 | | - ServerPing ping = new ServerPing(); |
62 | | - |
63 | | - ping.setDescriptionComponent(new TextComponent( |
64 | | - TextComponent.fromLegacyText(this.getMotd()) |
65 | | - )); |
66 | | - |
67 | | - ping.setVersion(new ServerPing.Protocol( |
68 | | - ProxyServer.getInstance().getName(), |
69 | | - protocolVersion |
70 | | - )); |
71 | | - |
72 | | - Collection<ProxiedPlayer> players = getPlayers(); |
73 | | - ping.setPlayers(new ServerPing.Players( |
74 | | - Integer.MAX_VALUE, |
75 | | - players.size(), |
76 | | - players.stream().map( |
77 | | - player -> new ServerPing.PlayerInfo(player.getName(), player.getUniqueId()) |
78 | | - ).toArray(ServerPing.PlayerInfo[]::new) |
79 | | - )); |
80 | | - |
81 | | - callback.done(ping, null); |
| 38 | + @EventHandler(priority = EventPriority.LOWEST) |
| 39 | + public void on(PluginMessageEvent event) { |
| 40 | + if (event.getTag().equals("BungeeCord")) { |
| 41 | + ByteArrayDataInput in = ByteStreams.newDataInput(event.getData()); |
| 42 | + String request = in.readUTF(); |
| 43 | + |
| 44 | + if (request.equals("Connect")) { |
| 45 | + if (event.getReceiver() instanceof ProxiedPlayer) { |
| 46 | + ProxiedPlayer player = (ProxiedPlayer) event.getReceiver(); |
| 47 | + String target = in.readUTF(); |
| 48 | + |
| 49 | + if (target.equals("@" + section.getProps().getServerName())) { |
| 50 | + ConnectionIntent.simple(plugin, player, section); |
| 51 | + event.setCancelled(true); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + } |
82 | 56 | } |
83 | 57 | } |
0 commit comments