Solved Socket getMotd

Discussion in 'Spigot Plugin Development' started by SmaxoS, Jun 20, 2018.

Thread Status:
Not open for further replies.
  1. Hi, I have this code:
    Code (Java):
        public ItemStack item(String ip, String servername) {
            try {
                Socket s = new Socket();
                s.connect(new InetSocketAddress(ip.split(":")[0], Integer.parseInt(ip.split(":")[1])), 1 * 1000);
                DataOutputStream out = new DataOutputStream(s.getOutputStream());
                DataInputStream in = new DataInputStream(s.getInputStream());

                out.write(0xFE);

                StringBuilder sb = new StringBuilder();

                int b;
                while ((b = in.read()) != -1) {
                    if (b != 0 && b > 16 && b != 255 && b != 23 && b != 24) {
                        sb.append((char) b);
                    }
                }

                String[] data = sb.toString().split("ยง");
                motd = data[0];
                count = Integer.valueOf(data[1]);
                max = Integer.valueOf(data[2]);
                s.close();
                state = true;

            } catch (Exception e) {
                state = false;
            }
            ItemStack is = new ItemStack(Material.APPLE, 1);
            ItemMeta meta = is.getItemMeta();
            meta.setDisplayName(servername);
            List<String> lore = new ArrayList<>();
            lore.add("MOTD: " + (state ? motd : ""));
            lore.add("Status: " + (state ? "Online" : "Offline"));
            lore.add("Players: " + count + "/" + max);
            meta.setLore(lore);
            is.setItemMeta(meta);
            return is;
        }
    But when I open this Item in created menu motd is default (A Minecraft Server), but i changing it with ServerListPingEvent.
     
  2. Thank you for your message, but I found the problem. Problem is in on Event, but I don't know what is wrong, JSON from site:
    Code (Text):
    "motd":"A Minecraft Server"
    and my event:

    Code (Java):
        @EventHandler
        public void onMotd(ServerListPingEvent e) {  
            Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(Main.plugin, new Runnable() {
                @Override
                public void run() {    
                    e.setMotd("motd");
                }  
            },0, 20);        
        }
     
  3. The scheduled repeating task inside ServerListPingEvent is misplaced, I'm not sure what you're trying to do there? If you're wanting to continuously change the motd at the multiplayer list, that is the wrong way of doing it.
     
  4. I need this for minigame and in the menu I need to get status of the game, map name, etc.
     
  5. FrostedSnowman

    Resource Staff

    Then just set it during the event. No need for the task, you're wasting so many resources doing so.
     
  6. Yes, but but even though I do not have the task, it shows "A Minecraft Server".
     
  7. FrostedSnowman

    Resource Staff

    is your listener registered?
     
  8. Yes, I have EventHandler and listener is registered in onEnable method, but I view custom motd in ServerList, but in menu and on the JSON from api is "A Minecraft Server".
     
  9. Get rid of the scheduler, first of all, that's not going to help you in any way. Secondly, have you tried using a modern ping packet? 0x00 handshake with next state set to 1 should get you the same thing, perhaps the legacy ping packet might not be handled by the server.
     
  10. How can I use new ping packet?
     
  11. ?
     
  12. Problem solved, I'm used pinger by derek. Thank you all.
     
Thread Status:
Not open for further replies.