From 0db44545cdcd691b3104c4fa6fe87901bfdce15c Mon Sep 17 00:00:00 2001 From: TheMode Date: Fri, 13 Aug 2021 03:25:11 +0200 Subject: [PATCH] Do not copy collections unless necessary --- .../java/net/minestom/server/item/ItemMeta.java | 15 +++++++++------ .../net/minestom/server/item/ItemMetaBuilder.java | 10 +++++----- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/main/java/net/minestom/server/item/ItemMeta.java b/src/main/java/net/minestom/server/item/ItemMeta.java index 3e5d2ad55..19042eaf0 100644 --- a/src/main/java/net/minestom/server/item/ItemMeta.java +++ b/src/main/java/net/minestom/server/item/ItemMeta.java @@ -13,7 +13,10 @@ import org.jetbrains.annotations.Nullable; import org.jglrxavpok.hephaistos.nbt.NBTCompound; import java.nio.ByteBuffer; -import java.util.*; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Set; import java.util.function.Consumer; public class ItemMeta implements TagReadable, Writeable { @@ -43,12 +46,12 @@ public class ItemMeta implements TagReadable, Writeable { this.unbreakable = metaBuilder.unbreakable; this.hideFlag = metaBuilder.hideFlag; this.displayName = metaBuilder.displayName; - this.lore = new ArrayList<>(metaBuilder.lore); - this.enchantmentMap = new HashMap<>(metaBuilder.enchantmentMap); - this.attributes = new ArrayList<>(metaBuilder.attributes); + this.lore = List.copyOf(metaBuilder.lore); + this.enchantmentMap = Map.copyOf(metaBuilder.enchantmentMap); + this.attributes = List.copyOf(metaBuilder.attributes); this.customModelData = metaBuilder.customModelData; - this.canDestroy = new HashSet<>(metaBuilder.canDestroy); - this.canPlaceOn = new HashSet<>(metaBuilder.canPlaceOn); + this.canDestroy = Set.copyOf(metaBuilder.canDestroy); + this.canPlaceOn = Set.copyOf(metaBuilder.canPlaceOn); this.metaBuilder = metaBuilder; this.nbt = metaBuilder.nbt(); diff --git a/src/main/java/net/minestom/server/item/ItemMetaBuilder.java b/src/main/java/net/minestom/server/item/ItemMetaBuilder.java index d668448ce..35d7317af 100644 --- a/src/main/java/net/minestom/server/item/ItemMetaBuilder.java +++ b/src/main/java/net/minestom/server/item/ItemMetaBuilder.java @@ -83,7 +83,7 @@ public abstract class ItemMetaBuilder implements TagWritable { @Contract("_ -> this") public @NotNull ItemMetaBuilder lore(@NotNull List<@NotNull Component> lore) { - this.lore = new ArrayList<>(lore); + this.lore = List.copyOf(lore); handleCompound("display", nbtCompound -> { final NBTList loreNBT = new NBTList<>(NBTTypes.TAG_String); for (Component line : lore) { @@ -102,7 +102,7 @@ public abstract class ItemMetaBuilder implements TagWritable { @Contract("_ -> this") public @NotNull ItemMetaBuilder enchantments(@NotNull Map enchantments) { - this.enchantmentMap = new HashMap<>(enchantments); + this.enchantmentMap = Map.copyOf(enchantments); handleMap(enchantmentMap, "Enchantments", () -> { NBTUtils.writeEnchant(nbt, "Enchantments", enchantmentMap); return nbt.get("Enchantments"); @@ -126,7 +126,7 @@ public abstract class ItemMetaBuilder implements TagWritable { @Contract("_ -> this") public @NotNull ItemMetaBuilder attributes(@NotNull List<@NotNull ItemAttribute> attributes) { - this.attributes = new ArrayList<>(attributes); + this.attributes = List.copyOf(attributes); handleCollection(attributes, "AttributeModifiers", () -> { NBTList attributesNBT = new NBTList<>(NBTTypes.TAG_Compound); @@ -157,7 +157,7 @@ public abstract class ItemMetaBuilder implements TagWritable { @Contract("_ -> this") public @NotNull ItemMetaBuilder canPlaceOn(@NotNull Set<@NotNull Block> blocks) { - this.canPlaceOn = new HashSet<>(blocks); + this.canPlaceOn = Set.copyOf(blocks); handleCollection(canPlaceOn, "CanPlaceOn", () -> { NBTList list = new NBTList<>(NBTTypes.TAG_String); canPlaceOn.forEach(block -> list.add(new NBTString(block.name()))); @@ -173,7 +173,7 @@ public abstract class ItemMetaBuilder implements TagWritable { @Contract("_ -> this") public @NotNull ItemMetaBuilder canDestroy(@NotNull Set<@NotNull Block> blocks) { - this.canDestroy = new HashSet<>(blocks); + this.canDestroy = Set.copyOf(blocks); handleCollection(canDestroy, "CanDestroy", () -> { NBTList list = new NBTList<>(NBTTypes.TAG_String); canDestroy.forEach(block -> list.add(new NBTString(block.name())));