Clarify / simplify packet logic

This commit is contained in:
William 2023-02-25 12:52:51 +00:00
parent ad99b1655c
commit 39b196b647
No known key found for this signature in database

View File

@ -46,42 +46,34 @@ public class UpdateTeamsPacket extends AbstractPacket {
@NotNull @NotNull
public static UpdateTeamsPacket create(@NotNull String teamName, @NotNull String member) { public static UpdateTeamsPacket create(@NotNull String teamName, @NotNull String member) {
if (teamName.length() > 16) { return new UpdateTeamsPacket()
teamName = teamName.substring(0, 16); .teamName(teamName.length() > 16 ? teamName.substring(0, 16) : teamName)
} .mode(UpdateMode.CREATE_TEAM)
final UpdateTeamsPacket updateTeamsPacket = new UpdateTeamsPacket(); .displayName(getChatString(teamName))
updateTeamsPacket.teamName(teamName); .friendlyFlags(List.of(FriendlyFlag.CAN_HURT_FRIENDLY))
updateTeamsPacket.mode(UpdateMode.CREATE); .nameTagVisibility(NameTagVisibility.ALWAYS)
updateTeamsPacket.displayName(getChatString(teamName)); .collisionRule(CollisionRule.ALWAYS)
updateTeamsPacket.friendlyFlags(List.of(FriendlyFlag.CAN_HURT_FRIENDLY)); .color(15)
updateTeamsPacket.nameTagVisibility(NameTagVisibility.ALWAYS); .prefix(getChatString(""))
updateTeamsPacket.collisionRule(CollisionRule.ALWAYS); .suffix(getChatString(""))
updateTeamsPacket.color(15); .entities(List.of(member));
updateTeamsPacket.prefix(getChatString(""));
updateTeamsPacket.suffix(getChatString(""));
updateTeamsPacket.entities(List.of(member));
return updateTeamsPacket;
} }
@NotNull @NotNull
public static UpdateTeamsPacket remove(@NotNull String teamName) { public static UpdateTeamsPacket remove(@NotNull String teamName) {
if (teamName.length() > 16) { return new UpdateTeamsPacket()
teamName = teamName.substring(0, 16); .teamName(teamName.length() > 16 ? teamName.substring(0, 16) : teamName)
} .mode(UpdateMode.REMOVE_TEAM);
final UpdateTeamsPacket updateTeamsPacket = new UpdateTeamsPacket();
updateTeamsPacket.teamName(teamName);
updateTeamsPacket.mode(UpdateMode.REMOVE);
return updateTeamsPacket;
} }
@Override @Override
public void read(ByteBuf byteBuf, PacketDirection packetDirection, int i) { public void read(ByteBuf byteBuf, PacketDirection packetDirection, int i) {
teamName = ProtocolUtil.readString(byteBuf); teamName = ProtocolUtil.readString(byteBuf);
mode = UpdateMode.byId(byteBuf.readByte()); mode = UpdateMode.byId(byteBuf.readByte());
if (mode == UpdateMode.REMOVE) { if (mode == UpdateMode.REMOVE_TEAM) {
return; return;
} }
if (mode == UpdateMode.CREATE || mode == UpdateMode.UPDATE_INFO) { if (mode == UpdateMode.CREATE_TEAM || mode == UpdateMode.UPDATE_INFO) {
displayName = ProtocolUtil.readString(byteBuf); displayName = ProtocolUtil.readString(byteBuf);
friendlyFlags = FriendlyFlag.fromBitMask(byteBuf.readByte()); friendlyFlags = FriendlyFlag.fromBitMask(byteBuf.readByte());
nameTagVisibility = NameTagVisibility.byId(ProtocolUtil.readString(byteBuf)); nameTagVisibility = NameTagVisibility.byId(ProtocolUtil.readString(byteBuf));
@ -90,7 +82,7 @@ public class UpdateTeamsPacket extends AbstractPacket {
prefix = ProtocolUtil.readString(byteBuf); prefix = ProtocolUtil.readString(byteBuf);
suffix = ProtocolUtil.readString(byteBuf); suffix = ProtocolUtil.readString(byteBuf);
} }
if (mode == UpdateMode.CREATE || mode == UpdateMode.ADD_PLAYERS || mode == UpdateMode.REMOVE_PLAYERS) { if (mode == UpdateMode.CREATE_TEAM || mode == UpdateMode.ADD_PLAYERS || mode == UpdateMode.REMOVE_PLAYERS) {
int entityCount = ProtocolUtil.readVarInt(byteBuf); int entityCount = ProtocolUtil.readVarInt(byteBuf);
entities = new ArrayList<>(entityCount); entities = new ArrayList<>(entityCount);
for (int j = 0; j < entityCount; j++) { for (int j = 0; j < entityCount; j++) {
@ -103,10 +95,10 @@ public class UpdateTeamsPacket extends AbstractPacket {
public void write(ByteBuf byteBuf, PacketDirection packetDirection, int i) { public void write(ByteBuf byteBuf, PacketDirection packetDirection, int i) {
ProtocolUtil.writeString(byteBuf, teamName); ProtocolUtil.writeString(byteBuf, teamName);
byteBuf.writeByte(mode.id()); byteBuf.writeByte(mode.id());
if (mode == UpdateMode.REMOVE) { if (mode == UpdateMode.REMOVE_TEAM) {
return; return;
} }
if (mode == UpdateMode.CREATE || mode == UpdateMode.UPDATE_INFO) { if (mode == UpdateMode.CREATE_TEAM || mode == UpdateMode.UPDATE_INFO) {
ProtocolUtil.writeString(byteBuf, displayName); ProtocolUtil.writeString(byteBuf, displayName);
byteBuf.writeByte(FriendlyFlag.toBitMask(friendlyFlags)); byteBuf.writeByte(FriendlyFlag.toBitMask(friendlyFlags));
ProtocolUtil.writeString(byteBuf, nameTagVisibility.id()); ProtocolUtil.writeString(byteBuf, nameTagVisibility.id());
@ -115,7 +107,7 @@ public class UpdateTeamsPacket extends AbstractPacket {
ProtocolUtil.writeString(byteBuf, prefix); ProtocolUtil.writeString(byteBuf, prefix);
ProtocolUtil.writeString(byteBuf, suffix); ProtocolUtil.writeString(byteBuf, suffix);
} }
if (mode == UpdateMode.CREATE || mode == UpdateMode.ADD_PLAYERS || mode == UpdateMode.REMOVE_PLAYERS) { if (mode == UpdateMode.CREATE_TEAM || mode == UpdateMode.ADD_PLAYERS || mode == UpdateMode.REMOVE_PLAYERS) {
ProtocolUtil.writeVarInt(byteBuf, entities.size()); ProtocolUtil.writeVarInt(byteBuf, entities.size());
for (String entity : entities) { for (String entity : entities) {
ProtocolUtil.writeString(byteBuf, entity); ProtocolUtil.writeString(byteBuf, entity);
@ -129,8 +121,8 @@ public class UpdateTeamsPacket extends AbstractPacket {
} }
public enum UpdateMode { public enum UpdateMode {
CREATE((byte) 0), CREATE_TEAM((byte) 0),
REMOVE((byte) 1), REMOVE_TEAM((byte) 1),
UPDATE_INFO((byte) 2), UPDATE_INFO((byte) 2),
ADD_PLAYERS((byte) 3), ADD_PLAYERS((byte) 3),
REMOVE_PLAYERS((byte) 4); REMOVE_PLAYERS((byte) 4);