Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.net.Proxy;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.Arrays;
- import java.util.Date;
- import java.util.Scanner;
- import org.spacehq.mc.auth.data.GameProfile;
- import org.spacehq.mc.auth.exception.request.RequestException;
- import org.spacehq.mc.protocol.MinecraftConstants;
- import org.spacehq.mc.protocol.MinecraftProtocol;
- import org.spacehq.mc.protocol.ServerLoginHandler;
- import org.spacehq.mc.protocol.data.SubProtocol;
- import org.spacehq.mc.protocol.data.game.entity.player.GameMode;
- import org.spacehq.mc.protocol.data.game.setting.Difficulty;
- import org.spacehq.mc.protocol.data.game.world.WorldType;
- import org.spacehq.mc.protocol.data.message.ChatColor;
- import org.spacehq.mc.protocol.data.message.ChatFormat;
- import org.spacehq.mc.protocol.data.message.Message;
- import org.spacehq.mc.protocol.data.message.MessageStyle;
- import org.spacehq.mc.protocol.data.message.TextMessage;
- import org.spacehq.mc.protocol.data.message.TranslationMessage;
- import org.spacehq.mc.protocol.data.status.PlayerInfo;
- import org.spacehq.mc.protocol.data.status.ServerStatusInfo;
- import org.spacehq.mc.protocol.data.status.VersionInfo;
- import org.spacehq.mc.protocol.data.status.handler.ServerInfoBuilder;
- import org.spacehq.mc.protocol.data.status.handler.ServerInfoHandler;
- import org.spacehq.mc.protocol.data.status.handler.ServerPingTimeHandler;
- import org.spacehq.mc.protocol.packet.ingame.client.ClientChatPacket;
- import org.spacehq.mc.protocol.packet.ingame.server.ServerChatPacket;
- import org.spacehq.mc.protocol.packet.ingame.server.ServerJoinGamePacket;
- import org.spacehq.packetlib.Client;
- import org.spacehq.packetlib.Server;
- import org.spacehq.packetlib.Session;
- import org.spacehq.packetlib.event.server.ServerAdapter;
- import org.spacehq.packetlib.event.server.SessionAddedEvent;
- import org.spacehq.packetlib.event.server.SessionRemovedEvent;
- import org.spacehq.packetlib.event.session.DisconnectedEvent;
- import org.spacehq.packetlib.event.session.PacketReceivedEvent;
- import org.spacehq.packetlib.event.session.SessionAdapter;
- import org.spacehq.packetlib.tcp.TcpSessionFactory;
- import io.netty.util.internal.StringUtil;
- public class MCBotMain {
- private static final boolean SPAWN_SERVER = false;
- private static final boolean VERIFY_USERS = true;
- private static final String HOST = "mc.ecocitycraft.com";
- private static final int PORT = 25565;
- private static final Proxy PROXY = Proxy.NO_PROXY;
- private static final Proxy AUTH_PROXY = Proxy.NO_PROXY;
- private static final String USERNAME = "Edit";
- private static final String PASSWORD = "Edit";
- private static String input = "";
- private static int voteCount = 0;
- private static Message message = null;
- /*public static void consoleInput() {
- Scanner scanner = new Scanner(System.in);
- input = scanner.nextLine();
- return;
- }
- */
- public static void main(String[] args) {
- //consoleInput();
- if(SPAWN_SERVER) {
- Server server = new Server(HOST, PORT, MinecraftProtocol.class, new TcpSessionFactory(PROXY));
- server.setGlobalFlag(MinecraftConstants.AUTH_PROXY_KEY, AUTH_PROXY);
- server.setGlobalFlag(MinecraftConstants.VERIFY_USERS_KEY, VERIFY_USERS);
- server.setGlobalFlag(MinecraftConstants.SERVER_INFO_BUILDER_KEY, new ServerInfoBuilder() {
- @Override
- public ServerStatusInfo buildInfo(Session session) {
- return new ServerStatusInfo(new VersionInfo(MinecraftConstants.GAME_VERSION, MinecraftConstants.PROTOCOL_VERSION), new PlayerInfo(100, 0, new GameProfile[0]), new TextMessage("Hello world!"), null);
- }
- });
- server.setGlobalFlag(MinecraftConstants.SERVER_LOGIN_HANDLER_KEY, new ServerLoginHandler() {
- @Override
- public void loggedIn(Session session) {
- session.send(new ServerJoinGamePacket(0, false, GameMode.SURVIVAL, 0, Difficulty.PEACEFUL, 10, WorldType.DEFAULT, false));
- }
- });
- server.setGlobalFlag(MinecraftConstants.SERVER_COMPRESSION_THRESHOLD, 100);
- server.addListener(new ServerAdapter() {
- @Override
- public void sessionAdded(SessionAddedEvent event) {
- event.getSession().addListener(new SessionAdapter() {
- @Override
- public void packetReceived(PacketReceivedEvent event) {
- if(event.getPacket() instanceof ClientChatPacket) {
- ClientChatPacket packet = event.getPacket();
- GameProfile profile = event.getSession().getFlag(MinecraftConstants.PROFILE_KEY);
- System.out.println(profile.getName() + ": " + packet.getMessage());
- Message msg = new TextMessage("Hello, ").setStyle(new MessageStyle().setColor(ChatColor.GREEN));
- Message name = new TextMessage(profile.getName()).setStyle(new MessageStyle().setColor(ChatColor.AQUA).addFormat(ChatFormat.UNDERLINED));
- Message end = new TextMessage("!");
- msg.addExtra(name);
- msg.addExtra(end);
- event.getSession().send(new ServerChatPacket(msg));
- }
- }
- });
- }
- @Override
- public void sessionRemoved(SessionRemovedEvent event) {
- MinecraftProtocol protocol = (MinecraftProtocol) event.getSession().getPacketProtocol();
- if(protocol.getSubProtocol() == SubProtocol.GAME) {
- System.out.println("Closing server.");
- event.getServer().close();
- }
- }
- });
- server.bind();
- }
- status();
- login();
- }
- private static void status() {
- MinecraftProtocol protocol = new MinecraftProtocol(SubProtocol.STATUS);
- Client client = new Client(HOST, PORT, protocol, new TcpSessionFactory(PROXY));
- client.getSession().setFlag(MinecraftConstants.AUTH_PROXY_KEY, AUTH_PROXY);
- client.getSession().setFlag(MinecraftConstants.SERVER_INFO_HANDLER_KEY, new ServerInfoHandler() {
- @Override
- public void handle(Session session, ServerStatusInfo info) {
- System.out.println("Version: " + info.getVersionInfo().getVersionName() + ", " + info.getVersionInfo().getProtocolVersion());
- System.out.println("Player Count: " + info.getPlayerInfo().getOnlinePlayers() + " / " + info.getPlayerInfo().getMaxPlayers());
- System.out.println("Players: " + Arrays.toString(info.getPlayerInfo().getPlayers()));
- System.out.println("Description: " + info.getDescription().getFullText());
- System.out.println("Icon: " + info.getIcon());
- }
- });
- client.getSession().setFlag(MinecraftConstants.SERVER_PING_TIME_HANDLER_KEY, new ServerPingTimeHandler() {
- @Override
- public void handle(Session session, long pingTime) {
- System.out.println("Server ping took " + pingTime + "ms");
- }
- });
- client.getSession().connect();
- while(client.getSession().isConnected()) {
- try {
- Thread.sleep(5);
- } catch(InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- private static void login() {
- MinecraftProtocol protocol = null;
- if(VERIFY_USERS) {
- try {
- protocol = new MinecraftProtocol(USERNAME, PASSWORD, false);
- System.out.println("Successfully authenticated user.");
- } catch(RequestException e) {
- e.printStackTrace();
- return;
- }
- } else {
- protocol = new MinecraftProtocol(USERNAME);
- }
- Client client = new Client(HOST, PORT, protocol, new TcpSessionFactory(PROXY));
- client.getSession().setFlag(MinecraftConstants.AUTH_PROXY_KEY, AUTH_PROXY);
- client.getSession().addListener(new SessionAdapter() {
- @Override
- public void packetReceived(PacketReceivedEvent event) {
- if(event.getPacket() instanceof ServerJoinGamePacket) {
- //event.getSession().send(new ClientChatPacket("Hello, this is a test of MCProtocolLib."));
- //eventRecieved.getSession().send(new ClientChatPacket(input));
- }
- else if(event.getPacket() instanceof ServerChatPacket) {
- message = event.<ServerChatPacket>getPacket().getMessage();
- System.out.println(message.getFullText());
- writeChatToFile();
- readChat(message.getFullText());
- if(message instanceof TranslationMessage) {
- System.out.println("Received Translation Components: " + Arrays.toString(((TranslationMessage) message).getTranslationParams()));
- }
- // event.getSession().disconnect("Finished");
- }
- }
- public void readChat(String message){
- //Check for PM
- if(message.substring(0,4).equals("From")){
- //Do PM Work here
- }
- else if(message.length() == 15){
- if(message.substring(0,15).equals("(**Mendi) !test")){
- //Do party command work here
- System.out.println("Mendi Issued Command Test");
- }
- }
- else if(message.length() == 13){
- if(message.substring(0,13).equals("(**Mendi) !hi")){
- //Do party command work here
- System.out.println("Mendi Issued Command Hi");
- }
- }
- else if(message.length() == 17){
- if(message.substring(0,17).equals("(Whammerist5) !hi")){
- //Do party command work here
- System.out.println("Wham Issued Command Hi");
- }
- }
- else if(message.length() == 19){
- if(message.substring(0,19).equals("(Whammerist5) !test")){
- //Do party command work here
- System.out.println("Wham Issued Command Test");
- }
- }
- }
- public void writeChatToFile(){
- DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy [HH:mm:ss]");
- Date date = new Date();
- BufferedWriter writer = null;
- try
- {
- writer = new BufferedWriter(new FileWriter("Log.txt", true));
- writer.write("\n");
- writer.write(dateFormat.format(date)+message.getFullText());
- }
- catch (IOException e)
- {
- }
- finally
- {
- try
- {
- if ( writer != null)
- writer.close( );
- }
- catch (IOException e)
- {
- }
- }
- }
- @Override
- public void disconnected(DisconnectedEvent event) {
- System.out.println("Disconnected: " + Message.fromString(event.getReason()).getFullText());
- if(event.getCause() != null) {
- event.getCause().printStackTrace();
- }
- }
- });
- client.getSession().connect();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement