|
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; |
8 | 3 | import com.jaimemartz.playerbalancer.settings.props.features.BalancerProps; |
9 | 4 | import net.md_5.bungee.BungeeServerInfo; |
10 | 5 | import net.md_5.bungee.api.Callback; |
|
13 | 8 | import net.md_5.bungee.api.chat.TextComponent; |
14 | 9 | import net.md_5.bungee.api.config.ServerInfo; |
15 | 10 | 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; |
21 | 11 |
|
22 | 12 | import java.net.InetSocketAddress; |
23 | 13 | import java.util.Collection; |
24 | 14 | import java.util.Collections; |
25 | 15 | import java.util.stream.Collectors; |
26 | 16 |
|
27 | | -public class SectionServer implements Listener { |
28 | | - private final PlayerBalancer plugin; |
| 17 | +public class SectionServer extends BungeeServerInfo { |
| 18 | + private final BalancerProps props; |
29 | 19 | private final ServerSection section; |
30 | 20 |
|
31 | | - public SectionServer(PlayerBalancer plugin, ServerSection section) { |
32 | | - this.plugin = plugin; |
| 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 | + |
| 29 | + this.props = props; |
33 | 30 | this.section = section; |
34 | 31 | } |
35 | 32 |
|
36 | | - @EventHandler(priority = EventPriority.LOWEST) |
37 | | - public void on(PluginMessageEvent event) { |
38 | | - if (event.getTag().equals("BungeeCord")) { |
39 | | - ByteArrayDataInput in = ByteStreams.newDataInput(event.getData()); |
40 | | - String request = in.readUTF(); |
41 | | - |
42 | | - if (request.equals("Connect")) { |
43 | | - if (event.getReceiver() instanceof ProxiedPlayer) { |
44 | | - ProxiedPlayer player = (ProxiedPlayer) event.getReceiver(); |
45 | | - String target = in.readUTF(); |
46 | | - |
47 | | - if (target.equals("@" + section.getProps().getServerName())) { |
48 | | - ConnectionIntent.simple(plugin, player, section); |
49 | | - event.setCancelled(true); |
50 | | - } |
51 | | - } |
52 | | - } |
53 | | - } |
| 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); |
54 | 82 | } |
55 | 83 | } |
0 commit comments