Archived
0

5 Commits

Author SHA1 Message Date
c705a2090d Scoreboard works 2021-05-08 17:58:15 +03:00
6dbf785d22 ScoreboardUpdateScorePacket 2021-05-08 17:01:32 +03:00
050130be60 ScoreboardObjectivePacket 2021-05-08 15:36:10 +03:00
9ccb9a6221 TeamsPacket 2021-05-08 04:48:28 +03:00
fda80d9f9d ScoreboardDisplayPacket 2021-05-06 17:05:20 +03:00
17 changed files with 362 additions and 100 deletions

View File

@@ -54,9 +54,12 @@ public enum State {
),
// client bound
Map.of(
SChatPacket.class, 0x0F,
PingPacket.class, 0x1F,
JoinGamePacket.class, 0x23,
ScoreboardDisplayPacket.class, 0x3B,
ScoreboardObjectivePacket.class, 0x42,
TeamsPacket.class, 0x44,
ScoreboardUpdateScorePacket.class, 0x45,
SpawnPositionPacket.class, 0x46,
ChunkDataPacket.class, 0x20,
PlayerAbilitiesPacket.class,0x2C,

View File

@@ -99,6 +99,11 @@ public class NetByteBuf extends ByteBuf {
}
public void writeString(String string) {
if (string == null) {
writeVarInt(0);
return;
}
byte[] buf = string.getBytes(StandardCharsets.UTF_8);
if (buf.length > Short.MAX_VALUE) {

View File

@@ -25,12 +25,6 @@ public enum TextColor {
WHITE ("white", 'f');
//@formatter:on
public static final char SPECIAL_CHAR = '\u00a7';
private final String name;
private final char code;
public String toLegacy() {
return "" + SPECIAL_CHAR + code;
}
}

View File

@@ -1,20 +0,0 @@
package mc.protocol.model.text;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
public enum TextStyleLegacy {
OBFUSCATED('k'),
BOLD('l'),
STRIKETHOUGH('m'),
UNDERLINE('n'),
ITALIC('o'),
RESET('r');
private final char code;
public String toLegacy() {
return "" + TextColor.SPECIAL_CHAR + code;
}
}

View File

@@ -1,43 +0,0 @@
package mc.protocol.packets.server;
import lombok.Data;
import mc.protocol.io.NetByteBuf;
import mc.protocol.model.text.Text;
import mc.protocol.model.text.TextColor;
import mc.protocol.model.text.TextStyle;
import mc.protocol.packets.ServerSidePacket;
import mc.protocol.serializer.TextSerializer;
import mc.protocol.utils.ChatPosition;
/**
* Chat packet.
*
* <p>Структура пакета</p>
* <pre>
* | FIELD | TYPE | NOTES |
* |-----------|------|-----------------------------------------------|
* | JSON Data | Text | Текст* |
* | Position | Byte | 0 - сообщение чата |
* | | | 1 - системное сообщение (отображается в чате) |
* | | | 2 - над панелью быстрого доступа (hotbar) |
* </pre>
*
* <p>
* * - стоит обратить внимание, что {@link TextColor} и {@link TextStyle} не работают: клиент не применяет
* стилистику в таком виде. Однако метод через символ "§" (\<span>u00a7</span>) работает.
* </p>
*
* @see <a href="https://wiki.vg/index.php?title=Protocol&oldid=14204#Chat_Message_.28clientbound.29">Chat Message (clientbound)</a>
*/
@Data
public class SChatPacket implements ServerSidePacket {
private Text message;
private ChatPosition position;
@Override
public void writeSelf(NetByteBuf netByteBuf) {
netByteBuf.writeString(TextSerializer.toJsonObject(this.message).toString());
netByteBuf.writeByte(this.position.getCode());
}
}

View File

@@ -0,0 +1,39 @@
package mc.protocol.packets.server;
import lombok.Data;
import mc.protocol.io.NetByteBuf;
import mc.protocol.packets.ServerSidePacket;
/**
* Отображение Scoreboard.
*
* <p>Структура пакета</p>
* <pre>
* | FIELD | TYPE | NOTES |
* |------------|-------------|--------------------------------|
* | Position | Byte | Положение: |
* | | | 0 - list |
* | | | 1 - sidebar |
* | | | 2 - below name |
* | | | 3-18 - team specific sidebar |
* | Score Name | String (16) | Уникальное название Scoreboard |
* </pre>
*
* @see <a href="https://wiki.vg/index.php?title=Protocol&oldid=14204#Display_Scoreboard" target="_top">Display Scoreboard</a>
*/
@Data
public class ScoreboardDisplayPacket implements ServerSidePacket {
private int position;
private String scoreName;
public void setPosition(int position) {
this.position = (position < 0) ? 0 : (Math.min(position, 18));
}
@Override
public void writeSelf(NetByteBuf netByteBuf) {
netByteBuf.writeByte(this.position);
netByteBuf.writeString(this.scoreName);
}
}

View File

@@ -0,0 +1,44 @@
package mc.protocol.packets.server;
import lombok.Data;
import mc.protocol.io.NetByteBuf;
import mc.protocol.packets.ServerSidePacket;
import mc.protocol.utils.ScoreboardObjectiveMode;
import mc.protocol.utils.ScoreboardObjectiveType;
/**
* Scoreboard objective packet.
*
* <p>Структура пакета</p>
* <pre>
* | FIELD | TYPE | NOTES |
* |-----------------|-------------|---------------------------------------------------|
* | Objective Name | String (16) | Уникальное наименование цели (objective) |
* | Mode | Byte | 0 - создание Scoreboard |
* | | | 1 - удаление Scoreboard |
* | | | 2 - обновление Scoreboard |
* | Objective Value | String (32) | Если "Mode" равен 0 или 2. Отображаемый текст |
* | Type | String (16) | Если "Mode" равен 0 или 2. "integer" или "hearts" |
* </pre>
*
* @see <a href="https://wiki.vg/index.php?title=Protocol&oldid=14204#Scoreboard_Objective" target="_top">Scoreboard Objective</a>
*/
@Data
public class ScoreboardObjectivePacket implements ServerSidePacket {
private String objectiveName;
private ScoreboardObjectiveMode mode;
private String objectiveValue;
private ScoreboardObjectiveType type;
@Override
public void writeSelf(NetByteBuf netByteBuf) {
netByteBuf.writeString(this.objectiveName);
netByteBuf.writeByte(this.mode.getCode());
if (ScoreboardObjectiveMode.CREATE.equals(this.mode) || ScoreboardObjectiveMode.UPDATE.equals(this.mode)) {
netByteBuf.writeString(this.objectiveValue);
netByteBuf.writeString(this.type.name().toLowerCase());
}
}
}

View File

@@ -0,0 +1,43 @@
package mc.protocol.packets.server;
import lombok.Data;
import mc.protocol.io.NetByteBuf;
import mc.protocol.packets.ServerSidePacket;
import mc.protocol.utils.ScoreboardUpdateScoreAction;
/**
* Update score packet.
*
* <p>Структура пакета</p>
* <pre>
* | FIELD | TYPE | NOTES |
* |-----------------|-------------|--------------------------------------------------- |
* | Entity Name | String (40) | Сущность, которой принадлежит счет (score). |
* | | | Для Игроков - это ник |
* | | | Для других сущностей - это UUID |
* | Action | Byte | 0 - создать или обновить счет (score); 1 - удалить |
* | Objective Name | String (16) | Имя сущности, которой принадлежит счет (score) |
* | Value | VarInt | Если "Action" = 0. Значение счета (score) |
* </pre>
*
* @see <a href="https://wiki.vg/index.php?title=Protocol&oldid=14204#Update_Score" target="_top">Update Score</a>
*/
@Data
public class ScoreboardUpdateScorePacket implements ServerSidePacket {
private String entityName;
private ScoreboardUpdateScoreAction action;
private String objective;
private int value;
@Override
public void writeSelf(NetByteBuf netByteBuf) {
netByteBuf.writeString(this.entityName);
netByteBuf.writeByte(this.action.getCode());
netByteBuf.writeString(this.objective);
if (ScoreboardUpdateScoreAction.CREATE_OR_UPDATE.equals(this.action)) {
netByteBuf.writeVarInt(this.value);
}
}
}

View File

@@ -0,0 +1,119 @@
package mc.protocol.packets.server;
import lombok.Data;
import mc.protocol.io.NetByteBuf;
import mc.protocol.packets.ServerSidePacket;
import mc.protocol.utils.TeamsCollisionRule;
import mc.protocol.utils.TeamsMode;
import mc.protocol.utils.TeamsNameTagVisibility;
import java.util.ArrayList;
import java.util.List;
/**
* Teams packet.
*
* <p>Структура пакета</p>
* <pre>
* | FIELD | TYPE | NOTES |
* |------------ |-------------|-------------------------------------------------------|
* | Team Name | String (16) | Уникальное название команды (совместно со scoreboard) |
* | Mode | Byte | Режим. Определяет остальые поля пакета |
* | Data Fields | - | Определяется "Mode" |
* </pre>
*
* <p>Варианты "Mode"</p>
* <pre>
* | MODE | DATA FIELD | TYPE | NOTES |
* | VALUE | DESCRIPTION | | | |
* |-------|--------------------------|---------------------|----------------------|---------------------------------------------------|
* | 0 | create team | Team Display Name | String (32) | |
* | | | Team Prefix | String (16) | Отображается перед именем игроков текущей команды |
* | | | Team Suffix | String (16) | Отображается после имени игроков текущей команды |
* | | | Friendly Flags | Byte | Битовая маска: |
* | | | | | 0x01 - разрешён friendly fire |
* | | | | | 0x02 - могут видеть невидимок своей команды |
* | | | Name Tag Visibility | String (32) | фиксированные значения: |
* | | | | | - always |
* | | | | | - hideForOtherTeams |
* | | | | | - hideForOwnTeam |
* | | | | | - never |
* | | | Collision Rule | String (32) | фиксированные значения: |
* | | | | | - always |
* | | | | | - pushOtherTeams |
* | | | | | - pushOwnTeam |
* | | | | | - never |
* | | | Color | Byte | For colors, the same Chat colors (0-15). |
* | | | | | -1 indicates RESET/no color. |
* | | | Entity Count | VarInt | Количество элементов в поле "Entities" |
* | | | Entities | Array of String (40) | Уникальные идентификаторы участников команды. |
* | | | | | Для Игроков - это Имена |
* | | | | | Для любых других сущностей - это UUID |
* | 1 | remove team | - | - | удаление текущей команды |
* | 2 | update team info | Team Display Name | String (32) | |
* | | | Team Prefix | String (16) | (см. выше) |
* | | | Team Suffix | String (16) | (см. выше) |
* | | | Friendly Flags | Byte | (см. выше) |
* | | | Name Tag Visibility | String (32) | (см. выше) |
* | | | Collision Rule | String (32) | (см. выше) |
* | | | Color | Byte | (см. выше) |
* | 3 | add players to team | Entity Count | VarInt | (см. выше) |
* | | | Entities | Array of String (40) | (см. выше) |
* | 4 | remove players from team | Entity Count | VarInt | (см. выше) |
* | | | Entities | Array of String (40) | (см. выше) |
* </pre>
*
* @see <a href="https://wiki.vg/index.php?title=Protocol&oldid=14204#Teams" target="_top">Teams</a>
*/
@Data
public class TeamsPacket implements ServerSidePacket {
private final List<String> members = new ArrayList<>();
private String name;
private TeamsMode mode;
private String displayName;
private String prefix;
private String suffix;
private TeamsNameTagVisibility nameTagVisibility;
private TeamsCollisionRule collisionRule;
private int color;
@Override
public void writeSelf(NetByteBuf netByteBuf) {
netByteBuf.writeString(this.name);
netByteBuf.writeByte(this.mode.getCode());
switch (this.mode) {
case CREATE:
netByteBuf.writeString(this.displayName);
netByteBuf.writeString(this.prefix);
netByteBuf.writeString(this.suffix);
netByteBuf.writeByte(0); // Friendly Flags
netByteBuf.writeString(this.nameTagVisibility.getCode());
netByteBuf.writeString(this.collisionRule.getCode());
netByteBuf.writeByte(this.color);
netByteBuf.writeVarInt(this.members.size());
this.members.forEach(netByteBuf::writeString);
break;
case UPDATE:
netByteBuf.writeString(this.displayName);
netByteBuf.writeString(this.prefix);
netByteBuf.writeString(this.suffix);
netByteBuf.writeByte(0); // Friendly Flags
netByteBuf.writeString(this.nameTagVisibility.getCode());
netByteBuf.writeString(this.collisionRule.getCode());
netByteBuf.writeByte(this.color);
break;
case ADD_MEMBER:
case REMOVE_MEMBER:
netByteBuf.writeVarInt(this.members.size());
this.members.forEach(netByteBuf::writeString);
break;
case REMOVE:
default:
break;
}
}
}

View File

@@ -4,11 +4,10 @@ import lombok.Getter;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
public enum ChatPosition {
CHAT(0),
SYSTEM(1),
HOTBAR(2);
public enum ScoreboardObjectiveMode {
CREATE(0),
REMOVE(1),
UPDATE(2);
@Getter
private final int code;

View File

@@ -0,0 +1,6 @@
package mc.protocol.utils;
public enum ScoreboardObjectiveType {
INTEGER,
HEARTS
}

View File

@@ -0,0 +1,10 @@
package mc.protocol.utils;
import lombok.experimental.UtilityClass;
@UtilityClass
public class ScoreboardPosition {
public final int LIST = 0;
public final int SIDEBAR = 1;
public final int BELOW_NAME = 2;
}

View File

@@ -0,0 +1,13 @@
package mc.protocol.utils;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
public enum ScoreboardUpdateScoreAction {
CREATE_OR_UPDATE(0),
REMOVE(1);
@Getter
private final int code;
}

View File

@@ -0,0 +1,15 @@
package mc.protocol.utils;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
public enum TeamsCollisionRule {
ALWAYS("always"),
PUSH_OTHER_TEAMS("pushOtherTeams"),
PUSH_OWN_TEAM("pushOwnTeam"),
NEVER("never");
@Getter
private final String code;
}

View File

@@ -0,0 +1,16 @@
package mc.protocol.utils;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
public enum TeamsMode {
CREATE(0),
REMOVE(1),
UPDATE(2),
ADD_MEMBER(3),
REMOVE_MEMBER(4);
@Getter
private final int code;
}

View File

@@ -0,0 +1,15 @@
package mc.protocol.utils;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
public enum TeamsNameTagVisibility {
ALWAYS("always"),
HIDE_FOR_OTHER_TEAMS("hideForOtherTeams"),
HIDE_FOR_OWN_TEAM("hideForOwnTeam"),
NEVER("never");
@Getter
private final String code;
}

View File

@@ -7,19 +7,13 @@ 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.model.text.TextStyleLegacy;
import mc.protocol.packets.PingPacket;
import mc.protocol.packets.client.HandshakePacket;
import mc.protocol.packets.client.LoginStartPacket;
import mc.protocol.packets.client.StatusServerRequestPacket;
import mc.protocol.packets.server.*;
import mc.protocol.serializer.TextSerializer;
import mc.protocol.utils.ChatPosition;
import mc.protocol.utils.Difficulty;
import mc.protocol.utils.GameMode;
import mc.protocol.utils.LevelType;
import mc.protocol.utils.*;
import mc.server.config.Config;
import org.apache.commons.io.IOUtils;
@@ -72,23 +66,27 @@ public class PacketHandler {
}
public void onLoginStart(ConnectionContext context, LoginStartPacket loginStartPacket) {
UUID playerUuid = UUID.randomUUID();
int playerEid = random.nextInt();
String playerName = loginStartPacket.getName();
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.setEntityId(playerEid);
joinGamePacket.setGameMode(GameMode.SPECTATOR);
joinGamePacket.setDimension(0/*Overworld*/);
joinGamePacket.setDifficulty(Difficulty.PEACEFUL);
joinGamePacket.setLevelType(LevelType.FLAT);
context.send(joinGamePacket);
Location spawnLocation = new Location(7d, 130d, 7d);
Location spawnLocation = new Location(0d, 63d, 0d);
var spawnPositionPacket = new SpawnPositionPacket();
spawnPositionPacket.setSpawn(spawnLocation);
@@ -127,25 +125,31 @@ public class PacketHandler {
context.flushSending();
// -- Эксперименты -- //
// --- Эксперименты --- //
var chatPacket = new SChatPacket();
chatPacket.setMessage(Text.of(TextColor.RED.toLegacy() + "== Hello! =="));
chatPacket.setPosition(ChatPosition.CHAT);
String scoreboardName = "Score::List";
context.send(chatPacket);
var scoreboardObjectivePacket = new ScoreboardObjectivePacket();
scoreboardObjectivePacket.setObjectiveName(scoreboardName);
scoreboardObjectivePacket.setMode(ScoreboardObjectiveMode.CREATE);
scoreboardObjectivePacket.setObjectiveValue(scoreboardName);
scoreboardObjectivePacket.setType(ScoreboardObjectiveType.INTEGER);
var systemChatPacket = new SChatPacket();
systemChatPacket.setMessage(Text.of(TextColor.RED.toLegacy() + "[SYSTEM]"));
systemChatPacket.setPosition(ChatPosition.SYSTEM);
context.send(scoreboardObjectivePacket);
context.send(systemChatPacket);
var scoreboardDisplayPacket = new ScoreboardDisplayPacket();
scoreboardDisplayPacket.setPosition(ScoreboardPosition.SIDEBAR);
scoreboardDisplayPacket.setScoreName(scoreboardName);
var hotbarChatPacket = new SChatPacket();
hotbarChatPacket.setMessage(Text.of(TextColor.RED.toLegacy() + TextStyleLegacy.BOLD.toLegacy() + "In game info"));
hotbarChatPacket.setPosition(ChatPosition.HOTBAR);
context.send(scoreboardDisplayPacket);
context.send(hotbarChatPacket);
var scoreboardUpdateScorePacket = new ScoreboardUpdateScorePacket();
scoreboardUpdateScorePacket.setEntityName(playerName);
scoreboardUpdateScorePacket.setAction(ScoreboardUpdateScoreAction.CREATE_OR_UPDATE);
scoreboardUpdateScorePacket.setObjective(scoreboardName);
scoreboardUpdateScorePacket.setValue(100500);
context.send(scoreboardUpdateScorePacket);
context.flushSending();
}