Skip to content

Commit

Permalink
Fix AvailableCommandsPacket encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
SupremeMortal committed Jun 13, 2023
1 parent e03e147 commit 5817d36
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
public class AvailableCommandsSerializer_v291 implements BedrockPacketSerializer<AvailableCommandsPacket> {

protected static final InternalLogger log = InternalLoggerFactory.getInstance(AvailableCommandsSerializer_v291.class);
protected static final CommandPermission[] PERMISSIONS = CommandPermission.values();

protected static final int ARG_FLAG_VALID = 0x100000;
protected static final int ARG_FLAG_ENUM = 0x200000;
Expand Down Expand Up @@ -112,9 +113,9 @@ protected void writeEnums(ByteBuf buffer, BedrockCodecHelper helper, List<String
// Determine width of enum index
ObjIntConsumer<ByteBuf> indexWriter;
int valuesSize = values.size();
if (valuesSize < 0x100) {
if (valuesSize <= 0x100) {
indexWriter = WRITE_BYTE;
} else if (valuesSize < 0x10000) {
} else if (valuesSize <= 0x10000) {
indexWriter = WRITE_SHORT;
} else {
indexWriter = WRITE_INT;
Expand All @@ -137,9 +138,9 @@ protected void readEnums(ByteBuf buffer, BedrockCodecHelper helper, List<String>
// Determine width of enum index
ToIntFunction<ByteBuf> indexReader;
int valuesSize = values.size();
if (valuesSize < 0x100) {
if (valuesSize <= 0x100) {
indexReader = READ_BYTE;
} else if (valuesSize < 0x10000) {
} else if (valuesSize <= 0x10000) {
indexReader = READ_SHORT;
} else {
indexReader = READ_INT;
Expand All @@ -163,10 +164,11 @@ protected void writeCommand(ByteBuf buffer, BedrockCodecHelper helper, CommandDa
helper.writeString(buffer, commandData.getName());
helper.writeString(buffer, commandData.getDescription());
this.writeFlags(buffer, commandData.getFlags());
buffer.writeByte(commandData.getPermission());
CommandPermission permission = commandData.getPermission() == null ? CommandPermission.ANY : commandData.getPermission();
buffer.writeByte(permission.ordinal());

CommandEnumData aliases = commandData.getAliases();
buffer.writeIntLE(enums.indexOf(aliases));
buffer.writeIntLE(aliases == null ? -1 : enums.indexOf(aliases));

CommandParamData[][] overloads = commandData.getOverloads();
VarInts.writeUnsignedInt(buffer, overloads.length);
Expand All @@ -183,8 +185,9 @@ protected CommandData readCommand(ByteBuf buffer, BedrockCodecHelper helper, Lis
String name = helper.readString(buffer);
String description = helper.readString(buffer);
Set<CommandData.Flag> flags = this.readFlags(buffer);
byte permissions = buffer.readByte();
CommandEnumData aliases = enums.get(buffer.readIntLE());
CommandPermission permissions = PERMISSIONS[buffer.readUnsignedByte()];
int aliasIndex = buffer.readIntLE();
CommandEnumData aliases = aliasIndex == -1 ? null : enums.get(aliasIndex);

CommandParamData[][] overloads = new CommandParamData[VarInts.readUnsignedInt(buffer)][];
for (int i = 0; i < overloads.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void serialize(ByteBuf buffer, BedrockCodecHelper helper, AvailableComman
enumValues.add(key);
if (!constraints.isEmpty()) {
int valueIndex = enumValues.indexOf(key);
enumConstraints.add(LongObjectPair.of(LongKeys.key(enumIndex, valueIndex), constraints));
enumConstraints.add(LongObjectPair.of(LongKeys.key(valueIndex, enumIndex), constraints));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class CommandData {
private final String name;
private final String description;
private final Set<Flag> flags;
private final byte permission;
private final CommandPermission permission;
private final CommandEnumData aliases;
private final CommandParamData[][] overloads;

Expand Down

0 comments on commit 5817d36

Please sign in to comment.