forked from GeyserMC/MCProtocolLib
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
General cleanup, fixes, and re-versioning.
- Loading branch information
Steven Smith
authored and
Steven Smith
committed
Jul 20, 2015
1 parent
2b39ead
commit b22cd66
Showing
240 changed files
with
10,819 additions
and
10,646 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
bin | ||
lib | ||
target | ||
testing | ||
|
||
.settings | ||
.classpath | ||
.project | ||
.directory | ||
|
||
*.iml | ||
.idea | ||
|
||
target | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,14 @@ | ||
<b><center><h1>MCProtocolLib</h></center></b> | ||
========== | ||
|
||
|
||
|
||
<b>About MCProtocolLib</b> | ||
-------- | ||
|
||
# MCProtocolLib | ||
MCProtocolLib is a simple library for communicating with a Minecraft client/server. It aims to allow people to make custom bots, clients, or servers for Minecraft easily. | ||
|
||
## Example Code | ||
See example/org/spacehq/mc/protocol/test | ||
|
||
<b>Example Code</b> | ||
-------- | ||
|
||
See example/org/spacehq/mc/protocol/test/Test.java | ||
|
||
|
||
<b>Building the Source</b> | ||
-------- | ||
|
||
## Building the Source | ||
MCProtocolLib uses Maven to manage dependencies. Simply run 'mvn clean install' in the source's directory. | ||
Snapshots (if any exist) can be downloaded <b>[here](http://repo.spacehq.org/content/repositories/snapshots/org/spacehq/mcprotocollib)</b>. | ||
Releases (if any exist) can be downloaded <b>[here](http://repo.spacehq.org/content/repositories/release/org/spacehq/mcprotocollib)</b>. | ||
|
||
|
||
<b>License</b> | ||
--------- | ||
Builds can be downloaded **[here](http://build.spacehq.org/job/MCProtocolLib)**. | ||
|
||
MCProtocolLib is licensed under the <b>[MIT license](http://www.opensource.org/licenses/mit-license.html)</b>. | ||
## License | ||
MCProtocolLib is licensed under the **[MIT license](http://www.opensource.org/licenses/mit-license.html)**. | ||
|
183 changes: 183 additions & 0 deletions
183
example/org/spacehq/mc/protocol/test/MinecraftProtocolTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
package org.spacehq.mc.protocol.test; | ||
|
||
import org.spacehq.mc.auth.GameProfile; | ||
import org.spacehq.mc.auth.exception.AuthenticationException; | ||
import org.spacehq.mc.protocol.MinecraftProtocol; | ||
import org.spacehq.mc.protocol.ProtocolConstants; | ||
import org.spacehq.mc.protocol.ProtocolMode; | ||
import org.spacehq.mc.protocol.ServerLoginHandler; | ||
import org.spacehq.mc.protocol.data.game.values.entity.player.GameMode; | ||
import org.spacehq.mc.protocol.data.game.values.setting.Difficulty; | ||
import org.spacehq.mc.protocol.data.game.values.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 java.net.Proxy; | ||
import java.util.Arrays; | ||
|
||
public class MinecraftProtocolTest { | ||
private static final boolean SPAWN_SERVER = true; | ||
private static final boolean VERIFY_USERS = false; | ||
private static final String HOST = "127.0.0.1"; | ||
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 = "Username"; | ||
private static final String PASSWORD = "Password"; | ||
|
||
public static void main(String[] args) { | ||
if(SPAWN_SERVER) { | ||
Server server = new Server(HOST, PORT, MinecraftProtocol.class, new TcpSessionFactory(PROXY)); | ||
server.setGlobalFlag(ProtocolConstants.AUTH_PROXY_KEY, AUTH_PROXY); | ||
server.setGlobalFlag(ProtocolConstants.VERIFY_USERS_KEY, VERIFY_USERS); | ||
server.setGlobalFlag(ProtocolConstants.SERVER_INFO_BUILDER_KEY, new ServerInfoBuilder() { | ||
@Override | ||
public ServerStatusInfo buildInfo(Session session) { | ||
return new ServerStatusInfo(new VersionInfo(ProtocolConstants.GAME_VERSION, ProtocolConstants.PROTOCOL_VERSION), new PlayerInfo(100, 0, new GameProfile[0]), new TextMessage("Hello world!"), null); | ||
} | ||
}); | ||
|
||
server.setGlobalFlag(ProtocolConstants.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(ProtocolConstants.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(ProtocolConstants.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.getMode() == ProtocolMode.GAME) { | ||
System.out.println("Closing server."); | ||
event.getServer().close(); | ||
} | ||
} | ||
}); | ||
|
||
server.bind(); | ||
} | ||
|
||
status(); | ||
login(); | ||
} | ||
|
||
private static void status() { | ||
MinecraftProtocol protocol = new MinecraftProtocol(ProtocolMode.STATUS); | ||
Client client = new Client(HOST, PORT, protocol, new TcpSessionFactory(PROXY)); | ||
client.getSession().setFlag(ProtocolConstants.AUTH_PROXY_KEY, AUTH_PROXY); | ||
client.getSession().setFlag(ProtocolConstants.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(ProtocolConstants.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(AuthenticationException e) { | ||
e.printStackTrace(); | ||
return; | ||
} | ||
} else { | ||
protocol = new MinecraftProtocol(USERNAME); | ||
} | ||
|
||
Client client = new Client(HOST, PORT, protocol, new TcpSessionFactory(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.")); | ||
} else if(event.getPacket() instanceof ServerChatPacket) { | ||
Message message = event.<ServerChatPacket>getPacket().getMessage(); | ||
System.out.println("Received Message: " + message.getFullText()); | ||
if(message instanceof TranslationMessage) { | ||
System.out.println("Received Translation Components: " + Arrays.toString(((TranslationMessage) message).getTranslationParams())); | ||
} | ||
|
||
event.getSession().disconnect("Finished"); | ||
} | ||
} | ||
|
||
@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(); | ||
} | ||
} |
Oops, something went wrong.