Improved ItemStackBuilder

This commit is contained in:
Marc Baloup 2022-12-27 01:17:26 +01:00
parent e43fc292ba
commit a6dde5b6a9

View File

@ -5,9 +5,11 @@ import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import fr.pandacube.lib.chat.ChatStatic; import fr.pandacube.lib.chat.ChatStatic;
import fr.pandacube.lib.util.Log;
import net.kyori.adventure.text.ComponentLike; import net.kyori.adventure.text.ComponentLike;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
@ -89,15 +91,30 @@ public class ItemStackBuilder {
stack.setItemMeta(cachedMeta); stack.setItemMeta(cachedMeta);
} }
public ItemStackBuilder meta(Consumer<ItemMeta> metaUpdater) {
metaUpdater.accept(getOrInitMeta());
updateMeta();
return this;
}
public <T extends ItemMeta> ItemStackBuilder meta(Consumer<T> metaUpdater, Class<T> metaType) {
ItemMeta m = getOrInitMeta();
if (!metaType.isInstance(m)) {
Log.warning("Item meta of " + stack.getType() + " is not of type " + metaType.getSimpleName(), new Throwable());
return this;
}
metaUpdater.accept(metaType.cast(m));
updateMeta();
return this;
}
public ItemStackBuilder amount(int a) { public ItemStackBuilder amount(int a) {
stack.setAmount(a); stack.setAmount(a);
return this; return this;
} }
public ItemStackBuilder rawDisplayName(Component displayName) { public ItemStackBuilder rawDisplayName(Component displayName) {
getOrInitMeta().displayName(displayName); return meta(m -> m.displayName(displayName));
updateMeta();
return this;
} }
public ItemStackBuilder displayName(ComponentLike displayName) { public ItemStackBuilder displayName(ComponentLike displayName) {
@ -109,9 +126,7 @@ public class ItemStackBuilder {
} }
public ItemStackBuilder rawLore(List<Component> lore) { public ItemStackBuilder rawLore(List<Component> lore) {
getOrInitMeta().lore(lore); return meta(m -> m.lore(lore));
updateMeta();
return this;
} }
public ItemStackBuilder lore(List<? extends ComponentLike> lore) { public ItemStackBuilder lore(List<? extends ComponentLike> lore) {
@ -147,15 +162,11 @@ public class ItemStackBuilder {
} }
public ItemStackBuilder enchant(Enchantment ench, int level) { public ItemStackBuilder enchant(Enchantment ench, int level) {
getOrInitMeta().addEnchant(ench, level, true); return meta(m -> m.addEnchant(ench, level, true));
updateMeta();
return this;
} }
public ItemStackBuilder flags(ItemFlag... flags) { public ItemStackBuilder flags(ItemFlag... flags) {
getOrInitMeta().addItemFlags(flags); return meta(m -> m.addItemFlags(flags));
updateMeta();
return this;
} }
public ItemStackBuilder hideEnchants() { public ItemStackBuilder hideEnchants() {
@ -172,29 +183,19 @@ public class ItemStackBuilder {
} }
public ItemStackBuilder unbreakable() { public ItemStackBuilder unbreakable() {
getOrInitMeta().setUnbreakable(true); return meta(m -> m.setUnbreakable(true));
updateMeta();
return this;
} }
public ItemStackBuilder canDestroy(Set<Material> destroyable) { public ItemStackBuilder canDestroy(Set<Material> destroyable) {
getOrInitMeta().setCanDestroy(destroyable); return meta(m -> m.setCanDestroy(destroyable));
updateMeta();
return this;
} }
public ItemStackBuilder canPlaceOn(Set<Material> placeOn) { public ItemStackBuilder canPlaceOn(Set<Material> placeOn) {
getOrInitMeta().setCanPlaceOn(placeOn); return meta(m -> m.setCanPlaceOn(placeOn));
updateMeta();
return this;
} }
public ItemStackBuilder damage(int d) { public ItemStackBuilder damage(int d) {
ItemMeta m = getOrInitMeta(); return meta(m -> m.setDamage(d), Damageable.class);
if (m instanceof Damageable)
((Damageable)m).setDamage(d);
updateMeta();
return this;
} }