Added component methods to FastItemStack

This commit is contained in:
Auxilor
2022-04-13 12:36:49 +01:00
parent 234b5fdd8e
commit d19cff9a42
5 changed files with 184 additions and 56 deletions

View File

@@ -1,6 +1,7 @@
package com.willfp.eco.core.fast;
import com.willfp.eco.core.Eco;
import net.kyori.adventure.text.Component;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemFlag;
@@ -64,6 +65,13 @@ public interface FastItemStack {
*/
void setLore(@Nullable List<String> lore);
/**
* Set the item lore.
*
* @param lore The lore.
*/
void setLoreComponents(@Nullable List<Component> lore);
/**
* Get the item lore.
*
@@ -71,6 +79,40 @@ public interface FastItemStack {
*/
List<String> getLore();
/**
* Get the item lore.
*
* @return The lore.
*/
List<Component> getLoreComponents();
/**
* Set the item name.
*
* @param name The name.
*/
void setDisplayName(@Nullable Component name);
/**
* Set the item name.
*
* @param name The name.
*/
void setDisplayName(@Nullable String name);
/**
* Get the item display name.
*
* @return The display name.
*/
Component getDisplayNameComponent();
/**
* Get the item display name.
*
* @return The display name.
*/
String getDisplayName();
/**
* Set the rework penalty.

View File

@@ -1,5 +1,6 @@
package com.willfp.eco.util;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.google.common.collect.ImmutableList;
@@ -76,33 +77,37 @@ public final class StringUtils {
.build(StringUtils::processFormatting);
/**
* Json -> Legacy Cache.
* Json -> Component Cache.
*/
private static final LoadingCache<String, String> JSON_TO_LEGACY = Caffeine.newBuilder()
private static final Cache<String, Component> JSON_TO_COMPONENT = Caffeine.newBuilder()
.expireAfterAccess(10, TimeUnit.SECONDS)
.build(
json -> {
try {
Component component = GSON_COMPONENT_SERIALIZER.deserialize(json);
return LEGACY_COMPONENT_SERIALIZER.serialize(component);
} catch (JsonSyntaxException e) {
return json;
}
}
);
.build();
/**
* Legacy -> Json Cache.
* Component -> Json Cache.
*/
private static final LoadingCache<String, String> LEGACY_TO_JSON = Caffeine.newBuilder()
private static final Cache<Component, String> COMPONENT_TO_JSON = Caffeine.newBuilder()
.expireAfterAccess(10, TimeUnit.SECONDS)
.build(
legacy -> GSON_COMPONENT_SERIALIZER.serialize(
Component.empty().decoration(TextDecoration.ITALIC, false).append(
LEGACY_COMPONENT_SERIALIZER.deserialize(legacy)
)
)
);
.build();
/**
* Legacy -> Component Cache.
*/
private static final Cache<String, Component> LEGACY_TO_COMPONENT = Caffeine.newBuilder()
.expireAfterAccess(10, TimeUnit.SECONDS)
.build();
/**
* Component -> Legacy Cache.
*/
private static final Cache<Component, String> COMPONENT_TO_LEGACY = Caffeine.newBuilder()
.expireAfterAccess(10, TimeUnit.SECONDS)
.build();
/**
* Empty JSON.
*/
private static final String EMPTY_JSON = GSON_COMPONENT_SERIALIZER.serialize(Component.empty());
/**
* Color map.
@@ -483,12 +488,7 @@ public final class StringUtils {
*/
@NotNull
public static String legacyToJson(@Nullable final String legacy) {
String processed = legacy;
if (legacy == null) {
processed = "";
}
return LEGACY_TO_JSON.get(processed);
return componentToJson(toComponent(legacy));
}
/**
@@ -499,11 +499,53 @@ public final class StringUtils {
*/
@NotNull
public static String jsonToLegacy(@Nullable final String json) {
if (json == null || json.isEmpty()) {
return "";
return toLegacy(jsonToComponent(json));
}
/**
* Convert Component to JSON String.
*
* @param component The Component.
* @return The JSON string.
*/
@NotNull
public static String componentToJson(@Nullable final Component component) {
if (component == null) {
return EMPTY_JSON;
}
return JSON_TO_LEGACY.get(json);
return COMPONENT_TO_JSON.get(component, it -> {
try {
return GSON_COMPONENT_SERIALIZER.serialize(
Component.empty().decoration(TextDecoration.ITALIC, false).append(
it
)
);
} catch (JsonSyntaxException e) {
return GSON_COMPONENT_SERIALIZER.serialize(Component.empty());
}
});
}
/**
* Convert JSON String to Component.
*
* @param json The JSON String.
* @return The component.
*/
@NotNull
public static Component jsonToComponent(@Nullable final String json) {
if (json == null || json.isEmpty()) {
return Component.empty();
}
return JSON_TO_COMPONENT.get(json, it -> {
try {
return GSON_COMPONENT_SERIALIZER.deserialize(it);
} catch (JsonSyntaxException e) {
return Component.empty();
}
});
}
/**
@@ -514,12 +556,7 @@ public final class StringUtils {
*/
@NotNull
public static Component toComponent(@Nullable final String legacy) {
String processed = legacy;
if (legacy == null) {
processed = "";
}
return LEGACY_COMPONENT_SERIALIZER.deserialize(processed);
return LEGACY_TO_COMPONENT.get(legacy == null ? "" : legacy, LEGACY_COMPONENT_SERIALIZER::deserialize);
}
/**
@@ -530,7 +567,7 @@ public final class StringUtils {
*/
@NotNull
public static String toLegacy(@NotNull final Component component) {
return LEGACY_COMPONENT_SERIALIZER.serialize(component);
return COMPONENT_TO_LEGACY.get(component, LEGACY_COMPONENT_SERIALIZER::serialize);
}
/**

View File

@@ -11,12 +11,24 @@ import org.bukkit.entity.Player
fun String.toComponent(): Component =
StringUtils.toComponent(this)
/**
* @see StringUtils.jsonToComponent
*/
fun String.jsonToComponent(): Component =
StringUtils.jsonToComponent(this)
/**
* @see StringUtils.toLegacy
*/
fun Component.toLegacy(): String =
StringUtils.toLegacy(this)
/**
* @see StringUtils.componentToJson
*/
fun Component.toJSON(): String =
StringUtils.componentToJson(this)
/**
* @see StringUtils.format
*/