Skip to content

Commit

Permalink
Add appendItemLore to AdventureUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
ceze88 committed Aug 21, 2024
1 parent bbeb958 commit d2766ca
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions Core/src/main/java/com/craftaro/core/chat/AdventureUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
Expand All @@ -33,7 +34,8 @@

public class AdventureUtils {
private static Method displayNameMethod = null;
private static Method loreMethod = null;
private static Method setLoreMethod = null;
private static Method getLoreMethod = null;
private static Object gsonComponentSerializer;
private static Method gsonDeserializeMethod;

Expand All @@ -42,7 +44,8 @@ public class AdventureUtils {
try {
Class<?> componentClass = Class.forName("net;kyori;adventure;text;Component".replace(";", "."));
displayNameMethod = ItemMeta.class.getDeclaredMethod("displayName", componentClass);
loreMethod = ItemMeta.class.getDeclaredMethod("lore", List.class);
setLoreMethod = ItemMeta.class.getDeclaredMethod("lore", List.class);
setLoreMethod = ItemMeta.class.getDeclaredMethod("lore");
gsonComponentSerializer = Class.forName("net;kyori;adventure;text;serializer;gson;GsonComponentSerializer".replace(";", ".")).getDeclaredMethod("gson").invoke(null);
gsonDeserializeMethod = gsonComponentSerializer.getClass().getDeclaredMethod("deserialize", String.class);
gsonDeserializeMethod.setAccessible(true);
Expand Down Expand Up @@ -172,8 +175,27 @@ public static void formatItemLore(ItemStack item, Component... lore) {
setItemLore(item, lore);
}

public static void appendItemLore(ItemStack item, List<Component> lore) {
List<Component> currentLore = new ArrayList<>();
ItemMeta meta = item.getItemMeta();
if (meta != null && meta.hasLore()) {
if (isMiniMessageEnabled()) {
try {
currentLore = (List<Component>) getLoreMethod.invoke(meta);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
} else {
currentLore = formatComponent(meta.getLore());
}
}

currentLore.addAll(lore);
setItemLore(item, currentLore.toArray(new Component[0]));
}

public static boolean isMiniMessageEnabled() {
return ServerProject.isServer(ServerProject.PAPER) && ServerVersion.isServerVersionAtLeast(ServerVersion.V1_16) && displayNameMethod != null && loreMethod != null;
return ServerProject.isServer(ServerProject.PAPER) && ServerVersion.isServerVersionAtLeast(ServerVersion.V1_16) && displayNameMethod != null && setLoreMethod != null;
}

private static void setItemName(ItemStack item, Component name) {
Expand Down Expand Up @@ -209,7 +231,7 @@ private static void setItemLore(ItemStack item, Component... lore) {
if (isMiniMessageEnabled()) {
//Set lore as component
try {
loreMethod.invoke(meta, convertToOriginalComponent(lore));
setLoreMethod.invoke(meta, convertToOriginalComponent(lore));
item.setItemMeta(meta);
return;
} catch (Exception ignored) {
Expand Down Expand Up @@ -262,7 +284,6 @@ public static List<Component> formatComponent(String... list) {
return result;
}


public static List<Component> formatComponent(List<String> list, MiniMessagePlaceholder... placeholders) {
List<Component> result = new ArrayList<>();
for (String line : list) {
Expand Down

0 comments on commit d2766ca

Please sign in to comment.