Skip to content

Commit

Permalink
Fix #7066: Lazily initalize AEKey display name cache (#7108)
Browse files Browse the repository at this point in the history
  • Loading branch information
Technici4n committed May 15, 2023
1 parent f52a632 commit 275596e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
7 changes: 6 additions & 1 deletion src/main/java/appeng/api/stacks/AEFluidKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -135,6 +135,11 @@ public void addDrops(long amount, List<ItemStack> 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) {
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/appeng/api/stacks/AEItemKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -217,6 +217,11 @@ public void addDrops(long amount, List<ItemStack> drops, Level level, BlockPos p
}
}

@Override
protected Component computeDisplayName() {
return Platform.getItemDisplayName(item, internedTag.tag);
}

@SuppressWarnings("unchecked")
@Override
public boolean isTagged(TagKey<?> tag) {
Expand Down
38 changes: 17 additions & 21 deletions src/main/java/appeng/api/stacks/AEKey.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package appeng.api.stacks;

import java.util.List;
import java.util.Objects;

import javax.annotation.Nullable;

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 275596e

Please sign in to comment.