public ProxyObject getFreeProxy(ProxyGroupObject group) { if (group.getProxyChooseStrategy() == null) { TimoCloudCord.getInstance().severe("Error while choosing proxy: ProxyChooseStrategy of group '" + group.getName() + "' is null. Please report this."); return null; } List<ProxyObject> proxies = group.getProxies().stream().filter(proxy -> proxy.getOnlinePlayerCount() < proxy.getGroup().getMaxPlayerCountPerProxy()).collect(Collectors.toList()); proxies.sort(Comparator.comparing(ProxyObject::getOnlinePlayerCount)); if (proxies.size() == 0) return null; switch (group.getProxyChooseStrategy()) { case RANDOM: return proxies.get(new Random().nextInt(proxies.size())); case FILL: return proxies.get(proxies.size() - 1); case BALANCE: return proxies.get(0); } return null; }
@EventHandler public void onProxyPingEvent(ProxyPingEvent event) { ProxyObject proxyObject = TimoCloudAPI.getBungeeAPI().getThisProxy(); ServerPing serverPing = event.getResponse(); serverPing.setPlayers(new ServerPing.Players( proxyObject.getGroup().getMaxPlayerCount(), proxyObject.getGroup().getOnlinePlayerCount(), serverPing.getPlayers().getSample() )); serverPing.setDescriptionComponent(new TextComponent(ChatColor.translateAlternateColorCodes('&', proxyObject.getGroup().getMotd()))); } }
@Override public void execute(CommandSender sender, String[] strings) { for (ServerGroupObject serverGroupObject : TimoCloudAPI.getBungeeAPI().getThisProxy().getGroup().getServerGroups()) { for (ServerObject serverObject : serverGroupObject.getServers()) { sender.sendMessage( new TextComponent(ChatColor.translateAlternateColorCodes('&', "&a[" + serverObject.getName() + "] &e(" + serverObject.getOnlinePlayerCount() + "): &r" + serverObject.getOnlinePlayers().stream().map(PlayerObject::getName).sorted(String.CASE_INSENSITIVE_ORDER).collect(Collectors.joining(", ")))) ); } } sender.sendMessage(new TextComponent(ChatColor.RESET + "Total players online: " + TimoCloudAPI.getBungeeAPI().getThisProxy().getGroup().getOnlinePlayerCount())); } }
@Override public ProxyObject getProxy(String proxyName) { for (ProxyGroupObject group : proxyGroups) for (ProxyObject proxy : group.getProxies()) if (proxy.getName().equals(proxyName)) return proxy; for (ProxyGroupObject group : proxyGroups) for (ProxyObject proxy : group.getProxies()) if (proxy.getName().equalsIgnoreCase(proxyName)) return proxy; return null; }
public ProxyGroupObject getProxyGroupByHostName(String hostName) { for (ProxyGroupObject group : TimoCloudAPI.getUniversalAPI().getProxyGroups()) for (String hostName1 : group.getHostNames()) if (matches(hostName, hostName1)) return group; return null; }
public static ByteBuf createStatusPacket(ProxyGroupObject proxyGroupObject, int protocolVersion) { ByteBuf buf = Unpooled.buffer(); writeVarInt(0, buf); writeString(Message.create() .set("version", Message.create() .set("name", "TimoCloudCord") .set("protocol", protocolVersion) .toJson()) .set("players", Message.create() .set("max", proxyGroupObject.getMaxPlayerCount()) .set("online", proxyGroupObject.getOnlinePlayerCount()) .toJson()) .set("description", Message.create() .set("text", proxyGroupObject.getMotd()) .toJson()) .toString(), buf); return buf; }
cf.addListener((ChannelFutureListener) future -> { if (future.isSuccess()) { TimoCloudCord.getInstance().info("[" + channel.remoteAddress() + "] connected to hostname '" + hostName + "'. Using proxy " + proxyObject.getName() + " of group " + proxyObject.getGroup().getName() + "."); if (channel.attr(UPSTREAM_HANDLER).get() == null) { ProxyUpstreamHandler upstreamHandler = new ProxyUpstreamHandler(cf.channel(), downstreamHandler);
ProxyGroupObject groupObject = getObjectMapper().readValue((String) object, proxyGroupObjectImplementation); List<ProxyObject> proxyObjects = new ArrayList<>(); for (ProxyObject proxyObject : groupObject.getProxies()) proxyObjects.add(proxyObject); groupObject.getProxies().clear(); groupObject.getProxies().addAll(proxyObjects); proxyGroups.add(groupObject);
public static void connectClient(Channel channel, ProxyGroupObject proxyGroupObject, String hostName, ByteBuf loginPacket) { ProxyObject proxyObject = TimoCloudCord.getInstance().getProxyManager().getFreeProxy(proxyGroupObject); if (proxyObject == null) { TimoCloudCord.getInstance().severe("No free proxy of group '" + proxyGroupObject.getName() + "' found. Disconnecting client."); channel.close(); return; } connectClient(channel, proxyObject, hostName, loginPacket); }