Search in sources :

Example 1 with ServerListPingEvent

use of org.bukkit.event.server.ServerListPingEvent in project CloudNet by Dytanic.

the class BukkitBootstrap method enableTasks.

private void enableTasks() {
    Bukkit.getScheduler().runTask(this, new Runnable() {

        @Override
        public void run() {
            if (CloudServer.getInstance().getGroupData() != null) {
                if (CloudAPI.getInstance().getServerGroupData(CloudAPI.getInstance().getGroup()).getMode().equals(ServerGroupMode.LOBBY) || CloudAPI.getInstance().getServerGroupData(CloudAPI.getInstance().getGroup()).getMode().equals(ServerGroupMode.STATIC_LOBBY)) {
                    CommandCloudServer server = new CommandCloudServer();
                    getCommand("cloudserver").setExecutor(server);
                    getCommand("cloudserver").setPermission("cloudnet.command.cloudserver");
                    getCommand("cloudserver").setTabCompleter(server);
                }
                Bukkit.getPluginManager().callEvent(new BukkitCloudServerInitEvent(CloudServer.getInstance()));
                CloudServer.getInstance().update();
                if (CloudAPI.getInstance().getServerGroupData(CloudAPI.getInstance().getGroup()).getAdvancedServerConfig().isDisableAutoSavingForWorlds())
                    for (World world : Bukkit.getWorlds()) world.setAutoSave(false);
            }
            if (CloudServer.getInstance().getServerConfig().getProperties().contains(NetworkUtils.DEV_PROPERTY) && CloudAPI.getInstance().getModuleProperties().contains("devservice"))
                CloudServer.getInstance().registerCommand(new CommandCloudDeploy());
            if (CloudServer.getInstance().getGroupData() != null) {
                getServer().getScheduler().runTaskTimer(BukkitBootstrap.this, new Runnable() {

                    @Override
                    public void run() {
                        try {
                            ServerListPingEvent serverListPingEvent = new ServerListPingEvent(new InetSocketAddress("127.0.0.1", 53345).getAddress(), CloudServer.getInstance().getMotd(), Bukkit.getOnlinePlayers().size(), CloudServer.getInstance().getMaxPlayers());
                            Bukkit.getPluginManager().callEvent(serverListPingEvent);
                            if (!serverListPingEvent.getMotd().equals(CloudServer.getInstance().getMotd())) {
                                CloudServer.getInstance().setMotd(serverListPingEvent.getMotd());
                                if (serverListPingEvent.getMotd().toLowerCase().contains("running") || serverListPingEvent.getMotd().toLowerCase().contains("ingame")) {
                                    CloudServer.getInstance().changeToIngame();
                                }
                            }
                            if (serverListPingEvent.getMaxPlayers() != CloudServer.getInstance().getMaxPlayers()) {
                                CloudServer.getInstance().setMaxPlayers(serverListPingEvent.getMaxPlayers());
                            }
                        } catch (Exception ex) {
                        }
                    }
                }, 0, 5);
            }
            if (CloudAPI.getInstance().getPermissionPool() != null && (getServer().getPluginManager().isPluginEnabled("VaultAPI") || getServer().getPluginManager().isPluginEnabled("Vault")))
                try {
                    Class.forName("de.dytanic.cloudnet.bridge.vault.VaultInvoker").getMethod("invoke", new Class[0]).invoke(null, new Object[0]);
                } catch (IllegalAccessException | ClassNotFoundException | NoSuchMethodException | InvocationTargetException e) {
                    e.printStackTrace();
                }
        }
    });
}
Also used : BukkitCloudServerInitEvent(de.dytanic.cloudnet.bridge.event.bukkit.BukkitCloudServerInitEvent) InetSocketAddress(java.net.InetSocketAddress) World(org.bukkit.World) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) CommandCloudDeploy(de.dytanic.cloudnet.bridge.internal.command.bukkit.CommandCloudDeploy) CommandCloudServer(de.dytanic.cloudnet.bridge.internal.command.bukkit.CommandCloudServer) ServerListPingEvent(org.bukkit.event.server.ServerListPingEvent)

Example 2 with ServerListPingEvent

use of org.bukkit.event.server.ServerListPingEvent in project Glowstone by GlowstoneMC.

the class LegacyPingHandler method channelRead.

@Override
public void channelRead(ChannelHandlerContext channelHandlerContext, Object object) throws Exception {
    ByteBuf bytebuf = (ByteBuf) object;
    bytebuf.markReaderIndex();
    boolean legacyPingProtocol = false;
    try {
        if (bytebuf.readByte() == (byte) 0xFE) {
            GlowServer server = networkServer.getServer();
            int readableBytes = bytebuf.readableBytes();
            InetSocketAddress inetsocketaddress = (InetSocketAddress) channelHandlerContext.channel().remoteAddress();
            ServerListPingEvent legacyPingEvent = new ServerListPingEvent(inetsocketaddress.getAddress(), server.getMotd(), server.getOnlinePlayers().size(), server.getMaxPlayers());
            EventFactory.callEvent(legacyPingEvent);
            switch(readableBytes) {
                case 0:
                    sendByteBuf(channelHandlerContext, responseToByteBuf(channelHandlerContext, String.format("%s§%d§%d", legacyPingEvent.getMotd(), legacyPingEvent.getNumPlayers(), legacyPingEvent.getMaxPlayers())));
                    legacyPingProtocol = true;
                    break;
                case 1:
                    if (bytebuf.readByte() == (byte) 0x01) {
                        sendByteBuf(channelHandlerContext, responseToByteBuf(channelHandlerContext, String.format("§1%d%s%s%d%d", GlowServer.PROTOCOL_VERSION, GlowServer.GAME_VERSION, legacyPingEvent.getMotd(), legacyPingEvent.getNumPlayers(), legacyPingEvent.getMaxPlayers())));
                        legacyPingProtocol = true;
                    }
                    break;
                default:
                    if (bytebuf.readByte() == (byte) 0x01 && bytebuf.readByte() == (byte) 0xFA && "MC|PingHost".equals(new String(bytebuf.readBytes(bytebuf.readShort() << 1).array(), Charsets.UTF_16BE))) {
                        int dataLength = bytebuf.readUnsignedShort();
                        short clientVersion = bytebuf.readUnsignedByte();
                        String hostname = bytebuf.readBytes(bytebuf.readShort() << 1).toString(Charsets.UTF_16BE);
                        @SuppressWarnings("unused") int port = bytebuf.readInt();
                        if (clientVersion >= 73 && 7 + (hostname.length() << 1) == dataLength && bytebuf.readableBytes() == 0) {
                            sendByteBuf(channelHandlerContext, responseToByteBuf(channelHandlerContext, String.format("§1%d%s%s%d%d", GlowServer.PROTOCOL_VERSION, GlowServer.GAME_VERSION, legacyPingEvent.getMotd(), legacyPingEvent.getNumPlayers(), legacyPingEvent.getMaxPlayers())));
                            legacyPingProtocol = true;
                        }
                    }
                    break;
            }
        }
    } catch (RuntimeException e) {
        // Silently catch the exception
        ;
    } finally {
        // check if not successful, otherwise the connection has already been closed
        if (!legacyPingProtocol) {
            bytebuf.resetReaderIndex();
            channelHandlerContext.pipeline().remove("legacy_ping");
            channelHandlerContext.fireChannelRead(object);
        }
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ByteBuf(io.netty.buffer.ByteBuf) GlowServer(net.glowstone.GlowServer) ServerListPingEvent(org.bukkit.event.server.ServerListPingEvent)

Aggregations

InetSocketAddress (java.net.InetSocketAddress)2 ServerListPingEvent (org.bukkit.event.server.ServerListPingEvent)2 BukkitCloudServerInitEvent (de.dytanic.cloudnet.bridge.event.bukkit.BukkitCloudServerInitEvent)1 CommandCloudDeploy (de.dytanic.cloudnet.bridge.internal.command.bukkit.CommandCloudDeploy)1 CommandCloudServer (de.dytanic.cloudnet.bridge.internal.command.bukkit.CommandCloudServer)1 ByteBuf (io.netty.buffer.ByteBuf)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 GlowServer (net.glowstone.GlowServer)1 World (org.bukkit.World)1