Search in sources :

Example 6 with GlowServer

use of net.glowstone.GlowServer 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)

Example 7 with GlowServer

use of net.glowstone.GlowServer in project Glowstone by GlowstoneMC.

the class QueryTest method setup.

@Before
public void setup() throws Exception {
    glowServer = mock(GlowServer.class);
    CountDownLatch latch = new CountDownLatch(1);
    this.queryPlugins = true;
    server = new QueryServer(glowServer, latch, queryPlugins);
    random = mock(ThreadLocalRandom.class);
    PowerMockito.mockStatic(ThreadLocalRandom.class);
    when(ThreadLocalRandom.current()).thenReturn(random);
    address = InetSocketAddress.createUnresolved("somehost", 12345);
}
Also used : QueryServer(net.glowstone.net.query.QueryServer) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) GlowServer(net.glowstone.GlowServer) CountDownLatch(java.util.concurrent.CountDownLatch) Before(org.junit.Before)

Example 8 with GlowServer

use of net.glowstone.GlowServer in project Glowstone by GlowstoneMC.

the class GlowTeam method getPlayers.

@Override
@Deprecated
public Set<OfflinePlayer> getPlayers() throws IllegalStateException {
    Set<OfflinePlayer> playerObjectSet = new HashSet<>(players.size());
    playerObjectSet.addAll(players.stream().map(s -> new GlowOfflinePlayer((GlowServer) Bukkit.getServer(), s)).collect(Collectors.toList()));
    return playerObjectSet;
}
Also used : GlowOfflinePlayer(net.glowstone.GlowOfflinePlayer) OfflinePlayer(org.bukkit.OfflinePlayer) GlowOfflinePlayer(net.glowstone.GlowOfflinePlayer) GlowServer(net.glowstone.GlowServer) HashSet(java.util.HashSet)

Example 9 with GlowServer

use of net.glowstone.GlowServer in project Glowstone by GlowstoneMC.

the class QueryHandler method handleFullStats.

private void handleFullStats(ChannelHandlerContext ctx, DatagramPacket packet, int sessionId) {
    GlowServer server = queryServer.getServer();
    StringBuilder plugins = new StringBuilder("Glowstone ").append(server.getVersion()).append(" on Bukkit ").append(server.getBukkitVersion());
    if (showPlugins) {
        char delim = ':';
        for (Plugin plugin : server.getPluginManager().getPlugins()) {
            plugins.append(delim).append(' ').append(plugin.getDescription().getFullName());
            delim = ';';
        }
    }
    Map<String, Object> data = new LinkedHashMap<>();
    data.put("hostname", server.getMotd());
    data.put("gametype", "SMP");
    data.put("game_id", "MINECRAFT");
    data.put("version", GlowServer.GAME_VERSION);
    data.put("plugins", plugins);
    data.put("map", server.getWorlds().get(0).getName());
    data.put("numplayers", server.getOnlinePlayers().size());
    data.put("maxplayers", server.getMaxPlayers());
    data.put("hostport", server.getPort());
    data.put("hostip", getIpString());
    ByteBuf buf = ctx.alloc().buffer();
    buf.writeByte(ACTION_STATS);
    buf.writeInt(sessionId);
    // constant: splitnum\x00\x80\x00
    buf.writeBytes(new byte[] { 0x73, 0x70, 0x6C, 0x69, 0x74, 0x6E, 0x75, 0x6D, 0x00, (byte) 0x80, 0x00 });
    for (Entry<String, Object> e : data.entrySet()) {
        writeString(buf, e.getKey());
        writeString(buf, String.valueOf(e.getValue()));
    }
    buf.writeByte(0);
    // constant: \x01player_\x00\x00
    buf.writeBytes(new byte[] { 0x01, 0x70, 0x6C, 0x61, 0x79, 0x65, 0x72, 0x5F, 0x00, 0x00 });
    for (Player player : server.getOnlinePlayers()) {
        writeString(buf, player.getName());
    }
    buf.writeByte(0);
    ctx.write(new DatagramPacket(buf, packet.sender()));
}
Also used : Player(org.bukkit.entity.Player) DatagramPacket(io.netty.channel.socket.DatagramPacket) GlowServer(net.glowstone.GlowServer) ByteBuf(io.netty.buffer.ByteBuf) Plugin(org.bukkit.plugin.Plugin) LinkedHashMap(java.util.LinkedHashMap)

Example 10 with GlowServer

use of net.glowstone.GlowServer in project Glowstone by GlowstoneMC.

the class QueryHandler method handleBasicStats.

private void handleBasicStats(ChannelHandlerContext ctx, DatagramPacket packet, int sessionId) {
    GlowServer server = queryServer.getServer();
    ByteBuf buf = ctx.alloc().buffer();
    buf.writeByte(ACTION_STATS);
    buf.writeInt(sessionId);
    writeString(buf, server.getMotd());
    writeString(buf, "SMP");
    writeString(buf, server.getWorlds().get(0).getName());
    writeString(buf, String.valueOf(server.getOnlinePlayers().size()));
    writeString(buf, String.valueOf(server.getMaxPlayers()));
    buf.order(ByteOrder.LITTLE_ENDIAN).writeShort(server.getPort());
    writeString(buf, getIpString());
    ctx.write(new DatagramPacket(buf, packet.sender()));
}
Also used : DatagramPacket(io.netty.channel.socket.DatagramPacket) GlowServer(net.glowstone.GlowServer) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

GlowServer (net.glowstone.GlowServer)10 ByteBuf (io.netty.buffer.ByteBuf)3 CraftingManager (net.glowstone.inventory.crafting.CraftingManager)3 DatagramPacket (io.netty.channel.socket.DatagramPacket)2 Player (org.bukkit.entity.Player)2 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1 GlowOfflinePlayer (net.glowstone.GlowOfflinePlayer)1 GlowFurnaceInventory (net.glowstone.inventory.GlowFurnaceInventory)1 StatusResponseMessage (net.glowstone.net.message.status.StatusResponseMessage)1 QueryServer (net.glowstone.net.query.QueryServer)1 CompoundTag (net.glowstone.util.nbt.CompoundTag)1 GameMode (org.bukkit.GameMode)1 OfflinePlayer (org.bukkit.OfflinePlayer)1 FurnaceBurnEvent (org.bukkit.event.inventory.FurnaceBurnEvent)1 FurnaceSmeltEvent (org.bukkit.event.inventory.FurnaceSmeltEvent)1