Skip to content

Commit 8a17dec

Browse files
committed
✨ Add methods for item name and lore retrieval as components, improve metadata handling
1 parent 283b4ed commit 8a17dec

File tree

1 file changed

+65
-32
lines changed
  • src/main/java/com/marcusslover/plus/lib/item

1 file changed

+65
-32
lines changed

src/main/java/com/marcusslover/plus/lib/item/Item.java

Lines changed: 65 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,71 @@ private Item(@NotNull Material material, int amount, @Nullable String name, @Nul
163163
});
164164
}
165165

166+
public boolean hasName() {
167+
ItemMeta itemMeta = this.itemStack.getItemMeta();
168+
if (itemMeta == null) {
169+
return false;
170+
}
171+
return itemMeta.hasDisplayName();
172+
}
173+
174+
public @Nullable Text name() {
175+
ItemMeta itemMeta = this.meta();
176+
if (itemMeta == null) {
177+
return null;
178+
}
179+
if (itemMeta.hasDisplayName()) {
180+
Component component = itemMeta.displayName();
181+
if (component == null) {
182+
return null;
183+
}
184+
return Text.of(component);
185+
}
186+
return null;
187+
}
188+
189+
/**
190+
* Retrieves the display name component of the item's metadata if available.
191+
*
192+
* @return the display name as a Component if the item metadata exists and has a display name,
193+
* or null otherwise.
194+
* @since 4.4.0
195+
*/
196+
public @Nullable Component nameComponent() {
197+
ItemMeta itemMeta = this.meta();
198+
if (itemMeta == null) {
199+
return null;
200+
}
201+
if (itemMeta.hasDisplayName()) {
202+
return itemMeta.displayName();
203+
}
204+
return null;
205+
}
206+
207+
public @NotNull List<@NotNull String> lore() {
208+
//noinspection ConstantConditions,deprecation
209+
return this.hasLore() ? ColorUtil.translateList(this.meta().getLore()) : new ArrayList<>();
210+
}
211+
212+
/**
213+
* Retrieves the lore components of the item.
214+
* If the item meta is null or does not contain lore, an empty list is returned.
215+
*
216+
* @return a list of lore {@code Component} objects, or an empty list if no lore is present or item meta is null
217+
* @since 4.4.0
218+
*/
219+
public @Nullable List<Component> loreComponents() {
220+
ItemMeta itemMeta = this.meta();
221+
if (itemMeta == null || !itemMeta.hasLore()) {
222+
return new ArrayList<>();
223+
}
224+
return itemMeta.lore();
225+
}
226+
227+
public boolean hasLore() {
228+
return this.itemStack.hasItemMeta() && this.itemStack.getItemMeta().hasLore();
229+
}
230+
166231
public boolean isEmpty() {
167232
return !this.isValid();
168233
}
@@ -674,38 +739,6 @@ public boolean hasCustomModelData() {
674739
return this.meta(itemMeta -> itemMeta.setCustomModelData(customModelData));
675740
}
676741

677-
public boolean hasName() {
678-
ItemMeta itemMeta = this.itemStack.getItemMeta();
679-
if (itemMeta == null) {
680-
return false;
681-
}
682-
return itemMeta.hasDisplayName();
683-
}
684-
685-
public @Nullable Text name() {
686-
ItemMeta itemMeta = this.meta();
687-
if (itemMeta == null) {
688-
return null;
689-
}
690-
if (itemMeta.hasDisplayName()) {
691-
Component component = itemMeta.displayName();
692-
if (component == null) {
693-
return null;
694-
}
695-
return Text.of(component);
696-
}
697-
return null;
698-
}
699-
700-
public @NotNull List<@NotNull String> lore() {
701-
//noinspection ConstantConditions,deprecation
702-
return this.hasLore() ? ColorUtil.translateList(this.meta().getLore()) : new ArrayList<>();
703-
}
704-
705-
public boolean hasLore() {
706-
return this.itemStack.hasItemMeta() && this.itemStack.getItemMeta().hasLore();
707-
}
708-
709742
public @NotNull Item skull(@Nullable PlayerProfile playerProfile) {
710743
return this.meta(itemMeta -> {
711744
if (itemMeta instanceof SkullMeta skullMeta) {

0 commit comments

Comments
 (0)