Call onConnect when a connection is made to a server, add documentation and utilities for responding to a server list ping.

This commit is contained in:
Steveice10 2013-03-05 06:59:38 -08:00
parent 69429e52a9
commit 1cc95973c6
6 changed files with 22 additions and 77 deletions

View file

@ -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. 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>How to respond to a server list ping</b>
--------
When you receive a server list ping packet when listening to a server, respond by calling connection.disconnect(Util.formatPingResponse(motd, players, maxplayers));
<b>Chat Bot Example</b> <b>Chat Bot Example</b>
-------- --------

View file

@ -10,6 +10,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
import ch.spacebase.mcprotocol.event.ConnectEvent;
import ch.spacebase.mcprotocol.event.ProtocolEvent; import ch.spacebase.mcprotocol.event.ProtocolEvent;
import ch.spacebase.mcprotocol.event.ServerListener; import ch.spacebase.mcprotocol.event.ServerListener;
import ch.spacebase.mcprotocol.util.Util; import ch.spacebase.mcprotocol.util.Util;
@ -90,7 +91,9 @@ public class Server {
try { try {
Socket client = this.sock.accept(); Socket client = this.sock.accept();
try { 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) { } catch (Exception e) {
Util.logger().severe("Failed to create server connection!"); Util.logger().severe("Failed to create server connection!");
e.printStackTrace(); e.printStackTrace();

View file

@ -11,13 +11,19 @@ import ch.spacebase.mcprotocol.packet.Packet;
public class PacketServerPing extends Packet { public class PacketServerPing extends Packet {
private static final byte MAGIC = 1; private static final byte MAGIC = 1;
public boolean newFormat = false;
public PacketServerPing() { public PacketServerPing() {
} }
@Override @Override
public void read(DataInputStream in) throws IOException { public void read(DataInputStream in) throws IOException {
if(in.available() > 0) {
in.readByte(); in.readByte();
this.newFormat = true;
} else {
this.newFormat = false;
}
} }
@Override @Override

View file

@ -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;
}
}

View file

@ -4,6 +4,7 @@ public class Constants {
public static final int LAUNCHER_VERSION = 13; public static final int LAUNCHER_VERSION = 13;
public static final byte STANDARD_PROTOCOL_VERSION = 51; 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 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 }; public static final byte[] POCKET_MAGIC = new byte[] { 0, -1, -1, 0, -2, -2, -2, -2, -3, -3, -3, -3, 18, 52, 86, 120 };

View file

@ -52,4 +52,8 @@ public class Util {
} }
} }
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;
}
} }