Archived
0

2 Commits

Author SHA1 Message Date
47ccc3252a PlayerListHeaderAndFooterPacket 2021-05-09 15:36:19 +03:00
0d72a8a29c PlayerListItemPacket 2021-05-09 15:17:44 +03:00
8 changed files with 269 additions and 129 deletions

View File

@@ -51,18 +51,18 @@ public enum State {
0x0D, PlayerPositionPacket.class,
0x0E, CPlayerPositionAndLookPacket.class,
0x0F, PlayerLookPacket.class,
0x15, EntityActionPacket.class,
0x1C, UpdateSignPacket.class
0x15, EntityActionPacket.class
),
// client bound
Map.of(
PingPacket.class, 0x1F,
ChunkDataPacket.class, 0x20,
JoinGamePacket.class, 0x23,
OpenSignEditorPacket.class, 0x2A,
PlayerAbilitiesPacket.class,0x2C,
PlayerListItemPacket.class,0x2E,
SPlayerPositionAndLookPacket.class, 0x2F,
SpawnPositionPacket.class, 0x46
SpawnPositionPacket.class, 0x46,
PlayerListHeaderAndFooterPacket.class,0x4A
)
);

View File

@@ -1,57 +0,0 @@
package mc.protocol.packets.client;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;
import mc.protocol.io.NetByteBuf;
import mc.protocol.model.Location;
import mc.protocol.packets.ClientSidePacket;
import mc.protocol.packets.server.OpenSignEditorPacket;
import mc.protocol.serializer.LocationSerializer;
/**
* Update Sign packet.
*
* <p>Отправляется клиентом, когда нажата кнопка "Done" в окне редактирования таблички.</p>
*
* <p>Структура пакета</p>
* <pre>
* | FIELD | TYPE | NOTES |
* |----------|--------------|--------------------------------|
* | Location | Location | Позиция редактируемой табилчки |
* | Line 1 | String (384) | Строка таблички |
* | Line 2 | String (384) | Строка таблички |
* | Line 3 | String (384) | Строка таблички |
* | Line 4 | String (384) | Строка таблички |
* </pre>
*
* @see <a href="https://wiki.vg/index.php?title=Protocol&oldid=14204#Update_Sign" target="_top">Update Sign</a>
* @see OpenSignEditorPacket
*/
@NoArgsConstructor
@Getter
@EqualsAndHashCode
@ToString
public class UpdateSignPacket implements ClientSidePacket {
private Location location;
@SuppressWarnings("MismatchedReadAndWriteOfArray")
private String[] lines;
@Override
public void readSelf(NetByteBuf netByteBuf) {
this.location = LocationSerializer.fromLongValue(netByteBuf.readLong());
this.lines = new String[4];
this.lines[0] = netByteBuf.readString(384);
this.lines[1] = netByteBuf.readString(384);
this.lines[2] = netByteBuf.readString(384);
this.lines[3] = netByteBuf.readString(384);
}
@Override
public void passivate() {
this.location = null;
this.lines = null;
}
}

View File

@@ -1,33 +0,0 @@
package mc.protocol.packets.server;
import lombok.Data;
import mc.protocol.io.NetByteBuf;
import mc.protocol.model.Location;
import mc.protocol.packets.ServerSidePacket;
import mc.protocol.packets.client.UpdateSignPacket;
import mc.protocol.serializer.LocationSerializer;
/**
* Открыть окно редактирования таблички.
*
* <p>Структура пакета</p>
* <pre>
* | FIELD | TYPE | NOTES |
* |----------|----------|-------|
* | Location | Location | |
* </pre>
*
* @see <a href="https://wiki.vg/index.php?title=Protocol&oldid=14204#Open_Sign_Editor">Open Sign Editor</a>
* @see UpdateSignPacket
*/
@Data
public class OpenSignEditorPacket implements ServerSidePacket {
private Location location;
@Override
public void writeSelf(NetByteBuf netByteBuf) {
long value = LocationSerializer.toLongValue(this.location);
netByteBuf.writeLong(value);
}
}

View File

@@ -0,0 +1,50 @@
package mc.protocol.packets.server;
import lombok.Data;
import mc.protocol.io.NetByteBuf;
import mc.protocol.model.text.Text;
import mc.protocol.packets.ServerSidePacket;
import mc.protocol.serializer.TextSerializer;
/**
* Установка текста для "шапки" и "подвала" Tab-листа.
*
* <p>Структура пакета</p>
* <pre>
* | FIELD | TYPE | NOTES |
* |--------|------|-------|
* | Header | Text | |
* | Footer | Text | |
* </pre>
*
* <p>Для удаления "шапки" и/или "подвала", нужно отправить следующий {@link Text} компонент:</p>
* <pre>
* {"translate":""}
* </pre>
*
* @see <a href="https://wiki.vg/index.php?title=Protocol&oldid=14204#Player_List_Header_And_Footer">Player List Header And Footer</a>
* @see PlayerListItemPacket
*/
@Data
public class PlayerListHeaderAndFooterPacket implements ServerSidePacket {
private static final String REMOVE_COMPONENT = "{\"translate\":\"\"}";
private Text header;
private Text foother;
@Override
public void writeSelf(NetByteBuf netByteBuf) {
if (this.header == null) {
netByteBuf.writeString(REMOVE_COMPONENT);
} else {
netByteBuf.writeString(TextSerializer.toJsonObject(this.header).toString());
}
if (this.foother == null) {
netByteBuf.writeString(REMOVE_COMPONENT);
} else {
netByteBuf.writeString(TextSerializer.toJsonObject(this.foother).toString());
}
}
}

View File

@@ -0,0 +1,172 @@
package mc.protocol.packets.server;
import lombok.*;
import mc.protocol.io.NetByteBuf;
import mc.protocol.packets.ServerSidePacket;
import mc.protocol.utils.GameMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.function.BiConsumer;
/**
* Player List Item packet.
*
* <p>Обновление списка игроков, по кнопке TAB.</p>
*
* <p>Структура пакета</p>
* <pre>
* | FIELD | TYPE | NOTES |
* |-------------------|-------------|-------------------------------------------------|
* | Action | VarInt | Определяет поля для секции "Player" |
* | Number of players | VarInt | Количество элементов в секции "Players" |
* | Players | Array | Перечисление полей для каждого игрока (player). |
* | | | Поля определяются по "Action" |
* </pre>
*
* <p>Варианты "Action"</p>
* <pre>
* | ACTION | PLAYER FIELD | TYPE | NOTES |
* | VALUE | DESCRIPTION | | | |
* |-------|---------------------|----------------------|-------------|----------------------------------------------------|
* | 0 | add player | UUID | UUID | |
* | | | Name | String (16) | |
* | | | Number Of Properties | VarInt | Количество элементов "Properties" |
* | | | Properties | - | см. поля ниже |
* | | | Gamemode | VarInt | |
* | | | Ping | VarInt | в милисекундах |
* | | | Has Display Name | Boolean | |
* | | | Display Name | Text | Опционально. Только если "Has Display Name" = true |
* | 1 | update gamemode | UUID | UUID | |
* | | | Gamemode | VarInt | |
* | 2 | update latency | UUID | UUID | |
* | | | Ping | VarInt | (см. выше) |
* | 3 | update display name | UUID | UUID | |
* | | | Has Display Name | Boolean | |
* | | | Display Name | Text | (см. выше) |
* | 4 | remove player | UUID | UUID | |
* </pre>
*
* <p>Поля секции "Properties"</p>
* <pre>
* | FIELD | TYPE | NOTES |
* |-----------|------------- |---------------------------------------------|
* | Name | String (32767) | |
* | Value | String (32767) | |
* | Is Signed | Boolean | |
* | Signature | String (32767) | Опционально. Только если "Is Signed" = true |
* </pre>
*
* @see <a href="https://wiki.vg/index.php?title=Protocol&oldid=14204#Player_List_Item">Player List Item</a>
* @see PlayerListHeaderAndFooterPacket
*/
@Data
public class PlayerListItemPacket implements ServerSidePacket {
private final List<PlayerItem> playerItems = new ArrayList<>();
private Action action;
@Override
public void writeSelf(NetByteBuf netByteBuf) {
netByteBuf.writeVarInt(this.action.getCode());
netByteBuf.writeVarInt(playerItems.size());
BiConsumer<NetByteBuf, PlayerItem> consumer = null;
switch (this.action) {
//@formatter:off
case ADD_PLAYER: consumer = this::addPlayer; break;
case UPDATE_GAMEMODE: consumer = this::updateGamemode; break;
case UPDATE_LATENCY: consumer = this::updateLatency; break;
case UPDATE_DISPLAY_NAME: consumer = this::updateDisplayName; break;
case REMOVE_PLAYER: consumer = this::removePlayer; break;
//@formatter:on
}
Objects.requireNonNull(consumer);
for (PlayerItem playerItem : playerItems) {
consumer.accept(netByteBuf, playerItem);
}
}
private void addPlayer(NetByteBuf netByteBuf, PlayerItem playerItem) {
netByteBuf.writeUUID(playerItem.getUuid());
netByteBuf.writeString(playerItem.getName());
netByteBuf.writeVarInt(playerItem.getProperties().size());
for (PlayerProperties property : playerItem.getProperties()) {
netByteBuf.writeString(property.getName());
netByteBuf.writeString(property.getValue());
netByteBuf.writeBoolean(property.getIsSigned());
if (property.getIsSigned()) {
netByteBuf.writeString(property.getSignature());
}
}
netByteBuf.writeVarInt(playerItem.getGamemode().getId());
netByteBuf.writeVarInt(playerItem.getPing());
netByteBuf.writeBoolean(playerItem.getHasDisplayName());
if (playerItem.getHasDisplayName()) {
netByteBuf.writeString(playerItem.getDisplayName());
}
}
private void updateGamemode(NetByteBuf netByteBuf, PlayerItem playerItem) {
netByteBuf.writeUUID(playerItem.getUuid());
netByteBuf.writeVarInt(playerItem.getGamemode().getId());
}
private void updateLatency(NetByteBuf netByteBuf, PlayerItem playerItem) {
netByteBuf.writeUUID(playerItem.getUuid());
netByteBuf.writeVarInt(playerItem.getPing());
}
private void updateDisplayName(NetByteBuf netByteBuf, PlayerItem playerItem) {
netByteBuf.writeUUID(playerItem.getUuid());
netByteBuf.writeBoolean(playerItem.getHasDisplayName());
if (playerItem.getHasDisplayName()) {
netByteBuf.writeString(playerItem.getDisplayName());
}
}
private void removePlayer(NetByteBuf netByteBuf, PlayerItem playerItem) {
netByteBuf.writeUUID(playerItem.getUuid());
}
@RequiredArgsConstructor
public enum Action {
ADD_PLAYER(0),
UPDATE_GAMEMODE(1),
UPDATE_LATENCY(2),
UPDATE_DISPLAY_NAME(3),
REMOVE_PLAYER(4);
@Getter
private final int code;
}
@Builder(builderClassName = "Builder")
@Value
public static class PlayerItem {
List<PlayerProperties> properties = new ArrayList<>();
UUID uuid;
String name;
GameMode gamemode;
Integer ping;
Boolean hasDisplayName;
String displayName;
}
@Builder(builderClassName = "Builder")
@Value
public static class PlayerProperties {
String name;
String value;
Boolean isSigned;
String signature;
}
}

View File

@@ -4,7 +4,6 @@ import lombok.Data;
import mc.protocol.io.NetByteBuf;
import mc.protocol.model.Location;
import mc.protocol.packets.ServerSidePacket;
import mc.protocol.serializer.LocationSerializer;
/**
* Спавн позиция игрока.
@@ -15,7 +14,7 @@ import mc.protocol.serializer.LocationSerializer;
* <pre>
* | FIELD | TYPE | NOTES |
* |----------|----------|-----------------------|
* | Location | Location | Локация спавна игрока |
* | Location | Position | Локация спавна игрока |
* </pre>
*
* @see <a href="https://wiki.vg/index.php?title=Protocol&oldid=14204#Spawn_Position">Spawn Position</a>
@@ -27,7 +26,16 @@ public class SpawnPositionPacket implements ServerSidePacket {
@Override
public void writeSelf(NetByteBuf netByteBuf) {
long spawnSerialized = LocationSerializer.toLongValue(this.spawn);
long spawnSerialized =
((long) (floorDouble(spawn.getX()) & 0x3FFFFFF) << 38)
| ((long) (floorDouble(spawn.getY()) & 0xFFF) << 26)
| (floorDouble(spawn.getZ()) & 0x3FFFFFF);
netByteBuf.writeLong(spawnSerialized);
}
private static int floorDouble(double value) {
int i = (int) value;
return value < (double) i ? i - 1 : i;
}
}

View File

@@ -1,26 +0,0 @@
package mc.protocol.serializer;
import lombok.experimental.UtilityClass;
import mc.protocol.model.Location;
@UtilityClass
public class LocationSerializer {
public long toLongValue(Location location) {
return ((long) (floorDouble(location.getX()) & 0x3FFFFFF) << 38)
| ((long) (floorDouble(location.getY()) & 0xFFF) << 26)
| (floorDouble(location.getZ()) & 0x3FFFFFF);
}
public Location fromLongValue(long value) {
return new Location(
value >> 38,
(value >> 26) & 0xFFF,
value << 38 >> 38);
}
private static int floorDouble(double value) {
int i = (int) value;
return value < (double) i ? i - 1 : i;
}
}

View File

@@ -7,6 +7,8 @@ import mc.protocol.api.ConnectionContext;
import mc.protocol.model.Location;
import mc.protocol.model.Look;
import mc.protocol.model.ServerInfo;
import mc.protocol.model.text.Text;
import mc.protocol.model.text.TextColor;
import mc.protocol.packets.PingPacket;
import mc.protocol.packets.client.HandshakePacket;
import mc.protocol.packets.client.LoginStartPacket;
@@ -68,16 +70,20 @@ public class PacketHandler {
}
public void onLoginStart(ConnectionContext context, LoginStartPacket loginStartPacket) {
var playerUuid = UUID.randomUUID();
var playerName = loginStartPacket.getName();
var playerGamemode = GameMode.SURVIVAL;
var loginSuccessPacket = new LoginSuccessPacket();
loginSuccessPacket.setUuid(UUID.randomUUID());
loginSuccessPacket.setName(loginStartPacket.getName());
loginSuccessPacket.setUuid(playerUuid);
loginSuccessPacket.setName(playerName);
context.sendNow(loginSuccessPacket);
context.setState(State.PLAY);
var joinGamePacket = new JoinGamePacket();
joinGamePacket.setEntityId(random.nextInt());
joinGamePacket.setGameMode(GameMode.SURVIVAL);
joinGamePacket.setGameMode(playerGamemode);
joinGamePacket.setDimension(0/*Overworld*/);
joinGamePacket.setDifficulty(Difficulty.PEACEFUL);
joinGamePacket.setLevelType(LevelType.FLAT);
@@ -125,10 +131,30 @@ public class PacketHandler {
// -- Эксперименты -- //
var openSignEditorPacket = new OpenSignEditorPacket();
openSignEditorPacket.setLocation(spawnLocation);
var playerListItemPacket = new PlayerListItemPacket();
playerListItemPacket.setAction(PlayerListItemPacket.Action.ADD_PLAYER);
playerListItemPacket.getPlayerItems().add(PlayerListItemPacket.PlayerItem.builder()
.uuid(playerUuid)
.name(playerName)
.gamemode(playerGamemode)
.ping(100)
.hasDisplayName(false)
.build());
context.sendNow(openSignEditorPacket);
context.send(playerListItemPacket);
var playerListHeaderAndFooterPacket = new PlayerListHeaderAndFooterPacket();
playerListHeaderAndFooterPacket.setHeader(Text.builder()
.append("") //TODO bug component
.append(Text.of(TextColor.GREEN, "==="))
.append(Text.of(TextColor.RED, " MC-PROJECT "))
.append(Text.of(TextColor.GREEN, "==="))
.build());
playerListHeaderAndFooterPacket.setFoother(Text.of(TextColor.GRAY, "develop by DmitriyMX"));
context.send(playerListHeaderAndFooterPacket);
context.flushSending();
}
private static String faviconToBase64(Path iconPath) {