Improve item lore

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2022-01-22 23:22:00 +01:00
parent 6e185c718e
commit 6340a10331
4 changed files with 53 additions and 4 deletions

View File

@ -78,7 +78,7 @@ public abstract class ItemMetaBuilder implements TagWritable {
}
@Contract("_ -> this")
public @NotNull ItemMetaBuilder lore(@NotNull List<@NotNull Component> lore) {
public @NotNull ItemMetaBuilder lore(@NotNull List<? extends Component> lore) {
this.lore = new ArrayList<>(lore);
handleCompound("display", nbtCompound -> {
final NBTList<NBTString> loreNBT = NBT.List(NBTType.TAG_String,
@ -86,7 +86,11 @@ public abstract class ItemMetaBuilder implements TagWritable {
.map(line -> new NBTString(GsonComponentSerializer.gson().serialize(line)))
.toList()
);
nbtCompound.set("Lore", loreNBT);
if (loreNBT.isEmpty()) {
nbtCompound.remove("Lore");
} else {
nbtCompound.set("Lore", loreNBT);
}
});
return this;
}

View File

@ -168,7 +168,7 @@ public final class ItemStack implements TagReadable, HoverEventSource<HoverEvent
}
@Contract(value = "_, -> new", pure = true)
public @NotNull ItemStack withLore(@NotNull List<@NotNull Component> lore) {
public @NotNull ItemStack withLore(@NotNull List<? extends Component> lore) {
return builder().lore(lore).build();
}

View File

@ -95,7 +95,7 @@ public final class ItemStackBuilder {
}
@Contract(value = "_ -> this")
public @NotNull ItemStackBuilder lore(@NotNull List<@NotNull Component> lore) {
public @NotNull ItemStackBuilder lore(@NotNull List<? extends Component> lore) {
this.metaBuilder.lore(lore);
return this;
}

View File

@ -2,9 +2,13 @@ package net.minestom.server.item;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import org.jglrxavpok.hephaistos.nbt.NBTString;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.*;
@ -108,6 +112,47 @@ public class ItemTest {
assertTrue(enchantments.isEmpty());
}
@Test
public void testLore() {
var item = ItemStack.of(Material.DIAMOND_SWORD);
assertEquals(List.of(), item.getLore());
assertNull(item.getMeta().toNBT().get("display"));
{
var lore = List.of(Component.text("Hello"));
item = item.withLore(lore);
assertEquals(lore, item.getLore());
var loreNbt = item.getMeta().toNBT().getCompound("display").<NBTString>getList("Lore");
assertNotNull(loreNbt);
assertEquals(loreNbt.getSize(), 1);
assertEquals(lore, loreNbt.asListView().stream().map(line -> GsonComponentSerializer.gson().deserialize(line.getValue())).toList());
}
{
var lore = List.of(Component.text("Hello"), Component.text("World"));
item = item.withLore(lore);
assertEquals(lore, item.getLore());
var loreNbt = item.getMeta().toNBT().getCompound("display").<NBTString>getList("Lore");
assertNotNull(loreNbt);
assertEquals(loreNbt.getSize(), 2);
assertEquals(lore, loreNbt.asListView().stream().map(line -> GsonComponentSerializer.gson().deserialize(line.getValue())).toList());
}
{
var lore = Stream.of("string test").map(Component::text).toList();
item = item.withLore(lore);
assertEquals(lore, item.getLore());
var loreNbt = item.getMeta().toNBT().getCompound("display").<NBTString>getList("Lore");
assertNotNull(loreNbt);
assertEquals(loreNbt.getSize(), 1);
assertEquals(lore, loreNbt.asListView().stream().map(line -> GsonComponentSerializer.gson().deserialize(line.getValue())).toList());
}
// Ensure that lore can be properly removed without residual (display compound)
item = item.withLore(List.of());
assertNull(item.getMeta().toNBT().get("display"));
}
@Test
public void testBuilderReuse() {
var builder = ItemStack.builder(Material.DIAMOND);