mirror of
https://github.com/GeyserMC/MCProtocolLib.git
synced 2025-03-13 22:49:51 -04:00
Formatting and readme update
This commit is contained in:
parent
5e3286b98e
commit
33adbca643
3 changed files with 25 additions and 29 deletions
|
@ -8,7 +8,9 @@
|
|||
|
||||
mc-protocol-lib 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.
|
||||
|
||||
<b>Chat Example</b>
|
||||
|
||||
<b>Chat Bot Example</b>
|
||||
--------
|
||||
|
||||
See ch.spacebase.mcprotocol.example.ChatBot
|
||||
|
||||
|
|
|
@ -18,48 +18,45 @@ import ch.spacebase.mcprotocol.net.packet.PacketPlayerPositionLook;
|
|||
* Otherwise supply a valid minecraft.net username and password.
|
||||
*/
|
||||
public class ChatBot {
|
||||
|
||||
private Client client;
|
||||
private Listener listener;
|
||||
|
||||
|
||||
public ChatBot(String host, int port) {
|
||||
client = new Client(host, port);
|
||||
listener = new Listener();
|
||||
|
||||
client.listen(listener);
|
||||
this.client = new Client(host, port);
|
||||
this.listener = new Listener();
|
||||
|
||||
this.client.listen(this.listener);
|
||||
}
|
||||
|
||||
|
||||
public void login(String username) {
|
||||
client.setUser(username);
|
||||
|
||||
this.client.setUser(username);
|
||||
|
||||
try {
|
||||
client.connect();
|
||||
this.client.connect();
|
||||
} catch (ConnectException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void say(String text) {
|
||||
PacketChat chat = new PacketChat();
|
||||
|
||||
chat.message = text;
|
||||
|
||||
client.send(chat);
|
||||
this.client.send(chat);
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
ChatBot bot = new ChatBot("127.0.0.1", 25565);
|
||||
|
||||
System.out.println("Logging in...");
|
||||
|
||||
bot.login("Heisenberg");
|
||||
}
|
||||
|
||||
|
||||
private class Listener extends ProtocolListener {
|
||||
@Override
|
||||
public void onPacketRecieve(PacketRecieveEvent event) {
|
||||
Packet packet = event.getPacket();
|
||||
|
||||
switch (event.getPacket().getId()) {
|
||||
|
||||
switch(event.getPacket().getId()) {
|
||||
case 0x0D:
|
||||
onPositionLook((PacketPlayerPositionLook) packet);
|
||||
break;
|
||||
|
@ -68,22 +65,20 @@ public class ChatBot {
|
|||
|
||||
@Override
|
||||
public void onLogin(LoginEvent event) {
|
||||
// no-op
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoginFinish(LoginFinishEvent event) {
|
||||
// no-op
|
||||
}
|
||||
|
||||
|
||||
public void onPositionLook(PacketPlayerPositionLook packet) {
|
||||
client.send(packet);
|
||||
|
||||
DecimalFormat format = new DecimalFormat("#.00");
|
||||
|
||||
|
||||
ChatBot.this.say("Hello, this is Heisenberg at coordinate (" +
|
||||
format.format(packet.x) + ", " + format.format(packet.y) + ", " + format.format(packet.z) +
|
||||
")");
|
||||
format.format(packet.x) + ", " + format.format(packet.y) + ", " + format.format(packet.z) +
|
||||
")");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -57,8 +57,7 @@ public class Client extends Connection {
|
|||
/**
|
||||
* Assigns username without logging into minecraft.net. Use this login method
|
||||
* together with the Bukkit server online-mode=false in server.properties.
|
||||
*
|
||||
* @param username pick one
|
||||
* @param username The username to assign.
|
||||
*/
|
||||
public void setUser(String username) {
|
||||
this.username = username;
|
||||
|
|
Loading…
Reference in a new issue