mirror of
https://github.com/Minestom/Minestom.git
synced 2024-12-26 11:07:53 +01:00
Improve item lore
Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
parent
6e185c718e
commit
6340a10331
@ -78,7 +78,7 @@ public abstract class ItemMetaBuilder implements TagWritable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Contract("_ -> this")
|
@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);
|
this.lore = new ArrayList<>(lore);
|
||||||
handleCompound("display", nbtCompound -> {
|
handleCompound("display", nbtCompound -> {
|
||||||
final NBTList<NBTString> loreNBT = NBT.List(NBTType.TAG_String,
|
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)))
|
.map(line -> new NBTString(GsonComponentSerializer.gson().serialize(line)))
|
||||||
.toList()
|
.toList()
|
||||||
);
|
);
|
||||||
nbtCompound.set("Lore", loreNBT);
|
if (loreNBT.isEmpty()) {
|
||||||
|
nbtCompound.remove("Lore");
|
||||||
|
} else {
|
||||||
|
nbtCompound.set("Lore", loreNBT);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
@ -168,7 +168,7 @@ public final class ItemStack implements TagReadable, HoverEventSource<HoverEvent
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Contract(value = "_, -> new", pure = true)
|
@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();
|
return builder().lore(lore).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,7 +95,7 @@ public final class ItemStackBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Contract(value = "_ -> this")
|
@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);
|
this.metaBuilder.lore(lore);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
@ -2,9 +2,13 @@ package net.minestom.server.item;
|
|||||||
|
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
import net.kyori.adventure.text.format.NamedTextColor;
|
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 org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
@ -108,6 +112,47 @@ public class ItemTest {
|
|||||||
assertTrue(enchantments.isEmpty());
|
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
|
@Test
|
||||||
public void testBuilderReuse() {
|
public void testBuilderReuse() {
|
||||||
var builder = ItemStack.builder(Material.DIAMOND);
|
var builder = ItemStack.builder(Material.DIAMOND);
|
||||||
|
Loading…
Reference in New Issue
Block a user