Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.tr.minechatpc;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.net.Proxy;
- import java.net.URL;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.UUID;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import javax.net.ssl.HttpsURLConnection;
- 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.data.SubProtocol;
- import org.spacehq.mc.protocol.data.message.Message;
- import org.spacehq.mc.protocol.data.message.TranslationMessage;
- import org.spacehq.mc.protocol.data.status.ServerStatusInfo;
- 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.ServerDisconnectPacket;
- import org.spacehq.mc.protocol.packet.ingame.server.ServerJoinGamePacket;
- import org.spacehq.packetlib.Client;
- import org.spacehq.packetlib.Session;
- 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 javafx.application.Application;
- import javafx.geometry.HPos;
- import javafx.geometry.Insets;
- import javafx.geometry.Pos;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.control.Label;
- import javafx.scene.control.PasswordField;
- import javafx.scene.control.TextArea;
- import javafx.scene.control.TextField;
- import javafx.scene.layout.GridPane;
- import javafx.stage.Stage;
- public class MinecraftProtocolTest extends Application{
- private static final boolean VERIFY_USERS = true;
- private static String HOST = "localhost";
- private static int PORT = 25565;
- private static final Proxy PROXY = Proxy.NO_PROXY;
- private static final Proxy AUTH_PROXY = Proxy.NO_PROXY;
- private static String USERNAME;
- private static String PASSWORD;
- private static TextArea chat;
- private static Client client;
- public static void main(String[] args) {
- launch(args);
- }
- @Override
- public void start(Stage myStage) throws Exception {
- myStage.setTitle("Minecraft chat/command client");
- GridPane rootNode = new GridPane();
- rootNode.setPadding(new Insets(15));
- rootNode.setHgap(5);
- rootNode.setVgap(5);
- rootNode.setAlignment(Pos.TOP_CENTER);
- Scene myScene = new Scene(rootNode, 1280, 720);
- rootNode.add(new Label("USERNAME/E-MAIL: "), 0, 0);
- TextField username = new TextField();
- rootNode.add(username, 1, 0);
- rootNode.add(new Label("PASSWORD: "), 0, 1);
- PasswordField password = new PasswordField();
- rootNode.add(password, 1, 1);
- Button aButton = new Button("Login");
- rootNode.add(aButton, 1, 2);
- rootNode.add(new Label("IP: "), 0, 4);
- TextField address = new TextField();
- rootNode.add(address, 1, 4);
- rootNode.add(new Label("PORT: "), 0, 5);
- TextField port = new TextField();
- rootNode.add(port, 1, 5);
- Button connectButton = new Button("Connect");
- rootNode.add(connectButton, 1, 6);
- rootNode.add(new Label("MESSAGE: "), 0, 8);
- TextField msg = new TextField();
- rootNode.add(msg, 1, 8);
- Button sendButton = new Button("Send");
- rootNode.add(sendButton, 2, 8);
- Button clearButton = new Button("Clear");
- rootNode.add(clearButton, 1, 11);
- Button disconnectButton = new Button("Disconnect");
- rootNode.add(disconnectButton, 1, 12);
- rootNode.add(new Label("This software is made by Niklas Haiminen."), 1, 50);
- GridPane.setHalignment(aButton, HPos.LEFT);
- chat = new TextArea();
- chat.setEditable(false);
- rootNode.add(new Label("Chat: "), 0, 10);
- rootNode.add(chat, 1, 10);
- aButton.setOnAction(e -> {
- if(username.getText().length() <= 0 || password.getText().length() <= 0){
- sendMessage("Please setup the right credentials!");
- return;
- }
- USERNAME = username.getText();
- PASSWORD = password.getText();
- sendMessage("Logged in");
- sendMessage("USERNAME: " + USERNAME + ", PASSWORD: " + PASSWORD);
- });
- sendButton.setOnAction(e -> {
- if(username.getText().length() <= 0 || password.getText().length() <= 0){
- sendMessage("Login before doing anything!");
- return;
- }
- else{
- if(msg.getText().length() > 0){
- client.getSession().send(new ClientChatPacket(msg.getText()));
- msg.setText("");
- }
- else{
- sendMessage("Can't send empty messages!");
- }
- }
- });
- clearButton.setOnAction(e -> {
- username.setText("");
- password.setText("");
- chat.setText("");
- address.setText("");
- port.setText("");
- });
- connectButton.setOnAction(e -> {
- if(username.getText().length() <= 0 || password.getText().length() <= 0){
- sendMessage("Login before doing anything!");
- return;
- }
- else{
- USERNAME = username.getText();
- PASSWORD = password.getText();
- sendMessage("Logged in");
- }
- if(address.getText().length() <= 0 || port.getText().length() <= 0){
- sendMessage("Please setup the right address and the port!");
- return;
- }
- sendMessage("Connecting...");
- HOST = address.getText();
- try{
- PORT = Integer.parseInt(port.getText());
- } catch(NumberFormatException ex){
- sendMessage("Please use only numbers when typing server port!");
- return;
- }
- try{
- chat.setText("");
- login();
- status();
- } catch (Exception e1) {
- e1.printStackTrace();
- }
- });
- disconnectButton.setOnAction(e -> {
- if(username.getText().length() <= 0 || password.getText().length() <= 0){
- sendMessage("Login before doing anything!");
- return;
- }
- client.getSession().send(new ServerDisconnectPacket("Bye bye!"));
- });
- myStage.setScene(myScene);
- myStage.show();
- }
- 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) {
- sendMessage("------------ Server Info ------------");
- sendMessage("IP: " + session.getHost() + ":" + session.getPort());
- sendMessage("Version: " + info.getVersionInfo().getVersionName() + ", " + info.getVersionInfo().getProtocolVersion());
- sendMessage("MOTD: " + info.getDescription().getFullText());
- //System.out.println("Icon: " + info.getIcon());
- sendMessage("Player Count: " + info.getPlayerInfo().getOnlinePlayers() + " / " + info.getPlayerInfo().getMaxPlayers());
- // Player list...
- ArrayList<GameProfile> topTen = new ArrayList<>();
- if(info.getPlayerInfo().getPlayers().length <= 10){
- sendMessage("Players: ");
- for(GameProfile g : info.getPlayerInfo().getPlayers()){
- topTen.add(g);
- sendMessage("- " + g.getName());
- }
- }
- else{
- sendMessage("Players: (TOP 10)");
- for(int i = 0; i <= 9; i++){
- GameProfile g = info.getPlayerInfo().getPlayers()[i];
- topTen.add(g);
- sendMessage("- " + g.getName());
- }
- sendMessage("And " + (info.getPlayerInfo().getOnlinePlayers() - topTen.size()) + " more players!");
- }
- sendMessage("-------------------------------------");
- }
- });
- client.getSession().setFlag(MinecraftConstants.SERVER_PING_TIME_HANDLER_KEY, new ServerPingTimeHandler() {
- @Override
- public void handle(Session session, long pingTime) {
- sendMessage("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;
- System.out.println("USERNAME: " + USERNAME + ", PASSWORD: " + PASSWORD);
- if(VERIFY_USERS) {
- try {
- protocol = new MinecraftProtocol(USERNAME, PASSWORD, false);
- sendMessage("Successfully authenticated user.");
- } catch(RequestException e) {
- e.printStackTrace();
- return;
- }
- } else {
- protocol = new MinecraftProtocol(USERNAME);
- sendMessage("Didn't authenticate the user.");
- }
- 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("TeamRaiderz häckäilee sen custom Minecraft clientillä!"));
- } else if(event.getPacket() instanceof ServerChatPacket) {
- Message message = event.<ServerChatPacket>getPacket().getMessage();
- sendMessage(message.getFullText());
- if(message instanceof TranslationMessage) {
- sendMessage("Received Translation Components: " + Arrays.toString(((TranslationMessage) message).getTranslationParams()));
- }
- }
- }
- @Override
- public void disconnected(DisconnectedEvent event) {
- sendMessage("Disconnected: " + Message.fromString(event.getReason()).getFullText());
- if(event.getCause() != null) {
- event.getCause().printStackTrace();
- }
- }
- });
- client.getSession().connect();
- }
- private static void sendMessage(String text){
- chat.appendText("\n" + text);
- }
- private final static String authserver = "https://authserver.mojang.com";
- public static String authenticate(String username, String password) throws Exception {
- String genClientToken = UUID.randomUUID().toString();
- // Setting up json POST request
- String payload = "{\"agent\": {\"name\": \"Minecraft\",\"version\": 1},\"username\": \"" + username
- + "\",\"password\": \"" + password + "\",\"clientToken\": \"" + genClientToken + "\"}";
- String output = postReadURL(payload, new URL(authserver + "/authenticate"));
- // Setting up patterns
- String authBeg = "{\"accessToken\":\"";
- String authEnd = "\",\"clientToken\":\"";
- String clientEnd = "\",\"selectedProfile\"";
- // What we are looking for
- String authtoken = getStringBetween(output, authBeg, authEnd);
- return authtoken;
- }
- private static String postReadURL(String payload, URL url) throws Exception {
- HttpsURLConnection con = (HttpsURLConnection) (url.openConnection());
- con.setReadTimeout(15000);
- con.setConnectTimeout(15000);
- con.setRequestMethod("POST");
- con.setRequestProperty("Content-Type", "application/json");
- con.setDoInput(true);
- con.setDoOutput(true);
- OutputStream out = con.getOutputStream();
- out.write(payload.getBytes("UTF-8"));
- out.close();
- BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
- String output = "";
- String line = null;
- while ((line = in.readLine()) != null)
- output += line;
- in.close();
- return output;
- }
- private static String getStringBetween(String base, String begin, String end) {
- Pattern patbeg = Pattern.compile(Pattern.quote(begin));
- Pattern patend = Pattern.compile(Pattern.quote(end));
- int resbeg = 0;
- int resend = base.length() - 1;
- Matcher matbeg = patbeg.matcher(base);
- if (matbeg.find())
- resbeg = matbeg.end();
- Matcher matend = patend.matcher(base);
- if (matend.find())
- resend = matend.start();
- return base.substring(resbeg, resend);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement