forked from Camotoy/ClassicProtocolLib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinecraftClassicProtocolTest.java
More file actions
148 lines (132 loc) · 7.4 KB
/
MinecraftClassicProtocolTest.java
File metadata and controls
148 lines (132 loc) · 7.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package com.github.steveice10.mc.classic.protocol.test;
import com.github.steveice10.mc.classic.protocol.ClassicConstants;
import com.github.steveice10.mc.classic.protocol.ClassicProtocol;
import com.github.steveice10.mc.classic.protocol.VerifyUsersListener;
import com.github.steveice10.mc.classic.protocol.data.game.UserType;
import com.github.steveice10.mc.classic.protocol.data.heartbeat.ServerInfo;
import com.github.steveice10.mc.classic.protocol.data.heartbeat.ServerInfoBuilder;
import com.github.steveice10.mc.classic.protocol.data.serverlist.ServerList;
import com.github.steveice10.mc.classic.protocol.data.serverlist.ServerURLInfo;
import com.github.steveice10.mc.classic.protocol.exception.AuthenticationException;
import com.github.steveice10.mc.classic.protocol.packet.client.ClientChatPacket;
import com.github.steveice10.mc.classic.protocol.packet.client.ClientIdentificationPacket;
import com.github.steveice10.mc.classic.protocol.packet.server.ServerChatPacket;
import com.github.steveice10.mc.classic.protocol.packet.server.ServerIdentificationPacket;
import com.github.steveice10.mc.classic.protocol.packet.server.ServerLevelFinalizePacket;
import com.github.steveice10.packetlib.Client;
import com.github.steveice10.packetlib.Server;
import com.github.steveice10.packetlib.event.server.ServerAdapter;
import com.github.steveice10.packetlib.event.server.SessionAddedEvent;
import com.github.steveice10.packetlib.event.server.SessionRemovedEvent;
import com.github.steveice10.packetlib.event.session.DisconnectedEvent;
import com.github.steveice10.packetlib.event.session.PacketReceivedEvent;
import com.github.steveice10.packetlib.event.session.SessionAdapter;
import com.github.steveice10.packetlib.tcp.TcpSessionFactory;
public class MinecraftClassicProtocolTest {
private static final boolean SPAWN_SERVER = true;
private static final boolean VERIFY_USERS = true; // Set to true to test ClassiCube Login
private static final String SERVER_HOST = "127.0.0.1";
private static final int SERVER_PORT = 25565;
private static final String USERNAME = "Username";
private static final String PASSWORD = "Password";
private static String serverUrl = "";
public static void main(String[] args) {
if(SPAWN_SERVER) {
Server server = new Server(SERVER_HOST, SERVER_PORT, ClassicProtocol.class, new TcpSessionFactory());
if(VERIFY_USERS) {
server.addListener(new VerifyUsersListener());
server.setGlobalFlag(ClassicConstants.SERVER_INFO_BUILDER_KEY, new ServerInfoBuilder() {
@Override
public ServerInfo build(Server server) {
// Corrected parameter order for the new ServerInfo constructor
return new ServerInfo("Test Server", server.getPort(), true, 0, 20);
}
});
}
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 ClientIdentificationPacket) {
ClientIdentificationPacket packet = event.getPacket();
// Store username in session flags
event.getSession().setFlag(ClassicConstants.USERNAME_KEY, packet.getUsername());
event.getSession().send(new ServerIdentificationPacket("Test Server", "A test of a server thing.", UserType.NOT_OP));
event.getSession().send(new ServerLevelFinalizePacket(128, 128, 128));
} else if(event.getPacket() instanceof ClientChatPacket) {
ClientChatPacket packet = event.getPacket();
String username = event.getSession().getFlag(ClassicConstants.USERNAME_KEY);
System.out.println("[SERVER] " + username + ": " + packet.getMessage());
event.getSession().send(new ServerChatPacket(0, username + ": " + packet.getMessage()));
}
}
});
}
@Override
public void sessionRemoved(SessionRemovedEvent event) {
System.out.println("Closing server.");
event.getServer().close();
}
});
server.bind();
if(VERIFY_USERS) {
System.out.println("Waiting for Heartbeat URL...");
while(!server.hasGlobalFlag(ClassicConstants.SERVER_URL_KEY)) {
try { Thread.sleep(250); } catch(InterruptedException e) {}
}
serverUrl = server.getGlobalFlag(ClassicConstants.SERVER_URL_KEY);
System.out.println("Server is public at: " + serverUrl);
}
}
login();
}
private static void login() {
ClassicProtocol protocol;
String host = SERVER_HOST;
int port = SERVER_PORT;
if(VERIFY_USERS) {
try {
// login now returns the Session Token
String token = ServerList.login(USERNAME, PASSWORD);
System.out.println("Successfully authenticated user.");
ServerURLInfo info = ServerList.getServerURLInfo(serverUrl);
host = info.getHost();
port = info.getPort();
// Initialize protocol with the returned token as verificationKey
protocol = new ClassicProtocol(USERNAME, token);
} catch(AuthenticationException e) {
System.err.println("Failed to authenticate user.");
e.printStackTrace();
return;
}
} else {
String cleanName = USERNAME.contains("@") ? USERNAME.substring(0, USERNAME.indexOf("@")) : USERNAME;
protocol = new ClassicProtocol(cleanName);
}
Client client = new Client(host, port, protocol, new TcpSessionFactory());
client.getSession().addListener(new SessionAdapter() {
@Override
public void packetReceived(PacketReceivedEvent event) {
if(event.getPacket() instanceof ServerLevelFinalizePacket) {
event.getSession().send(new ClientChatPacket("Hello, this is a test of ClassicProtocolLib."));
} else if(event.getPacket() instanceof ServerChatPacket) {
String message = event.<ServerChatPacket>getPacket().getMessage();
System.out.println("[CLIENT] " + message);
if(message.contains("Hello, this is a test of ClassicProtocolLib.")) {
event.getSession().disconnect("Finished");
}
}
}
@Override
public void disconnected(DisconnectedEvent event) {
System.out.println("Disconnected: " + event.getReason());
}
});
client.getSession().connect(true);
while(client.getSession().isConnected()) {
try { Thread.sleep(5); } catch(InterruptedException e) { break; }
}
}
}