Skip to content

Commit b0a21a5

Browse files
authored
Merge pull request #627 from Nxer/fix
fix Logic bug in Mega Crafting Center
2 parents 76f0005 + bed6954 commit b0a21a5

File tree

1 file changed

+73
-67
lines changed

1 file changed

+73
-67
lines changed

src/main/java/com/Nxer/TwistSpaceTechnology/common/machine/TST_MegaCraftingCenter.java

Lines changed: 73 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@
2424

2525
import net.minecraft.entity.player.EntityPlayer;
2626
import net.minecraft.inventory.InventoryCrafting;
27+
import net.minecraft.item.Item;
2728
import net.minecraft.item.ItemStack;
2829
import net.minecraft.nbt.NBTTagCompound;
2930
import net.minecraft.nbt.NBTTagList;
3031
import net.minecraft.util.StatCollector;
3132
import net.minecraftforge.common.util.ForgeDirection;
33+
import net.minecraftforge.oredict.OreDictionary;
3234

3335
import org.jetbrains.annotations.NotNull;
3436
import org.jetbrains.annotations.Nullable;
@@ -62,6 +64,7 @@
6264
import gregtech.api.interfaces.ITexture;
6365
import gregtech.api.interfaces.metatileentity.IMetaTileEntity;
6466
import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
67+
import gregtech.api.objects.ItemData;
6568
import gregtech.api.recipe.RecipeMap;
6669
import gregtech.api.recipe.check.CheckRecipeResult;
6770
import gregtech.api.recipe.check.CheckRecipeResultRegistry;
@@ -72,6 +75,7 @@
7275
import gregtech.api.util.GT_Recipe;
7376
import gregtech.api.util.GT_Utility;
7477
import gtPlusPlus.core.block.ModBlocks;
78+
import scala.actors.migration.pattern;
7579

7680
public class TST_MegaCraftingCenter extends TT_MultiMachineBase_EM
7781
implements ICraftingProvider, IActionHost, IGridProxyable, ISurvivalConstructable {
@@ -103,6 +107,70 @@ public static ItemStack[] convertAEToMC(IAEItemStack... STACK) {
103107
.toArray(ItemStack[]::new);
104108
}
105109

110+
/**
111+
*
112+
* @param r The find recipe of this pattern.
113+
* @param in The pattern input items.
114+
* @param out The pattern output item.
115+
* @return If this pattern is valid.
116+
*/
117+
protected static boolean checkPatternRecipe(GT_Recipe r, ItemStack[] in, ItemStack out) {
118+
ItemStack rOut = r.mOutputs[0];
119+
if (out == null || out.getItem() == null || out.stackSize < 1) return false;
120+
if (!Utils.metaItemEqual(out, rOut)) return false;
121+
// check amount
122+
if (out.stackSize < rOut.stackSize || out.stackSize % rOut.stackSize != 0) return false;
123+
// the multiple of pattern
124+
int multiple = out.stackSize / rOut.stackSize;
125+
126+
Map<TST_ItemID, Long> rItemMap = new HashMap<>();
127+
Map<Item, Long> rWildcardMap = new HashMap<>();
128+
for (ItemStack i : r.mInputs) {
129+
if (i == null || i.getItem() == null || i.stackSize < 1) continue;
130+
ItemData iData = GT_OreDictUnificator.getAssociation(i);
131+
if (iData != null) {
132+
rItemMap.merge(TST_ItemID.createNoNBT(iData.mUnificationTarget), (long) i.stackSize, Long::sum);
133+
} else if (i.getItemDamage() == OreDictionary.WILDCARD_VALUE) {
134+
rWildcardMap.merge(i.getItem(), (long) i.stackSize, Long::sum);
135+
} else {
136+
rItemMap.merge(TST_ItemID.createNoNBT(i), (long) i.stackSize, Long::sum);
137+
}
138+
}
139+
140+
Map<TST_ItemID, Long> pItemMap = new HashMap<>();
141+
Map<Item, Long> pWildcardMap = new HashMap<>();
142+
for (ItemStack i : in) {
143+
if (i == null || i.getItem() == null || i.stackSize < 1) continue;
144+
ItemData iData = GT_OreDictUnificator.getAssociation(i);
145+
if (iData != null) {
146+
pItemMap.merge(TST_ItemID.createNoNBT(iData.mUnificationTarget), (long) i.stackSize, Long::sum);
147+
} else {
148+
Item ii = i.getItem();
149+
if (rWildcardMap.containsKey(ii)) {
150+
pWildcardMap.merge(ii, (long) i.stackSize, Long::sum);
151+
} else {
152+
pItemMap.merge(TST_ItemID.createNoNBT(i), (long) i.stackSize, Long::sum);
153+
}
154+
155+
}
156+
}
157+
158+
if (rItemMap.size() != pItemMap.size() || rWildcardMap.size() != pWildcardMap.size()) return false;
159+
160+
for (Map.Entry<TST_ItemID, Long> rItemEntry : rItemMap.entrySet()) {
161+
Long pAmount = pItemMap.get(rItemEntry.getKey());
162+
if (pAmount == null || pAmount == 0 || !pAmount.equals(rItemEntry.getValue() * multiple)) return false;
163+
}
164+
165+
for (Map.Entry<Item, Long> rWildcardEntry : rWildcardMap.entrySet()) {
166+
Long pAmount = pWildcardMap.get(rWildcardEntry.getKey());
167+
if (pAmount == null || pAmount == 0 || !pAmount.equals(rWildcardEntry.getValue() * multiple)) return false;
168+
}
169+
170+
return true;
171+
172+
}
173+
106174
public static @Nullable ICraftingPatternDetails checkPattern(ItemStack pattern) {
107175
if (pattern == null || pattern.stackSize < 1) return null;
108176
if (pattern.getItem() instanceof ICraftingPatternItem patternItem) {
@@ -132,85 +200,23 @@ public static ItemStack[] convertAEToMC(IAEItemStack... STACK) {
132200
if (validResult.length == 1) {
133201
if (validResult[0] == null) return null;
134202

135-
@NotNull
136-
ItemStack rItems = validResult[0].mOutputs[0];
137-
if (Utils.metaItemEqual(pItems, rItems)) {
138-
if (pItems.stackSize < rItems.stackSize || (pItems.stackSize & rItems.stackSize) != 0)
139-
return null;
140-
// allow to double pattern
141-
int multiple = pItems.stackSize / rItems.stackSize;
142-
if (multiple < 1) return null;
143-
144-
ItemStack[] rInputs = validResult[0].mInputs;
145-
Map<TST_ItemID, Long> rInputMap = new HashMap<>();
146-
for (int t = 0; t < rInputs.length; t++) {
147-
rInputMap.merge(
148-
TST_ItemID.createNoNBT(GT_OreDictUnificator.get_nocopy(rInputs[t])),
149-
(long) rInputs[t].stackSize,
150-
Long::sum);
151-
}
152-
153-
Map<TST_ItemID, Long> pInputMap = new HashMap<>();
154-
for (int t = 0; t < dInputs.length; t++) {
155-
pInputMap.merge(
156-
TST_ItemID.createNoNBT(GT_OreDictUnificator.get_nocopy(dInputs[t])),
157-
(long) dInputs[t].stackSize,
158-
Long::sum);
159-
}
160-
161-
if (rInputMap.size() != pInputMap.size()) return null;
162-
163-
for (Map.Entry<TST_ItemID, Long> rEntry : rInputMap.entrySet()) {
164-
Long pAmount = pInputMap.get(rEntry.getKey());
165-
if (pAmount == null || !pAmount.equals(rEntry.getValue() * multiple)) return null;
166-
}
167-
203+
if (checkPatternRecipe(validResult[0], dInputs, pItems)) {
168204
return d;
169205
} else {
170206
return null;
171207
}
172208

173209
} else {
174-
recipesLoop: for (int i = 0; i < validResult.length; i++) {
175-
@NotNull
176-
ItemStack rItems = validResult[i].mOutputs[0];
177-
if (Utils.metaItemEqual(pItems, rItems)) {
178-
if (pItems.stackSize < rItems.stackSize || (pItems.stackSize & rItems.stackSize) != 0)
179-
continue;
180-
// allow to double pattern
181-
int multiple = pItems.stackSize / rItems.stackSize;
182-
if (multiple < 1) continue;
183-
184-
ItemStack[] rInputs = validResult[i].mInputs;
185-
Map<TST_ItemID, Long> rInputMap = new HashMap<>();
186-
for (int t = 0; t < rInputs.length; t++) {
187-
rInputMap.merge(
188-
TST_ItemID.createNoNBT(GT_OreDictUnificator.get_nocopy(rInputs[t])),
189-
(long) rInputs[t].stackSize,
190-
Long::sum);
191-
}
192-
193-
Map<TST_ItemID, Long> pInputMap = new HashMap<>();
194-
for (int t = 0; t < dInputs.length; t++) {
195-
pInputMap.merge(
196-
TST_ItemID.createNoNBT(GT_OreDictUnificator.get_nocopy(dInputs[t])),
197-
(long) dInputs[t].stackSize,
198-
Long::sum);
199-
}
200-
201-
if (rInputMap.size() != pInputMap.size()) continue;
202-
for (Map.Entry<TST_ItemID, Long> rEntry : rInputMap.entrySet()) {
203-
Long pAmount = pInputMap.get(rEntry.getKey());
204-
if (pAmount == null || !pAmount.equals(rEntry.getValue() * multiple))
205-
continue recipesLoop;
206-
}
207-
210+
for (int i = 0; i < validResult.length; i++) {
211+
if (checkPatternRecipe(validResult[i], dInputs, pItems)) {
208212
return d;
209213
}
210214
}
215+
211216
}
212217
}
213218
}
219+
214220
return null;
215221
}
216222

0 commit comments

Comments
 (0)