use of cn.nukkit.utils.BinaryStream in project Nukkit by Nukkit.
the class CraftingDataPacket method encode.
@Override
public void encode() {
this.reset();
this.putUnsignedVarInt(entries.size());
BinaryStream writer = new BinaryStream();
for (Object entry : entries) {
int entryType = writeEntry(entry, writer);
if (entryType >= 0) {
this.putVarInt(entryType);
this.put(writer.getBuffer());
} else {
this.putVarInt(-1);
}
writer.reset();
}
this.putBoolean(cleanRecipes);
}
use of cn.nukkit.utils.BinaryStream in project Nukkit by Nukkit.
the class AvailableCommandsPacket method encode.
@Override
public void encode() {
this.reset();
BinaryStream commandsStream = new BinaryStream();
this.commands.forEach((name, versions) -> {
if (name.equals("help"))
return;
ArrayList<String> aliases = new ArrayList<>();
aliases.addAll(Arrays.asList(versions.versions.get(0).aliases));
aliases.add(name);
for (String alias : aliases) {
commandsStream.putString(alias);
commandsStream.putString(versions.versions.get(0).description);
commandsStream.putByte((byte) 0);
commandsStream.putByte((byte) 0);
commandsStream.putLInt(-1);
commandsStream.putUnsignedVarInt(versions.versions.get(0).overloads.size());
for (CommandOverload overload : versions.versions.get(0).overloads.values()) {
commandsStream.putUnsignedVarInt(overload.input.parameters.length);
for (CommandParameter parameter : overload.input.parameters) {
commandsStream.putString(parameter.name);
commandsStream.putLInt(ARG_FLAG_VALID | getFlag(parameter.type));
commandsStream.putBoolean(parameter.optional);
}
}
}
aliasCommands += (aliases.size() - 1);
});
this.putUnsignedVarInt(0);
this.putUnsignedVarInt(0);
this.putUnsignedVarInt(0);
this.putUnsignedVarInt(this.commands.size() + aliasCommands);
this.put(commandsStream.getBuffer());
}
use of cn.nukkit.utils.BinaryStream in project Nukkit by Nukkit.
the class CraftingManager method getMultiItemHash.
private String getMultiItemHash(Collection<Item> items) {
BinaryStream stream = new BinaryStream();
for (Item item : items) {
stream.putUnsignedVarInt(item.getId());
stream.putVarInt(item.getDamage());
}
return new String(stream.getByteArray());
}
use of cn.nukkit.utils.BinaryStream in project Nukkit by Nukkit.
the class Session method handleSplit.
private void handleSplit(EncapsulatedPacket packet) throws Exception {
if (packet.splitCount >= MAX_SPLIT_SIZE || packet.splitIndex >= MAX_SPLIT_SIZE || packet.splitIndex < 0) {
return;
}
if (!this.splitPackets.containsKey(packet.splitID)) {
if (this.splitPackets.size() >= MAX_SPLIT_COUNT) {
return;
}
this.splitPackets.put(packet.splitID, new HashMap<Integer, EncapsulatedPacket>() {
{
put(packet.splitIndex, packet);
}
});
} else {
this.splitPackets.get(packet.splitID).put(packet.splitIndex, packet);
}
if (this.splitPackets.get(packet.splitID).size() == packet.splitCount) {
EncapsulatedPacket pk = new EncapsulatedPacket();
BinaryStream stream = new BinaryStream();
for (int i = 0; i < packet.splitCount; i++) {
stream.put(this.splitPackets.get(packet.splitID).get(i).buffer);
}
pk.buffer = stream.getBuffer();
pk.length = pk.buffer.length;
this.splitPackets.remove(packet.splitID);
this.handleEncapsulatedPacketRoute(pk);
}
}
use of cn.nukkit.utils.BinaryStream in project Nukkit by Nukkit.
the class AcknowledgePacket method encode.
@Override
public void encode() {
super.encode();
int count = this.packets.size();
int[] packets = new int[count];
int index = 0;
for (int i : this.packets.values()) {
packets[index++] = i;
}
short records = 0;
BinaryStream payload = new BinaryStream();
if (count > 0) {
int pointer = 1;
int start = packets[0];
int last = packets[0];
while (pointer < count) {
int current = packets[pointer++];
int diff = current - last;
if (diff == 1) {
last = current;
} else if (diff > 1) {
if (start == last) {
payload.putByte((byte) 0x01);
payload.put(Binary.writeLTriad(start));
start = last = current;
} else {
payload.putByte((byte) 0x00);
payload.put(Binary.writeLTriad(start));
payload.put(Binary.writeLTriad(last));
start = last = current;
}
++records;
}
}
if (start == last) {
payload.putByte((byte) 0x01);
payload.put(Binary.writeLTriad(start));
} else {
payload.putByte((byte) 0x00);
payload.put(Binary.writeLTriad(start));
payload.put(Binary.writeLTriad(last));
}
++records;
}
this.putShort(records);
this.buffer = Binary.appendBytes(this.buffer, payload.getBuffer());
}
Aggregations