Solitario Postado Maio 17, 2017 Denunciar Postado Maio 17, 2017 Gostaria de saber como pegar o motd personalizado do servidor já que se eu tentar pegar o motd (data[3]) fica dando o motd da config ... em vez do motd do ServerListPingEvent Prints
Solitario Postado Maio 21, 2017 Autor Denunciar Postado Maio 21, 2017 (editado) @UP ;-; derrepente começou a só dar connection refused .... mesmo com o servidor on import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; public class Server { private boolean online; private String motd; public String getMotd() { return motd; } public void setMotd(String motd) { this.motd = motd; } public boolean isOnline() { return online; } public void setOnline(boolean online) { this.online = online; } public Server(String host, int port) { new Thread(new Runnable() { @Override public void run() { Socket socket = null; InetAddress addr = null; // INSERT hostname here (name or ip) String hostname = host; //========== Converting hostname to ip =========== try { addr = InetAddress.getByName(hostname); } catch (UnknownHostException e2) { e2.printStackTrace(); } hostname = addr.getCanonicalHostName(); //============ Creating Socket ============ try { socket = new Socket(hostname, port); online = true; }catch (IOException e1) { e1.printStackTrace(); online = false; } if(socket == null){ return; } DataOutputStream output = null; BufferedReader reader = null; StringBuilder str = new StringBuilder(); try { output = new DataOutputStream(socket.getOutputStream()); reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8)); // notice the UTF-8 here! // Allocate Buffer with the right amount of bytes ByteBuffer buffer = ByteBuffer.allocate(7 + hostname.length()); //========= START Writing Protocol header ======== buffer.put((byte) (6 + hostname.length())); // send length of whole packet buffer.put((byte) 0); // send packetID buffer.put((byte) 47); // send protocol Version //========= Start Writing Hostname =========== //Write length of HostName buffer.put((byte) hostname.length()); for(int i = 0; i < hostname.length(); i++) { buffer.put((byte) hostname.toCharArray()[i]); } //=============start writing port================ int[] portBytes = new int[2]; portBytes[0] = (byte) (port & 0xFF); portBytes[1] = (byte) ((port >>> 8) & 0xFF); buffer.put((byte) portBytes[1]); buffer.put((byte) portBytes[0]); //==========Add State========== buffer.put((byte) 1); output.write(buffer.array()); output.flush(); //=========== Send Request ========= buffer = ByteBuffer.allocate(2); buffer.put((byte) 1); buffer.put((byte) 0); output.write(buffer.array()); output.flush(); int tmpInt; while((tmpInt = reader.read()) != -1 && tmpInt < 65535) { if(str.toString().endsWith("fav")) { break; } str.append((char) tmpInt); } //get Result String result = str.toString(); // get String int descriptionIndex = result.indexOf("\"description\":\"") + 15; // get MOTD start index int descEndIndex = result.indexOf("\",\"", descriptionIndex); // get MOTD end index result = result.substring(descriptionIndex, descEndIndex); // get the MOTD motd = result; } catch(StringIndexOutOfBoundsException ex) { // maybe do sth? } catch(IOException e) { // do sth } finally { try { if(reader != null) { reader.close(); } if(output != null) { output.close(); } if(socket != null) { socket.close(); } } catch(IOException e) { e.printStackTrace(); } } } }).start(); } } Editado Maio 21, 2017 por Solitario
leonardosc Postado Maio 21, 2017 Denunciar Postado Maio 21, 2017 @UP ;-; derrepente começou a só dar connection refused .... mesmo com o servidor on import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; public class Server { private boolean online; private String motd; public String getMotd() { return motd; } public void setMotd(String motd) { this.motd = motd; } public boolean isOnline() { return online; } public void setOnline(boolean online) { this.online = online; } public Server(String host, int port) { new Thread(new Runnable() { @Override public void run() { Socket socket = null; InetAddress addr = null; // INSERT hostname here (name or ip) String hostname = host; //========== Converting hostname to ip =========== try { addr = InetAddress.getByName(hostname); } catch (UnknownHostException e2) { e2.printStackTrace(); } hostname = addr.getCanonicalHostName(); //============ Creating Socket ============ try { socket = new Socket(hostname, port); online = true; }catch (IOException e1) { e1.printStackTrace(); online = false; } if(socket == null){ return; } DataOutputStream output = null; BufferedReader reader = null; StringBuilder str = new StringBuilder(); try { output = new DataOutputStream(socket.getOutputStream()); reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8)); // notice the UTF-8 here! // Allocate Buffer with the right amount of bytes ByteBuffer buffer = ByteBuffer.allocate(7 + hostname.length()); //========= START Writing Protocol header ======== buffer.put((byte) (6 + hostname.length())); // send length of whole packet buffer.put((byte) 0); // send packetID buffer.put((byte) 47); // send protocol Version //========= Start Writing Hostname =========== //Write length of HostName buffer.put((byte) hostname.length()); for(int i = 0; i < hostname.length(); i++) { buffer.put((byte) hostname.toCharArray()[i]); } //=============start writing port================ int[] portBytes = new int[2]; portBytes[0] = (byte) (port & 0xFF); portBytes[1] = (byte) ((port >>> 8) & 0xFF); buffer.put((byte) portBytes[1]); buffer.put((byte) portBytes[0]); //==========Add State========== buffer.put((byte) 1); output.write(buffer.array()); output.flush(); //=========== Send Request ========= buffer = ByteBuffer.allocate(2); buffer.put((byte) 1); buffer.put((byte) 0); output.write(buffer.array()); output.flush(); int tmpInt; while((tmpInt = reader.read()) != -1 && tmpInt < 65535) { if(str.toString().endsWith("fav")) { break; } str.append((char) tmpInt); } //get Result String result = str.toString(); // get String int descriptionIndex = result.indexOf("\"description\":\"") + 15; // get MOTD start index int descEndIndex = result.indexOf("\",\"", descriptionIndex); // get MOTD end index result = result.substring(descriptionIndex, descEndIndex); // get the MOTD motd = result; } catch(StringIndexOutOfBoundsException ex) { // maybe do sth? } catch(IOException e) { // do sth } finally { try { if(reader != null) { reader.close(); } if(output != null) { output.close(); } if(socket != null) { socket.close(); } } catch(IOException e) { e.printStackTrace(); } } } }).start(); } } Ta bem zuado dessa maneira ;-; Vou ver se faço um com CompletableFuture... vai ficar melhor. 1
Solitario Postado Maio 21, 2017 Autor Denunciar Postado Maio 21, 2017 Ta bem zuado dessa maneira ;-; Vou ver se faço um com CompletableFuture... vai ficar melhor. O pior que foi tão sem motivo .-. Ele começou dando os resultado e refused no meu pc achei que o problema seria por tar no meu pc ... porém mandei para a host e foi do mesmo jeito '-' só que sem dar resultado só refused mesmo '-' ...
leonardosc Postado Maio 21, 2017 Denunciar Postado Maio 21, 2017 O pior que foi tão sem motivo .-. Ele começou dando os resultado e refused no meu pc achei que o problema seria por tar no meu pc ... porém mandei para a host e foi do mesmo jeito '-' só que sem dar resultado só refused mesmo '-' ... Qual versão do seu sv mesmo? preciso testar.
Solitario Postado Maio 21, 2017 Autor Denunciar Postado Maio 21, 2017 Qual versão do seu sv mesmo? preciso testar. 1.7.10
leonardosc Postado Maio 21, 2017 Denunciar Postado Maio 21, 2017 1.7.10 Eu testei aquele MinecraftServerPing na 1.7.10 e ta funcionando...
Solitario Postado Maio 21, 2017 Autor Denunciar Postado Maio 21, 2017 Eu testei aquele MinecraftServerPing na 1.7.10 e ta funcionando... e.e ... manda como você usou e.e
leonardosc Postado Maio 21, 2017 Denunciar Postado Maio 21, 2017 (editado) e.e ... manda como você usou e.e Adaptei aquele MinecraftServerPing. Deixei em 1 classe só e adicione CompletableFuture pra rodar async. https://gist.github.com/leonardosnt/dad9d0c75a51f5117326340734fccdbe Como você pode usar: // Isso fica no escopo da classe.... public Result resultDoServer1; public Result resultDoServer2; // Você pode colocar em uma task. // Pode criar um desse pra cada server ServerQuery q = new ServerQuery("ip do server1", porta do server 1); q.runQueryAsync() .whenComplete((result, err) -> { if (err == null) { // Sincroniza pra evitar dar merda, pois isso roda em uma Thread separada. syncronized (resultDoServer1) { resultDoServer1 = result; } } else { // deu erro, printa no console ou sla; } }); Editado Maio 21, 2017 por leonardosc
Solitario Postado Maio 21, 2017 Autor Denunciar Postado Maio 21, 2017 Adaptei aquele MinecraftServerPing. Deixei em 1 classe só e adicione CompletableFuture pra rodar async. https://gist.github.com/leonardosnt/dad9d0c75a51f5117326340734fccdbe Como você pode usar: // Isso fica no escopo da classe.... public Result resultDoServer1; public Result resultDoServer2; // Você pode colocar em uma task. // Pode criar um desse pra cada server ServerQuery q = new ServerQuery("ip do server1", porta do server 1); q.runQueryAsync() .whenComplete((result, err) -> { if (err == null) { // Sincroniza pra evitar dar merda, pois isso roda em uma Thread separada. syncronized (resultDoServer1) { resultDoServer1 = result; } } else { // deu erro, printa no console ou sla; } });
leonardosc Postado Maio 21, 2017 Denunciar Postado Maio 21, 2017 (editado) '-'-'-'-'-'-'-''-'-'-'-'-'-'-'-'-'-'-'-'-'-'-' Adiciona o spigot/craftbukkit (o jar do server) como dependência. E muda pra org.json.simple.JSONObject Editado Maio 21, 2017 por leonardosc
Solitario Postado Maio 21, 2017 Autor Denunciar Postado Maio 21, 2017 '-'-'-'-'-'-'-''-'-'-'-'-'-'-'-'-'-'-'-'-'-'-' Adiciona o spigot/craftbukkit (o jar do server) como dependência.
leonardosc Postado Maio 21, 2017 Denunciar Postado Maio 21, 2017 Editei o post... E muda pra org.json.simple.JSONObject
Solitario Postado Maio 21, 2017 Autor Denunciar Postado Maio 21, 2017 (editado) Editei o post... E muda pra org.json.simple.JSONObject @Edit Também queria saber qual import devo usar se é o da class para o Result '-' Editado Maio 21, 2017 por Solitario
leonardosc Postado Maio 21, 2017 Denunciar Postado Maio 21, 2017 ;=; Vou ver... Tu não tem discord não? esse tópico ta ficando grande '-'
Solitario Postado Maio 21, 2017 Autor Denunciar Postado Maio 21, 2017 ;=; Vou ver... Tu não tem discord não? esse tópico ta ficando grande '-' '-' ... não tinha ... agora tenho '-' só não sei usar
leonardosc Postado Maio 21, 2017 Denunciar Postado Maio 21, 2017 '-' ... não tinha ... agora tenho '-' só não sei usar Muda o final pra isso: JSONObject obj = (JSONObject) new JSONParser().parse(json); JSONObject players = (JSONObject) obj.get("players"); int onlinePlayers = ((Long) players.get("online")).intValue(); String motd = (String) obj.get("description"); return new Result(onlinePlayers, motd); Me adiciona: leonardosnt#4963
nathampa0909 Postado Maio 30, 2017 Denunciar Postado Maio 30, 2017 Sua dúvida foi marcada como [Resolvido] e movido à área de dúvidas resolvidas.Atenciosamente,Gamer's Board
Pergunta
Solitario
Gostaria de saber como pegar o motd personalizado do servidor já que se eu tentar pegar o motd (data[3]) fica dando o motd da config ... em vez do motd do ServerListPingEvent
Prints
47 respostass a esta questão
Posts Recomendados