[Help] Set the Real MOTD?

Discussion in 'Plugin Development' started by ksbdude, Jun 2, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    ksbdude

    I have this code in my plugin to set the motd to the game state and it works perfectly
    Code:java
    1. @EventHandler
    2. public void onPing(ServerListPingEvent e) {
    3. e.setMotd(getState());
    4. }
    When I ping the server in the server list it displays the motd correct motd to me.

    The problem is that I have some code that pings this servers motd in an external server and It displays the motd defined in this servers config file instead of the motd set in the code above. The ServerListPingEvent appers only to get activated by a player pinging the server and not an external servers ping thus not setting the motd.

    Is there a way for me to set the motd in the server config file through a plugin?
     
  2. Offline

    Nateb1121

    I mean, you could modify it, but it would need to be restart in order for the servers settings to be reloaded.

    Are you sure the service your using just doesn't have the MOTD cached?
     
  3. Offline

    ksbdude

    Nateb1121
    I have this class im using, with the <IP> <Port> replaced.
    Doing
    Ping.ping();
    Should update it all
    Code:java
    1. package src.ksbdude.hub;
    2.  
    3. import java.io.DataOutputStream;
    4. import java.io.IOException;
    5. import java.io.InputStream;
    6. import java.io.InputStreamReader;
    7. import java.io.OutputStream;
    8. import java.net.InetSocketAddress;
    9. import java.net.Socket;
    10. import java.net.SocketException;
    11. import java.nio.charset.Charset;
    12.  
    13. public class Ping {
    14. private static String address = "<IP>";
    15. private static int port = <Port>;
    16. private static int timeout = 100;
    17.  
    18. private static boolean online;
    19. private static int playercount;
    20. private static int maxplayers;
    21. private static String motd;
    22.  
    23. public static String getAddress() {
    24. return address;
    25. }
    26.  
    27. public static void setAddress(String address2) {
    28. address = address2;
    29. }
    30.  
    31. public static int getPort() {
    32. return port;
    33. }
    34.  
    35. public static void setPort(int port2) {
    36. port = port2;
    37. }
    38.  
    39. public static int getTimeout() {
    40. return timeout;
    41. }
    42.  
    43. public static void setTimeout(int timeout2) {
    44. timeout = timeout2;
    45. }
    46.  
    47. public static boolean isOnline() {
    48. return online;
    49. }
    50.  
    51. private static void setOnline(boolean online2) {
    52. online = online2;
    53. }
    54.  
    55. public static int getPlayerCount() {
    56. return playercount;
    57. }
    58.  
    59. private static void setPlayerCount(int playercount2) {
    60. playercount = playercount2;
    61. }
    62.  
    63. public static int getMaxPlayers() {
    64. return maxplayers;
    65. }
    66.  
    67. private static void setMaxPlayers(int maxplayers2) {
    68. maxplayers = maxplayers2;
    69. }
    70.  
    71. public static String getMotd() {
    72. return motd;
    73. }
    74.  
    75. private static void setMotd(String motd2) {
    76. motd = motd2;
    77. }
    78.  
    79. public static void ping() {
    80. try {
    81. Socket socket = new Socket();
    82. OutputStream outputStream;
    83. DataOutputStream dataOutputStream;
    84. InputStream inputStream;
    85. InputStreamReader inputStreamReader;
    86. socket.setSoTimeout(timeout);
    87. socket.connect(new InetSocketAddress(getAddress(), getPort()),
    88. getTimeout());
    89. outputStream = socket.getOutputStream();
    90. dataOutputStream = new DataOutputStream(outputStream);
    91. inputStream = socket.getInputStream();
    92. inputStreamReader = new InputStreamReader(inputStream,
    93. Charset.forName("UTF-16BE"));
    94. dataOutputStream.write(new byte[] { (byte) 0xFE, (byte) 0x01 });
    95. int packetId = inputStream.read();
    96. if (packetId == -1) {
    97. dataOutputStream.close();
    98. outputStream.close();
    99. inputStreamReader.close();
    100. inputStream.close();
    101. socket.close();
    102. throw new IOException("Premature end of stream.");
    103. }
    104. if (packetId != 0xFF) {
    105. dataOutputStream.close();
    106. outputStream.close();
    107. inputStreamReader.close();
    108. inputStream.close();
    109. socket.close();
    110. throw new IOException("Invalid packet ID (" + packetId + ").");
    111. }
    112. int length = inputStreamReader.read();
    113. if (length == -1) {
    114. dataOutputStream.close();
    115. outputStream.close();
    116. inputStreamReader.close();
    117. inputStream.close();
    118. socket.close();
    119. throw new IOException("Premature end of stream.");
    120. }
    121. if (length == 0) {
    122. dataOutputStream.close();
    123. outputStream.close();
    124. inputStreamReader.close();
    125. inputStream.close();
    126. socket.close();
    127. throw new IOException("Invalid string length.");
    128. }
    129. char[] chars = new char[length];
    130. if (inputStreamReader.read(chars, 0, length) != length) {
    131. dataOutputStream.close();
    132. outputStream.close();
    133. inputStreamReader.close();
    134. inputStream.close();
    135. socket.close();
    136. throw new IOException("Premature end of stream.");
    137. }
    138. String string = new String(chars);
    139. if (string.startsWith("§")) {
    140. String[] data = string.split("\0");
    141. setMotd(data[3]);
    142. setPlayerCount(Integer.parseInt(data[4]));
    143. setMaxPlayers(Integer.parseInt(data[5]));
    144. } else {
    145. String[] data = string.split("§");
    146. setMotd(data[0]);
    147. setPlayerCount(Integer.parseInt(data[1]));
    148. setMaxPlayers(Integer.parseInt(data[2]));
    149. }
    150. dataOutputStream.close();
    151. outputStream.close();
    152. inputStreamReader.close();
    153. inputStream.close();
    154. socket.close();
    155. } catch (SocketException exception) {
    156. setOnline(false);
    157. } catch (IOException exception) {
    158. setOnline(false);
    159. }
    160. }
    161. }
    162.  
     
  4. Offline

    Deleted user

    ksbdude
    Soo, if you wanted to use NMS/Reflection, MinecraftServer.class stores the MOTD as a String, I believe
     
  5. Offline

    ResultStatic

    ksbdude
    Code:
    import org.bukkit.craftbukkit.v1_7_R3.CraftServer;
    import net.minecraft.server.v1_7_R3.DedicatedServer;
     
     
    DedicatedServer s = (((CraftServer)Bukkit.getServer()).getHandle().getServer());
    String MOTD = s.getMotd();
    s.setMotd("WhatEverYouWant");
    ksbdude im not really experience with writing reflection but here is what i could do to set it with reflection. if anyone knows how i would fix this reflection tell me. also you would need a way to get the nms class.

    it would go something like this. dont use this

    Code:
    public void setMOTD(String newmotd) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
        Class<?> nms = net.minecraft.server.v1_7_R3.DedicatedServer.class;
        Field f = nms.getDeclaredField("motd");
        f.setAccessible(true);
        String obj = "old motd";
        f.set(obj, newmotd);
    }
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 30, 2016
  6. Offline

    Deleted user

    ResultStatic

    Pretty good. Just note that to set a variable using field, the arguments are f.set(theObjectThatIsBeingAffected, theValue)

    In this case, you would need to get the DedicatedServer instance, and do f.set(instance, "motd here);
     
Thread Status:
Not open for further replies.

Share This Page