2020-04-24 03:25:58 +02:00
|
|
|
package net.minestom.server.utils;
|
2019-08-03 15:25:24 +02:00
|
|
|
|
2020-04-17 01:16:02 +02:00
|
|
|
import io.netty.buffer.ByteBuf;
|
2020-04-24 03:25:58 +02:00
|
|
|
import net.minestom.server.chat.Chat;
|
2020-04-26 19:17:04 +02:00
|
|
|
import net.minestom.server.instance.Chunk;
|
2020-04-29 20:17:04 +02:00
|
|
|
import net.minestom.server.item.Enchantment;
|
2020-04-24 03:25:58 +02:00
|
|
|
import net.minestom.server.item.ItemStack;
|
|
|
|
import net.minestom.server.network.packet.PacketReader;
|
|
|
|
import net.minestom.server.network.packet.PacketWriter;
|
|
|
|
import net.minestom.server.utils.buffer.BufferWrapper;
|
2019-08-03 15:25:24 +02:00
|
|
|
|
2020-02-13 15:14:41 +01:00
|
|
|
import java.util.ArrayList;
|
2020-04-29 20:17:04 +02:00
|
|
|
import java.util.Map;
|
2019-08-03 15:25:24 +02:00
|
|
|
|
|
|
|
public class Utils {
|
|
|
|
|
2020-04-17 01:16:02 +02:00
|
|
|
public static void writeVarIntBuf(ByteBuf buffer, int value) {
|
|
|
|
do {
|
|
|
|
byte temp = (byte) (value & 0b01111111);
|
|
|
|
value >>>= 7;
|
|
|
|
if (value != 0) {
|
|
|
|
temp |= 0b10000000;
|
|
|
|
}
|
|
|
|
buffer.writeByte(temp);
|
|
|
|
} while (value != 0);
|
2020-02-13 15:14:41 +01:00
|
|
|
}
|
|
|
|
|
2019-09-06 16:05:36 +02:00
|
|
|
public static void writeVarIntBuffer(BufferWrapper buffer, int value) {
|
2019-08-03 15:25:24 +02:00
|
|
|
do {
|
|
|
|
byte temp = (byte) (value & 0b01111111);
|
|
|
|
value >>>= 7;
|
|
|
|
if (value != 0) {
|
|
|
|
temp |= 0b10000000;
|
|
|
|
}
|
|
|
|
buffer.putByte(temp);
|
|
|
|
} while (value != 0);
|
|
|
|
}
|
|
|
|
|
2020-04-17 01:16:02 +02:00
|
|
|
public static void writeVarInt(PacketWriter writer, int value) {
|
2019-09-02 06:02:12 +02:00
|
|
|
do {
|
|
|
|
byte temp = (byte) (value & 0b01111111);
|
|
|
|
value >>>= 7;
|
|
|
|
if (value != 0) {
|
|
|
|
temp |= 0b10000000;
|
|
|
|
}
|
2020-04-17 01:16:02 +02:00
|
|
|
writer.writeByte(temp);
|
2019-09-02 06:02:12 +02:00
|
|
|
} while (value != 0);
|
|
|
|
}
|
|
|
|
|
2019-08-03 15:25:24 +02:00
|
|
|
public static int lengthVarInt(int value) {
|
|
|
|
int i = 0;
|
|
|
|
do {
|
|
|
|
i++;
|
|
|
|
byte temp = (byte) (value & 0b01111111);
|
|
|
|
value >>>= 7;
|
|
|
|
if (value != 0) {
|
|
|
|
temp |= 0b10000000;
|
|
|
|
}
|
2019-08-07 03:42:48 +02:00
|
|
|
} while (value != 0);
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2020-04-17 01:16:02 +02:00
|
|
|
public static int readVarInt(ByteBuf buffer) {
|
|
|
|
int numRead = 0;
|
|
|
|
int result = 0;
|
|
|
|
byte read;
|
|
|
|
do {
|
|
|
|
read = buffer.readByte();
|
|
|
|
int value = (read & 0b01111111);
|
|
|
|
result |= (value << (7 * numRead));
|
|
|
|
|
|
|
|
numRead++;
|
|
|
|
if (numRead > 5) {
|
|
|
|
throw new RuntimeException("VarInt is too big");
|
|
|
|
}
|
|
|
|
} while ((read & 0b10000000) != 0);
|
|
|
|
|
|
|
|
return result;
|
2019-08-03 15:25:24 +02:00
|
|
|
}
|
|
|
|
|
2020-04-17 01:16:02 +02:00
|
|
|
public static void writeItemStack(PacketWriter packet, ItemStack itemStack) {
|
2020-02-13 15:14:41 +01:00
|
|
|
if (itemStack == null || itemStack.isAir()) {
|
2020-04-17 01:16:02 +02:00
|
|
|
packet.writeBoolean(false);
|
2019-08-13 17:52:09 +02:00
|
|
|
} else {
|
2020-04-17 01:16:02 +02:00
|
|
|
packet.writeBoolean(true);
|
2020-03-29 20:58:30 +02:00
|
|
|
Utils.writeVarInt(packet, itemStack.getMaterialId());
|
2020-04-17 01:16:02 +02:00
|
|
|
packet.writeByte(itemStack.getAmount());
|
2019-08-22 14:52:32 +02:00
|
|
|
|
2020-02-13 15:14:41 +01:00
|
|
|
if (!itemStack.hasNbtTag()) {
|
2020-04-17 01:16:02 +02:00
|
|
|
packet.writeByte((byte) 0x00); // No nbt
|
2020-02-13 15:14:41 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-17 01:16:02 +02:00
|
|
|
packet.writeByte((byte) 0x0A); // Compound
|
|
|
|
packet.writeShort((short) 0); // Empty compound name
|
2019-08-22 14:52:32 +02:00
|
|
|
|
|
|
|
// Unbreakable
|
|
|
|
if (itemStack.isUnbreakable()) {
|
2020-04-17 01:16:02 +02:00
|
|
|
packet.writeByte((byte) 0x03); // Integer
|
|
|
|
packet.writeShortSizedString("Unbreakable");
|
|
|
|
packet.writeInt(1);
|
2019-08-22 14:52:32 +02:00
|
|
|
}
|
|
|
|
|
2019-09-06 16:05:36 +02:00
|
|
|
// Damage
|
2020-04-17 01:16:02 +02:00
|
|
|
packet.writeByte((byte) 0x02);
|
|
|
|
packet.writeShortSizedString("Damage");
|
|
|
|
packet.writeShort(itemStack.getDamage());
|
2019-09-06 16:05:36 +02:00
|
|
|
|
2019-08-22 14:52:32 +02:00
|
|
|
// Display
|
2020-02-13 15:14:41 +01:00
|
|
|
boolean hasDisplayName = itemStack.hasDisplayName();
|
|
|
|
boolean hasLore = itemStack.hasLore();
|
2019-08-22 14:52:32 +02:00
|
|
|
|
2020-02-13 15:14:41 +01:00
|
|
|
if (hasDisplayName || hasLore) {
|
2020-04-17 01:16:02 +02:00
|
|
|
packet.writeByte((byte) 0x0A); // Start display compound
|
|
|
|
packet.writeShortSizedString("display");
|
2020-02-13 15:14:41 +01:00
|
|
|
|
|
|
|
if (hasDisplayName) {
|
2020-04-17 01:16:02 +02:00
|
|
|
packet.writeByte((byte) 0x08);
|
|
|
|
packet.writeShortSizedString("Name");
|
|
|
|
packet.writeShortSizedString(Chat.legacyTextString(itemStack.getDisplayName()));
|
2020-02-13 15:14:41 +01:00
|
|
|
}
|
2019-08-22 14:52:32 +02:00
|
|
|
|
2020-02-13 15:14:41 +01:00
|
|
|
if (hasLore) {
|
|
|
|
ArrayList<String> lore = itemStack.getLore();
|
2019-08-22 14:52:32 +02:00
|
|
|
|
2020-04-17 01:16:02 +02:00
|
|
|
packet.writeByte((byte) 0x09);
|
|
|
|
packet.writeShortSizedString("Lore");
|
|
|
|
packet.writeByte((byte) 0x08);
|
|
|
|
packet.writeInt(lore.size());
|
2020-02-13 15:14:41 +01:00
|
|
|
for (String line : lore) {
|
2020-04-17 01:16:02 +02:00
|
|
|
packet.writeShortSizedString(Chat.legacyTextString(line));
|
2020-02-13 15:14:41 +01:00
|
|
|
}
|
|
|
|
}
|
2019-08-22 14:52:32 +02:00
|
|
|
|
2020-04-17 01:16:02 +02:00
|
|
|
packet.writeByte((byte) 0); // End display compound
|
2020-02-13 15:14:41 +01:00
|
|
|
}
|
|
|
|
// End display
|
2019-09-06 16:05:36 +02:00
|
|
|
|
2020-04-29 20:17:04 +02:00
|
|
|
// FIXME: Enchantment
|
|
|
|
{
|
|
|
|
Map<Enchantment, Integer> enchantmentMap = itemStack.getEnchantmentMap();
|
|
|
|
if (!enchantmentMap.isEmpty()) {
|
|
|
|
packet.writeByte((byte) 0x09); // list
|
|
|
|
packet.writeShortSizedString("StoredEnchantments");
|
|
|
|
|
|
|
|
packet.writeByte((byte) 0x0A); // Compound
|
|
|
|
packet.writeInt(enchantmentMap.size()); // Map size
|
|
|
|
|
|
|
|
for (Map.Entry<Enchantment, Integer> entry : enchantmentMap.entrySet()) {
|
|
|
|
Enchantment enchantment = entry.getKey();
|
|
|
|
int level = entry.getValue();
|
|
|
|
|
|
|
|
packet.writeByte((byte) 0x02);
|
|
|
|
packet.writeShortSizedString("lvl");
|
|
|
|
packet.writeShort((short) level);
|
|
|
|
|
|
|
|
packet.writeByte((byte) 0x08);
|
|
|
|
packet.writeShortSizedString("id");
|
|
|
|
packet.writeShort((short) enchantment.getId());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//packet.writeByte((byte) 0); // End enchantment compound
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// End enchantment
|
|
|
|
|
2020-04-17 01:16:02 +02:00
|
|
|
packet.writeByte((byte) 0); // End nbt
|
2019-08-13 17:52:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-17 01:16:02 +02:00
|
|
|
public static ItemStack readItemStack(PacketReader reader) {
|
|
|
|
boolean present = reader.readBoolean();
|
2020-02-16 19:11:36 +01:00
|
|
|
|
2020-04-17 01:16:02 +02:00
|
|
|
if (!present) {
|
2020-04-23 12:30:49 +02:00
|
|
|
return ItemStack.getAirItem();
|
2020-04-17 01:16:02 +02:00
|
|
|
}
|
2020-02-16 19:11:36 +01:00
|
|
|
|
2020-04-17 01:16:02 +02:00
|
|
|
int id = reader.readVarInt();
|
|
|
|
if (id == -1) {
|
|
|
|
// Drop mode
|
2020-04-23 12:30:49 +02:00
|
|
|
return ItemStack.getAirItem();
|
2020-04-17 01:16:02 +02:00
|
|
|
}
|
2020-02-13 15:14:41 +01:00
|
|
|
|
2020-04-17 01:16:02 +02:00
|
|
|
byte count = reader.readByte();
|
2020-02-09 15:34:09 +01:00
|
|
|
|
2020-04-17 01:16:02 +02:00
|
|
|
ItemStack item = new ItemStack((short) id, count);
|
2020-02-09 15:34:09 +01:00
|
|
|
|
2020-04-17 01:16:02 +02:00
|
|
|
byte nbt = reader.readByte(); // Should be compound start (0x0A) or 0 if there isn't NBT data
|
2020-02-09 15:34:09 +01:00
|
|
|
|
2020-04-17 01:16:02 +02:00
|
|
|
if (nbt == 0x00) {
|
|
|
|
return item;
|
|
|
|
} else if (nbt == 0x0A) {
|
|
|
|
short compoundName = reader.readShort(); // Ignored, should be empty (main compound name)
|
|
|
|
NbtReaderUtils.readItemStackNBT(reader, item);
|
|
|
|
}
|
2020-02-09 15:34:09 +01:00
|
|
|
|
2020-04-17 01:16:02 +02:00
|
|
|
return item;
|
2020-02-09 15:34:09 +01:00
|
|
|
}
|
|
|
|
|
2019-09-06 16:05:36 +02:00
|
|
|
public static void writeBlocks(BufferWrapper buffer, short[] blocksId, int bitsPerEntry) {
|
2019-08-21 16:50:52 +02:00
|
|
|
short count = 0;
|
|
|
|
for (short id : blocksId)
|
|
|
|
if (id != 0)
|
|
|
|
count++;
|
|
|
|
|
|
|
|
buffer.putShort(count);
|
2019-08-10 04:16:01 +02:00
|
|
|
buffer.putByte((byte) bitsPerEntry);
|
2020-04-26 19:17:04 +02:00
|
|
|
int[] blocksData = new int[Chunk.CHUNK_SIZE_X * Chunk.CHUNK_SECTION_SIZE * Chunk.CHUNK_SIZE_Z];
|
|
|
|
for (int y = 0; y < Chunk.CHUNK_SECTION_SIZE; y++) {
|
|
|
|
for (int x = 0; x < Chunk.CHUNK_SIZE_X; x++) {
|
|
|
|
for (int z = 0; z < Chunk.CHUNK_SIZE_Z; z++) {
|
2019-08-10 04:16:01 +02:00
|
|
|
int sectionIndex = (((y * 16) + x) * 16) + z;
|
|
|
|
int index = y << 8 | z << 4 | x;
|
2019-08-18 20:38:09 +02:00
|
|
|
blocksData[index] = blocksId[sectionIndex];
|
2019-08-10 04:16:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-06 16:05:36 +02:00
|
|
|
long[] data = encodeBlocks(blocksData, bitsPerEntry);
|
|
|
|
buffer.putVarInt(data.length);
|
2019-08-10 04:16:01 +02:00
|
|
|
for (int i = 0; i < data.length; i++) {
|
|
|
|
buffer.putLong(data[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static long[] encodeBlocks(int[] blocks, int bitsPerEntry) {
|
|
|
|
long maxEntryValue = (1L << bitsPerEntry) - 1;
|
|
|
|
|
|
|
|
int length = (int) Math.ceil(blocks.length * bitsPerEntry / 64.0);
|
|
|
|
long[] data = new long[length];
|
|
|
|
|
|
|
|
for (int index = 0; index < blocks.length; index++) {
|
|
|
|
int value = blocks[index];
|
|
|
|
int bitIndex = index * bitsPerEntry;
|
|
|
|
int startIndex = bitIndex / 64;
|
|
|
|
int endIndex = ((index + 1) * bitsPerEntry - 1) / 64;
|
|
|
|
int startBitSubIndex = bitIndex % 64;
|
|
|
|
data[startIndex] = data[startIndex] & ~(maxEntryValue << startBitSubIndex) | ((long) value & maxEntryValue) << startBitSubIndex;
|
|
|
|
if (startIndex != endIndex) {
|
|
|
|
int endBitSubIndex = 64 - startBitSubIndex;
|
|
|
|
data[endIndex] = data[endIndex] >>> endBitSubIndex << endBitSubIndex | ((long) value & maxEntryValue) >> endBitSubIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2019-08-03 15:25:24 +02:00
|
|
|
}
|