Skip to content

Commit

Permalink
Add alternate item mark in DefaultItemRendererImpl2
Browse files Browse the repository at this point in the history
  • Loading branch information
new1271 committed Jul 24, 2024
1 parent c45fdd2 commit 400bda4
Showing 1 changed file with 77 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.ricetea.barleyteaapi.internal.item.renderer;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
import org.bukkit.NamespacedKey;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeModifier;
Expand All @@ -23,20 +25,29 @@
import org.ricetea.barleyteaapi.api.item.render.ItemSubRenderer;
import org.ricetea.barleyteaapi.api.item.render.ItemSubRendererSupportingState;
import org.ricetea.barleyteaapi.api.item.render.util.AlternativeItemState;
import org.ricetea.barleyteaapi.api.localization.LocalizationRegister;
import org.ricetea.barleyteaapi.api.localization.LocalizedMessageFormat;
import org.ricetea.barleyteaapi.api.persistence.ExtraPersistentDataType;
import org.ricetea.barleyteaapi.util.NamespacedKeyUtil;
import org.ricetea.utils.Lazy;
import org.ricetea.utils.ObjectUtil;
import org.ricetea.utils.SoftCache;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.inject.Singleton;
import java.text.MessageFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;

@Singleton
@ApiStatus.Internal
public class DefaultItemRendererImpl2 extends AbstractItemRendererImpl {

@Nonnull
public static final String ALTERNATE_ITEM_ID_FORMAT = "barleyteaapi.message.alternate_item_id_format";

@Nonnull
private static final NamespacedKey[] alternateAttributeNameKeys = new NamespacedKey[]{
NamespacedKey.minecraft("default_attack_damage_real_name"),
Expand All @@ -58,9 +69,19 @@ public class DefaultItemRendererImpl2 extends AbstractItemRendererImpl {
"minecraft:default_attack_damage", "minecraft:default_attack_speed"
};

@Nonnull
private static final ThreadLocal<SoftCache<StringBuilder>> localBuilderCache =
ThreadLocal.withInitial(() -> SoftCache.create(StringBuilder::new));

@Nonnull
private static final Map<String, String> literalNamespaceMap = new ConcurrentHashMap<>();

public DefaultItemRendererImpl2() {
super(NamespacedKeyUtil.BarleyTeaAPI("default_item_renderer"));
ObjectUtil.tryCall(() -> ItemRendererRegister.getInstance().register(this));
LocalizedMessageFormat format = LocalizedMessageFormat.create(ALTERNATE_ITEM_ID_FORMAT);
format.setFormat(new MessageFormat("[{0}/{1}]"));
LocalizationRegister.getInstance().register(format);
}

@Nonnull
Expand All @@ -86,9 +107,13 @@ public ItemStack render(@Nonnull ItemStack itemStack, @Nullable Player player) {

CustomItem itemType = CustomItem.get(itemStack);
if (itemType != null) {
List<Component> output = outputLazy.get();

Component defaultNameComponent = ItemHelper.getDefaultNameComponent(itemType);

boolean isRenamed;
if (displayName == null) {
displayName = ItemHelper.getDefaultNameComponent(itemType);
displayName = defaultNameComponent;
isRenamed = false;
} else {
isRenamed = true;
Expand All @@ -101,6 +126,17 @@ public ItemStack render(@Nonnull ItemStack itemStack, @Nullable Player player) {

displayName = rarity.apply(displayName, isRenamed, false);

NamespacedKey itemTypeKey = itemType.getKey();
String namespace = itemTypeKey.getNamespace();
output.add(Component.empty());
output.add(Component.translatable(ALTERNATE_ITEM_ID_FORMAT)
.color(NamedTextColor.DARK_GRAY)
.decoration(TextDecoration.ITALIC, TextDecoration.State.FALSE)
.args(
Component.translatable("namespace." + namespace, getFallbackNamespaceString(namespace)),
defaultNameComponent
));

{
FeatureItemCustomDurability feature = itemType.getFeature(FeatureItemCustomDurability.class);
if (feature != null) {
Expand All @@ -116,7 +152,7 @@ public ItemStack render(@Nonnull ItemStack itemStack, @Nullable Player player) {
{
FeatureItemDisplay feature = itemType.getFeature(FeatureItemDisplay.class);
if (feature != null) {
data = new DataItemDisplay(player, itemStack, displayName, outputLazy.get());
data = new DataItemDisplay(player, itemStack, displayName, output);
ObjectUtil.tryCall(data, feature::handleItemDisplay);
}
}
Expand All @@ -135,10 +171,10 @@ public ItemStack render(@Nonnull ItemStack itemStack, @Nullable Player player) {

if (data != null) {
displayName = data.getDisplayName();
List<Component> output = outputLazy.getUnsafe();
if (output != null) {
meta.lore(output);
}
}
List<Component> output = outputLazy.getUnsafe();
if (output != null) {
meta.lore(output);
}
meta.displayName(displayName);
itemStack.setItemMeta(meta);
Expand Down Expand Up @@ -244,4 +280,39 @@ private void restore0(@Nonnull ItemMeta meta) {
ObjectUtil.tryCallSilently(idKey, container::remove);
}
}

@Nonnull
private static String getFallbackNamespaceString(@Nonnull String namespace) {
return literalNamespaceMap.computeIfAbsent(namespace, DefaultItemRendererImpl2::getFallbackNamespaceStringNoCached);
}

@Nonnull
private static String getFallbackNamespaceStringNoCached(@Nonnull String namespace) {
if (namespace.isEmpty())
return "";
if (namespace.contains("_")) {
String[] paths = namespace.split(Pattern.quote("_"));
StringBuilder builder = localBuilderCache.get().get();
for (String path : paths) {
builder.append(normalizeString(path));
}
String result = builder.toString();
builder.setLength(0);
return result;
} else {
return normalizeString(namespace);
}
}

@Nonnull
private static String normalizeString(@Nonnull String str) {
char c = str.charAt(0);
if (Character.isLowerCase(c)) {
c = Character.toUpperCase(c);
if (str.length() > 1)
return c + str.substring(1);
return Character.toString(c);
}
return str;
}
}

0 comments on commit 400bda4

Please sign in to comment.