From 1cc95973c602d2aaff42ed563500ba69406e985f Mon Sep 17 00:00:00 2001 From: Steveice10 Date: Tue, 5 Mar 2013 06:59:38 -0800 Subject: [PATCH] Call onConnect when a connection is made to a server, add documentation and utilities for responding to a server list ping. --- README.md | 6 ++ .../ch/spacebase/mcprotocol/net/Server.java | 5 +- .../standard/packet/PacketServerPing.java | 8 +- .../packet/PacketSpawnDroppedItem.java | 75 ------------------- .../spacebase/mcprotocol/util/Constants.java | 1 + .../ch/spacebase/mcprotocol/util/Util.java | 4 + 6 files changed, 22 insertions(+), 77 deletions(-) delete mode 100644 src/main/java/ch/spacebase/mcprotocol/standard/packet/PacketSpawnDroppedItem.java diff --git a/README.md b/README.md index d235c9ef..bcca6158 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,12 @@ 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. +How to respond to a server list ping +-------- + +When you receive a server list ping packet when listening to a server, respond by calling connection.disconnect(Util.formatPingResponse(motd, players, maxplayers)); + + Chat Bot Example -------- diff --git a/src/main/java/ch/spacebase/mcprotocol/net/Server.java b/src/main/java/ch/spacebase/mcprotocol/net/Server.java index 47ca0d0c..c2de430d 100644 --- a/src/main/java/ch/spacebase/mcprotocol/net/Server.java +++ b/src/main/java/ch/spacebase/mcprotocol/net/Server.java @@ -10,6 +10,7 @@ import java.util.ArrayList; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; +import ch.spacebase.mcprotocol.event.ConnectEvent; import ch.spacebase.mcprotocol.event.ProtocolEvent; import ch.spacebase.mcprotocol.event.ServerListener; import ch.spacebase.mcprotocol.util.Util; @@ -90,7 +91,9 @@ public class Server { try { Socket client = this.sock.accept(); try { - connections.add(new ServerConnection(Server.this.protocol.getDeclaredConstructor().newInstance(), Server.this, client).connect()); + ServerConnection conn = new ServerConnection(Server.this.protocol.getDeclaredConstructor().newInstance(), Server.this, client).connect(); + connections.add(conn); + call(new ConnectEvent(conn)); } catch (Exception e) { Util.logger().severe("Failed to create server connection!"); e.printStackTrace(); diff --git a/src/main/java/ch/spacebase/mcprotocol/standard/packet/PacketServerPing.java b/src/main/java/ch/spacebase/mcprotocol/standard/packet/PacketServerPing.java index a7acf351..2e28a7ac 100644 --- a/src/main/java/ch/spacebase/mcprotocol/standard/packet/PacketServerPing.java +++ b/src/main/java/ch/spacebase/mcprotocol/standard/packet/PacketServerPing.java @@ -11,13 +11,19 @@ import ch.spacebase.mcprotocol.packet.Packet; public class PacketServerPing extends Packet { private static final byte MAGIC = 1; + public boolean newFormat = false; public PacketServerPing() { } @Override public void read(DataInputStream in) throws IOException { - in.readByte(); + if(in.available() > 0) { + in.readByte(); + this.newFormat = true; + } else { + this.newFormat = false; + } } @Override diff --git a/src/main/java/ch/spacebase/mcprotocol/standard/packet/PacketSpawnDroppedItem.java b/src/main/java/ch/spacebase/mcprotocol/standard/packet/PacketSpawnDroppedItem.java deleted file mode 100644 index 469e68d0..00000000 --- a/src/main/java/ch/spacebase/mcprotocol/standard/packet/PacketSpawnDroppedItem.java +++ /dev/null @@ -1,75 +0,0 @@ -package ch.spacebase.mcprotocol.standard.packet; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; - -import ch.spacebase.mcprotocol.net.Client; -import ch.spacebase.mcprotocol.net.ServerConnection; -import ch.spacebase.mcprotocol.packet.Packet; -import ch.spacebase.mcprotocol.standard.data.ItemStack; - -public class PacketSpawnDroppedItem extends Packet { - - public int entityId; - public ItemStack item; - public int x; - public int y; - public int z; - public byte yaw; - public byte pitch; - public byte roll; - - public PacketSpawnDroppedItem() { - } - - public PacketSpawnDroppedItem(int entityId, ItemStack item, int x, int y, int z, byte yaw, byte pitch, byte roll) { - this.entityId = entityId; - this.item = item; - this.x = x; - this.y = y; - this.z = z; - this.yaw = yaw; - this.pitch = pitch; - this.roll = roll; - } - - @Override - public void read(DataInputStream in) throws IOException { - this.entityId = in.readInt(); - this.item = new ItemStack(); - this.item.read(in); - this.x = in.readInt(); - this.y = in.readInt(); - this.z = in.readInt(); - this.yaw = in.readByte(); - this.pitch = in.readByte(); - this.roll = in.readByte(); - } - - @Override - public void write(DataOutputStream out) throws IOException { - out.writeInt(this.entityId); - this.item.write(out); - out.writeInt(this.x); - out.writeInt(this.y); - out.writeInt(this.z); - out.writeByte(this.yaw); - out.writeByte(this.pitch); - out.writeByte(this.roll); - } - - @Override - public void handleClient(Client conn) { - } - - @Override - public void handleServer(ServerConnection conn) { - } - - @Override - public int getId() { - return 21; - } - -} diff --git a/src/main/java/ch/spacebase/mcprotocol/util/Constants.java b/src/main/java/ch/spacebase/mcprotocol/util/Constants.java index 85e07186..7319f639 100644 --- a/src/main/java/ch/spacebase/mcprotocol/util/Constants.java +++ b/src/main/java/ch/spacebase/mcprotocol/util/Constants.java @@ -4,6 +4,7 @@ public class Constants { public static final int LAUNCHER_VERSION = 13; public static final byte STANDARD_PROTOCOL_VERSION = 51; + public static final String STANDARD_MINECRAFT_VERSION = "1.4.7"; public static final byte CLASSIC_PROTOCOL_VERSION = 7; public static final byte[] POCKET_MAGIC = new byte[] { 0, -1, -1, 0, -2, -2, -2, -2, -3, -3, -3, -3, 18, 52, 86, 120 }; diff --git a/src/main/java/ch/spacebase/mcprotocol/util/Util.java b/src/main/java/ch/spacebase/mcprotocol/util/Util.java index 6bc6d318..7c3aae21 100644 --- a/src/main/java/ch/spacebase/mcprotocol/util/Util.java +++ b/src/main/java/ch/spacebase/mcprotocol/util/Util.java @@ -51,5 +51,9 @@ public class Util { return null; } } + + public static String formatPingResponse(String motd, int players, int maxplayers) { + return "ยง1\0" + Constants.STANDARD_PROTOCOL_VERSION + "\0" + Constants.STANDARD_MINECRAFT_VERSION + "\0" + motd + "\0" + players + "\0" + maxplayers; + } }