From b997b87dc1bba6d00c9578201d9bf5031ad8ba77 Mon Sep 17 00:00:00 2001 From: themode Date: Sun, 19 Dec 2021 20:52:37 +0100 Subject: [PATCH] Improve item meta creation performance --- .../server/item/metadata/BundleMeta.java | 16 +- .../server/item/metadata/CompassMeta.java | 15 +- .../server/item/metadata/CrossbowMeta.java | 15 +- .../item/metadata/EnchantedBookMeta.java | 7 +- .../item/metadata/FireworkEffectMeta.java | 4 +- .../server/item/metadata/FireworkMeta.java | 21 +-- .../item/metadata/LeatherArmorMeta.java | 8 +- .../server/item/metadata/MapMeta.java | 155 ++++-------------- .../server/item/metadata/PlayerHeadMeta.java | 28 +--- .../server/item/metadata/PotionMeta.java | 29 ++-- .../item/metadata/WritableBookMeta.java | 15 +- .../server/item/metadata/WrittenBookMeta.java | 23 ++- .../server/potion/CustomPotionEffect.java | 77 +-------- 13 files changed, 120 insertions(+), 293 deletions(-) diff --git a/src/main/java/net/minestom/server/item/metadata/BundleMeta.java b/src/main/java/net/minestom/server/item/metadata/BundleMeta.java index 2661cfe20..541e778bd 100644 --- a/src/main/java/net/minestom/server/item/metadata/BundleMeta.java +++ b/src/main/java/net/minestom/server/item/metadata/BundleMeta.java @@ -5,7 +5,10 @@ import net.minestom.server.item.ItemMetaBuilder; import net.minestom.server.item.ItemStack; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; -import org.jglrxavpok.hephaistos.nbt.*; +import org.jglrxavpok.hephaistos.nbt.NBT; +import org.jglrxavpok.hephaistos.nbt.NBTCompound; +import org.jglrxavpok.hephaistos.nbt.NBTList; +import org.jglrxavpok.hephaistos.nbt.NBTType; import java.util.ArrayList; import java.util.List; @@ -55,16 +58,15 @@ public class BundleMeta extends ItemMeta implements ItemMetaBuilder.Provider { - compound.set("Items", NBT.List(NBTType.TAG_Compound, items.size(), i -> items.get(i).toItemNBT())); - }); + mutateNbt(compound -> compound.set("Items", NBT.List(NBTType.TAG_Compound, + items.size(), i -> items.get(i).toItemNBT()))); } @Override public void read(@NotNull NBTCompound nbtCompound) { - if (nbtCompound.containsKey("Items")) { - final NBTList items = nbtCompound.getList("Items"); - for (NBTCompound item : items) { + if (nbtCompound.get("Items") instanceof NBTList list && + list.getSubtagType() == NBTType.TAG_Compound) { + for (NBTCompound item : list.asListOf()) { this.items.add(ItemStack.fromItemNBT(item)); } } diff --git a/src/main/java/net/minestom/server/item/metadata/CompassMeta.java b/src/main/java/net/minestom/server/item/metadata/CompassMeta.java index 8e7c0223d..5cd98cd71 100644 --- a/src/main/java/net/minestom/server/item/metadata/CompassMeta.java +++ b/src/main/java/net/minestom/server/item/metadata/CompassMeta.java @@ -7,7 +7,9 @@ import net.minestom.server.item.ItemMetaBuilder; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jglrxavpok.hephaistos.nbt.NBT; +import org.jglrxavpok.hephaistos.nbt.NBTByte; import org.jglrxavpok.hephaistos.nbt.NBTCompound; +import org.jglrxavpok.hephaistos.nbt.NBTString; import java.util.Map; import java.util.function.Supplier; @@ -90,18 +92,17 @@ public class CompassMeta extends ItemMeta implements ItemMetaBuilder.Provider list && + list.getSubtagType() == NBTType.TAG_Compound) { + NBTUtils.loadEnchantments(list.asListOf(), this::enchantment); } } diff --git a/src/main/java/net/minestom/server/item/metadata/FireworkEffectMeta.java b/src/main/java/net/minestom/server/item/metadata/FireworkEffectMeta.java index 373dfcb3d..a204c6aba 100644 --- a/src/main/java/net/minestom/server/item/metadata/FireworkEffectMeta.java +++ b/src/main/java/net/minestom/server/item/metadata/FireworkEffectMeta.java @@ -39,8 +39,8 @@ public class FireworkEffectMeta extends ItemMeta implements ItemMetaBuilder.Prov @Override public void read(@NotNull NBTCompound nbtCompound) { - if (nbtCompound.containsKey("Explosion")) { - effect(FireworkEffect.fromCompound(nbtCompound.getCompound("Explosion"))); + if (nbtCompound.get("Explosion") instanceof NBTCompound explosionCompound) { + this.fireworkEffect = FireworkEffect.fromCompound(explosionCompound); } } diff --git a/src/main/java/net/minestom/server/item/metadata/FireworkMeta.java b/src/main/java/net/minestom/server/item/metadata/FireworkMeta.java index dc0cf41bc..567ad556f 100644 --- a/src/main/java/net/minestom/server/item/metadata/FireworkMeta.java +++ b/src/main/java/net/minestom/server/item/metadata/FireworkMeta.java @@ -4,10 +4,7 @@ import net.minestom.server.item.ItemMeta; import net.minestom.server.item.ItemMetaBuilder; import net.minestom.server.item.firework.FireworkEffect; import org.jetbrains.annotations.NotNull; -import org.jglrxavpok.hephaistos.nbt.NBT; -import org.jglrxavpok.hephaistos.nbt.NBTCompound; -import org.jglrxavpok.hephaistos.nbt.NBTList; -import org.jglrxavpok.hephaistos.nbt.NBTType; +import org.jglrxavpok.hephaistos.nbt.*; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; @@ -65,20 +62,16 @@ public class FireworkMeta extends ItemMeta implements ItemMetaBuilder.Provider explosions = fireworksCompound.getList("Explosions"); - - for (NBTCompound explosion : explosions) { + if (fireworksCompound.get("Explosions") instanceof NBTList list && + list.getSubtagType() == NBTType.TAG_Compound) { + for (NBTCompound explosion : list.asListOf()) { this.effects.add(FireworkEffect.fromCompound(explosion)); } - effects(effects); } } } diff --git a/src/main/java/net/minestom/server/item/metadata/LeatherArmorMeta.java b/src/main/java/net/minestom/server/item/metadata/LeatherArmorMeta.java index 507a6a153..9ec289a71 100644 --- a/src/main/java/net/minestom/server/item/metadata/LeatherArmorMeta.java +++ b/src/main/java/net/minestom/server/item/metadata/LeatherArmorMeta.java @@ -6,6 +6,7 @@ import net.minestom.server.item.ItemMetaBuilder; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jglrxavpok.hephaistos.nbt.NBTCompound; +import org.jglrxavpok.hephaistos.nbt.NBTInt; import java.util.function.Supplier; @@ -45,10 +46,9 @@ public class LeatherArmorMeta extends ItemMeta implements ItemMetaBuilder.Provid @Override public void read(@NotNull NBTCompound nbtCompound) { - if (nbtCompound.containsKey("display")) { - final NBTCompound displayCompound = nbtCompound.getCompound("display"); - if (displayCompound.containsKey("color")) { - color(new Color(displayCompound.getInt("color"))); + if (nbtCompound.get("display") instanceof NBTCompound displayCompound) { + if (displayCompound.get("color") instanceof NBTInt colorInt) { + this.color = new Color(colorInt.getValue()); } } } diff --git a/src/main/java/net/minestom/server/item/metadata/MapMeta.java b/src/main/java/net/minestom/server/item/metadata/MapMeta.java index 2f68aa6ea..3eafd415d 100644 --- a/src/main/java/net/minestom/server/item/metadata/MapMeta.java +++ b/src/main/java/net/minestom/server/item/metadata/MapMeta.java @@ -1,15 +1,10 @@ package net.minestom.server.item.metadata; -import net.minestom.server.MinecraftServer; import net.minestom.server.color.Color; import net.minestom.server.item.ItemMeta; import net.minestom.server.item.ItemMetaBuilder; -import net.minestom.server.utils.clone.PublicCloneable; import org.jetbrains.annotations.NotNull; -import org.jglrxavpok.hephaistos.nbt.NBT; -import org.jglrxavpok.hephaistos.nbt.NBTCompound; -import org.jglrxavpok.hephaistos.nbt.NBTList; -import org.jglrxavpok.hephaistos.nbt.NBTType; +import org.jglrxavpok.hephaistos.nbt.*; import java.util.ArrayList; import java.util.List; @@ -21,18 +16,18 @@ public class MapMeta extends ItemMeta implements ItemMetaBuilder.Provider decorations; + private final List decorations; private final Color mapColor; protected MapMeta(ItemMetaBuilder metaBuilder, int mapId, int mapScaleDirection, - @NotNull List decorations, + @NotNull List decorations, @NotNull Color mapColor) { super(metaBuilder); this.mapId = mapId; this.mapScaleDirection = mapScaleDirection; - this.decorations = decorations; + this.decorations = List.copyOf(decorations); this.mapColor = mapColor; } @@ -59,7 +54,7 @@ public class MapMeta extends ItemMeta implements ItemMetaBuilder.Provider getDecorations() { + public List getDecorations() { return decorations; } @@ -76,7 +71,7 @@ public class MapMeta extends ItemMeta implements ItemMetaBuilder.Provider decorations = new CopyOnWriteArrayList<>(); + private List decorations = new CopyOnWriteArrayList<>(); private Color mapColor = new Color(0, 0, 0); public Builder mapId(int value) { @@ -91,32 +86,26 @@ public class MapMeta extends ItemMeta implements ItemMetaBuilder.Provider value) { + public Builder decorations(List value) { this.decorations = new ArrayList<>(value); - NBTList decorationsList = NBT.List( + mutateNbt(compound -> compound.set("Decorations", NBT.List( NBTType.TAG_Compound, decorations.stream() .map(decoration -> NBT.Compound(Map.of( - "id", NBT.String(decoration.getId()), - "type", NBT.Byte(decoration.getType()), - "x", NBT.Byte(decoration.getX()), - "z", NBT.Byte(decoration.getZ()), - "rot", NBT.Double(decoration.getRotation())))) + "id", NBT.String(decoration.id()), + "type", NBT.Byte(decoration.type()), + "x", NBT.Byte(decoration.x()), + "z", NBT.Byte(decoration.z()), + "rot", NBT.Double(decoration.rotation())))) .toList() - ); - mutateNbt(compound -> compound.set("Decorations", decorationsList)); - + ))); return this; } public Builder mapColor(Color value) { this.mapColor = value; - - handleCompound("display", displayCompound -> { - displayCompound.setInt("MapColor", mapColor.asRGB()); - }); - + handleCompound("display", displayCompound -> displayCompound.setInt("MapColor", mapColor.asRGB())); return this; } @@ -127,45 +116,43 @@ public class MapMeta extends ItemMeta implements ItemMetaBuilder.Provider decorationsList = compound.getList("Decorations"); - List mapDecorations = new ArrayList<>(); - for (NBTCompound decorationCompound : decorationsList) { + if (compound.get("Decorations") instanceof NBTList decorationsList && + decorationsList.getSubtagType() == NBTType.TAG_Compound) { + List decorations = new ArrayList<>(); + for (NBTCompound decorationCompound : decorationsList.asListOf()) { final String id = decorationCompound.getString("id"); final byte type = decorationCompound.getAsByte("type"); byte x = 0; - if (decorationCompound.containsKey("x")) { - x = decorationCompound.getAsByte("x"); + if (decorationCompound.get("x") instanceof NBTByte xByte) { + x = xByte.getValue(); } byte z = 0; - if (decorationCompound.containsKey("z")) { - z = decorationCompound.getAsByte("z"); + if (decorationCompound.get("z") instanceof NBTByte zByte) { + z = zByte.getValue(); } double rotation = 0.0; - if (decorationCompound.containsKey("rot")) { - rotation = decorationCompound.getAsDouble("rot"); + if (decorationCompound.get("rot") instanceof NBTDouble rotDouble) { + rotation = rotDouble.getValue(); } - mapDecorations.add(new MapDecoration(id, type, x, z, rotation)); + decorations.add(new Decoration(id, type, x, z, rotation)); } - decorations(mapDecorations); + this.decorations = decorations; } - if (compound.containsKey("display")) { - final NBTCompound displayCompound = compound.getCompound("display"); - if (displayCompound.containsKey("MapColor")) { - mapColor(new Color(displayCompound.getAsInt("MapColor"))); + if (compound.get("display") instanceof NBTCompound displayCompound) { + if (displayCompound.get("MapColor") instanceof NBTInt mapColor) { + this.mapColor = new Color(mapColor.getValue()); } } } @@ -176,76 +163,6 @@ public class MapMeta extends ItemMeta implements ItemMetaBuilder.Provider { - private final String id; - private final byte type; - private final byte x, z; - private final double rotation; - - public MapDecoration(@NotNull String id, byte type, byte x, byte z, double rotation) { - this.id = id; - this.type = type; - this.x = x; - this.z = z; - this.rotation = rotation; - } - - /** - * Gets the arbitrary decoration id. - * - * @return the decoration id - */ - public String getId() { - return id; - } - - /** - * Gets the decoration type. - * - * @return the decoration type - * @see Map icons - */ - public byte getType() { - return type; - } - - /** - * Gets the X position of the decoration. - * - * @return the X position - */ - public byte getX() { - return x; - } - - /** - * Gets the Z position of the decoration. - * - * @return the Z position - */ - public byte getZ() { - return z; - } - - /** - * Gets the rotation of the symbol (0;360). - * - * @return the rotation of the symbol - */ - public double getRotation() { - return rotation; - } - - @NotNull - @Override - public MapDecoration clone() { - try { - return (MapDecoration) super.clone(); - } catch (CloneNotSupportedException e) { - MinecraftServer.getExceptionManager().handleException(e); - throw new IllegalStateException("Something weird happened"); - } - } + public record Decoration(String id, byte type, byte x, byte z, double rotation) { } - } 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 48d9c0abf..943abadd0 100644 --- a/src/main/java/net/minestom/server/item/metadata/PlayerHeadMeta.java +++ b/src/main/java/net/minestom/server/item/metadata/PlayerHeadMeta.java @@ -6,10 +6,7 @@ import net.minestom.server.item.ItemMetaBuilder; import net.minestom.server.utils.Utils; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import org.jglrxavpok.hephaistos.nbt.NBT; -import org.jglrxavpok.hephaistos.nbt.NBTCompound; -import org.jglrxavpok.hephaistos.nbt.NBTList; -import org.jglrxavpok.hephaistos.nbt.NBTType; +import org.jglrxavpok.hephaistos.nbt.*; import java.util.List; import java.util.Map; @@ -75,24 +72,17 @@ public class PlayerHeadMeta extends ItemMeta implements ItemMetaBuilder.Provider @Override public void read(@NotNull NBTCompound nbtCompound) { - if (nbtCompound.containsKey("SkullOwner")) { - NBTCompound skullOwnerCompound = nbtCompound.getCompound("SkullOwner"); - - if (skullOwnerCompound.containsKey("Id")) { - skullOwner(Utils.intArrayToUuid(skullOwnerCompound.getIntArray("Id").copyArray())); + if (nbtCompound.get("SkullOwner") instanceof NBTCompound skullOwnerCompound) { + if (skullOwnerCompound.get("Id") instanceof NBTIntArray id) { + this.skullOwner = Utils.intArrayToUuid(id.getValue().copyArray()); } - if (skullOwnerCompound.containsKey("Properties")) { - NBTCompound propertyCompound = skullOwnerCompound.getCompound("Properties"); - - if (propertyCompound.containsKey("textures")) { - NBTList textures = propertyCompound.getList("textures"); - if (textures != null) { - NBTCompound nbt = textures.get(0); - playerSkin(new PlayerSkin(nbt.getString("Value"), nbt.getString("Signature"))); - } + if (skullOwnerCompound.get("Properties") instanceof NBTCompound propertyCompound) { + if (propertyCompound.get("textures") instanceof NBTList textures && + textures.getSubtagType() == NBTType.TAG_Compound) { + NBTCompound nbt = (NBTCompound) textures.get(0); + this.playerSkin = new PlayerSkin(nbt.getString("Value"), nbt.getString("Signature")); } - } } } 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 fec299a41..ca9e7c342 100644 --- a/src/main/java/net/minestom/server/item/metadata/PotionMeta.java +++ b/src/main/java/net/minestom/server/item/metadata/PotionMeta.java @@ -7,10 +7,7 @@ import net.minestom.server.potion.CustomPotionEffect; import net.minestom.server.potion.PotionType; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import org.jglrxavpok.hephaistos.nbt.NBT; -import org.jglrxavpok.hephaistos.nbt.NBTCompound; -import org.jglrxavpok.hephaistos.nbt.NBTList; -import org.jglrxavpok.hephaistos.nbt.NBTType; +import org.jglrxavpok.hephaistos.nbt.*; import java.time.Duration; import java.util.ArrayList; @@ -64,9 +61,9 @@ public class PotionMeta extends ItemMeta implements ItemMetaBuilder.Provider NBT.Compound(Map.of( - "Id", NBT.Byte(customPotionEffect.getId()), - "Amplifier", NBT.Byte(customPotionEffect.getAmplifier()), - "Duration", NBT.Int(customPotionEffect.getDuration()), + "Id", NBT.Byte(customPotionEffect.id()), + "Amplifier", NBT.Byte(customPotionEffect.amplifier()), + "Duration", NBT.Int(customPotionEffect.duration()), "Ambient", NBT.Boolean(customPotionEffect.isAmbient()), "ShowParticles", NBT.Boolean(customPotionEffect.showParticles()), "ShowIcon", NBT.Boolean(customPotionEffect.showIcon())))) @@ -90,13 +87,13 @@ public class PotionMeta extends ItemMeta implements ItemMetaBuilder.Provider customEffectList = nbtCompound.getList("CustomPotionEffects"); - for (NBTCompound potionCompound : customEffectList) { + if (nbtCompound.get("CustomPotionEffects") instanceof NBTList customEffectList && + customEffectList.getSubtagType() == NBTType.TAG_Compound) { + for (NBTCompound potionCompound : customEffectList.asListOf()) { final byte id = potionCompound.getAsByte("Id"); final byte amplifier = potionCompound.getAsByte("Amplifier"); final int duration = potionCompound.containsKey("Duration") ? potionCompound.getNumber("Duration").intValue() : (int) Duration.ofSeconds(30).toMillis(); @@ -104,14 +101,12 @@ public class PotionMeta extends ItemMeta implements ItemMetaBuilder.Provider list = nbtCompound.getList("pages"); - for (NBTString page : list) { + if (nbtCompound.get("pages") instanceof NBTList list && + list.getSubtagType() == NBTType.TAG_String) { + for (NBTString page : list.asListOf()) { this.pages.add(LegacyComponentSerializer.legacySection().deserialize(page.getValue())); } - pages(pages); } } diff --git a/src/main/java/net/minestom/server/item/metadata/WrittenBookMeta.java b/src/main/java/net/minestom/server/item/metadata/WrittenBookMeta.java index 560c84442..73ee062e2 100644 --- a/src/main/java/net/minestom/server/item/metadata/WrittenBookMeta.java +++ b/src/main/java/net/minestom/server/item/metadata/WrittenBookMeta.java @@ -139,24 +139,23 @@ public class WrittenBookMeta extends ItemMeta implements ItemMetaBuilder.Provide @Override public void read(@NotNull NBTCompound nbtCompound) { - if (nbtCompound.containsKey("resolved")) { - resolved(nbtCompound.getByte("resolved") == 1); + if (nbtCompound.get("resolved") instanceof NBTByte resolved) { + this.resolved = resolved.asBoolean(); } - if (nbtCompound.containsKey("generation")) { - generation(WrittenBookGeneration.values()[nbtCompound.getInt("generation")]); + if (nbtCompound.get("generation") instanceof NBTInt generation) { + this.generation = WrittenBookGeneration.values()[generation.getValue()]; } - if (nbtCompound.containsKey("author")) { - author(nbtCompound.getString("author")); + if (nbtCompound.get("author") instanceof NBTString author) { + this.author = author.getValue(); } - if (nbtCompound.containsKey("title")) { - title(nbtCompound.getString("title")); + if (nbtCompound.get("title") instanceof NBTString title) { + this.title = title.getValue(); } - if (nbtCompound.containsKey("pages")) { - final NBTList list = nbtCompound.getList("pages"); - for (NBTString page : list) { + if (nbtCompound.get("pages") instanceof NBTList list && + list.getSubtagType() == NBTType.TAG_String) { + for (NBTString page : list.asListOf()) { this.pages.add(GsonComponentSerializer.gson().deserialize(page.getValue())); } - pages(pages); } } diff --git a/src/main/java/net/minestom/server/potion/CustomPotionEffect.java b/src/main/java/net/minestom/server/potion/CustomPotionEffect.java index 6faa1ab91..e9ecad664 100644 --- a/src/main/java/net/minestom/server/potion/CustomPotionEffect.java +++ b/src/main/java/net/minestom/server/potion/CustomPotionEffect.java @@ -1,80 +1,9 @@ package net.minestom.server.potion; -import net.minestom.server.MinecraftServer; -import net.minestom.server.utils.clone.PublicCloneable; -import org.jetbrains.annotations.NotNull; - -import java.util.Objects; - /** * Represents a custom effect in {@link net.minestom.server.item.metadata.PotionMeta}. - *

- * This is an immutable class. */ -public class CustomPotionEffect implements PublicCloneable { - - private final byte id; - private final byte amplifier; - private final int duration; - private final boolean ambient; - private final boolean showParticles; - private final boolean showIcon; - - public CustomPotionEffect(byte id, byte amplifier, int duration, - boolean ambient, boolean showParticles, boolean showIcon) { - this.id = id; - this.amplifier = amplifier; - this.duration = duration; - this.ambient = ambient; - this.showParticles = showParticles; - this.showIcon = showIcon; - } - - public byte getId() { - return id; - } - - public byte getAmplifier() { - return amplifier; - } - - public int getDuration() { - return duration; - } - - public boolean isAmbient() { - return ambient; - } - - public boolean showParticles() { - return showParticles; - } - - public boolean showIcon() { - return showIcon; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - CustomPotionEffect that = (CustomPotionEffect) o; - return id == that.id && amplifier == that.amplifier && duration == that.duration && ambient == that.ambient && showParticles == that.showParticles && showIcon == that.showIcon; - } - - @Override - public int hashCode() { - return Objects.hash(id, amplifier, duration, ambient, showParticles, showIcon); - } - - @NotNull - @Override - public CustomPotionEffect clone() { - try { - return (CustomPotionEffect) super.clone(); - } catch (CloneNotSupportedException e) { - MinecraftServer.getExceptionManager().handleException(e); - throw new IllegalStateException("Weird thing happened"); - } - } +public record CustomPotionEffect(byte id, byte amplifier, int duration, + boolean isAmbient, boolean showParticles, + boolean showIcon) { }