Improved ItemStackBuilder

This commit is contained in:
Marc Baloup 2022-11-22 14:30:08 +01:00
parent 8755725d51
commit 41878b72f9
Signed by: marcbal
GPG Key ID: BBC0FE3ABC30B893

View File

@ -1,8 +1,10 @@
package fr.pandacube.lib.paper.util; package fr.pandacube.lib.paper.util;
import java.util.Arrays; import java.util.Arrays;
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.stream.Collectors; import java.util.stream.Collectors;
import fr.pandacube.lib.chat.ChatStatic; import fr.pandacube.lib.chat.ChatStatic;
@ -22,6 +24,8 @@ import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.TextDecoration; import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.text.format.TextDecoration.State; import net.kyori.adventure.text.format.TextDecoration.State;
import static fr.pandacube.lib.chat.ChatStatic.chatComponent;
public class ItemStackBuilder { public class ItemStackBuilder {
/** /**
@ -98,10 +102,7 @@ public class ItemStackBuilder {
public ItemStackBuilder displayName(ComponentLike displayName) { public ItemStackBuilder displayName(ComponentLike displayName) {
if (displayName != null) { if (displayName != null) {
Component cmp = displayName.asComponent(); return rawDisplayName(Chat.italicFalseIfNotSet(chatComponent(displayName)).asComponent());
if (cmp.style().decoration(TextDecoration.ITALIC) == State.NOT_SET)
cmp.style().decoration(TextDecoration.ITALIC, State.FALSE);
return rawDisplayName(cmp);
} }
else else
return rawDisplayName(null); return rawDisplayName(null);
@ -116,7 +117,7 @@ public class ItemStackBuilder {
public ItemStackBuilder lore(List<? extends ComponentLike> lore) { public ItemStackBuilder lore(List<? extends ComponentLike> lore) {
if (lore != null) { if (lore != null) {
return rawLore(lore.stream() return rawLore(lore.stream()
.map(line -> Chat.italicFalseIfNotSet(ChatStatic.chatComponent(line)).getAdv()) .map(line -> Chat.italicFalseIfNotSet(chatComponent(line)).getAdv())
.toList()); .toList());
} }
else else
@ -131,7 +132,7 @@ public class ItemStackBuilder {
Streams.concat( Streams.concat(
baseLore.stream(), baseLore.stream(),
lores.stream() lores.stream()
.map(line -> Chat.italicFalseIfNotSet(ChatStatic.chatComponent(line)).getAdv()) .map(line -> Chat.italicFalseIfNotSet(chatComponent(line)).getAdv())
) )
.toList()); .toList());
} }
@ -170,6 +171,24 @@ public class ItemStackBuilder {
return hideEnchants(); return hideEnchants();
} }
public ItemStackBuilder unbreakable() {
getOrInitMeta().setUnbreakable(true);
updateMeta();
return this;
}
public ItemStackBuilder canDestroy(Set<Material> destroyable) {
getOrInitMeta().setCanDestroy(destroyable);
updateMeta();
return this;
}
public ItemStackBuilder canPlaceOn(Set<Material> placeOn) {
getOrInitMeta().setCanPlaceOn(placeOn);
updateMeta();
return this;
}
public ItemStackBuilder damage(int d) { public ItemStackBuilder damage(int d) {
ItemMeta m = getOrInitMeta(); ItemMeta m = getOrInitMeta();
if (m instanceof Damageable) if (m instanceof Damageable)