Update everything around chat component + some other improvement

This commit is contained in:
2021-07-11 16:14:47 +02:00
parent 304faa072e
commit f4642e22a1
7 changed files with 228 additions and 74 deletions

View File

@@ -34,6 +34,10 @@
<groupId>net.kyori</groupId>
<artifactId>adventure-api</artifactId>
</exclusion>
<exclusion>
<groupId>net.kyori</groupId>
<artifactId>adventure-text-serializer-plain</artifactId>
</exclusion>
</exclusions>
</dependency>

View File

@@ -1,5 +1,6 @@
package fr.pandacube.lib.paper.util;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
@@ -21,12 +22,41 @@ import net.kyori.adventure.text.format.TextDecoration.State;
public class ItemStackBuilder {
/**
* Create a builder with a clone of the provided ItemStack.
*
* The returned builder will not alter the provided ItemStack.
* IF you want to modify the ItemStack with the builder, please use {@link #wrap(ItemStack)}.
*
* @param base the original ItemStack.
* @return the builder
*/
public static ItemStackBuilder of(ItemStack base) {
return new ItemStackBuilder(base.clone());
return wrap(base.clone());
}
/**
* Create a builder of a new ItemStack with the specified Material.
*
* @param mat the material of the new builded ItemStack
* @return the builder
*/
public static ItemStackBuilder of(Material mat) {
return new ItemStackBuilder(new ItemStack(mat));
return wrap(new ItemStack(mat));
}
/**
* Create a builder that will alter the data of the provided ItemStack.
*
* The {@link #build()} method of thez returned builder will return the same instance
* of ItemStack as the parameter of this method.
*
* To create a builder that doesnt modify the provided ItemStack, use {@link #of(ItemStack)}.
* @param stack
* @return the builder
*/
public static ItemStackBuilder wrap(ItemStack stack) {
return new ItemStackBuilder(stack);
}
@@ -95,14 +125,14 @@ public class ItemStackBuilder {
return rawLore(Collections.emptyList());
}
public ItemStackBuilder addLoreAfter(List<Chat> lore) {
if (lore != null) {
public ItemStackBuilder addLoreAfter(List<Chat> lores) {
if (lores != null) {
List<Component> baseLore = getOrInitMeta().lore();
if (baseLore == null) baseLore = Collections.emptyList();
return rawLore(
Streams.concat(
baseLore.stream(),
lore.stream()
lores.stream()
.map(line -> Chat.italicFalseIfNotSet(line).getAdv())
)
.collect(Collectors.toList()));
@@ -111,6 +141,12 @@ public class ItemStackBuilder {
return this;
}
public ItemStackBuilder addLoreAfter(Chat... lores) {
if (lores == null || lores.length == 0)
return this;
return addLoreAfter(Arrays.asList(lores));
}
public ItemStackBuilder enchant(Enchantment ench, int level) {
getOrInitMeta().addEnchant(ench, level, true);
updateMeta();