Skip to content

Commit

Permalink
Merge branch 'Nan1t_main'
Browse files Browse the repository at this point in the history
# Conflicts:
#	api/src/main/java/ua/nanit/limbo/connection/PacketSnapshots.java
#	api/src/main/java/ua/nanit/limbo/protocol/NbtMessage.java
#	api/src/main/java/ua/nanit/limbo/protocol/packets/play/PacketEmptyChunk.java
#	api/src/main/java/ua/nanit/limbo/protocol/packets/play/PacketGameEvent.java
#	api/src/main/java/ua/nanit/limbo/util/NbtMessageUtil.java
#	build.gradle
  • Loading branch information
bivashy committed Dec 31, 2023
2 parents 1fe7a69 + 72774fb commit f1c4f31
Show file tree
Hide file tree
Showing 19 changed files with 396 additions and 73 deletions.
1 change: 0 additions & 1 deletion .github/FUNDING.yml

This file was deleted.

4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ Symbol `X` means all minor versions.
- [x] 1.17.X
- [x] 1.18.X
- [x] 1.19.X
- [x] 1.20-1.20.2
- [x] 1.20-1.20.4

The server **doesn't** support snapshot versions.

Expand Down Expand Up @@ -190,8 +190,6 @@ Then add your tokens to `tokens` list.
Feel free to create a pull request if you found some bug or optimization opportunity, or if you want
to add some functionality that is suitable for a limbo server and won't significantly load the server.

All PRs should target the `dev` branch to keep the `main` branch stable and clean.

### Building

Required software:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import ua.nanit.limbo.connection.pipeline.PacketEncoder;
import ua.nanit.limbo.protocol.ByteMessage;
import ua.nanit.limbo.protocol.Packet;
import ua.nanit.limbo.protocol.PacketSnapshot;
import ua.nanit.limbo.protocol.packets.login.PacketDisconnect;
import ua.nanit.limbo.protocol.packets.play.PacketKeepAlive;
import ua.nanit.limbo.protocol.registry.State;
Expand Down Expand Up @@ -177,6 +178,14 @@ public void spawnPlayer() {
if (server.getPacketSnapshots().getPacketHeaderAndFooter() != null && clientVersion.moreOrEqual(Version.V1_8))
writePacket(server.getPacketSnapshots().getPacketHeaderAndFooter());

if (clientVersion.moreOrEqual(Version.V1_20_3)) {
writePacket(PacketSnapshots.PACKET_START_WAITING_CHUNKS);

for (PacketSnapshot chunk : PacketSnapshots.PACKETS_EMPTY_CHUNKS) {
writePacket(chunk);
}
}

sendKeepAlive();
};

Expand Down
42 changes: 39 additions & 3 deletions api/src/main/java/ua/nanit/limbo/connection/PacketSnapshots.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import ua.nanit.limbo.protocol.packets.play.*;
import ua.nanit.limbo.server.LimboServer;
import ua.nanit.limbo.server.data.Title;
import ua.nanit.limbo.util.NbtMessageUtil;
import ua.nanit.limbo.util.UuidUtil;
import ua.nanit.limbo.protocol.packets.play.PacketBossBar;
import ua.nanit.limbo.protocol.packets.play.PacketChatMessage;
Expand All @@ -40,7 +41,9 @@
import ua.nanit.limbo.protocol.packets.play.PacketTitleSetTitle;
import ua.nanit.limbo.protocol.packets.play.PacketTitleTimes;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;

Expand Down Expand Up @@ -72,6 +75,9 @@ public final class PacketSnapshots {
private PacketSnapshot packetRegistryData;
private PacketSnapshot packetFinishConfiguration;

private List<PacketSnapshot> packetsEmptyChunks;
private PacketSnapshot packetStartWaitingChunks;

public PacketSnapshots(LimboServer server) {
final String username = server.getConfig().getPingData().getVersion();
final UUID uuid = UuidUtil.getOfflineModeUuid(username);
Expand Down Expand Up @@ -132,8 +138,8 @@ public PacketSnapshots(LimboServer server) {

if (server.getConfig().isUseHeaderAndFooter()) {
PacketPlayerListHeader header = new PacketPlayerListHeader();
header.setHeader(server.getConfig().getPlayerListHeader());
header.setFooter(server.getConfig().getPlayerListFooter());
header.setHeader(NbtMessageUtil.create(server.getConfig().getPlayerListHeader()));
header.setFooter(NbtMessageUtil.create(server.getConfig().getPlayerListFooter()));
packetHeaderAndFooter = PacketSnapshot.of(header);
}

Expand All @@ -146,7 +152,7 @@ public PacketSnapshots(LimboServer server) {

if (server.getConfig().isUseJoinMessage()) {
PacketChatMessage joinMessage = new PacketChatMessage();
joinMessage.setJsonData(server.getConfig().getJoinMessage());
joinMessage.setMessage(NbtMessageUtil.create(server.getConfig().getJoinMessage()));
joinMessage.setPosition(PacketChatMessage.PositionLegacy.SYSTEM_MESSAGE);
joinMessage.setSender(UUID.randomUUID());
packetJoinMessage = PacketSnapshot.of(joinMessage);
Expand Down Expand Up @@ -199,6 +205,28 @@ public PacketSnapshots(LimboServer server) {

this.packetRegistryData = PacketSnapshot.of(packetRegistryData);
packetFinishConfiguration = PacketSnapshot.of(new PacketFinishConfiguration());

PacketGameEvent packetGameEvent = new PacketGameEvent();
packetGameEvent.setType((byte) 13); // Waiting for chunks type
packetGameEvent.setValue(0);
packetStartWaitingChunks = PacketSnapshot.of(packetGameEvent);

int chunkXOffset = (int) 0 >> 4; // Default x position is 0
int chunkZOffset = (int) 0 >> 4; // Default z position is 0
int chunkEdgeSize = 1; // TODO Make configurable?

List<PacketSnapshot> emptyChunks = new ArrayList<>();
// Make multiple chunks for edges
for (int chunkX = chunkXOffset - chunkEdgeSize; chunkX <= chunkXOffset + chunkEdgeSize; ++chunkX) {
for (int chunkZ = chunkZOffset - chunkEdgeSize; chunkZ <= chunkZOffset + chunkEdgeSize; ++chunkZ) {
PacketEmptyChunk packetEmptyChunk = new PacketEmptyChunk();
packetEmptyChunk.setX(chunkX);
packetEmptyChunk.setZ(chunkZ);

emptyChunks.add(PacketSnapshot.of(packetEmptyChunk));
}
}
packetsEmptyChunks = emptyChunks;
}

public PacketSnapshot getPacketLoginSuccess() {
Expand Down Expand Up @@ -281,4 +309,12 @@ public PacketSnapshot getPacketFinishConfiguration() {
return packetFinishConfiguration;
}

public List<PacketSnapshot> getPacketsEmptyChunks() {
return packetsEmptyChunks;
}

public PacketSnapshot getPacketStartWaitingChunks() {
return packetStartWaitingChunks;
}

}
58 changes: 52 additions & 6 deletions api/src/main/java/ua/nanit/limbo/protocol/ByteMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
import io.netty.handler.codec.DecoderException;
import io.netty.handler.codec.EncoderException;
import io.netty.util.ByteProcessor;
import net.kyori.adventure.nbt.BinaryTagIO;
import net.kyori.adventure.nbt.BinaryTagTypes;
import net.kyori.adventure.nbt.CompoundBinaryTag;
import net.kyori.adventure.nbt.*;
import ua.nanit.limbo.protocol.registry.Version;

import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -194,16 +193,63 @@ public void writeCompoundTag(CompoundBinaryTag compoundTag) {
}
}

public void writeNamelessCompoundTag(CompoundBinaryTag compoundTag) {
public void writeNamelessCompoundTag(BinaryTag binaryTag) {
try (ByteBufOutputStream stream = new ByteBufOutputStream(buf)) {
stream.writeByte(10); // CompoundTag ID
BinaryTagTypes.COMPOUND.write(compoundTag, stream);
stream.writeByte(binaryTag.type().id());

// TODO Find a way to improve this...
if (binaryTag instanceof CompoundBinaryTag) {
CompoundBinaryTag tag = (CompoundBinaryTag) binaryTag;
tag.type().write(tag, stream);
}
else if (binaryTag instanceof ByteBinaryTag) {
ByteBinaryTag tag = (ByteBinaryTag) binaryTag;
tag.type().write(tag, stream);
}
else if (binaryTag instanceof ShortBinaryTag) {
ShortBinaryTag tag = (ShortBinaryTag) binaryTag;
tag.type().write(tag, stream);
}
else if (binaryTag instanceof IntBinaryTag) {
IntBinaryTag tag = (IntBinaryTag) binaryTag;
tag.type().write(tag, stream);
}
else if (binaryTag instanceof LongBinaryTag) {
LongBinaryTag tag = (LongBinaryTag) binaryTag;
tag.type().write(tag, stream);
}
else if (binaryTag instanceof DoubleBinaryTag) {
DoubleBinaryTag tag = (DoubleBinaryTag) binaryTag;
tag.type().write(tag, stream);
}
else if (binaryTag instanceof StringBinaryTag) {
StringBinaryTag tag = (StringBinaryTag) binaryTag;
tag.type().write(tag, stream);
}
else if (binaryTag instanceof ListBinaryTag) {
ListBinaryTag tag = (ListBinaryTag) binaryTag;
tag.type().write(tag, stream);
}
else if (binaryTag instanceof EndBinaryTag) {
EndBinaryTag tag = (EndBinaryTag) binaryTag;
tag.type().write(tag, stream);
}

}
catch (IOException e) {
throw new EncoderException("Cannot write NBT CompoundTag");
}
}

public void writeNbtMessage(NbtMessage nbtMessage, Version version) {
if (version.moreOrEqual(Version.V1_20_3)) {
writeNamelessCompoundTag(nbtMessage.getTag());
}
else {
writeString(nbtMessage.getJson());
}
}

public <E extends Enum<E>> void writeEnumSet(EnumSet<E> enumset, Class<E> oclass) {
E[] enums = oclass.getEnumConstants();
BitSet bits = new BitSet(enums.length);
Expand Down
30 changes: 30 additions & 0 deletions api/src/main/java/ua/nanit/limbo/protocol/NbtMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package ua.nanit.limbo.protocol;

import net.kyori.adventure.nbt.BinaryTag;

public class NbtMessage {

private String json;
private BinaryTag tag;

public NbtMessage(String json, BinaryTag tag) {
this.json = json;
this.tag = tag;
}

public String getJson() {
return json;
}

public void setJson(String json) {
this.json = json;
}

public BinaryTag getTag() {
return tag;
}

public void setTag(BinaryTag tag) {
this.tag = tag;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void setFlags(int flags) {
public void encode(ByteMessage msg, Version version) {
msg.writeUuid(uuid);
msg.writeVarInt(0); // Create bossbar
msg.writeString(bossBar.getText());
msg.writeNbtMessage(bossBar.getText(), version);
msg.writeFloat(bossBar.getHealth());
msg.writeVarInt(bossBar.getColor().getIndex());
msg.writeVarInt(bossBar.getDivision().getIndex());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,20 @@
package ua.nanit.limbo.protocol.packets.play;

import ua.nanit.limbo.protocol.ByteMessage;
import ua.nanit.limbo.protocol.NbtMessage;
import ua.nanit.limbo.protocol.PacketOut;
import ua.nanit.limbo.protocol.registry.Version;

import java.util.UUID;

public class PacketChatMessage implements PacketOut {

private String jsonData;
private NbtMessage message;
private PositionLegacy position;
private UUID sender;

public void setJsonData(String jsonData) {
this.jsonData = jsonData;
public void setMessage(NbtMessage message) {
this.message = message;
}

public void setPosition(PositionLegacy position) {
Expand All @@ -43,7 +44,7 @@ public void setSender(UUID sender) {

@Override
public void encode(ByteMessage msg, Version version) {
msg.writeString(jsonData);
msg.writeNbtMessage(message, version);
if (version.moreOrEqual(Version.V1_19_1)) {
msg.writeBoolean(position.index == PositionLegacy.ACTION_BAR.index);
} else if (version.moreOrEqual(Version.V1_19)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package ua.nanit.limbo.protocol.packets.play;

import net.kyori.adventure.nbt.CompoundBinaryTag;
import net.kyori.adventure.nbt.LongArrayBinaryTag;
import ua.nanit.limbo.protocol.ByteMessage;
import ua.nanit.limbo.protocol.PacketOut;
import ua.nanit.limbo.protocol.registry.Version;

public class PacketEmptyChunk implements PacketOut {

private int x;
private int z;

public void setX(int x) {
this.x = x;
}

public void setZ(int z) {
this.z = z;
}

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

LongArrayBinaryTag longArrayTag = LongArrayBinaryTag.longArrayBinaryTag(new long[37]);
CompoundBinaryTag tag = CompoundBinaryTag.builder()
.put("MOTION_BLOCKING", longArrayTag).build();
CompoundBinaryTag rootTag = CompoundBinaryTag.builder()
.put("root", tag).build();
msg.writeNamelessCompoundTag(rootTag);

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.ensureWritable(lightData.length);
msg.writeBytes(lightData, 1, lightData.length - 1);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package ua.nanit.limbo.protocol.packets.play;

import ua.nanit.limbo.protocol.ByteMessage;
import ua.nanit.limbo.protocol.PacketOut;
import ua.nanit.limbo.protocol.registry.Version;

public class PacketGameEvent implements PacketOut {

private byte type;
private float value;

public void setType(byte type) {
this.type = type;
}

public void setValue(float value) {
this.value = value;
}

@Override
public void encode(ByteMessage msg, Version version) {
msg.writeByte(type);
msg.writeFloat(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,26 @@
package ua.nanit.limbo.protocol.packets.play;

import ua.nanit.limbo.protocol.ByteMessage;
import ua.nanit.limbo.protocol.NbtMessage;
import ua.nanit.limbo.protocol.PacketOut;
import ua.nanit.limbo.protocol.registry.Version;

public class PacketPlayerListHeader implements PacketOut {

private String header;
private String footer;
private NbtMessage header;
private NbtMessage footer;

public void setHeader(String header) {
public void setHeader(NbtMessage header) {
this.header = header;
}

public void setFooter(String footer) {
public void setFooter(NbtMessage footer) {
this.footer = footer;
}

@Override
public void encode(ByteMessage msg, Version version) {
msg.writeString(header);
msg.writeString(footer);
msg.writeNbtMessage(header, version);
msg.writeNbtMessage(footer, version);
}
}
Loading

0 comments on commit f1c4f31

Please sign in to comment.