Use file to handle packet id mappings

This commit is contained in:
LOOHP 2020-08-04 00:19:38 +08:00
parent 96bb492fc4
commit 212c8f2d41
26 changed files with 164 additions and 99 deletions

View File

@ -1,6 +1,7 @@
package com.loohp.limbo;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
@ -9,31 +10,16 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import com.loohp.limbo.File.ServerProperties;
import com.loohp.limbo.Location.Location;
import com.loohp.limbo.Server.ServerConnection;
import com.loohp.limbo.Server.Packets.Packet;
import com.loohp.limbo.Server.Packets.PacketHandshakingIn;
import com.loohp.limbo.Server.Packets.PacketLoginInLoginStart;
import com.loohp.limbo.Server.Packets.PacketLoginOutLoginSuccess;
import com.loohp.limbo.Server.Packets.PacketPlayInChat;
import com.loohp.limbo.Server.Packets.PacketPlayInKeepAlive;
import com.loohp.limbo.Server.Packets.PacketPlayInPosition;
import com.loohp.limbo.Server.Packets.PacketPlayInPositionAndLook;
import com.loohp.limbo.Server.Packets.PacketPlayOutChat;
import com.loohp.limbo.Server.Packets.PacketPlayOutKeepAlive;
import com.loohp.limbo.Server.Packets.PacketPlayOutLogin;
import com.loohp.limbo.Server.Packets.PacketPlayOutMapChunk;
import com.loohp.limbo.Server.Packets.PacketPlayOutPlayerAbilities;
import com.loohp.limbo.Server.Packets.PacketPlayOutPlayerInfo;
import com.loohp.limbo.Server.Packets.PacketPlayOutPositionAndLook;
import com.loohp.limbo.Server.Packets.PacketPlayOutShowPlayerSkins;
import com.loohp.limbo.Server.Packets.PacketPlayOutSpawnPosition;
import com.loohp.limbo.Server.Packets.PacketPlayOutUpdateViewPosition;
import com.loohp.limbo.Server.Packets.PacketStatusInPing;
import com.loohp.limbo.Server.Packets.PacketStatusInRequest;
import com.loohp.limbo.Server.Packets.PacketStatusOutPong;
import com.loohp.limbo.Server.Packets.PacketStatusOutResponse;
import com.loohp.limbo.Server.Packets.PacketIn;
import com.loohp.limbo.Server.Packets.PacketOut;
import com.loohp.limbo.Utils.ImageUtils;
import com.loohp.limbo.World.Schematic;
import com.loohp.limbo.World.World;
@ -45,7 +31,7 @@ public class Limbo {
private static Limbo instance;
public static void main(String args[]) throws IOException {
public static void main(String args[]) throws IOException, ParseException, NumberFormatException, ClassNotFoundException {
new Limbo();
}
@ -62,7 +48,8 @@ public class Limbo {
private ServerProperties properties;
public Limbo() throws IOException {
@SuppressWarnings("unchecked")
public Limbo() throws IOException, ParseException, NumberFormatException, ClassNotFoundException {
instance = this;
console = new Console(System.in, System.out);
@ -78,46 +65,67 @@ public class Limbo {
}
properties = new ServerProperties(sp);
Map<Integer, Class<? extends Packet>> HandshakeIn = new HashMap<>();
HandshakeIn.put(0x00, PacketHandshakingIn.class);
String mappingName = "mapping.json";
File mappingFile = new File(mappingName);
if (!mappingFile.exists()) {
try (InputStream in = getClass().getClassLoader().getResourceAsStream(mappingName)) {
Files.copy(in, mappingFile.toPath());
} catch (IOException e) {
e.printStackTrace();
}
}
JSONObject json = (JSONObject) new JSONParser().parse(new FileReader(mappingFile));
String classPrefix = Packet.class.getName().substring(0, Packet.class.getName().lastIndexOf(".") + 1);
Map<Integer, Class<? extends PacketIn>> HandshakeIn = new HashMap<>();
for (Object key : ((JSONObject) json.get("HandshakeIn")).keySet()) {
int packetId = Integer.decode((String) key);
HandshakeIn.put(packetId, (Class<? extends PacketIn>) Class.forName(classPrefix + (String) ((JSONObject) json.get("HandshakeIn")).get(key)));
}
Packet.setHandshakeIn(HandshakeIn);
Map<Integer, Class<? extends Packet>> StatusIn = new HashMap<>();
StatusIn.put(0x00, PacketStatusInRequest.class);
StatusIn.put(0x01, PacketStatusInPing.class);
Map<Integer, Class<? extends PacketIn>> StatusIn = new HashMap<>();
for (Object key : ((JSONObject) json.get("StatusIn")).keySet()) {
int packetId = Integer.decode((String) key);
StatusIn.put(packetId, (Class<? extends PacketIn>) Class.forName(classPrefix + (String) ((JSONObject) json.get("StatusIn")).get(key)));
}
Packet.setStatusIn(StatusIn);
Map<Class<? extends Packet>, Integer> StatusOut = new HashMap<>();
StatusOut.put(PacketStatusOutResponse.class, 0x00);
StatusOut.put(PacketStatusOutPong.class, 0x01);
Map<Class<? extends PacketOut>, Integer> StatusOut = new HashMap<>();
for (Object key : ((JSONObject) json.get("StatusOut")).keySet()) {
Class<? extends PacketOut> packetClass = (Class<? extends PacketOut>) Class.forName(classPrefix + (String) key);
StatusOut.put(packetClass, Integer.decode((String) ((JSONObject) json.get("StatusOut")).get(key)));
}
Packet.setStatusOut(StatusOut);
Map<Integer, Class<? extends Packet>> LoginIn = new HashMap<>();
LoginIn.put(0x00, PacketLoginInLoginStart.class);
Map<Integer, Class<? extends PacketIn>> LoginIn = new HashMap<>();
for (Object key : ((JSONObject) json.get("LoginIn")).keySet()) {
int packetId = Integer.decode((String) key);
LoginIn.put(packetId, (Class<? extends PacketIn>) Class.forName(classPrefix + (String) ((JSONObject) json.get("LoginIn")).get(key)));
}
Packet.setLoginIn(LoginIn);
Map<Class<? extends Packet>, Integer> LoginOut = new HashMap<>();
LoginOut.put(PacketLoginOutLoginSuccess.class, 0x02);
Map<Class<? extends PacketOut>, Integer> LoginOut = new HashMap<>();
for (Object key : ((JSONObject) json.get("LoginOut")).keySet()) {
Class<? extends PacketOut> packetClass = (Class<? extends PacketOut>) Class.forName(classPrefix + (String) key);
LoginOut.put(packetClass, Integer.decode((String) ((JSONObject) json.get("LoginOut")).get(key)));
}
Packet.setLoginOut(LoginOut);
Map<Integer, Class<? extends Packet>> PlayIn = new HashMap<>();
PlayIn.put(0x10, PacketPlayInKeepAlive.class);
PlayIn.put(0x12, PacketPlayInPosition.class);
PlayIn.put(0x13, PacketPlayInPositionAndLook.class);
PlayIn.put(0x03, PacketPlayInChat.class);
Map<Integer, Class<? extends PacketIn>> PlayIn = new HashMap<>();
for (Object key : ((JSONObject) json.get("PlayIn")).keySet()) {
int packetId = Integer.decode((String) key);
PlayIn.put(packetId, (Class<? extends PacketIn>) Class.forName(classPrefix + (String) ((JSONObject) json.get("PlayIn")).get(key)));
}
Packet.setPlayIn(PlayIn);
Map<Class<? extends Packet>, Integer> PlayOut = new HashMap<>();
PlayOut.put(PacketPlayOutLogin.class, 0x25);
PlayOut.put(PacketPlayOutSpawnPosition.class, 0x42);
PlayOut.put(PacketPlayOutPositionAndLook.class, 0x35);
PlayOut.put(PacketPlayOutMapChunk.class, 0x21);
PlayOut.put(PacketPlayOutKeepAlive.class, 0x20);
PlayOut.put(PacketPlayOutUpdateViewPosition.class, 0x40);
PlayOut.put(PacketPlayOutPlayerInfo.class, 0x33);
PlayOut.put(PacketPlayOutShowPlayerSkins.class, 0x44);
PlayOut.put(PacketPlayOutPlayerAbilities.class, 0x31);
PlayOut.put(PacketPlayOutChat.class, 0x0E);
Map<Class<? extends PacketOut>, Integer> PlayOut = new HashMap<>();
for (Object key : ((JSONObject) json.get("PlayOut")).keySet()) {
Class<? extends PacketOut> packetClass = (Class<? extends PacketOut>) Class.forName(classPrefix + (String) key);
PlayOut.put(packetClass, Integer.decode((String) ((JSONObject) json.get("PlayOut")).get(key)));
}
Packet.setPlayOut(PlayOut);
worlds.add(loadDefaultWorld());

View File

@ -4,77 +4,71 @@ import java.util.Map;
public class Packet {
private static Map<Integer, Class<? extends Packet>> HandshakeIn;
private static Map<Integer, Class<? extends PacketIn>> HandshakeIn;
private static Map<Integer, Class<? extends Packet>> StatusIn;
private static Map<Class<? extends Packet>, Integer> StatusOut;
private static Map<Integer, Class<? extends PacketIn>> StatusIn;
private static Map<Class<? extends PacketOut>, Integer> StatusOut;
private static Map<Integer, Class<? extends Packet>> LoginIn;
private static Map<Class<? extends Packet>, Integer> LoginOut;
private static Map<Integer, Class<? extends PacketIn>> LoginIn;
private static Map<Class<? extends PacketOut>, Integer> LoginOut;
private static Map<Integer, Class<? extends Packet>> PlayIn;
private static Map<Class<? extends Packet>, Integer> PlayOut;
private static Map<Integer, Class<? extends PacketIn>> PlayIn;
private static Map<Class<? extends PacketOut>, Integer> PlayOut;
public static Map<Integer, Class<? extends Packet>> getHandshakeIn() {
public static Map<Integer, Class<? extends PacketIn>> getHandshakeIn() {
return HandshakeIn;
}
public static void setHandshakeIn(Map<Integer, Class<? extends Packet>> handshakeIn) {
public static void setHandshakeIn(Map<Integer, Class<? extends PacketIn>> handshakeIn) {
HandshakeIn = handshakeIn;
}
public static Map<Integer, Class<? extends Packet>> getStatusIn() {
public static Map<Integer, Class<? extends PacketIn>> getStatusIn() {
return StatusIn;
}
public static void setStatusIn(Map<Integer, Class<? extends Packet>> statusIn) {
public static void setStatusIn(Map<Integer, Class<? extends PacketIn>> statusIn) {
StatusIn = statusIn;
}
public static Map<Class<? extends Packet>, Integer> getStatusOut() {
public static Map<Class<? extends PacketOut>, Integer> getStatusOut() {
return StatusOut;
}
public static void setStatusOut(Map<Class<? extends Packet>, Integer> statusOut) {
public static void setStatusOut(Map<Class<? extends PacketOut>, Integer> statusOut) {
StatusOut = statusOut;
}
public static Map<Integer, Class<? extends Packet>> getLoginIn() {
public static Map<Integer, Class<? extends PacketIn>> getLoginIn() {
return LoginIn;
}
public static void setLoginIn(Map<Integer, Class<? extends Packet>> loginIn) {
public static void setLoginIn(Map<Integer, Class<? extends PacketIn>> loginIn) {
LoginIn = loginIn;
}
public static Map<Class<? extends Packet>, Integer> getLoginOut() {
public static Map<Class<? extends PacketOut>, Integer> getLoginOut() {
return LoginOut;
}
public static void setLoginOut(Map<Class<? extends Packet>, Integer> loginOut) {
public static void setLoginOut(Map<Class<? extends PacketOut>, Integer> loginOut) {
LoginOut = loginOut;
}
public static Map<Integer, Class<? extends Packet>> getPlayIn() {
public static Map<Integer, Class<? extends PacketIn>> getPlayIn() {
return PlayIn;
}
public static void setPlayIn(Map<Integer, Class<? extends Packet>> playIn) {
public static void setPlayIn(Map<Integer, Class<? extends PacketIn>> playIn) {
PlayIn = playIn;
}
public static Map<Class<? extends Packet>, Integer> getPlayOut() {
public static Map<Class<? extends PacketOut>, Integer> getPlayOut() {
return PlayOut;
}
public static void setPlayOut(Map<Class<? extends Packet>, Integer> playOut) {
public static void setPlayOut(Map<Class<? extends PacketOut>, Integer> playOut) {
PlayOut = playOut;
}
//===========================================
public Packet() {
}
}

View File

@ -5,7 +5,7 @@ import java.io.IOException;
import com.loohp.limbo.Utils.DataTypeIO;
public class PacketHandshakingIn extends Packet {
public class PacketHandshakingIn extends PacketIn {
public static enum HandshakeType {
STATUS(1),

View File

@ -0,0 +1,5 @@
package com.loohp.limbo.Server.Packets;
public abstract class PacketIn extends Packet {
}

View File

@ -5,7 +5,7 @@ import java.io.IOException;
import com.loohp.limbo.Utils.DataTypeIO;
public class PacketLoginInLoginStart extends Packet {
public class PacketLoginInLoginStart extends PacketIn {
private String username;

View File

@ -8,7 +8,7 @@ import java.util.UUID;
import com.loohp.limbo.Utils.DataTypeIO;
public class PacketLoginOutLoginSuccess extends Packet {
public class PacketLoginOutLoginSuccess extends PacketOut {
private UUID uuid;
private String username;
@ -26,6 +26,7 @@ public class PacketLoginOutLoginSuccess extends Packet {
return username;
}
@Override
public byte[] getBytes() throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();

View File

@ -0,0 +1,9 @@
package com.loohp.limbo.Server.Packets;
import java.io.IOException;
public abstract class PacketOut extends Packet {
public abstract byte[] getBytes() throws IOException;
}

View File

@ -5,7 +5,7 @@ import java.io.IOException;
import com.loohp.limbo.Utils.DataTypeIO;
public class PacketPlayInChat extends Packet {
public class PacketPlayInChat extends PacketIn {
private String message;

View File

@ -3,7 +3,7 @@ package com.loohp.limbo.Server.Packets;
import java.io.DataInputStream;
import java.io.IOException;
public class PacketPlayInKeepAlive extends Packet {
public class PacketPlayInKeepAlive extends PacketIn {
long payload;

View File

@ -3,7 +3,7 @@ package com.loohp.limbo.Server.Packets;
import java.io.DataInputStream;
import java.io.IOException;
public class PacketPlayInPosition extends Packet {
public class PacketPlayInPosition extends PacketIn {
private double x;
private double y;

View File

@ -3,7 +3,7 @@ package com.loohp.limbo.Server.Packets;
import java.io.DataInputStream;
import java.io.IOException;
public class PacketPlayInPositionAndLook extends Packet {
public class PacketPlayInPositionAndLook extends PacketIn {
private double x;
private double y;

View File

@ -8,7 +8,7 @@ import java.util.UUID;
import com.loohp.limbo.Utils.DataTypeIO;
public class PacketPlayOutChat extends Packet {
public class PacketPlayOutChat extends PacketOut {
private String json;
private int position;
@ -32,6 +32,7 @@ public class PacketPlayOutChat extends Packet {
return sender;
}
@Override
public byte[] getBytes() throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();

View File

@ -4,7 +4,7 @@ import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class PacketPlayOutKeepAlive extends Packet {
public class PacketPlayOutKeepAlive extends PacketOut {
long payload;
@ -16,6 +16,7 @@ public class PacketPlayOutKeepAlive extends Packet {
return payload;
}
@Override
public byte[] getBytes() throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();

View File

@ -10,7 +10,7 @@ import com.loohp.limbo.Utils.GameMode;
import net.querz.nbt.tag.CompoundTag;
public class PacketPlayOutLogin extends Packet {
public class PacketPlayOutLogin extends PacketOut {
private int entityId;
private boolean isHardcore;
@ -103,6 +103,7 @@ public class PacketPlayOutLogin extends Packet {
return isFlat;
}
@Override
public byte[] getBytes() throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();

View File

@ -14,7 +14,7 @@ import net.querz.mca.Section;
import net.querz.nbt.tag.CompoundTag;
import net.querz.nbt.tag.ListTag;
public class PacketPlayOutMapChunk extends Packet {
public class PacketPlayOutMapChunk extends PacketOut {
private int chunkX;
private int chunkZ;
@ -38,6 +38,7 @@ public class PacketPlayOutMapChunk extends Packet {
return chunkZ;
}
@Override
public byte[] getBytes() throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();

View File

@ -4,7 +4,7 @@ import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class PacketPlayOutPlayerAbilities extends Packet {
public class PacketPlayOutPlayerAbilities extends PacketOut {
public enum PlayerAbilityFlags {
INVULNERABLE(0x01),
@ -45,6 +45,7 @@ public class PacketPlayOutPlayerAbilities extends Packet {
return fieldOfField;
}
@Override
public byte[] getBytes() throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();

View File

@ -11,7 +11,7 @@ import com.loohp.limbo.Server.Packets.PacketPlayOutPlayerInfo.PlayerInfoData.Pla
import com.loohp.limbo.Utils.DataTypeIO;
import com.loohp.limbo.Utils.GameMode;
public class PacketPlayOutPlayerInfo extends Packet {
public class PacketPlayOutPlayerInfo extends PacketOut {
public enum PlayerInfoAction {
ADD_PLAYER(0), UPDATE_GAMEMODE(1), UPDATE_LATENCY(2), UPDATE_DISPLAY_NAME(3), REMOVE_PLAYER(4);
@ -49,6 +49,7 @@ public class PacketPlayOutPlayerInfo extends Packet {
return data;
}
@Override
public byte[] getBytes() throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();

View File

@ -9,7 +9,7 @@ import java.util.stream.Collectors;
import com.loohp.limbo.Utils.DataTypeIO;
public class PacketPlayOutPositionAndLook extends Packet {
public class PacketPlayOutPositionAndLook extends PacketOut {
public enum PlayerTeleportFlags {
X((byte) 0x01),
@ -75,6 +75,7 @@ public class PacketPlayOutPositionAndLook extends Packet {
return teleportId;
}
@Override
public byte[] getBytes() throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();

View File

@ -6,7 +6,7 @@ import java.io.IOException;
import com.loohp.limbo.Utils.DataTypeIO;
public class PacketPlayOutShowPlayerSkins extends Packet {
public class PacketPlayOutShowPlayerSkins extends PacketOut {
private int entityId;
@ -18,6 +18,7 @@ public class PacketPlayOutShowPlayerSkins extends Packet {
return entityId;
}
@Override
public byte[] getBytes() throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();

View File

@ -6,7 +6,7 @@ import java.io.IOException;
import com.loohp.limbo.World.BlockPosition;
public class PacketPlayOutSpawnPosition extends Packet {
public class PacketPlayOutSpawnPosition extends PacketOut {
private BlockPosition position;

View File

@ -6,7 +6,7 @@ import java.io.IOException;
import com.loohp.limbo.Utils.DataTypeIO;
public class PacketPlayOutUpdateViewPosition extends Packet {
public class PacketPlayOutUpdateViewPosition extends PacketOut {
private int chunkX;
private int chunkZ;
@ -24,6 +24,7 @@ public class PacketPlayOutUpdateViewPosition extends Packet {
return chunkZ;
}
@Override
public byte[] getBytes() throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();

View File

@ -3,7 +3,7 @@ package com.loohp.limbo.Server.Packets;
import java.io.DataInputStream;
import java.io.IOException;
public class PacketStatusInPing extends Packet {
public class PacketStatusInPing extends PacketIn {
private long payload;

View File

@ -2,7 +2,7 @@ package com.loohp.limbo.Server.Packets;
import java.io.DataInputStream;
public class PacketStatusInRequest extends Packet {
public class PacketStatusInRequest extends PacketIn {
public PacketStatusInRequest() {

View File

@ -4,7 +4,7 @@ import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class PacketStatusOutPong extends Packet {
public class PacketStatusOutPong extends PacketOut {
private long payload;
@ -16,6 +16,7 @@ public class PacketStatusOutPong extends Packet {
return payload;
}
@Override
public byte[] getBytes() throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();

View File

@ -7,7 +7,7 @@ import java.nio.charset.StandardCharsets;
import com.loohp.limbo.Utils.DataTypeIO;
public class PacketStatusOutResponse extends Packet {
public class PacketStatusOutResponse extends PacketOut {
private String json;
@ -19,6 +19,7 @@ public class PacketStatusOutResponse extends Packet {
return json;
}
@Override
public byte[] getBytes() throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();

37
src/mapping.json Normal file
View File

@ -0,0 +1,37 @@
{
"HandshakeIn": {
"0x00": "PacketHandshakingIn"
},
"LoginIn": {
"0x00": "PacketLoginInLoginStart"
},
"LoginOut": {
"PacketLoginOutLoginSuccess": "0x02"
},
"PlayIn": {
"0x10": "PacketPlayInKeepAlive",
"0x03": "PacketPlayInChat",
"0x13": "PacketPlayInPositionAndLook",
"0x12": "PacketPlayInPosition"
},
"PlayOut": {
"PacketPlayOutLogin": "0x25",
"PacketPlayOutPositionAndLook": "0x35",
"PacketPlayOutSpawnPosition": "0x42",
"PacketPlayOutChat": "0x0E",
"PacketPlayOutPlayerAbilities": "0x31",
"PacketPlayOutMapChunk": "0x21",
"PacketPlayOutKeepAlive": "0x20",
"PacketPlayOutPlayerInfo": "0x33",
"PacketPlayOutUpdateViewPosition": "0x40",
"PacketPlayOutShowPlayerSkins": "0x44"
},
"StatusIn": {
"0x01": "PacketStatusInPing",
"0x00": "PacketStatusInRequest"
},
"StatusOut": {
"PacketStatusOutResponse": "0x00",
"PacketStatusOutPong": "0x01"
}
}