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
*/

View File

@@ -4,9 +4,12 @@ import com.willfp.eco.core.entities.ai.EntityGoal
import com.willfp.eco.core.entities.ai.TargetGoal
import com.willfp.eco.internal.spigot.proxy.common.ai.EntityGoalFactory
import com.willfp.eco.internal.spigot.proxy.common.ai.TargetGoalFactory
import net.minecraft.core.Registry
import net.minecraft.resources.ResourceLocation
import net.minecraft.world.entity.LivingEntity
import net.minecraft.world.entity.PathfinderMob
import net.minecraft.world.item.Item
import org.bukkit.Material
import org.bukkit.NamespacedKey
import org.bukkit.entity.Mob
import org.bukkit.inventory.ItemStack
@@ -39,6 +42,14 @@ fun <T : EntityGoal<*>> T.getVersionSpecificEntityGoalFactory(): EntityGoalFacto
fun <T : TargetGoal<*>> T.getVersionSpecificEntityGoalFactory(): TargetGoalFactory<T>? =
impl.getVersionSpecificTargetGoalFactory(this)
private val MATERIAL_TO_ITEM = mutableMapOf<Material, Item>()
fun Material.toItem(): Item =
MATERIAL_TO_ITEM.getOrPut(this) {
Registry.ITEM.getOptional(this.key.toResourceLocation())
.orElseThrow { IllegalArgumentException("Material is not item!") }
}
interface CommonsProvider {
val nbtTagString: Int

View File

@@ -4,8 +4,12 @@ import com.willfp.eco.core.fast.FastItemStack
import com.willfp.eco.internal.spigot.proxy.common.NBT_TAG_STRING
import com.willfp.eco.internal.spigot.proxy.common.asNMSStack
import com.willfp.eco.internal.spigot.proxy.common.mergeIfNeeded
import com.willfp.eco.internal.spigot.proxy.common.toItem
import com.willfp.eco.util.NamespacedKeyUtils
import com.willfp.eco.util.StringUtils
import com.willfp.eco.util.toComponent
import com.willfp.eco.util.toLegacy
import net.kyori.adventure.text.Component
import net.minecraft.nbt.CompoundTag
import net.minecraft.nbt.ListTag
import net.minecraft.nbt.StringTag
@@ -20,7 +24,6 @@ import kotlin.experimental.and
class EcoFastItemStack(
private val bukkit: org.bukkit.inventory.ItemStack
) : FastItemStack {
private var loreCache: List<String>? = null
private val handle = bukkit.asNMSStack()
override fun getEnchants(checkStored: Boolean): Map<Enchantment, Int> {
@@ -60,13 +63,14 @@ class EcoFastItemStack(
return 0
}
override fun setLore(lore: List<String>?) {
loreCache = null
val jsonLore: MutableList<String> = ArrayList()
override fun setLore(lore: List<String>?) = setLoreComponents(lore?.map { it.toComponent() })
override fun setLoreComponents(lore: List<Component>?) {
val jsonLore = mutableListOf<String>()
if (lore != null) {
for (s in lore) {
jsonLore.add(StringUtils.legacyToJson(s))
jsonLore.add(StringUtils.componentToJson(s))
}
}
@@ -87,17 +91,7 @@ class EcoFastItemStack(
apply()
}
override fun getLore(): List<String> {
if (loreCache != null) {
return loreCache!!
}
val lore = this.getLoreJSON().map { StringUtils.jsonToLegacy(it) }
loreCache = lore
return lore
}
private fun getLoreJSON(): List<String> {
override fun getLoreComponents(): List<Component> {
val displayTag = handle.getTagElement("display") ?: return emptyList()
if (!displayTag.contains("Lore")) {
@@ -105,15 +99,47 @@ class EcoFastItemStack(
}
val loreTag = displayTag.getList("Lore", NBT_TAG_STRING)
val lore = ArrayList<String>(loreTag.size)
val jsonLore = mutableListOf<String>()
for (i in loreTag.indices) {
lore.add(loreTag.getString(i))
jsonLore.add(loreTag.getString(i))
}
return lore
return jsonLore.map { StringUtils.jsonToComponent(it) }
}
override fun getLore(): List<String> =
getLoreComponents().map { StringUtils.toLegacy(it) }
override fun setDisplayName(name: Component?) {
val displayTag = handle.getOrCreateTagElement("display")
displayTag.remove("Name")
if (name != null) {
displayTag.put("Name", StringTag.valueOf(StringUtils.componentToJson(name)))
}
apply()
}
override fun setDisplayName(name: String?) = setDisplayName(name?.toComponent())
override fun getDisplayNameComponent(): Component {
val displayTag =
handle.getTagElement("display") ?: return Component.translatable(bukkit.type.toItem().getDescriptionId())
if (!displayTag.contains("Name")) {
return Component.translatable(bukkit.type.toItem().getDescriptionId())
}
val nameTag = displayTag.getString("Name")
return StringUtils.jsonToComponent(nameTag)
}
override fun getDisplayName(): String = displayNameComponent.toLegacy()
override fun addItemFlags(vararg hideFlags: ItemFlag) {
for (flag in hideFlags) {
this.flagBits = this.flagBits or getBitModifier(flag)