diff --git a/src/main/java/appeng/api/stacks/AEFluidKey.java b/src/main/java/appeng/api/stacks/AEFluidKey.java index 8a507fdb761..87501e42b28 100644 --- a/src/main/java/appeng/api/stacks/AEFluidKey.java +++ b/src/main/java/appeng/api/stacks/AEFluidKey.java @@ -11,6 +11,7 @@ import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.nbt.CompoundTag; import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; import net.minecraft.tags.TagKey; import net.minecraft.world.item.ItemStack; @@ -31,7 +32,6 @@ public final class AEFluidKey extends AEKey { private final int hashCode; private AEFluidKey(Fluid fluid, @Nullable CompoundTag tag) { - super(Platform.getFluidDisplayName(fluid, tag)); this.fluid = fluid; this.tag = tag; this.hashCode = Objects.hash(fluid, tag); @@ -135,6 +135,11 @@ public void addDrops(long amount, List drops, Level level, BlockPos p // Fluids are voided } + @Override + protected Component computeDisplayName() { + return Platform.getFluidDisplayName(fluid, tag); + } + @SuppressWarnings("unchecked") @Override public boolean isTagged(TagKey tag) { diff --git a/src/main/java/appeng/api/stacks/AEItemKey.java b/src/main/java/appeng/api/stacks/AEItemKey.java index 451647255f2..78ffba09739 100644 --- a/src/main/java/appeng/api/stacks/AEItemKey.java +++ b/src/main/java/appeng/api/stacks/AEItemKey.java @@ -13,6 +13,7 @@ import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.NumericTag; import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; import net.minecraft.tags.TagKey; import net.minecraft.world.item.Item; @@ -31,7 +32,6 @@ public final class AEItemKey extends AEKey { private final int cachedDamage; private AEItemKey(Item item, InternedTag internedTag) { - super(Platform.getItemDisplayName(item, internedTag.tag)); this.item = item; this.internedTag = internedTag; this.hashCode = item.hashCode() * 31 + internedTag.hashCode; @@ -217,6 +217,11 @@ public void addDrops(long amount, List drops, Level level, BlockPos p } } + @Override + protected Component computeDisplayName() { + return Platform.getItemDisplayName(item, internedTag.tag); + } + @SuppressWarnings("unchecked") @Override public boolean isTagged(TagKey tag) { diff --git a/src/main/java/appeng/api/stacks/AEKey.java b/src/main/java/appeng/api/stacks/AEKey.java index c2c3589b100..65df343bc3e 100644 --- a/src/main/java/appeng/api/stacks/AEKey.java +++ b/src/main/java/appeng/api/stacks/AEKey.java @@ -1,7 +1,6 @@ package appeng.api.stacks; import java.util.List; -import java.util.Objects; import javax.annotation.Nullable; @@ -36,25 +35,10 @@ public abstract class AEKey { /** - * The display name, which is used to sort by name in client terminal + * The display name, which is used to sort by name in client terminal. Lazily initialized to avoid unnecessary work + * on the server. Volatile ensures that this cache is thread-safe (but can be initialized multiple times). */ - private final Component displayName; - - /** - * @deprecated It has been deprecated because we should cache the display name in the initialization to speed up the - * sorting process. - */ - @Deprecated - public AEKey() { - this.displayName = null; - } - - /** - * @param displayName the display name, which is used to sort by name in client terminal - */ - public AEKey(Component displayName) { - this.displayName = displayName; - } + private volatile Component cachedDisplayName; @Nullable public static AEKey fromTagGeneric(CompoundTag tag) { @@ -264,10 +248,22 @@ public final boolean supportsFuzzyRangeSearch() { return getType().supportsFuzzyRangeSearch(); } - public Component getDisplayName() { - return Objects.requireNonNull(this.displayName); + public final Component getDisplayName() { + var ret = cachedDisplayName; + + if (ret == null) { + cachedDisplayName = ret = computeDisplayName(); + } + + return ret; } + /** + * Compute the display name, which is used to sort by name in client terminal. Will be cached by + * {@link #getDisplayName()}. + */ + protected abstract Component computeDisplayName(); + /** * Adds the drops if the container holding this key is broken, such as an interface holding stacks. Item stacks * should be placed in the list and not spawned directly into the world