Skip to content

Commit

Permalink
Support 1.18.2
Browse files Browse the repository at this point in the history
  • Loading branch information
BoomEaro committed Mar 4, 2022
1 parent 9269751 commit 7fb11d2
Show file tree
Hide file tree
Showing 10 changed files with 2,216 additions and 42 deletions.
3 changes: 3 additions & 0 deletions src/main/java/ru/nanit/limbo/connection/ClientConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ public void fireLoginSuccess() {
if (PacketSnapshots.PACKET_HEADER_AND_FOOTER != null)
writePacket(PacketSnapshots.PACKET_HEADER_AND_FOOTER);

if (clientVersion.moreOrEqual(Version.V1_18_2))
writePacket(PacketSnapshots.PACKET_EMPTY_CHUNK);

sendKeepAlive();
}

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/ru/nanit/limbo/connection/PacketSnapshots.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public final class PacketSnapshots {
public static PacketSnapshot PACKET_JOIN_MESSAGE;
public static PacketSnapshot PACKET_BOSS_BAR;
public static PacketSnapshot PACKET_HEADER_AND_FOOTER;
public static PacketSnapshot PACKET_EMPTY_CHUNK;

public static PacketSnapshot PACKET_TITLE_TITLE;
public static PacketSnapshot PACKET_TITLE_SUBTITLE;
Expand All @@ -52,6 +53,7 @@ public final class PacketSnapshots {
public static PacketSnapshot PACKET_TITLE_LEGACY_SUBTITLE;
public static PacketSnapshot PACKET_TITLE_LEGACY_TIMES;


private PacketSnapshots() { }

public static void initPackets(LimboServer server) {
Expand Down Expand Up @@ -108,6 +110,8 @@ public static void initPackets(LimboServer server) {

PACKET_DECLARE_COMMANDS = PacketSnapshot.of(declareCommands);

PACKET_EMPTY_CHUNK = PacketSnapshot.of(new PacketEmptyChunk());

if (server.getConfig().isUseHeaderAndFooter()) {
PacketPlayerListHeader header = new PacketPlayerListHeader();
header.setHeader(server.getConfig().getPlayerListHeader());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package ru.nanit.limbo.protocol.packets.play;

import io.netty.buffer.ByteBufOutputStream;
import ru.nanit.limbo.protocol.ByteMessage;
import ru.nanit.limbo.protocol.PacketOut;
import ru.nanit.limbo.protocol.registry.Version;

import java.io.IOException;

public class PacketEmptyChunk implements PacketOut {

@Override
public void encode(ByteMessage msg, Version version) {
msg.writeInt(0);
msg.writeInt(0);

writeHeightmaps(msg, version.getProtocolNumber());

byte[] sectionData = new byte[]{0, 0, 0, 0, 0, 0, 1, 0};
msg.writeVarInt(sectionData.length * 16);
for (int i = 0; i < 16; i++) {
msg.writeBytes(sectionData);
}

msg.writeVarInt(0);

byte[] lightData = new byte[]{1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 3, -1, -1, 0, 0};
msg.writeBytes(lightData);
}

private void writeHeightmaps(ByteMessage buf, int version) {
try (ByteBufOutputStream output = new ByteBufOutputStream(buf)) {
output.writeByte(10); //CompoundTag
output.writeUTF(""); // CompoundName
output.writeByte(10); //CompoundTag
output.writeUTF("root"); //root compound
output.writeByte(12); //long array
output.writeUTF("MOTION_BLOCKING");
long[] longArrayTag = new long[version < Version.V1_18.getProtocolNumber() ? 36 : 37];
output.writeInt(longArrayTag.length);
for (int i = 0, length = longArrayTag.length; i < length; i++) {
output.writeLong(longArrayTag[i]);
}
buf.writeByte(0); //end of compound
buf.writeByte(0); //end of compound
}
catch (IOException ex) {
throw new RuntimeException(ex);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void encode(ByteMessage msg, Version version) {

if (version.fromTo(Version.V1_8, Version.V1_9_1)) {
msg.writeByte(gameMode);
msg.writeByte(dimensionRegistry.getDefaultDimension().getId());
msg.writeByte(dimensionRegistry.getDefaultDimension_1_16().getId());
msg.writeByte(0); // Difficulty
msg.writeByte(maxPlayers);
msg.writeString("flat"); // Level type
Expand All @@ -110,7 +110,7 @@ public void encode(ByteMessage msg, Version version) {

if (version.fromTo(Version.V1_9_2, Version.V1_13_2)) {
msg.writeByte(gameMode);
msg.writeInt(dimensionRegistry.getDefaultDimension().getId());
msg.writeInt(dimensionRegistry.getDefaultDimension_1_16().getId());
msg.writeByte(0); // Difficulty
msg.writeByte(maxPlayers);
msg.writeString("flat"); // Level type
Expand All @@ -119,7 +119,7 @@ public void encode(ByteMessage msg, Version version) {

if (version.fromTo(Version.V1_14, Version.V1_14_4)) {
msg.writeByte(gameMode);
msg.writeInt(dimensionRegistry.getDefaultDimension().getId());
msg.writeInt(dimensionRegistry.getDefaultDimension_1_16().getId());
msg.writeByte(maxPlayers);
msg.writeString("flat"); // Level type
msg.writeVarInt(viewDistance);
Expand All @@ -128,7 +128,7 @@ public void encode(ByteMessage msg, Version version) {

if (version.fromTo(Version.V1_15, Version.V1_15_2)) {
msg.writeByte(gameMode);
msg.writeInt(dimensionRegistry.getDefaultDimension().getId());
msg.writeInt(dimensionRegistry.getDefaultDimension_1_16().getId());
msg.writeLong(hashedSeed);
msg.writeByte(maxPlayers);
msg.writeString("flat"); // Level type
Expand All @@ -142,7 +142,7 @@ public void encode(ByteMessage msg, Version version) {
msg.writeByte(previousGameMode);
msg.writeStringsArray(worldNames);
msg.writeCompoundTag(dimensionRegistry.getOldCodec());
msg.writeString(dimensionRegistry.getDefaultDimension().getName());
msg.writeString(dimensionRegistry.getDefaultDimension_1_16().getName());
msg.writeString(worldName);
msg.writeLong(hashedSeed);
msg.writeByte(maxPlayers);
Expand All @@ -158,8 +158,8 @@ public void encode(ByteMessage msg, Version version) {
msg.writeByte(gameMode);
msg.writeByte(previousGameMode);
msg.writeStringsArray(worldNames);
msg.writeCompoundTag(dimensionRegistry.getCodec());
msg.writeCompoundTag(dimensionRegistry.getDefaultDimension().getData());
msg.writeCompoundTag(dimensionRegistry.getCodec_1_16());
msg.writeCompoundTag(dimensionRegistry.getDefaultDimension_1_16().getData());
msg.writeString(worldName);
msg.writeLong(hashedSeed);
msg.writeVarInt(maxPlayers);
Expand All @@ -175,8 +175,14 @@ public void encode(ByteMessage msg, Version version) {
msg.writeByte(gameMode);
msg.writeByte(previousGameMode);
msg.writeStringsArray(worldNames);
msg.writeCompoundTag(dimensionRegistry.getCodec());
msg.writeCompoundTag(dimensionRegistry.getDefaultDimension().getData());
if (version.moreOrEqual(Version.V1_18_2)) {
msg.writeCompoundTag(dimensionRegistry.getCodec_1_18_2());
msg.writeCompoundTag(dimensionRegistry.getDefaultDimension_1_18_2().getData());
}
else {
msg.writeCompoundTag(dimensionRegistry.getCodec_1_16());
msg.writeCompoundTag(dimensionRegistry.getDefaultDimension_1_16().getData());
}
msg.writeString(worldName);
msg.writeLong(hashedSeed);
msg.writeVarInt(maxPlayers);
Expand Down
31 changes: 17 additions & 14 deletions src/main/java/ru/nanit/limbo/protocol/registry/State.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ public enum State {
map(0x0E, V1_13, V1_13_2),
map(0x0F, V1_14, V1_15_2),
map(0x10, V1_16, V1_16_4),
map(0x0F, V1_17, V1_18)
map(0x0F, V1_17, V1_18_2)
);

clientBound.register(PacketDeclareCommands::new,
map(0x11, V1_13, V1_14_4),
map(0x12, V1_15, V1_15_2),
map(0x11, V1_16, V1_16_1),
map(0x10, V1_16_2, V1_16_4),
map(0x12, V1_17, V1_18)
map(0x12, V1_17, V1_18_2)
);
clientBound.register(PacketJoinGame::new,
map(0x01, V1_8, V1_8),
Expand All @@ -101,15 +101,15 @@ public enum State {
map(0x26, V1_15, V1_15_2),
map(0x25, V1_16, V1_16_1),
map(0x24, V1_16_2, V1_16_4),
map(0x26, V1_17, V1_18)
map(0x26, V1_17, V1_18_2)
);
clientBound.register(PacketPluginMessage::new,
map(0x19, V1_13, V1_13_2),
map(0x18, V1_14, V1_14_4),
map(0x19, V1_15, V1_15_2),
map(0x18, V1_16, V1_16_1),
map(0x17, V1_16_2, V1_16_4),
map(0x18, V1_17, V1_18)
map(0x18, V1_17, V1_18_2)
);
clientBound.register(PacketPlayerAbilities::new,
map(0x39, V1_8, V1_8),
Expand All @@ -120,7 +120,7 @@ public enum State {
map(0x32, V1_15, V1_15_2),
map(0x31, V1_16, V1_16_1),
map(0x30, V1_16_2, V1_16_4),
map(0x32, V1_17, V1_18)
map(0x32, V1_17, V1_18_2)
);
clientBound.register(PacketPlayerPositionAndLook::new,
map(0x08, V1_8, V1_8),
Expand All @@ -131,7 +131,7 @@ public enum State {
map(0x36, V1_15, V1_15_2),
map(0x35, V1_16, V1_16_1),
map(0x34, V1_16_2, V1_16_4),
map(0x38, V1_17, V1_18)
map(0x38, V1_17, V1_18_2)
);
clientBound.register(PacketKeepAlive::new,
map(0x00, V1_8, V1_8),
Expand All @@ -141,21 +141,21 @@ public enum State {
map(0x21, V1_15, V1_15_2),
map(0x20, V1_16, V1_16_1),
map(0x1F, V1_16_2, V1_16_4),
map(0x21, V1_17, V1_18)
map(0x21, V1_17, V1_18_2)
);
clientBound.register(PacketChatMessage::new,
map(0x02, V1_8, V1_8),
map(0x0F, V1_9, V1_12_2),
map(0x0E, V1_13, V1_14_4),
map(0x0F, V1_15, V1_15_2),
map(0x0E, V1_16, V1_16_4),
map(0x0F, V1_17, V1_18)
map(0x0F, V1_17, V1_18_2)
);
clientBound.register(PacketBossBar::new,
map(0x0C, V1_9, V1_14_4),
map(0x0D, V1_15, V1_15_2),
map(0x0C, V1_16, V1_16_4),
map(0x0D, V1_17, V1_18)
map(0x0D, V1_17, V1_18_2)
);
clientBound.register(PacketPlayerInfo::new,
map(0x38, V1_8, V1_8),
Expand All @@ -166,7 +166,7 @@ public enum State {
map(0x34, V1_15, V1_15_2),
map(0x33, V1_16, V1_16_1),
map(0x32, V1_16_2, V1_16_4),
map(0x36, V1_17, V1_18)
map(0x36, V1_17, V1_18_2)
);
clientBound.register(PacketTitleLegacy::new,
map(0x45, V1_8, V1_11_1),
Expand All @@ -179,15 +179,15 @@ public enum State {
);
clientBound.register(PacketTitleSetTitle::new,
map(0x59, V1_17, V1_17_1),
map(0x5A, V1_18, V1_18)
map(0x5A, V1_18, V1_18_2)
);
clientBound.register(PacketTitleSetSubTitle::new,
map(0x57, V1_17, V1_17_1),
map(0x58, V1_18, V1_18)
map(0x58, V1_18, V1_18_2)
);
clientBound.register(PacketTitleTimes::new,
map(0x5A, V1_17, V1_17_1),
map(0x5B, V1_18, V1_18)
map(0x5B, V1_18, V1_18_2)
);
clientBound.register(PacketPlayerListHeader::new,
map(0x47, V1_8, V1_8),
Expand All @@ -200,7 +200,10 @@ public enum State {
map(0x54, V1_15, V1_15_2),
map(0x53, V1_16, V1_16_4),
map(0x5E, V1_17, V1_17_1),
map(0x5F, V1_18, V1_18)
map(0x5F, V1_18, V1_18_2)
);
clientBound.register(PacketEmptyChunk::new,
map(0x22, V1_18, V1_18_2)
);
}
};
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/ru/nanit/limbo/protocol/registry/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ public enum Version {
// 1.16.5 has same protocol number
V1_17(755),
V1_17_1(756),
V1_18(757);
V1_18(757),
// 1.18.1 has same protocol number
V1_18_2(758);

private static final Map<Integer, Version> VERSION_MAP;
private static final Version MAX;
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/ru/nanit/limbo/util/Colors.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
public final class Colors {

private static final char CHAR_FROM = '&';
private static final char CHAR_TO = '§';
private static final char CHAR_TO = '\u00a7';

private Colors() {}
private Colors() {
}

public static String of(String text) {
if (text == null) return null;
Expand Down
44 changes: 28 additions & 16 deletions src/main/java/ru/nanit/limbo/world/dimension/DimensionRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,52 +31,64 @@ public final class DimensionRegistry {

private final LimboServer server;

private Dimension defaultDimension;
private Dimension defaultDimension_1_16;
private Dimension defaultDimension_1_18_2;

private CompoundBinaryTag codec;
private CompoundBinaryTag codec_1_16;
private CompoundBinaryTag codec_1_18_2;
private CompoundBinaryTag oldCodec;

public DimensionRegistry(LimboServer server) {
this.server = server;
}

public CompoundBinaryTag getCodec() {
return codec;
public CompoundBinaryTag getCodec_1_16() {
return codec_1_16;
}

public CompoundBinaryTag getCodec_1_18_2() {
return codec_1_18_2;
}

public CompoundBinaryTag getOldCodec() {
return oldCodec;
}

public Dimension getDefaultDimension() {
return defaultDimension;
public Dimension getDefaultDimension_1_16() {
return defaultDimension_1_16;
}

public Dimension getDefaultDimension_1_18_2() {
return defaultDimension_1_18_2;
}

public void load(String def) throws IOException {
codec = readCodecFile("/dimension/codec_new.snbt");
codec_1_16 = readCodecFile("/dimension/codec_1_16.snbt");
codec_1_18_2 = readCodecFile("/dimension/codec_1_18_2.snbt");
// On 1.16-1.16.1 different codec format
oldCodec = readCodecFile("/dimension/codec_old.snbt");

ListBinaryTag dimensions = codec.getCompound("minecraft:dimension_type").getList("value");
defaultDimension_1_16 = getDefaultDimension(def, codec_1_16);
defaultDimension_1_18_2 = getDefaultDimension(def, codec_1_18_2);
}

private Dimension getDefaultDimension(String def, CompoundBinaryTag tag) {
ListBinaryTag dimensions = tag.getCompound("minecraft:dimension_type").getList("value");

CompoundBinaryTag overWorld = (CompoundBinaryTag) ((CompoundBinaryTag) dimensions.get(0)).get("element");
CompoundBinaryTag nether = (CompoundBinaryTag) ((CompoundBinaryTag) dimensions.get(2)).get("element");
CompoundBinaryTag theEnd = (CompoundBinaryTag) ((CompoundBinaryTag) dimensions.get(3)).get("element");

switch (def.toLowerCase()) {
case "overworld":
defaultDimension = new Dimension(0, "minecraft:overworld", overWorld);
break;
return new Dimension(0, "minecraft:overworld", overWorld);
case "nether":
defaultDimension = new Dimension(-1, "minecraft:nether", nether);
break;
return new Dimension(-1, "minecraft:nether", nether);
case "the_end":
defaultDimension = new Dimension(1, "minecraft:the_end", theEnd);
break;
return new Dimension(1, "minecraft:the_end", theEnd);
default:
defaultDimension = new Dimension(1, "minecraft:the_end", theEnd);
Logger.warning("Undefined dimension type: '%s'. Using THE_END as default", def);
break;
return new Dimension(1, "minecraft:the_end", theEnd);
}
}

Expand Down
File renamed without changes.
Loading

0 comments on commit 7fb11d2

Please sign in to comment.