Do not copy collections unless necessary

This commit is contained in:
TheMode 2021-08-13 03:25:11 +02:00
parent 322572c96d
commit 0db44545cd
2 changed files with 14 additions and 11 deletions

View File

@ -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();

View File

@ -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<NBTString> 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<Enchantment, Short> 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<NBTCompound> 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<NBTString> 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<NBTString> list = new NBTList<>(NBTTypes.TAG_String);
canDestroy.forEach(block -> list.add(new NBTString(block.name())));