Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ dependencies {

compileOnly("org.jetbrains:annotations:21.0.1")

val adventureVersion = "4.22.0"
val adventureVersion = "4.26.1"
api("net.kyori:adventure-api:$adventureVersion")
api("net.kyori:adventure-text-serializer-legacy:$adventureVersion")
api("net.kyori:adventure-text-serializer-gson:$adventureVersion")
Expand All @@ -41,8 +41,8 @@ publishing {
maven {
name = "triumph"
credentials {
username = providers.gradleProperty("triumph.repo.user").get()
password = providers.gradleProperty("triumph.repo.token").get()
username = providers.gradleProperty("triumph.repo.user").orNull
password = providers.gradleProperty("triumph.repo.token").orNull
}

url = uri("https://repo.triumphteam.dev/snapshots/")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@
import dev.triumphteam.gui.components.util.Legacy;
import dev.triumphteam.gui.components.util.VersionHelper;
import dev.triumphteam.gui.guis.GuiItem;
import net.kyori.adventure.platform.bukkit.MinecraftComponentSerializer;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import org.bukkit.Bukkit;
import org.bukkit.Color;
import org.bukkit.Material;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@
import dev.triumphteam.gui.components.exception.GuiException;
import dev.triumphteam.gui.components.util.Legacy;
import dev.triumphteam.gui.components.util.VersionHelper;
import net.kyori.adventure.platform.bukkit.MinecraftComponentSerializer;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
Expand All @@ -44,6 +46,8 @@ public final class BukkitNameLoreHandler implements NameLoreHandler {

private static final Field DISPLAY_NAME_FIELD;
private static final Field LORE_FIELD;
private static final Method COMPONENT_FROM_JSON;
private static final Method COMPONENT_TO_JSON;

private static final BukkitNameLoreHandler INSTANCE = new BukkitNameLoreHandler();

Expand All @@ -56,6 +60,17 @@ public final class BukkitNameLoreHandler implements NameLoreHandler {

LORE_FIELD = metaClass.getDeclaredField("lore");
LORE_FIELD.setAccessible(true);

if (VersionHelper.IS_ITEM_NAME_COMPONENT) {
final Class<?> componentClass = DISPLAY_NAME_FIELD.getType();
final Class<?> craftChatMessageClass = VersionHelper.craftClass("util.CraftChatMessage");

COMPONENT_FROM_JSON = findComponentFromJson(craftChatMessageClass, componentClass);
COMPONENT_TO_JSON = findComponentToJson(craftChatMessageClass, componentClass);
} else {
COMPONENT_FROM_JSON = null;
COMPONENT_TO_JSON = null;
}
} catch (NoSuchFieldException | ClassNotFoundException exception) {
throw new GuiException("Could not retrieve displayName nor lore field for ItemBuilder.", exception);
}
Expand Down Expand Up @@ -119,32 +134,71 @@ public void lore(final @NotNull ItemMeta itemMeta, final @NotNull Consumer<List<
}

/**
* Serializes the component with the right {@link net.kyori.adventure.text.serializer.ComponentSerializer} for the current MC version
* Serializes a component into the representation used by the current Minecraft version.
*
* @param component component to serialize
* @return the serialized representation of the component
*/
private @NotNull Object serializeComponent(@NotNull final Component component) {
if (VersionHelper.IS_ITEM_NAME_COMPONENT) {
//noinspection UnstableApiUsage
return MinecraftComponentSerializer.get().serialize(component);
return invokeComponentFromJson(GsonComponentSerializer.gson().serialize(component));
} else {
return GsonComponentSerializer.gson().serialize(component);
}
}

/**
* Deserializes the object with the right {@link net.kyori.adventure.text.serializer.ComponentSerializer} for the current MC version
* Deserializes a component from the representation used by the current Minecraft version.
*
* @param obj object to deserialize
* @return the component
*/
private @NotNull Component deserializeComponent(@NotNull final Object obj) {
if (VersionHelper.IS_ITEM_NAME_COMPONENT) {
//noinspection UnstableApiUsage
return MinecraftComponentSerializer.get().deserialize(obj);
return GsonComponentSerializer.gson().deserialize(invokeComponentToJson(obj));
} else {
return GsonComponentSerializer.gson().deserialize((String) obj);
}
}

private static @NotNull Method findComponentFromJson(final @NotNull Class<?> craftChatMessageClass, final @NotNull Class<?> componentClass) {
return findComponentMethod(craftChatMessageClass, "fromJSON", componentClass, String.class);
}

private static @NotNull Method findComponentToJson(final @NotNull Class<?> craftChatMessageClass, final @NotNull Class<?> componentClass) {
return findComponentMethod(craftChatMessageClass, "fromComponent", String.class, componentClass);
}

private static @NotNull Method findComponentMethod(
final @NotNull Class<?> craftChatMessageClass,
final @NotNull String name,
final @NotNull Class<?> returnType,
final @NotNull Class<?> parameterType
) {
try {
final Method method = craftChatMessageClass.getMethod(name, parameterType);
if (!Modifier.isStatic(method.getModifiers()) || !returnType.isAssignableFrom(method.getReturnType())) {
throw new NoSuchMethodException(name + " has an incompatible signature");
}
return method;
} catch (NoSuchMethodException exception) {
throw new GuiException("Could not retrieve " + name + " method for ItemBuilder.", exception);
}
}

private static @NotNull Object invokeComponentFromJson(final @NotNull String json) {
try {
return COMPONENT_FROM_JSON.invoke(null, json);
} catch (IllegalAccessException | InvocationTargetException exception) {
throw new GuiException("Could not serialize component for ItemBuilder.", exception);
}
}

private static @NotNull String invokeComponentToJson(final @NotNull Object component) {
try {
return (String) COMPONENT_TO_JSON.invoke(null, component);
} catch (IllegalAccessException | InvocationTargetException exception) {
throw new GuiException("Could not deserialize component for ItemBuilder.", exception);
}
}
}
8 changes: 4 additions & 4 deletions core/src/main/java/dev/triumphteam/gui/guis/StorageGui.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/**
* MIT License
* <p>
*
* Copyright (c) 2021 TriumphTeam
* <p>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* <p>
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand Down