Fixed item attributes not showing in-game

This commit is contained in:
themode 2020-10-04 03:04:51 +02:00
parent 39a9e5531d
commit 683415e75e
4 changed files with 44 additions and 15 deletions

View File

@ -141,7 +141,7 @@ public class DynamicChunk extends Chunk {
@Override
public byte[] getSerializedData() {
// Used for blocks data
// Used for blocks data (unused if empty at the end)
Object2ShortMap<String> typeToIndexMap = new Object2ShortOpenHashMap<>();
BinaryWriter binaryWriter = new BinaryWriter();
@ -196,9 +196,9 @@ public class DynamicChunk extends Chunk {
// If the chunk data contains SerializableData type, it needs to be added in the header
BinaryWriter indexWriter = new BinaryWriter();
final boolean hasIndex = !typeToIndexMap.isEmpty();
indexWriter.writeBoolean(hasIndex);
if (hasIndex) {
final boolean hasDataIndex = !typeToIndexMap.isEmpty();
indexWriter.writeBoolean(hasDataIndex);
if (hasDataIndex) {
// Get the index buffer (prefixed by true to say that the chunk contains data indexes)
SerializableData.writeDataIndexHeader(indexWriter, typeToIndexMap);
}
@ -218,8 +218,8 @@ public class DynamicChunk extends Chunk {
try {
// Get if the chunk has data indexes (used for blocks data)
final boolean hasIndex = reader.readBoolean();
if (hasIndex) {
final boolean hasDataIndex = reader.readBoolean();
if (hasDataIndex) {
// Get the data indexes which will be used to read all the individual data
typeToIndexMap = SerializableData.readDataIndexes(reader);
}

View File

@ -460,10 +460,15 @@ public class ItemStack implements DataContainer {
* @return true if the item has nbt tag, false otherwise
*/
public boolean hasNbtTag() {
return hasDisplayName() || hasLore() || damage != 0 || isUnbreakable() ||
return hasDisplayName() ||
hasLore() ||
damage != 0 ||
isUnbreakable() ||
!enchantmentMap.isEmpty() ||
!attributes.isEmpty() ||
hideFlag != 0 || customModelData != 0 || (itemMeta != null && itemMeta.hasNbt());
hideFlag != 0 ||
customModelData != 0 ||
(itemMeta != null && itemMeta.hasNbt());
}
/**

View File

@ -140,9 +140,11 @@ public final class NBTUtils {
if (nbt.containsKey("AttributeModifiers")) {
NBTList<NBTCompound> attributes = nbt.getList("AttributeModifiers");
for (NBTCompound attributeNBT : attributes) {
final long uuidMost = attributeNBT.getLong("UUIDMost");
final long uuidLeast = attributeNBT.getLong("UUIDLeast");
final UUID uuid = new UUID(uuidMost, uuidLeast);
final UUID uuid;
{
final int[] uuidArray = attributeNBT.getIntArray("UUID");
uuid = Utils.intArrayToUuid(uuidArray);
}
final double value = attributeNBT.getDouble("Amount");
final String slot = attributeNBT.getString("Slot");
final String attributeName = attributeNBT.getString("AttributeName");
@ -277,14 +279,12 @@ public final class NBTUtils {
for (ItemAttribute itemAttribute : itemAttributes) {
final UUID uuid = itemAttribute.getUuid();
attributesNBT.add(
new NBTCompound()
.setLong("UUIDMost", uuid.getMostSignificantBits())
.setLong("UUIDLeast", uuid.getLeastSignificantBits())
.setIntArray("UUID", Utils.uuidToIntArray(uuid))
.setDouble("Amount", itemAttribute.getValue())
.setString("Slot", itemAttribute.getSlot().name().toLowerCase())
.setString("itemAttribute", itemAttribute.getAttribute().getKey())
.setString("AttributeName", itemAttribute.getAttribute().getKey())
.setInt("Operation", itemAttribute.getOperation().getId())
.setString("Name", itemAttribute.getInternalName())
);

View File

@ -4,6 +4,8 @@ import io.netty.buffer.ByteBuf;
import net.minestom.server.instance.Chunk;
import net.minestom.server.utils.binary.BinaryWriter;
import java.util.UUID;
public final class Utils {
private Utils() {
@ -69,6 +71,28 @@ public final class Utils {
} while (value != 0);
}
public static int[] uuidToIntArray(UUID uuid) {
int[] array = new int[4];
final long uuidMost = uuid.getMostSignificantBits();
final long uuidLeast = uuid.getLeastSignificantBits();
array[0] = (int) (uuidMost >> 32);
array[1] = (int) uuidMost;
array[2] = (int) (uuidLeast >> 32);
array[3] = (int) uuidLeast;
return array;
}
public static UUID intArrayToUuid(int[] array) {
final long uuidMost = (long) array[0] << 32 | array[1] & 0xFFFFFFFFL;
final long uuidLeast = (long) array[2] << 32 | array[3] & 0xFFFFFFFFL;
return new UUID(uuidMost, uuidLeast);
}
private static final int[] MAGIC = {
-1, -1, 0, Integer.MIN_VALUE, 0, 0, 1431655765, 1431655765, 0, Integer.MIN_VALUE,
0, 1, 858993459, 858993459, 0, 715827882, 715827882, 0, 613566756, 613566756,