Advertisement
Guest User

Untitled

a guest
Feb 7th, 2017
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.35 KB | None | 0 0
  1.  
  2. import java.io.BufferedWriter;
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.io.PrintWriter;
  8. import java.net.Proxy;
  9. import java.text.DateFormat;
  10. import java.text.SimpleDateFormat;
  11. import java.util.Arrays;
  12. import java.util.Date;
  13. import java.util.Scanner;
  14.  
  15. import org.spacehq.mc.auth.data.GameProfile;
  16. import org.spacehq.mc.auth.exception.request.RequestException;
  17. import org.spacehq.mc.protocol.MinecraftConstants;
  18. import org.spacehq.mc.protocol.MinecraftProtocol;
  19. import org.spacehq.mc.protocol.ServerLoginHandler;
  20. import org.spacehq.mc.protocol.data.SubProtocol;
  21. import org.spacehq.mc.protocol.data.game.entity.player.GameMode;
  22. import org.spacehq.mc.protocol.data.game.setting.Difficulty;
  23. import org.spacehq.mc.protocol.data.game.world.WorldType;
  24. import org.spacehq.mc.protocol.data.message.ChatColor;
  25. import org.spacehq.mc.protocol.data.message.ChatFormat;
  26. import org.spacehq.mc.protocol.data.message.Message;
  27. import org.spacehq.mc.protocol.data.message.MessageStyle;
  28. import org.spacehq.mc.protocol.data.message.TextMessage;
  29. import org.spacehq.mc.protocol.data.message.TranslationMessage;
  30. import org.spacehq.mc.protocol.data.status.PlayerInfo;
  31. import org.spacehq.mc.protocol.data.status.ServerStatusInfo;
  32. import org.spacehq.mc.protocol.data.status.VersionInfo;
  33. import org.spacehq.mc.protocol.data.status.handler.ServerInfoBuilder;
  34. import org.spacehq.mc.protocol.data.status.handler.ServerInfoHandler;
  35. import org.spacehq.mc.protocol.data.status.handler.ServerPingTimeHandler;
  36. import org.spacehq.mc.protocol.packet.ingame.client.ClientChatPacket;
  37. import org.spacehq.mc.protocol.packet.ingame.server.ServerChatPacket;
  38. import org.spacehq.mc.protocol.packet.ingame.server.ServerJoinGamePacket;
  39. import org.spacehq.packetlib.Client;
  40. import org.spacehq.packetlib.Server;
  41. import org.spacehq.packetlib.Session;
  42. import org.spacehq.packetlib.event.server.ServerAdapter;
  43. import org.spacehq.packetlib.event.server.SessionAddedEvent;
  44. import org.spacehq.packetlib.event.server.SessionRemovedEvent;
  45. import org.spacehq.packetlib.event.session.DisconnectedEvent;
  46. import org.spacehq.packetlib.event.session.PacketReceivedEvent;
  47. import org.spacehq.packetlib.event.session.SessionAdapter;
  48. import org.spacehq.packetlib.tcp.TcpSessionFactory;
  49.  
  50. import io.netty.util.internal.StringUtil;
  51.  
  52. public class MCBotMain {
  53. private static final boolean SPAWN_SERVER = false;
  54. private static final boolean VERIFY_USERS = true;
  55. private static final String HOST = "mc.ecocitycraft.com";
  56. private static final int PORT = 25565;
  57. private static final Proxy PROXY = Proxy.NO_PROXY;
  58. private static final Proxy AUTH_PROXY = Proxy.NO_PROXY;
  59. private static final String USERNAME = "Edit";
  60. private static final String PASSWORD = "Edit";
  61. private static String input = "";
  62. private static int voteCount = 0;
  63. private static Message message = null;
  64.  
  65. /*public static void consoleInput() {
  66. Scanner scanner = new Scanner(System.in);
  67. input = scanner.nextLine();
  68. return;
  69.  
  70. }
  71. */
  72. public static void main(String[] args) {
  73.  
  74. //consoleInput();
  75.  
  76. if(SPAWN_SERVER) {
  77. Server server = new Server(HOST, PORT, MinecraftProtocol.class, new TcpSessionFactory(PROXY));
  78. server.setGlobalFlag(MinecraftConstants.AUTH_PROXY_KEY, AUTH_PROXY);
  79. server.setGlobalFlag(MinecraftConstants.VERIFY_USERS_KEY, VERIFY_USERS);
  80. server.setGlobalFlag(MinecraftConstants.SERVER_INFO_BUILDER_KEY, new ServerInfoBuilder() {
  81. @Override
  82. public ServerStatusInfo buildInfo(Session session) {
  83. return new ServerStatusInfo(new VersionInfo(MinecraftConstants.GAME_VERSION, MinecraftConstants.PROTOCOL_VERSION), new PlayerInfo(100, 0, new GameProfile[0]), new TextMessage("Hello world!"), null);
  84. }
  85. });
  86.  
  87. server.setGlobalFlag(MinecraftConstants.SERVER_LOGIN_HANDLER_KEY, new ServerLoginHandler() {
  88. @Override
  89. public void loggedIn(Session session) {
  90. session.send(new ServerJoinGamePacket(0, false, GameMode.SURVIVAL, 0, Difficulty.PEACEFUL, 10, WorldType.DEFAULT, false));
  91. }
  92.  
  93.  
  94.  
  95. });
  96.  
  97.  
  98.  
  99. server.setGlobalFlag(MinecraftConstants.SERVER_COMPRESSION_THRESHOLD, 100);
  100. server.addListener(new ServerAdapter() {
  101. @Override
  102. public void sessionAdded(SessionAddedEvent event) {
  103. event.getSession().addListener(new SessionAdapter() {
  104. @Override
  105. public void packetReceived(PacketReceivedEvent event) {
  106. if(event.getPacket() instanceof ClientChatPacket) {
  107. ClientChatPacket packet = event.getPacket();
  108. GameProfile profile = event.getSession().getFlag(MinecraftConstants.PROFILE_KEY);
  109. System.out.println(profile.getName() + ": " + packet.getMessage());
  110. Message msg = new TextMessage("Hello, ").setStyle(new MessageStyle().setColor(ChatColor.GREEN));
  111. Message name = new TextMessage(profile.getName()).setStyle(new MessageStyle().setColor(ChatColor.AQUA).addFormat(ChatFormat.UNDERLINED));
  112. Message end = new TextMessage("!");
  113. msg.addExtra(name);
  114. msg.addExtra(end);
  115. event.getSession().send(new ServerChatPacket(msg));
  116. }
  117.  
  118.  
  119. }
  120.  
  121. });
  122. }
  123.  
  124. @Override
  125. public void sessionRemoved(SessionRemovedEvent event) {
  126. MinecraftProtocol protocol = (MinecraftProtocol) event.getSession().getPacketProtocol();
  127. if(protocol.getSubProtocol() == SubProtocol.GAME) {
  128. System.out.println("Closing server.");
  129. event.getServer().close();
  130. }
  131. }
  132. });
  133.  
  134. server.bind();
  135. }
  136.  
  137. status();
  138. login();
  139.  
  140. }
  141.  
  142. private static void status() {
  143. MinecraftProtocol protocol = new MinecraftProtocol(SubProtocol.STATUS);
  144. Client client = new Client(HOST, PORT, protocol, new TcpSessionFactory(PROXY));
  145. client.getSession().setFlag(MinecraftConstants.AUTH_PROXY_KEY, AUTH_PROXY);
  146. client.getSession().setFlag(MinecraftConstants.SERVER_INFO_HANDLER_KEY, new ServerInfoHandler() {
  147. @Override
  148. public void handle(Session session, ServerStatusInfo info) {
  149. System.out.println("Version: " + info.getVersionInfo().getVersionName() + ", " + info.getVersionInfo().getProtocolVersion());
  150. System.out.println("Player Count: " + info.getPlayerInfo().getOnlinePlayers() + " / " + info.getPlayerInfo().getMaxPlayers());
  151. System.out.println("Players: " + Arrays.toString(info.getPlayerInfo().getPlayers()));
  152. System.out.println("Description: " + info.getDescription().getFullText());
  153. System.out.println("Icon: " + info.getIcon());
  154. }
  155. });
  156.  
  157. client.getSession().setFlag(MinecraftConstants.SERVER_PING_TIME_HANDLER_KEY, new ServerPingTimeHandler() {
  158. @Override
  159. public void handle(Session session, long pingTime) {
  160. System.out.println("Server ping took " + pingTime + "ms");
  161. }
  162. });
  163.  
  164. client.getSession().connect();
  165. while(client.getSession().isConnected()) {
  166. try {
  167. Thread.sleep(5);
  168. } catch(InterruptedException e) {
  169. e.printStackTrace();
  170. }
  171. }
  172. }
  173.  
  174. private static void login() {
  175. MinecraftProtocol protocol = null;
  176. if(VERIFY_USERS) {
  177. try {
  178. protocol = new MinecraftProtocol(USERNAME, PASSWORD, false);
  179. System.out.println("Successfully authenticated user.");
  180. } catch(RequestException e) {
  181. e.printStackTrace();
  182. return;
  183. }
  184. } else {
  185. protocol = new MinecraftProtocol(USERNAME);
  186. }
  187.  
  188. Client client = new Client(HOST, PORT, protocol, new TcpSessionFactory(PROXY));
  189. client.getSession().setFlag(MinecraftConstants.AUTH_PROXY_KEY, AUTH_PROXY);
  190. client.getSession().addListener(new SessionAdapter() {
  191. @Override
  192. public void packetReceived(PacketReceivedEvent event) {
  193.  
  194. if(event.getPacket() instanceof ServerJoinGamePacket) {
  195. //event.getSession().send(new ClientChatPacket("Hello, this is a test of MCProtocolLib."));
  196. //eventRecieved.getSession().send(new ClientChatPacket(input));
  197.  
  198. }
  199. else if(event.getPacket() instanceof ServerChatPacket) {
  200. message = event.<ServerChatPacket>getPacket().getMessage();
  201. System.out.println(message.getFullText());
  202.  
  203. writeChatToFile();
  204. readChat(message.getFullText());
  205. if(message instanceof TranslationMessage) {
  206. System.out.println("Received Translation Components: " + Arrays.toString(((TranslationMessage) message).getTranslationParams()));
  207. }
  208.  
  209.  
  210. // event.getSession().disconnect("Finished");
  211.  
  212.  
  213. }
  214.  
  215.  
  216. }
  217.  
  218.  
  219. public void readChat(String message){
  220. //Check for PM
  221. if(message.substring(0,4).equals("From")){
  222. //Do PM Work here
  223. }
  224. else if(message.length() == 15){
  225. if(message.substring(0,15).equals("(**Mendi) !test")){
  226. //Do party command work here
  227. System.out.println("Mendi Issued Command Test");
  228. }
  229. }
  230. else if(message.length() == 13){
  231. if(message.substring(0,13).equals("(**Mendi) !hi")){
  232. //Do party command work here
  233. System.out.println("Mendi Issued Command Hi");
  234. }
  235. }
  236. else if(message.length() == 17){
  237. if(message.substring(0,17).equals("(Whammerist5) !hi")){
  238. //Do party command work here
  239. System.out.println("Wham Issued Command Hi");
  240. }
  241. }
  242. else if(message.length() == 19){
  243. if(message.substring(0,19).equals("(Whammerist5) !test")){
  244. //Do party command work here
  245. System.out.println("Wham Issued Command Test");
  246. }
  247. }
  248.  
  249. }
  250.  
  251. public void writeChatToFile(){
  252. DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy [HH:mm:ss]");
  253. Date date = new Date();
  254.  
  255.  
  256. BufferedWriter writer = null;
  257. try
  258. {
  259. writer = new BufferedWriter(new FileWriter("Log.txt", true));
  260. writer.write("\n");
  261. writer.write(dateFormat.format(date)+message.getFullText());
  262.  
  263. }
  264. catch (IOException e)
  265. {
  266. }
  267. finally
  268. {
  269. try
  270. {
  271. if ( writer != null)
  272. writer.close( );
  273. }
  274. catch (IOException e)
  275. {
  276. }
  277. }
  278.  
  279. }
  280.  
  281. @Override
  282. public void disconnected(DisconnectedEvent event) {
  283. System.out.println("Disconnected: " + Message.fromString(event.getReason()).getFullText());
  284. if(event.getCause() != null) {
  285. event.getCause().printStackTrace();
  286. }
  287. }
  288. });
  289.  
  290. client.getSession().connect();
  291. }
  292. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement