Skip to content

Commit ef553e8

Browse files
authored
Merge pull request #923 from Nxer/OreDict
Extreme Crafting Center Rework : Perfectly compatible with Ore Dict system
2 parents ac70ceb + 5f8cdd2 commit ef553e8

File tree

11 files changed

+544
-86
lines changed

11 files changed

+544
-86
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.Nxer.TwistSpaceTechnology.common.api;
2+
3+
import java.util.HashSet;
4+
import java.util.Objects;
5+
import java.util.Set;
6+
7+
import net.minecraft.item.ItemStack;
8+
9+
import org.jetbrains.annotations.NotNull;
10+
11+
import com.Nxer.TwistSpaceTechnology.util.rewrites.TST_ItemID;
12+
13+
public class AlternativeItem implements IAlternativeItem {
14+
15+
public final String name;
16+
private final Set<TST_ItemID> containedItems = new HashSet<>();
17+
18+
public AlternativeItem(@NotNull String name, ItemStack... itemStacks) {
19+
this.name = name;
20+
for (ItemStack itemStack : itemStacks) {
21+
if (itemStack == null) continue;
22+
containedItems.add(TST_ItemID.create(itemStack));
23+
}
24+
}
25+
26+
@Override
27+
public boolean containsItem(TST_ItemID itemID) {
28+
return containedItems.contains(itemID);
29+
}
30+
31+
@Override
32+
public boolean hasSameMember(IAlternativeItem another) {
33+
for (TST_ItemID itemID : containedItems) {
34+
if (another.containsItem(itemID)) return true;
35+
}
36+
return false;
37+
}
38+
39+
@Override
40+
public @NotNull String getName() {
41+
return name;
42+
}
43+
44+
/**
45+
* Add a new member to containing list. It would be better not to use this method because it will break consistency.
46+
*
47+
* @return True if adding is success.
48+
*/
49+
public boolean addMember(TST_ItemID itemID) {
50+
return containedItems.add(itemID);
51+
}
52+
53+
/**
54+
* Add a new member to containing list. It would be better not to use this method because it will break consistency.
55+
*
56+
* @return True if adding is success.
57+
*/
58+
public boolean addMember(ItemStack itemStack) {
59+
return addMember(TST_ItemID.create(itemStack));
60+
}
61+
62+
@Override
63+
public boolean equals(Object o) {
64+
if (this == o) {
65+
return true;
66+
}
67+
if (!(o instanceof AlternativeItem that)) {
68+
return false;
69+
}
70+
return Objects.equals(getName(), that.getName()) && Objects.equals(containedItems, that.containedItems);
71+
}
72+
73+
@Override
74+
public int hashCode() {
75+
return Objects.hash(getName(), containedItems);
76+
}
77+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.Nxer.TwistSpaceTechnology.common.api;
2+
3+
public class AlternativeItemStack {
4+
5+
public final IAlternativeItem alternativeItem;
6+
public int stackSize;
7+
8+
public AlternativeItemStack(IAlternativeItem alternativeItem, int stackSize) {
9+
this.alternativeItem = alternativeItem;
10+
this.stackSize = stackSize;
11+
}
12+
13+
public boolean stackEqual(AlternativeItemStack another) {
14+
return alternativeItem.equals(another.alternativeItem) && stackSize == another.stackSize;
15+
}
16+
17+
public boolean isSameAlternativeItem(AlternativeItemStack another) {
18+
return alternativeItem.equals(another.alternativeItem);
19+
}
20+
21+
public AlternativeItemStack setStackSize(int stackSize) {
22+
this.stackSize = stackSize;
23+
return this;
24+
}
25+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.Nxer.TwistSpaceTechnology.common.api;
2+
3+
import java.util.Collection;
4+
5+
import net.minecraft.item.ItemStack;
6+
7+
import org.jetbrains.annotations.NotNull;
8+
import org.jetbrains.annotations.Nullable;
9+
10+
import com.Nxer.TwistSpaceTechnology.util.rewrites.TST_ItemID;
11+
12+
/**
13+
* To conveniently check an alternative items list we can make them a special Object to process.
14+
*
15+
* @author Nxer
16+
*/
17+
public interface IAlternativeItem {
18+
19+
/**
20+
* @return True if the item stack is a member of this item list.
21+
*/
22+
default boolean containsItem(ItemStack itemStack) {
23+
return containsItem(TST_ItemID.create(itemStack)) || containsItem(TST_ItemID.createAsWildcard(itemStack));
24+
}
25+
26+
boolean containsItem(TST_ItemID itemID);
27+
28+
/**
29+
* @return True if two lists has same member in it.
30+
*/
31+
boolean hasSameMember(IAlternativeItem another);
32+
33+
/**
34+
* @return The name of this item list.
35+
*/
36+
@NotNull
37+
String getName();
38+
39+
static @Nullable IAlternativeItem checkAlternativeItemContaining(Collection<IAlternativeItem> alternativeItems,
40+
TST_ItemID itemID) {
41+
for (IAlternativeItem alternativeItem : alternativeItems) {
42+
if (alternativeItem.containsItem(itemID)) return alternativeItem;
43+
}
44+
return null;
45+
}
46+
47+
static @Nullable IAlternativeItem checkAlternativeItemContaining(Collection<IAlternativeItem> alternativeItems,
48+
ItemStack itemStack) {
49+
for (IAlternativeItem alternativeItem : alternativeItems) {
50+
if (alternativeItem.containsItem(itemStack)) return alternativeItem;
51+
}
52+
return null;
53+
}
54+
55+
static @Nullable IAlternativeItem checkAlternativeItemContainingFast(Collection<IAlternativeItem> alternativeItems,
56+
ItemStack itemStack) {
57+
return checkAlternativeItemContaining(alternativeItems, TST_ItemID.create(itemStack));
58+
}
59+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.Nxer.TwistSpaceTechnology.common.api;
2+
3+
import java.util.HashMap;
4+
import java.util.HashSet;
5+
import java.util.Map;
6+
import java.util.Objects;
7+
import java.util.Set;
8+
9+
import net.minecraft.item.ItemStack;
10+
import net.minecraftforge.oredict.OreDictionary;
11+
12+
import org.jetbrains.annotations.NotNull;
13+
14+
import com.Nxer.TwistSpaceTechnology.util.rewrites.TST_ItemID;
15+
16+
public class OreDictItem implements IAlternativeItem {
17+
18+
public static final Map<String, OreDictItem> UsedOreDictItems = new HashMap<>();
19+
20+
public static OreDictItem getOreDictItems(String oreDictName) {
21+
if (UsedOreDictItems.containsKey(oreDictName)) return UsedOreDictItems.get(oreDictName);
22+
23+
OreDictItem o = new OreDictItem(oreDictName);
24+
UsedOreDictItems.put(oreDictName, o);
25+
return o;
26+
}
27+
28+
public final String oreDictName;
29+
private final Set<TST_ItemID> containedItems = new HashSet<>();
30+
31+
public OreDictItem(@NotNull String oreDictName) {
32+
this.oreDictName = oreDictName;
33+
for (ItemStack itemStack : OreDictionary.getOres(oreDictName)) {
34+
if (itemStack == null) continue;
35+
containedItems.add(TST_ItemID.createNoNBT(itemStack));
36+
}
37+
38+
}
39+
40+
@Override
41+
public boolean containsItem(ItemStack itemStack) {
42+
return containedItems.contains(TST_ItemID.createNoNBT(itemStack))
43+
|| containedItems.contains(TST_ItemID.createAsWildcard(itemStack));
44+
}
45+
46+
@Override
47+
public @NotNull String getName() {
48+
return oreDictName;
49+
}
50+
51+
public boolean containsItem(TST_ItemID itemID) {
52+
return containedItems.contains(itemID);
53+
}
54+
55+
@Override
56+
public boolean hasSameMember(IAlternativeItem another) {
57+
for (TST_ItemID itemID : containedItems) {
58+
if (another.containsItem(itemID)) return true;
59+
}
60+
return false;
61+
}
62+
63+
@Override
64+
public boolean equals(Object o) {
65+
if (this == o) {
66+
return true;
67+
}
68+
if (!(o instanceof OreDictItem that)) {
69+
return false;
70+
}
71+
return Objects.equals(oreDictName, that.oreDictName);
72+
}
73+
74+
@Override
75+
public int hashCode() {
76+
return Objects.hash(oreDictName);
77+
}
78+
79+
}

0 commit comments

Comments
 (0)