Skip to content

Commit 4e4b37c

Browse files
committed
修复bug
1 parent e9d365f commit 4e4b37c

5 files changed

Lines changed: 90 additions & 50 deletions

File tree

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ show_testing_output = false
1515

1616
# Mod Information
1717
# HIGHLY RECOMMEND complying with SemVer for mod_version: https://semver.org/
18-
mod_version = 1.2.2
18+
mod_version = 1.2.3
1919
root_package = com.circulation
2020
mod_id = only_one_item
2121
mod_name = OnlyOneItem

src/main/java/com/circulation/only_one_item/crt/CrtConversionItemTarget.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public CrtConversionItemTarget(String id, int meta) {
3636
@ZenMethod
3737
public static CrtConversionItemTarget create(IItemStack target) {
3838
if (target == null) {
39-
var i = SimpleItem.getInstance(ItemStack.EMPTY);
39+
var i = SimpleItem.getNoNBTInstance(ItemStack.EMPTY);
4040
return new CrtConversionItemTarget(i.getItemID(), i.getMeta());
4141
}
4242
return new CrtConversionItemTarget(target.getDefinition().getId(), target.getMetadata());

src/main/java/com/circulation/only_one_item/handler/MatchItemHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public static boolean isModify(String odName) {
110110
public static boolean isModify(Multiset<SimpleItem> set) {
111111
for (SimpleItem item : set) {
112112
ResourceLocation ii;
113-
if ((itemIdToTargetMap.containsKey(ii = item.getItemRL())
113+
if ((itemIdToTargetMap.containsKey(ii = item.getRegistryName())
114114
&& itemIdToTargetMap.get(ii).containsKey(item.getMeta()))
115115
|| allTarget.contains(item)) {
116116
return true;
@@ -274,7 +274,7 @@ private static void Init(List<ItemConversionTarget> items) {
274274
ResourceLocation rl = item.getRegistryName();
275275
int meta = stack.getMetadata();
276276
if (rl == null) return false;
277-
return !allTarget.contains(SimpleItem.getInstance(stack))
277+
return !allTarget.contains(SimpleItem.getNoNBTInstance(stack))
278278
&& !((finalItemBlackMap.containsKey(rl) && finalItemBlackMap.get(rl).contains(meta))
279279
|| finalMODIDBlackSet.contains(rl.getNamespace()));
280280
})

src/main/java/com/circulation/only_one_item/util/RecipeSignature.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import com.circulation.only_one_item.handler.MatchItemHandler;
55
import com.google.common.collect.HashMultiset;
66
import com.google.common.collect.Multiset;
7-
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
7+
import it.unimi.dsi.fastutil.ints.Int2IntMap;
8+
import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap;
89
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
910
import lombok.Getter;
1011
import net.minecraft.item.ItemStack;
@@ -24,7 +25,6 @@
2425
import java.util.Arrays;
2526
import java.util.Collection;
2627
import java.util.List;
27-
import java.util.Map;
2828
import java.util.Objects;
2929

3030
public class RecipeSignature {
@@ -67,13 +67,13 @@ private List<Object> createInputSignatures(IRecipe recipe) {
6767
int ii = 0;
6868
for (Ingredient ingredient : ingredients) {
6969
ItemStack[] matching = ingredient.getMatchingStacks();
70-
Map<Integer, Integer> map = new Object2ObjectOpenHashMap<>();
70+
Int2IntMap map = new Int2IntOpenHashMap();
7171
if (isOD(map, signatures, matching)) {
7272
String odName = "";
7373
int max = 0;
74-
for (Map.Entry<Integer, Integer> integerIntegerEntry : map.entrySet()) {
75-
var od = integerIntegerEntry.getKey();
76-
var i = integerIntegerEntry.getValue();
74+
for (var integerIntegerEntry : map.int2IntEntrySet()) {
75+
var od = integerIntegerEntry.getIntKey();
76+
var i = integerIntegerEntry.getIntValue();
7777

7878
if (i > max) {
7979
max = i;
@@ -123,7 +123,7 @@ private boolean isCorrectOD(ItemStack[] matching, String odName) {
123123
return true;
124124
}
125125

126-
private boolean isOD(Map<Integer, Integer> map, List<Object> signatures, ItemStack[] matching) {
126+
private boolean isOD(Int2IntMap map, List<Object> signatures, ItemStack[] matching) {
127127
for (ItemStack stack : matching) {
128128
var ods = stack.isEmpty() ? new int[0] : OreDictionary.getOreIDs(stack);
129129
if (ods.length == 0) {
@@ -141,9 +141,10 @@ else if (repeat) {
141141
}
142142
}
143143
return false;
144-
}
145-
for (int oreID : ods) {
146-
map.put(oreID, map.getOrDefault(oreID, 0) + 1);
144+
} else {
145+
for (int oreID : ods) {
146+
map.put(oreID, map.get(oreID) + 1);
147+
}
147148
}
148149
}
149150
return true;
Lines changed: 75 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,128 @@
11
package com.circulation.only_one_item.util;
22

3-
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
43
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
5-
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
64
import lombok.Getter;
7-
import lombok.NonNull;
8-
import net.minecraft.init.Items;
5+
import lombok.ToString;
96
import net.minecraft.item.Item;
107
import net.minecraft.item.ItemStack;
8+
import net.minecraft.nbt.NBTTagCompound;
119
import net.minecraft.util.ResourceLocation;
1210

1311
import java.util.Map;
1412
import java.util.Objects;
13+
import java.util.concurrent.ConcurrentHashMap;
14+
import java.util.function.Function;
1515

16+
@ToString
1617
public final class SimpleItem {
17-
private static final Map<ResourceLocation, Int2ObjectMap<SimpleItem>> chace = new Object2ObjectOpenHashMap<>();
18-
public static final SimpleItem EMPTY = SimpleItem.getInstance(ItemStack.EMPTY);
19-
@NonNull
18+
2019
private final ResourceLocation item;
2120
@Getter
2221
private final int meta;
22+
public static final SimpleItem empty = new SimpleItem(ItemStack.EMPTY);
2323
private final int hashCode;
24+
private static final NBTTagCompound NullNbt = new NBTTagCompound() {
25+
@Override
26+
public boolean equals(Object nbt) {
27+
return nbt == this;
28+
}
2429

25-
private SimpleItem(@NonNull ResourceLocation item, int meta) {
30+
@Override
31+
public int hashCode() {
32+
return Integer.MIN_VALUE;
33+
}
34+
};
35+
private static final Map<ResourceLocation, Int2ObjectOpenHashMapS> chane = new ConcurrentHashMap<>();
36+
private static final Function<ResourceLocation, Int2ObjectOpenHashMapS> intMap = item -> new Int2ObjectOpenHashMapS();
37+
private final NBTTagCompound nbt;
38+
private SimpleItem(ResourceLocation item, int meta, NBTTagCompound nbt) {
2639
this.item = item;
2740
this.meta = meta;
28-
this.hashCode = Objects.hash(item, meta);
41+
this.nbt = nbt;
42+
this.hashCode = Objects.hash(item, meta, nbt);
2943
}
3044

31-
public static SimpleItem getInstance(@NonNull String item, int meta) {
32-
return getInstance(new ResourceLocation(item), meta);
45+
private SimpleItem(ItemStack stack) {
46+
this(stack.getItem().getRegistryName(), stack.getItemDamage(), stack.getTagCompound());
3347
}
3448

35-
public static SimpleItem getInstance(ResourceLocation item, int meta) {
36-
var map = chace.computeIfAbsent(item, rl -> new Int2ObjectOpenHashMap<>());
37-
if (map.containsKey(meta)) {
38-
return map.get(meta);
39-
}
40-
var value = new SimpleItem(item, meta);
41-
map.put(meta, value);
42-
return value;
49+
public static SimpleItem getInstance(final String rl, final int meta) {
50+
return getInstance(new ResourceLocation(rl), meta);
4351
}
4452

45-
public static SimpleItem getInstance(@NonNull ItemStack stack) {
46-
return getInstance(stack.getItem().getRegistryName(), stack.getMetadata());
53+
public static SimpleItem getInstance(final ResourceLocation rl, final int meta) {
54+
return chane.computeIfAbsent(rl, intMap)
55+
.computeIfAbsent(meta)
56+
.computeIfAbsent(NullNbt, n -> new SimpleItem(rl, meta, null));
4757
}
4858

49-
public boolean isEmpty() {
50-
return this == EMPTY || Objects.equals(this.item, ItemStack.EMPTY.getItem().getRegistryName());
59+
public static SimpleItem getInstance(final ItemStack stack) {
60+
if (stack.isEmpty()) return empty;
61+
var nbt = stack.getTagCompound();
62+
return chane.computeIfAbsent(stack.getItem().getRegistryName(), intMap)
63+
.computeIfAbsent(stack.getItemDamage())
64+
.computeIfAbsent(nbt == null ? NullNbt : nbt, n -> new SimpleItem(stack));
5165
}
5266

53-
public Item getItem() {
54-
return Item.getByNameOrId(item.toString());
67+
public static SimpleItem getNoNBTInstance(final ItemStack stack) {
68+
if (stack.isEmpty()) return empty;
69+
return chane.computeIfAbsent(stack.getItem().getRegistryName(), intMap)
70+
.computeIfAbsent(stack.getItemDamage())
71+
.computeIfAbsent(NullNbt, n -> new SimpleItem(stack));
5572
}
5673

57-
public String getItemID() {
58-
return item.toString();
74+
public Item getItem() {
75+
return Item.REGISTRY.getObject(item);
5976
}
6077

61-
public ResourceLocation getItemRL() {
78+
public ResourceLocation getRegistryName() {
6279
return item;
6380
}
6481

82+
public String getItemID() {
83+
return item.toString();
84+
}
85+
6586
public ItemStack getItemStack(int amount) {
66-
var stack = getItem();
67-
if (stack != null && stack != Items.AIR) {
68-
return new ItemStack(stack, amount, meta);
87+
var i = new ItemStack(getItem(), amount, meta);
88+
if (nbt != null && !nbt.isEmpty()) {
89+
i.setTagCompound(nbt);
6990
}
70-
return ItemStack.EMPTY;
91+
return i;
92+
}
93+
94+
public boolean isEmpty() {
95+
return this == empty;
7196
}
7297

7398
@Override
7499
public boolean equals(Object o) {
75100
if (o == null || getClass() != o.getClass()) return false;
76101
SimpleItem that = (SimpleItem) o;
77-
return meta == that.meta && Objects.equals(item, that.item);
102+
return meta == that.meta && Objects.equals(item, that.item) && Objects.equals(nbt, that.nbt);
78103
}
79104

80105
@Override
81106
public int hashCode() {
82107
return hashCode;
83108
}
84109

85-
@Override
86-
public String toString() {
87-
return item + ":" + meta;
110+
private static class Int2ObjectOpenHashMapS extends Int2ObjectOpenHashMap<Map<NBTTagCompound, SimpleItem>> {
111+
112+
public Map<NBTTagCompound, SimpleItem> computeIfAbsent(int key) {
113+
Map<NBTTagCompound, SimpleItem> v;
114+
115+
if ((v = get(key)) == null) {
116+
synchronized (this) {
117+
if ((v = get(key)) == null) {
118+
v = new ConcurrentHashMap<>();
119+
put(key, v);
120+
}
121+
}
122+
}
123+
124+
return v;
125+
}
126+
88127
}
89128
}

0 commit comments

Comments
 (0)