From ac7ca6f8e422469d7b87695903d236a541e9ecf0 Mon Sep 17 00:00:00 2001 From: ALS Date: Sat, 20 Feb 2021 17:35:02 +0100 Subject: [PATCH 1/6] Fixed somme read for NBT Tag to ItemStack --- .../server/item/metadata/PlayerHeadMeta.java | 6 +++--- .../minestom/server/item/metadata/PotionMeta.java | 9 +++++---- .../java/net/minestom/server/utils/NBTUtils.java | 13 ++++++------- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/main/java/net/minestom/server/item/metadata/PlayerHeadMeta.java b/src/main/java/net/minestom/server/item/metadata/PlayerHeadMeta.java index f7f49dbc4..bfa7e9486 100644 --- a/src/main/java/net/minestom/server/item/metadata/PlayerHeadMeta.java +++ b/src/main/java/net/minestom/server/item/metadata/PlayerHeadMeta.java @@ -1,6 +1,5 @@ package net.minestom.server.item.metadata; -import net.minestom.server.MinecraftServer; import net.minestom.server.entity.Player; import net.minestom.server.entity.PlayerSkin; import net.minestom.server.utils.Utils; @@ -118,7 +117,6 @@ public class PlayerHeadMeta extends ItemMeta { } } - } /** @@ -135,7 +133,9 @@ public class PlayerHeadMeta extends ItemMeta { } NBTList textures = new NBTList<>(NBTTypes.TAG_Compound); - textures.add(new NBTCompound().setString("Value", this.playerSkin.getTextures()).setString("Signature", this.playerSkin.getSignature())); + String value = this.playerSkin.getTextures() == null ? "" : this.playerSkin.getTextures(); + String signature = this.playerSkin.getSignature() == null ? "" : this.playerSkin.getSignature(); + textures.add(new NBTCompound().setString("Value", value).setString("Signature", signature)); skullOwnerCompound.set("Properties", new NBTCompound().set("textures", textures)); compound.set("SkullOwner", skullOwnerCompound); diff --git a/src/main/java/net/minestom/server/item/metadata/PotionMeta.java b/src/main/java/net/minestom/server/item/metadata/PotionMeta.java index cb3b8f5b1..a88545481 100644 --- a/src/main/java/net/minestom/server/item/metadata/PotionMeta.java +++ b/src/main/java/net/minestom/server/item/metadata/PotionMeta.java @@ -5,6 +5,7 @@ import net.minestom.server.potion.CustomPotionEffect; import net.minestom.server.potion.PotionType; import net.minestom.server.registry.Registries; import net.minestom.server.utils.clone.CloneUtils; +import net.minestom.server.utils.time.TimeUnit; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jglrxavpok.hephaistos.nbt.NBTCompound; @@ -109,10 +110,10 @@ public class PotionMeta extends ItemMeta { for (NBTCompound potionCompound : customEffectList) { final byte id = potionCompound.getAsByte("Id"); final byte amplifier = potionCompound.getAsByte("Amplifier"); - final int duration = potionCompound.getAsInt("Duration"); - final boolean ambient = potionCompound.getAsByte("Ambient") == 1; - final boolean showParticles = potionCompound.getAsByte("ShowParticles") == 1; - final boolean showIcon = potionCompound.getAsByte("ShowIcon") == 1; + final int duration = potionCompound.containsKey("Duration") ? potionCompound.getNumber("Duration").intValue() : (int) TimeUnit.SECOND.toMilliseconds(30); + final boolean ambient = potionCompound.containsKey("Ambient") ? potionCompound.getAsByte("Ambient") == 1 : false; + final boolean showParticles = potionCompound.containsKey("ShowParticles") ? potionCompound.getAsByte("ShowParticles") == 1 : true; + final boolean showIcon = potionCompound.containsKey("ShowIcon") ? potionCompound.getAsByte("ShowIcon") == 1 : true; this.customPotionEffects.add( new CustomPotionEffect(id, amplifier, duration, ambient, showParticles, showIcon)); diff --git a/src/main/java/net/minestom/server/utils/NBTUtils.java b/src/main/java/net/minestom/server/utils/NBTUtils.java index f1e91d941..0c2bcff4a 100644 --- a/src/main/java/net/minestom/server/utils/NBTUtils.java +++ b/src/main/java/net/minestom/server/utils/NBTUtils.java @@ -126,7 +126,7 @@ public final class NBTUtils { public static void loadDataIntoItem(@NotNull ItemStack item, @NotNull NBTCompound nbt) { if (nbt.containsKey("Damage")) item.setDamage(nbt.getInt("Damage")); - if (nbt.containsKey("Unbreakable")) item.setUnbreakable(nbt.getInt("Unbreakable") == 1); + if (nbt.containsKey("Unbreakable")) item.setUnbreakable(nbt.getNumber("Unbreakable").byteValue() == 1); if (nbt.containsKey("HideFlags")) item.setHideFlag(nbt.getInt("HideFlags")); if (nbt.containsKey("display")) { final NBTCompound display = nbt.getCompound("display"); @@ -159,10 +159,11 @@ public final class NBTUtils { final int[] uuidArray = attributeNBT.getIntArray("UUID"); uuid = Utils.intArrayToUuid(uuidArray); } - final double value = attributeNBT.getDouble("Amount"); - final String slot = attributeNBT.getString("Slot"); + + final double value = attributeNBT.getNumber("Amount").doubleValue(); + final String slot = attributeNBT.containsKey("Slot") ? attributeNBT.getString("Slot") : "MAINHAND"; final String attributeName = attributeNBT.getString("AttributeName"); - final int operation = attributeNBT.getInt("Operation"); + final int operation = attributeNBT.getInt("Operation").intValue(); final String name = attributeNBT.getString("Name"); final Attribute attribute = Attribute.fromKey(attributeName); @@ -174,11 +175,9 @@ public final class NBTUtils { if (attributeOperation == null) { break; } + final AttributeSlot attributeSlot = AttributeSlot.valueOf(slot.toUpperCase()); // Wrong attribute slot, stop here - if (attributeSlot == null) { - break; - } // Add attribute final ItemAttribute itemAttribute = From 01ff9e2123cb9a09804c41a8e9e2fbcb6c7ab6e7 Mon Sep 17 00:00:00 2001 From: ALS Date: Sat, 20 Feb 2021 19:51:45 +0100 Subject: [PATCH 2/6] Fix failed issue --- src/main/java/net/minestom/server/utils/NBTUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/net/minestom/server/utils/NBTUtils.java b/src/main/java/net/minestom/server/utils/NBTUtils.java index 0c2bcff4a..145c3ad36 100644 --- a/src/main/java/net/minestom/server/utils/NBTUtils.java +++ b/src/main/java/net/minestom/server/utils/NBTUtils.java @@ -163,7 +163,7 @@ public final class NBTUtils { final double value = attributeNBT.getNumber("Amount").doubleValue(); final String slot = attributeNBT.containsKey("Slot") ? attributeNBT.getString("Slot") : "MAINHAND"; final String attributeName = attributeNBT.getString("AttributeName"); - final int operation = attributeNBT.getInt("Operation").intValue(); + final int operation = attributeNBT.getNumber("Operation").intValue(); final String name = attributeNBT.getString("Name"); final Attribute attribute = Attribute.fromKey(attributeName); From 0f9a2b5b4ff68b778806dc89e7744715413b4644 Mon Sep 17 00:00:00 2001 From: ALS Date: Sat, 20 Feb 2021 20:39:49 +0100 Subject: [PATCH 3/6] Fixed a possible null for attributeSlot --- .../server/item/attribute/AttributeSlot.java | 19 ++++++++++++++++++- .../net/minestom/server/utils/NBTUtils.java | 2 +- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/minestom/server/item/attribute/AttributeSlot.java b/src/main/java/net/minestom/server/item/attribute/AttributeSlot.java index 46d3c9678..01882c6cc 100644 --- a/src/main/java/net/minestom/server/item/attribute/AttributeSlot.java +++ b/src/main/java/net/minestom/server/item/attribute/AttributeSlot.java @@ -6,5 +6,22 @@ public enum AttributeSlot { FEET, LEGS, CHEST, - HEAD + HEAD; + + public static AttributeSlot parse(String string) { + switch (string.toUpperCase()) { + case "OFFHAND": + return OFFHAND; + case "FEET": + return FEET; + case "LEGS": + return LEGS; + case "CHEST": + return CHEST; + case "HEAD": + return HEAD; + default: + return MAINHAND; + } + } } diff --git a/src/main/java/net/minestom/server/utils/NBTUtils.java b/src/main/java/net/minestom/server/utils/NBTUtils.java index 145c3ad36..469882a7d 100644 --- a/src/main/java/net/minestom/server/utils/NBTUtils.java +++ b/src/main/java/net/minestom/server/utils/NBTUtils.java @@ -176,7 +176,7 @@ public final class NBTUtils { break; } - final AttributeSlot attributeSlot = AttributeSlot.valueOf(slot.toUpperCase()); + final AttributeSlot attributeSlot = AttributeSlot.parse(slot.toUpperCase()); // Wrong attribute slot, stop here // Add attribute From b842f07c3c7c9ee4d0035184f98dc3ef918fe0ce Mon Sep 17 00:00:00 2001 From: ALS Date: Sat, 20 Feb 2021 23:54:25 +0100 Subject: [PATCH 4/6] Use getAsInt and getAsDouble --- src/main/java/net/minestom/server/utils/NBTUtils.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/minestom/server/utils/NBTUtils.java b/src/main/java/net/minestom/server/utils/NBTUtils.java index 469882a7d..33c7a78f5 100644 --- a/src/main/java/net/minestom/server/utils/NBTUtils.java +++ b/src/main/java/net/minestom/server/utils/NBTUtils.java @@ -160,10 +160,10 @@ public final class NBTUtils { uuid = Utils.intArrayToUuid(uuidArray); } - final double value = attributeNBT.getNumber("Amount").doubleValue(); + final double value = attributeNBT.getAsDouble("Amount"); final String slot = attributeNBT.containsKey("Slot") ? attributeNBT.getString("Slot") : "MAINHAND"; final String attributeName = attributeNBT.getString("AttributeName"); - final int operation = attributeNBT.getNumber("Operation").intValue(); + final int operation = attributeNBT.getAsInt("Operation"); final String name = attributeNBT.getString("Name"); final Attribute attribute = Attribute.fromKey(attributeName); From 12f0190f97d237734bb08b6645b3f051a2b9dacb Mon Sep 17 00:00:00 2001 From: ALS Date: Sun, 21 Feb 2021 16:56:06 +0100 Subject: [PATCH 5/6] Revert AttributeSlot.parse(String string) --- .../server/item/attribute/AttributeSlot.java | 19 +------------------ .../net/minestom/server/utils/NBTUtils.java | 5 ++++- 2 files changed, 5 insertions(+), 19 deletions(-) diff --git a/src/main/java/net/minestom/server/item/attribute/AttributeSlot.java b/src/main/java/net/minestom/server/item/attribute/AttributeSlot.java index 01882c6cc..46d3c9678 100644 --- a/src/main/java/net/minestom/server/item/attribute/AttributeSlot.java +++ b/src/main/java/net/minestom/server/item/attribute/AttributeSlot.java @@ -6,22 +6,5 @@ public enum AttributeSlot { FEET, LEGS, CHEST, - HEAD; - - public static AttributeSlot parse(String string) { - switch (string.toUpperCase()) { - case "OFFHAND": - return OFFHAND; - case "FEET": - return FEET; - case "LEGS": - return LEGS; - case "CHEST": - return CHEST; - case "HEAD": - return HEAD; - default: - return MAINHAND; - } - } + HEAD } diff --git a/src/main/java/net/minestom/server/utils/NBTUtils.java b/src/main/java/net/minestom/server/utils/NBTUtils.java index 33c7a78f5..786c9d057 100644 --- a/src/main/java/net/minestom/server/utils/NBTUtils.java +++ b/src/main/java/net/minestom/server/utils/NBTUtils.java @@ -176,9 +176,12 @@ public final class NBTUtils { break; } - final AttributeSlot attributeSlot = AttributeSlot.parse(slot.toUpperCase()); + AttributeSlot attributeSlot = AttributeSlot.valueOf(slot.toUpperCase()); // Wrong attribute slot, stop here + if (attributeSlot == null) + attributeSlot = AttributeSlot.MAINHAND; + // Add attribute final ItemAttribute itemAttribute = new ItemAttribute(uuid, name, attribute, attributeOperation, value, attributeSlot); From fb6c51204b77ab5f823780c0c7eea0323c882fb2 Mon Sep 17 00:00:00 2001 From: ALS Date: Sun, 21 Feb 2021 16:59:50 +0100 Subject: [PATCH 6/6] getAsByte --- src/main/java/net/minestom/server/utils/NBTUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/net/minestom/server/utils/NBTUtils.java b/src/main/java/net/minestom/server/utils/NBTUtils.java index 786c9d057..c7346a0b2 100644 --- a/src/main/java/net/minestom/server/utils/NBTUtils.java +++ b/src/main/java/net/minestom/server/utils/NBTUtils.java @@ -126,7 +126,7 @@ public final class NBTUtils { public static void loadDataIntoItem(@NotNull ItemStack item, @NotNull NBTCompound nbt) { if (nbt.containsKey("Damage")) item.setDamage(nbt.getInt("Damage")); - if (nbt.containsKey("Unbreakable")) item.setUnbreakable(nbt.getNumber("Unbreakable").byteValue() == 1); + if (nbt.containsKey("Unbreakable")) item.setUnbreakable(nbt.getAsByte("Unbreakable") == 1); if (nbt.containsKey("HideFlags")) item.setHideFlag(nbt.getInt("HideFlags")); if (nbt.containsKey("display")) { final NBTCompound display = nbt.getCompound("display");