diff --git a/dough-items/src/main/java/io/github/bakedlibs/dough/items/CustomItemStack.java b/dough-items/src/main/java/io/github/bakedlibs/dough/items/CustomItemStack.java index 4c43d6bd..157cda58 100644 --- a/dough-items/src/main/java/io/github/bakedlibs/dough/items/CustomItemStack.java +++ b/dough-items/src/main/java/io/github/bakedlibs/dough/items/CustomItemStack.java @@ -1,6 +1,5 @@ package io.github.bakedlibs.dough.items; -import org.bukkit.Color; import org.bukkit.Material; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; @@ -18,11 +17,11 @@ private CustomItemStack() { } public static ItemStack create(ItemStack itemStack, Consumer metaConsumer) { - return new ItemStackEditor(itemStack).appendMetaConsumer(metaConsumer).create(); + return new ItemStackEditor(itemStack).andMetaConsumer(metaConsumer).create(); } public static ItemStack create(Material material, Consumer metaConsumer) { - return new ItemStackEditor(material).appendMetaConsumer(metaConsumer).create(); + return new ItemStackEditor(material).andMetaConsumer(metaConsumer).create(); } public static ItemStack create(ItemStack item, @Nullable String name, String... lore) { @@ -63,7 +62,7 @@ public static ItemStack create(ItemStack item, int amount) { */ @Deprecated(forRemoval = true) public static ItemStack create(ItemStack itemStack, Material type) { - return new ItemStackEditor(itemStack).appendStackConsumer(item -> item.setType(type)).create(); + return new ItemStackEditor(itemStack).andStackConsumer(item -> item.setType(type)).create(); } } diff --git a/dough-items/src/main/java/io/github/bakedlibs/dough/items/ItemStackEditor.java b/dough-items/src/main/java/io/github/bakedlibs/dough/items/ItemStackEditor.java index 7e2983b8..d86268ef 100644 --- a/dough-items/src/main/java/io/github/bakedlibs/dough/items/ItemStackEditor.java +++ b/dough-items/src/main/java/io/github/bakedlibs/dough/items/ItemStackEditor.java @@ -17,7 +17,7 @@ /** * Fluent class to apply edits/transformations to an {@link ItemStack} and it's {@link ItemMeta} instance *

- * All methods in this class which are not getters mutate this instance.
+ * This class is immutable. * The {@link ItemStack} instance which this class holds on to is never mutated. * * @see #create() @@ -27,35 +27,43 @@ public class ItemStackEditor { private final ItemStack itemStack; - private Consumer metaTransform = null; - private Consumer stackTransform = null; + private final Consumer metaTransform; + private final Consumer stackTransform; + + private ItemStackEditor(ItemStack itemStack, + @Nullable Consumer metaTransform, + @Nullable Consumer stackTransform) { + this.itemStack = itemStack; + this.metaTransform = metaTransform; + this.stackTransform = stackTransform; + } public ItemStackEditor(ItemStack item) { - this.itemStack = item.clone(); + this(item.clone(), null, null); } public ItemStackEditor(Material type) { - this.itemStack = new ItemStack(type); + this(new ItemStack(type)); } public ItemStackEditor addFlags(ItemFlag... flags) { - return appendMetaConsumer(ItemStackUtil.appendItemFlags(flags)); + return andMetaConsumer(ItemStackUtil.appendItemFlags(flags)); } public ItemStackEditor setCustomModel(int data) { - return appendMetaConsumer(ItemStackUtil.editCustomModelData(data)); + return andMetaConsumer(ItemStackUtil.editCustomModelData(data)); } public ItemStackEditor setCustomModel(@Nullable Integer data) { - return appendMetaConsumer(ItemStackUtil.editCustomModelData(data)); + return andMetaConsumer(ItemStackUtil.editCustomModelData(data)); } public ItemStackEditor setAmount(int amount) { - return appendStackConsumer(stack -> stack.setAmount(amount)); + return andStackConsumer(stack -> stack.setAmount(amount)); } public ItemStackEditor setColor(Color color) { - return appendMetaConsumer(meta -> { + return andMetaConsumer(meta -> { if (meta instanceof LeatherArmorMeta) { ((LeatherArmorMeta) meta).setColor(color); } @@ -70,35 +78,33 @@ public ItemStackEditor setLore(String... lore) { } public ItemStackEditor setLore(List list) { - return appendMetaConsumer(ItemStackUtil.editLore(list)); + return andMetaConsumer(ItemStackUtil.editLore(list)); } public ItemStackEditor setDisplayName(@Nullable String name) { - return appendMetaConsumer(ItemStackUtil.editDisplayName(name)); + return andMetaConsumer(ItemStackUtil.editDisplayName(name)); } - public ItemStackEditor appendMetaConsumer(Consumer consumer) { + public ItemStackEditor andMetaConsumer(Consumer consumer) { if (this.metaTransform == null) { - return setMetaConsumer(consumer); + return withMetaConsumer(consumer); } - return setMetaConsumer(this.metaTransform.andThen(consumer)); + return withMetaConsumer(this.metaTransform.andThen(consumer)); } - public ItemStackEditor setMetaConsumer(@Nullable Consumer consumer) { - this.metaTransform = consumer; - return this; + public ItemStackEditor withMetaConsumer(@Nullable Consumer consumer) { + return new ItemStackEditor(this.itemStack, consumer, this.stackTransform); } - public ItemStackEditor setStackConsumer(@Nullable Consumer consumer) { - this.stackTransform = consumer; - return this; + public ItemStackEditor withStackConsumer(@Nullable Consumer consumer) { + return new ItemStackEditor(this.itemStack, this.metaTransform, consumer); } - public ItemStackEditor appendStackConsumer(Consumer consumer) { + public ItemStackEditor andStackConsumer(Consumer consumer) { if (this.stackTransform == null) { - return setStackConsumer(consumer); + return withStackConsumer(consumer); } - return setStackConsumer(this.stackTransform.andThen(consumer)); + return withStackConsumer(this.stackTransform.andThen(consumer)); } public ItemStack create() { diff --git a/dough-items/src/test/java/io/github/bakedlibs/dough/items/TestItemStackEditor.java b/dough-items/src/test/java/io/github/bakedlibs/dough/items/TestItemStackEditor.java index 9ed6ff13..f99166df 100644 --- a/dough-items/src/test/java/io/github/bakedlibs/dough/items/TestItemStackEditor.java +++ b/dough-items/src/test/java/io/github/bakedlibs/dough/items/TestItemStackEditor.java @@ -222,8 +222,7 @@ void testItemFlagsChanged() { @Test void testItemMetaNotSetIfMetaTransformIsNull() { ItemStack expected = new ItemStack(Material.AIR); - ItemStackEditor editor = new ItemStackEditor(Material.AIR); - editor.setMetaConsumer(null); + ItemStackEditor editor = new ItemStackEditor(Material.AIR).withMetaConsumer(null); Assertions.assertEquals(expected, editor.create()); } @@ -231,8 +230,7 @@ void testItemMetaNotSetIfMetaTransformIsNull() { void testItemMetaSetIfMetaTransformIsNotNull() { ItemStack expected = new ItemStack(Material.AIR); expected.setItemMeta(expected.getItemMeta()); - ItemStackEditor editor = new ItemStackEditor(Material.AIR); - editor.setMetaConsumer(meta -> {}); + ItemStackEditor editor = new ItemStackEditor(Material.AIR).withMetaConsumer(meta -> {}); Assertions.assertEquals(expected, editor.create()); }