ItemBuilder modifiers

This commit is contained in:
NichtStudioCode 2021-10-06 17:17:49 +02:00
parent 0ecea11053
commit 0b96a820ec

@ -28,6 +28,7 @@ import java.io.Serializable;
import java.util.*; import java.util.*;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class ItemBuilder implements ItemProvider { public class ItemBuilder implements ItemProvider {
@ -42,6 +43,7 @@ public class ItemBuilder implements ItemProvider {
protected List<ItemFlag> itemFlags; protected List<ItemFlag> itemFlags;
protected HashMap<Enchantment, Pair<Integer, Boolean>> enchantments; protected HashMap<Enchantment, Pair<Integer, Boolean>> enchantments;
protected GameProfile gameProfile; protected GameProfile gameProfile;
protected List<Function<ItemStack, ItemStack>> modifiers;
/** /**
* Constructs a new {@link ItemBuilder} based on the given {@link Material}. * Constructs a new {@link ItemBuilder} based on the given {@link Material}.
@ -143,6 +145,12 @@ public class ItemBuilder implements ItemProvider {
itemStack.setItemMeta(itemMeta); itemStack.setItemMeta(itemMeta);
} }
// run modifiers
if (modifiers != null) {
for (Function<ItemStack, ItemStack> modifier : modifiers)
itemStack = modifier.apply(itemStack);
}
return itemStack; return itemStack;
} }
@ -219,14 +227,12 @@ public class ItemBuilder implements ItemProvider {
} }
public ItemBuilder removeLoreLine(int index) { public ItemBuilder removeLoreLine(int index) {
if (lore == null) lore = new ArrayList<>(); if (lore != null) lore.remove(index);
lore.remove(index);
return this; return this;
} }
public ItemBuilder clearLore() { public ItemBuilder clearLore() {
lore.clear(); if (lore != null) lore.clear();
return this; return this;
} }
@ -237,20 +243,18 @@ public class ItemBuilder implements ItemProvider {
public ItemBuilder addItemFlags(@NotNull ItemFlag... itemFlags) { public ItemBuilder addItemFlags(@NotNull ItemFlag... itemFlags) {
if (this.itemFlags == null) this.itemFlags = new ArrayList<>(); if (this.itemFlags == null) this.itemFlags = new ArrayList<>();
this.itemFlags.addAll(Arrays.asList(itemFlags)); this.itemFlags.addAll(Arrays.asList(itemFlags));
return this; return this;
} }
public ItemBuilder removeItemFlags(@NotNull ItemFlag... itemFlags) { public ItemBuilder removeItemFlags(@NotNull ItemFlag... itemFlags) {
if (this.itemFlags == null) this.itemFlags = new ArrayList<>(); if (this.itemFlags != null)
this.itemFlags.removeAll(Arrays.asList(itemFlags));
this.itemFlags.removeAll(Arrays.asList(itemFlags));
return this; return this;
} }
public ItemBuilder clearItemFlags() { public ItemBuilder clearItemFlags() {
itemFlags.clear(); if (itemFlags != null) itemFlags.clear();
return this; return this;
} }
@ -261,22 +265,29 @@ public class ItemBuilder implements ItemProvider {
public ItemBuilder addEnchantment(Enchantment enchantment, int level, boolean ignoreLevelRestriction) { public ItemBuilder addEnchantment(Enchantment enchantment, int level, boolean ignoreLevelRestriction) {
if (enchantments == null) enchantments = new HashMap<>(); if (enchantments == null) enchantments = new HashMap<>();
enchantments.put(enchantment, new Pair<>(level, ignoreLevelRestriction)); enchantments.put(enchantment, new Pair<>(level, ignoreLevelRestriction));
return this; return this;
} }
public ItemBuilder removeEnchantment(Enchantment enchantment) { public ItemBuilder removeEnchantment(Enchantment enchantment) {
if (enchantments == null) enchantments = new HashMap<>(); if (enchantments == null) enchantments = new HashMap<>();
enchantments.remove(enchantment); enchantments.remove(enchantment);
return this; return this;
} }
public ItemBuilder clearEnchantments() { public ItemBuilder clearEnchantments() {
if (enchantments == null) enchantments = new HashMap<>(); if (enchantments != null) enchantments.clear();
return this;
}
enchantments.clear(); public ItemBuilder addModifier(Function<ItemStack, ItemStack> modifier) {
if (modifiers == null) modifiers = new ArrayList<>();
modifiers.add(modifier);
return this;
}
public ItemBuilder clearModifiers() {
if (modifiers != null) modifiers.clear();
return this; return this;
} }
@ -320,6 +331,10 @@ public class ItemBuilder implements ItemProvider {
return gameProfile; return gameProfile;
} }
public List<Function<ItemStack, ItemStack>> getModifiers() {
return modifiers;
}
@Override @Override
public ItemBuilder clone() { public ItemBuilder clone() {
try { try {
@ -328,10 +343,11 @@ public class ItemBuilder implements ItemProvider {
if (lore != null) clone.lore = new ArrayList<>(lore); if (lore != null) clone.lore = new ArrayList<>(lore);
if (itemFlags != null) clone.itemFlags = new ArrayList<>(itemFlags); if (itemFlags != null) clone.itemFlags = new ArrayList<>(itemFlags);
if (enchantments != null) clone.enchantments = new HashMap<>(enchantments); if (enchantments != null) clone.enchantments = new HashMap<>(enchantments);
if (modifiers != null) clone.modifiers = new ArrayList<>(modifiers);
return clone; return clone;
} catch (CloneNotSupportedException e) { } catch (CloneNotSupportedException e) {
throw new Error(e); throw new AssertionError(e);
} }
} }