Skip to content

Commit 6a3dbe5

Browse files
author
Circulate233
committed
调整了一下工具类的结构
1 parent 530ae81 commit 6a3dbe5

1 file changed

Lines changed: 115 additions & 46 deletions

File tree

Lines changed: 115 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,132 @@
11
package com.circulation.random_complement.common.util;
22

3-
import appeng.api.storage.data.IAEItemStack;
4-
import com.github.bsideup.jabel.Desugar;
5-
import com.google.common.cache.CacheBuilder;
6-
import com.google.common.cache.CacheLoader;
7-
import com.google.common.cache.LoadingCache;
3+
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
4+
import lombok.Getter;
5+
import lombok.ToString;
6+
import net.minecraft.item.Item;
87
import net.minecraft.item.ItemStack;
9-
import org.jetbrains.annotations.NotNull;
10-
11-
import java.util.concurrent.ExecutionException;
12-
13-
@Desugar
14-
public record SimpleItem(@NotNull String str) {
15-
public static final SimpleItem empty = new SimpleItem("e");
16-
17-
private static final LoadingCache<String, SimpleItem> CRAFTABLE_ITEM_POOL =
18-
CacheBuilder.newBuilder()
19-
.maximumSize(10000)
20-
.weakValues()
21-
.build(new CacheLoader<>() {
22-
@Override
23-
public SimpleItem load(@NotNull String str) {
24-
return new SimpleItem(str);
25-
}
26-
});
27-
28-
public static SimpleItem getInstance(@NotNull ItemStack itemStack) {
29-
try {
30-
if (itemStack == null || itemStack.isEmpty()) return empty;
31-
var key = new StringBuilder(itemStack.getItem().getRegistryName().toString()).append(itemStack.getItemDamage());
32-
if (itemStack.hasTagCompound()) {
33-
key.append(itemStack.getTagCompound().hashCode());
34-
}
35-
return CRAFTABLE_ITEM_POOL.get(key.toString());
36-
} catch (ExecutionException e) {
37-
return empty;
8+
import net.minecraft.nbt.NBTTagCompound;
9+
import net.minecraft.util.ResourceLocation;
10+
11+
import java.util.Map;
12+
import java.util.Objects;
13+
import java.util.concurrent.ConcurrentHashMap;
14+
15+
@ToString
16+
public final class SimpleItem {
17+
18+
public static final SimpleItem empty = new SimpleItem(ItemStack.EMPTY);
19+
private static final Map<ResourceLocation, CachedMetaItems> noNbtCache = new ConcurrentHashMap<>();
20+
private final ResourceLocation item;
21+
@Getter
22+
private final int meta;
23+
private final int hashCode;
24+
private final NBTTagCompound nbt;
25+
26+
private SimpleItem(ResourceLocation item, int meta, NBTTagCompound nbt) {
27+
this.item = item;
28+
this.meta = meta;
29+
this.nbt = nbt;
30+
this.hashCode = computeHash(item, meta, nbt);
31+
}
32+
33+
private SimpleItem(ItemStack stack) {
34+
this(stack.getItem().getRegistryName(), stack.getItemDamage(), copyNBT(stack.getTagCompound()));
35+
}
36+
37+
public static SimpleItem getInstance(final String rl, final int meta) {
38+
return getInstance(new ResourceLocation(rl), meta);
39+
}
40+
41+
public static SimpleItem getInstance(final ResourceLocation rl, final int meta) {
42+
return noNbtCache.computeIfAbsent(rl, CachedMetaItems::new)
43+
.computeIfAbsent(meta);
44+
}
45+
46+
public static SimpleItem getInstance(final ItemStack stack) {
47+
if (stack == null || stack.isEmpty()) return empty;
48+
var nbt = stack.getTagCompound();
49+
if (nbt == null || nbt.isEmpty()) {
50+
return getNoNBTInstance(stack);
51+
}
52+
return new SimpleItem(stack);
53+
}
54+
55+
public static SimpleItem getNoNBTInstance(final ItemStack stack) {
56+
if (stack.isEmpty()) return empty;
57+
return getInstance(stack.getItem().getRegistryName(), stack.getItemDamage());
58+
}
59+
60+
private static NBTTagCompound copyNBT(NBTTagCompound nbt) {
61+
if (nbt == null || nbt.isEmpty()) {
62+
return null;
3863
}
64+
return nbt.copy();
3965
}
4066

41-
public static SimpleItem getInstance(@NotNull IAEItemStack itemStack) {
42-
return getInstance(itemStack.getDefinition());
67+
private static int computeHash(ResourceLocation item, int meta, NBTTagCompound nbt) {
68+
int result = item == null ? 0 : item.hashCode();
69+
result = 31 * result + meta;
70+
result = 31 * result + (nbt == null ? 0 : nbt.hashCode());
71+
return result;
72+
}
73+
74+
public Item getItem() {
75+
return Item.REGISTRY.getObject(item);
76+
}
77+
78+
public ResourceLocation getRegistryName() {
79+
return item;
80+
}
81+
82+
public String getItemID() {
83+
return item.toString();
84+
}
85+
86+
public ItemStack getItemStack(int amount) {
87+
var i = new ItemStack(getItem(), amount, meta);
88+
if (nbt != null && !nbt.isEmpty()) {
89+
i.setTagCompound(nbt.copy());
90+
}
91+
return i;
4392
}
4493

4594
public boolean isEmpty() {
46-
return empty.equals(this);
95+
return this == empty;
4796
}
4897

4998
@Override
50-
public boolean equals(Object obj) {
51-
if (obj == this) return true;
52-
if (obj instanceof SimpleItem si) {
53-
return this.str.equals(si.str);
54-
}
55-
return false;
99+
public boolean equals(Object o) {
100+
if (o == null || getClass() != o.getClass()) return false;
101+
SimpleItem that = (SimpleItem) o;
102+
return meta == that.meta && Objects.equals(item, that.item) && Objects.equals(nbt, that.nbt);
56103
}
57104

58105
@Override
59-
public @NotNull String toString() {
60-
return this.str;
106+
public int hashCode() {
107+
return hashCode;
61108
}
62109

63-
}
110+
private static class CachedMetaItems extends Int2ObjectOpenHashMap<SimpleItem> {
111+
private final ResourceLocation item;
112+
113+
private CachedMetaItems(ResourceLocation item) {
114+
this.item = item;
115+
}
116+
117+
public SimpleItem computeIfAbsent(int key) {
118+
SimpleItem v;
119+
if ((v = get(key)) == null) {
120+
synchronized (this) {
121+
if ((v = get(key)) == null) {
122+
v = new SimpleItem(item, key, null);
123+
put(key, v);
124+
}
125+
}
126+
}
127+
128+
return v;
129+
}
130+
131+
}
132+
}

0 commit comments

Comments
 (0)