Skip to content

Commit

Permalink
Make ItemStackEditor immutable
Browse files Browse the repository at this point in the history
  • Loading branch information
md5sha256 committed Oct 23, 2024
1 parent 42fdd58 commit 98ff994
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -18,11 +17,11 @@ private CustomItemStack() {
}

public static ItemStack create(ItemStack itemStack, Consumer<ItemMeta> metaConsumer) {
return new ItemStackEditor(itemStack).appendMetaConsumer(metaConsumer).create();
return new ItemStackEditor(itemStack).andMetaConsumer(metaConsumer).create();
}

public static ItemStack create(Material material, Consumer<ItemMeta> 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) {
Expand Down Expand Up @@ -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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* Fluent class to apply edits/transformations to an {@link ItemStack} and it's {@link ItemMeta} instance
* <p>
* All methods in this class which are not getters mutate this instance.<br>
* This class is immutable.
* The {@link ItemStack} instance which this class holds on to is never mutated.
*
* @see #create()
Expand All @@ -27,35 +27,43 @@
public class ItemStackEditor {

private final ItemStack itemStack;
private Consumer<ItemMeta> metaTransform = null;
private Consumer<ItemStack> stackTransform = null;
private final Consumer<ItemMeta> metaTransform;
private final Consumer<ItemStack> stackTransform;

private ItemStackEditor(ItemStack itemStack,
@Nullable Consumer<ItemMeta> metaTransform,
@Nullable Consumer<ItemStack> 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);
}
Expand All @@ -70,35 +78,33 @@ public ItemStackEditor setLore(String... lore) {
}

public ItemStackEditor setLore(List<String> 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<ItemMeta> consumer) {
public ItemStackEditor andMetaConsumer(Consumer<ItemMeta> 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<ItemMeta> consumer) {
this.metaTransform = consumer;
return this;
public ItemStackEditor withMetaConsumer(@Nullable Consumer<ItemMeta> consumer) {
return new ItemStackEditor(this.itemStack, consumer, this.stackTransform);
}

public ItemStackEditor setStackConsumer(@Nullable Consumer<ItemStack> consumer) {
this.stackTransform = consumer;
return this;
public ItemStackEditor withStackConsumer(@Nullable Consumer<ItemStack> consumer) {
return new ItemStackEditor(this.itemStack, this.metaTransform, consumer);
}

public ItemStackEditor appendStackConsumer(Consumer<ItemStack> consumer) {
public ItemStackEditor andStackConsumer(Consumer<ItemStack> 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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,17 +222,15 @@ 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());
}

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

Expand Down

0 comments on commit 98ff994

Please sign in to comment.