Skip to content

Commit 9b7ca1d

Browse files
committed
Fix #27; Realize #25; AdvancedItemChecker and AdvancedItemModifier are now supported by IngredientArray.
1 parent 99f3dba commit 9b7ca1d

File tree

15 files changed

+247
-112
lines changed

15 files changed

+247
-112
lines changed

src/main/java/github/kasuminova/mmce/common/helper/AdvancedItemNBTChecker.java renamed to src/main/java/github/kasuminova/mmce/common/helper/AdvancedItemChecker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
import net.minecraft.item.ItemStack;
55

66
@FunctionalInterface
7-
public interface AdvancedItemNBTChecker {
7+
public interface AdvancedItemChecker {
88
boolean isMatch(TileMultiblockMachineController controller, ItemStack stack);
99
}

src/main/java/github/kasuminova/mmce/common/itemtype/IngredientStack.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
package github.kasuminova.mmce.common.itemtype;
22

3+
import github.kasuminova.mmce.common.helper.AdvancedItemChecker;
4+
import github.kasuminova.mmce.common.helper.AdvancedItemModifier;
35
import net.minecraft.item.ItemStack;
46
import net.minecraft.nbt.NBTTagCompound;
57

8+
import java.util.ArrayList;
9+
import java.util.List;
10+
611
public class IngredientStack {
712
public final IngredientType ingredientType;
813
public String oreDictName = "";
914
public ItemStack itemStack;
1015
public int count;
1116
public NBTTagCompound tag = null;
17+
public AdvancedItemChecker itemChecker = null;
18+
public List<AdvancedItemModifier> itemModifierList = new ArrayList<>();
1219

1320
public IngredientStack(ItemStack itemStack) {
1421
this.itemStack = itemStack;

src/main/java/hellfirepvp/modularmachinery/common/crafting/helper/ComponentRequirement.java

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ public abstract class ComponentRequirement<T, V extends RequirementType<T, ? ext
3636
protected final IOType actionType;
3737
protected ComponentSelectorTag tag = null;
3838

39+
protected boolean triggered = false;
40+
protected boolean triggerRepeatable = false;
41+
protected int triggerTime = 0;
42+
3943
public ComponentRequirement(V requirementType, IOType actionType) {
4044
this.requirementType = requirementType;
4145
this.actionType = actionType;
@@ -57,6 +61,30 @@ public final void setTag(ComponentSelectorTag tag) {
5761
this.tag = tag;
5862
}
5963

64+
public void setTriggerTime(int tickTime) {
65+
triggerTime = tickTime;
66+
}
67+
68+
public int getTriggerTime() {
69+
return triggerTime;
70+
}
71+
72+
public void setTriggerRepeatable(boolean triggerRepeatable) {
73+
this.triggerRepeatable = triggerRepeatable;
74+
}
75+
76+
public boolean isTriggerRepeatable() {
77+
return triggerRepeatable;
78+
}
79+
80+
public boolean isTriggered() {
81+
return triggered;
82+
}
83+
84+
public void setTriggered(final boolean triggered) {
85+
this.triggered = triggered;
86+
}
87+
6088
public int getSortingWeight() {
6189
return 0;
6290
}
@@ -141,19 +169,6 @@ public interface Parallelizable {
141169
void setParallelizeUnaffected(boolean unaffected);
142170
}
143171

144-
/**
145-
* <p>ModifiableParallelize(可修改并行)</p>
146-
* <p>在实现了 {@link Parallelizable} 的基础上,它还支持为并行添加额外的乘数。</p>
147-
*/
148-
public interface ModifiableParallelize extends Parallelizable {
149-
/**
150-
* 设置并行倍率(例如能量输入倍率)
151-
*
152-
* @param multiplier 倍率
153-
*/
154-
void setParallelMultiplier(float multiplier);
155-
}
156-
157172
public abstract static class JEIComponent<T> {
158173

159174
public abstract Class<T> getJEIRequirementClass();

src/main/java/hellfirepvp/modularmachinery/common/crafting/helper/RecipeCraftingContext.java

Lines changed: 75 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,21 @@ public Iterable<ProcessingComponent<?>> getComponentsFor(ComponentRequirement<?,
118118
}
119119

120120
public CraftingCheckResult ioTick(int currentTick) {
121+
ResultChance chance = new ResultChance(RAND.nextLong());
122+
CraftingCheckResult checkResult = new CraftingCheckResult();
121123
float durMultiplier = this.getDurationMultiplier();
122124

123125
//Input / Output tick
124126
for (ComponentRequirement<?, ?> requirement : requirements) {
125-
if (!(requirement instanceof ComponentRequirement.PerTick)) continue;
127+
if (!(requirement instanceof ComponentRequirement.PerTick)) {
128+
if (requirement.getTriggerTime() >= 1) {
129+
checkAndTriggerRequirement(checkResult, currentTick, chance, requirement);
130+
if (checkResult.isFailure()) return checkResult;
131+
continue;
132+
}
133+
continue;
134+
}
135+
126136
ComponentRequirement.PerTick<?, ?> perTickRequirement = (ComponentRequirement.PerTick<?, ?>) requirement;
127137

128138
perTickRequirement.resetIOTick(this);
@@ -142,9 +152,8 @@ public CraftingCheckResult ioTick(int currentTick) {
142152

143153
CraftCheck result = perTickRequirement.resetIOTick(this);
144154
if (!result.isSuccess()) {
145-
CraftingCheckResult res = new CraftingCheckResult();
146-
res.addError(result.getUnlocalizedMessage());
147-
return res;
155+
checkResult.addError(result.getUnlocalizedMessage());
156+
return checkResult;
148157
}
149158
}
150159

@@ -153,6 +162,19 @@ public CraftingCheckResult ioTick(int currentTick) {
153162
return CraftingCheckResult.SUCCESS;
154163
}
155164

165+
private void checkAndTriggerRequirement(final CraftingCheckResult res, final int currentTick, final ResultChance chance, final ComponentRequirement<?, ?> req) {
166+
int triggerTime = req.getTriggerTime() * Math.round(RecipeModifier.applyModifiers(
167+
this, RequirementTypesMM.REQUIREMENT_DURATION, null, 1, false));
168+
if (triggerTime <= 0 || triggerTime != currentTick || (req.isTriggered() && !req.isTriggerRepeatable())) {
169+
return;
170+
}
171+
172+
if (canStartCrafting(res, req)) {
173+
startCrafting(chance, req);
174+
req.setTriggered(true);
175+
}
176+
}
177+
156178
public void startCrafting() {
157179
startCrafting(RAND.nextLong());
158180
}
@@ -164,26 +186,30 @@ public void startCrafting(long seed) {
164186
ResultChance chance = event.getResultChance();
165187

166188
for (ComponentRequirement<?, ?> requirement : requirements) {
167-
requirement.startRequirementCheck(chance, this);
168-
169-
for (ProcessingComponent<?> component : requirementComponents.getOrDefault(requirement, Collections.emptyList())) {
170-
AtomicBoolean success = new AtomicBoolean(false);
171-
if (requirement instanceof Asyncable) {
172-
success.set(requirement.startCrafting(component, this, chance));
173-
} else {
174-
Sync.doSyncAction(() -> success.set(requirement.startCrafting(component, this, chance)));
175-
}
176-
if (success.get()) {
177-
requirement.endRequirementCheck();
178-
break;
179-
}
180-
}
181-
requirement.endRequirementCheck();
189+
startCrafting(chance, requirement);
182190
}
183191

184192
this.getParentRecipe().getCommandContainer().runStartCommands(this.commandSender);
185193
}
186194

195+
private void startCrafting(final ResultChance chance, final ComponentRequirement<?, ?> requirement) {
196+
requirement.startRequirementCheck(chance, this);
197+
198+
for (ProcessingComponent<?> component : requirementComponents.getOrDefault(requirement, Collections.emptyList())) {
199+
AtomicBoolean success = new AtomicBoolean(false);
200+
if (requirement instanceof Asyncable) {
201+
success.set(requirement.startCrafting(component, this, chance));
202+
} else {
203+
Sync.doSyncAction(() -> success.set(requirement.startCrafting(component, this, chance)));
204+
}
205+
if (success.get()) {
206+
requirement.endRequirementCheck();
207+
break;
208+
}
209+
}
210+
requirement.endRequirementCheck();
211+
}
212+
187213
public void finishCrafting() {
188214
finishCrafting(RAND.nextLong());
189215
}
@@ -275,37 +301,44 @@ public CraftingCheckResult canStartCrafting(Predicate<ComponentRequirement<?, ?>
275301

276302
lblRequirements:
277303
for (ComponentRequirement<?, ?> requirement : requirements) {
278-
requirement.startRequirementCheck(ResultChance.GUARANTEED, this);
304+
if (canStartCrafting(result, requirement)) {
305+
successfulRequirements++;
306+
continue lblRequirements;
307+
}
308+
}
309+
result.setValidity(successfulRequirements / requirements.size());
279310

280-
Iterable<ProcessingComponent<?>> components = requirementComponents.getOrDefault(requirement, Collections.emptyList());
281-
if (!Iterables.isEmpty(components)) {
311+
currentRestrictions.clear();
312+
return result;
313+
}
282314

283-
List<String> errorMessages = new ArrayList<>();
284-
for (ProcessingComponent<?> component : components) {
285-
CraftCheck check = requirement.canStartCrafting(component, this, this.currentRestrictions);
315+
private boolean canStartCrafting(final CraftingCheckResult result, final ComponentRequirement<?, ?> requirement) {
316+
requirement.startRequirementCheck(ResultChance.GUARANTEED, this);
286317

287-
if (check.isSuccess()) {
288-
requirement.endRequirementCheck();
289-
successfulRequirements += 1;
290-
continue lblRequirements;
291-
}
318+
Iterable<ProcessingComponent<?>> components = requirementComponents.getOrDefault(requirement, Collections.emptyList());
319+
if (!Iterables.isEmpty(components)) {
292320

293-
if (!check.isInvalid() && !check.getUnlocalizedMessage().isEmpty()) {
294-
errorMessages.add(check.getUnlocalizedMessage());
295-
}
321+
List<String> errorMessages = new ArrayList<>();
322+
for (ProcessingComponent<?> component : components) {
323+
CraftCheck check = requirement.canStartCrafting(component, this, this.currentRestrictions);
324+
325+
if (check.isSuccess()) {
326+
requirement.endRequirementCheck();
327+
return true;
296328
}
297-
errorMessages.forEach(result::addError);
298-
} else {
299-
// No component found that would apply for the given requirement
300-
result.addError(requirement.getMissingComponentErrorMessage(requirement.actionType));
301-
}
302329

303-
requirement.endRequirementCheck();
330+
if (!check.isInvalid() && !check.getUnlocalizedMessage().isEmpty()) {
331+
errorMessages.add(check.getUnlocalizedMessage());
332+
}
333+
}
334+
errorMessages.forEach(result::addError);
335+
} else {
336+
// No component found that would apply for the given requirement
337+
result.addError(requirement.getMissingComponentErrorMessage(requirement.actionType));
304338
}
305-
result.setValidity(successfulRequirements / requirements.size());
306339

307-
currentRestrictions.clear();
308-
return result;
340+
requirement.endRequirementCheck();
341+
return false;
309342
}
310343

311344
public <T> void addComponent(MachineComponent<T> component, @Nullable ComponentSelectorTag tag) {

src/main/java/hellfirepvp/modularmachinery/common/crafting/requirement/RequirementCatalyst.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,12 @@ public RequirementCatalyst deepCopy() {
7272
catalyst = new RequirementCatalyst(this.required.copy());
7373
break;
7474
}
75+
catalyst.triggerTime = this.triggerTime;
76+
catalyst.triggerRepeatable = this.triggerRepeatable;
7577
catalyst.chance = this.chance;
7678
catalyst.parallelizeUnaffected = this.parallelizeUnaffected;
77-
if (this.nbtChecker != null) {
78-
catalyst.nbtChecker = this.nbtChecker;
79+
if (this.itemChecker != null) {
80+
catalyst.itemChecker = this.itemChecker;
7981
} else if (this.tag != null) {
8082
catalyst.tag = this.tag.copy();
8183
}

src/main/java/hellfirepvp/modularmachinery/common/crafting/requirement/RequirementEnergy.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
* Created by HellFirePvP
3434
* Date: 24.02.2018 / 12:26
3535
*/
36-
public class RequirementEnergy extends ComponentRequirement.PerTick<Long, RequirementTypeEnergy> implements Asyncable, ComponentRequirement.ModifiableParallelize {
36+
public class RequirementEnergy extends ComponentRequirement.PerTick<Long, RequirementTypeEnergy> implements Asyncable, ComponentRequirement.Parallelizable {
3737

3838
public final long requirementPerTick;
3939
public float parallelMultiplier = 1F;
@@ -244,9 +244,4 @@ public void setParallelizeUnaffected(boolean unaffected) {
244244
this.parallelMultiplier = 1;
245245
}
246246
}
247-
248-
@Override
249-
public void setParallelMultiplier(float multiplier) {
250-
this.parallelMultiplier = multiplier;
251-
}
252247
}

src/main/java/hellfirepvp/modularmachinery/common/crafting/requirement/RequirementFluid.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ public int getSortingWeight() {
7676
@Override
7777
public ComponentRequirement<HybridFluid, RequirementTypeFluid> deepCopy() {
7878
RequirementFluid fluid = new RequirementFluid(this.requirementType, this.actionType, this.required.copy());
79+
fluid.triggerTime = this.triggerTime;
80+
fluid.triggerRepeatable = this.triggerRepeatable;
7981
fluid.chance = this.chance;
8082
fluid.tagMatch = getTagMatch();
8183
fluid.tagDisplay = getTagDisplay();

src/main/java/hellfirepvp/modularmachinery/common/crafting/requirement/RequirementIngredientArray.java

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,15 @@ public boolean startCrafting(ProcessingComponent<?> component, RecipeCraftingCon
5757
int amt = Math.round(RecipeModifier.applyModifiers(context, this, copiedStack.getCount(), false)) * parallelism;
5858
copiedStack.setCount(amt);
5959

60-
if (ItemUtils.consumeFromInventory(handler, copiedStack, true, stack.tag)) {
60+
if (stack.itemChecker != null) {
61+
if (ItemUtils.consumeFromInventory(handler, copiedStack, true, stack.itemChecker, context.getMachineController())) {
62+
if (chance.canProduce(productionChance)) {
63+
return true;
64+
} else {
65+
return ItemUtils.consumeFromInventory(handler, copiedStack, false, stack.itemChecker, context.getMachineController());
66+
}
67+
}
68+
} else if (ItemUtils.consumeFromInventory(handler, copiedStack, true, stack.tag)) {
6169
if (chance.canProduce(productionChance)) {
6270
return true;
6371
} else {
@@ -69,7 +77,15 @@ public boolean startCrafting(ProcessingComponent<?> component, RecipeCraftingCon
6977
case ORE_DICT: {
7078
int amt = Math.round(RecipeModifier.applyModifiers(context, this, stack.count, false)) * parallelism;
7179

72-
if (ItemUtils.consumeFromInventoryOreDict(handler, stack.oreDictName, amt, true, stack.tag)) {
80+
if (stack.itemChecker != null) {
81+
if (ItemUtils.consumeFromInventoryOreDict(handler, stack.oreDictName, amt, true, stack.itemChecker, context.getMachineController())) {
82+
if (chance.canProduce(productionChance)) {
83+
return true;
84+
} else {
85+
return ItemUtils.consumeFromInventoryOreDict(handler, stack.oreDictName, amt, false, stack.itemChecker, context.getMachineController());
86+
}
87+
}
88+
} else if (ItemUtils.consumeFromInventoryOreDict(handler, stack.oreDictName, amt, true, stack.tag)) {
7389
if (chance.canProduce(productionChance)) {
7490
return true;
7591
} else {
@@ -101,14 +117,24 @@ public CraftCheck canStartCrafting(ProcessingComponent<?> component, RecipeCraft
101117
ItemStack copiedStack = stack.itemStack.copy();
102118
int amt = Math.round(RecipeModifier.applyModifiers(context, this, copiedStack.getCount(), false)) * parallelism;
103119
copiedStack.setCount(amt);
104-
if (ItemUtils.consumeFromInventory(handler, copiedStack, true, copiedStack.getTagCompound())) {
120+
121+
if (stack.itemChecker != null) {
122+
if (ItemUtils.consumeFromInventory(handler, copiedStack, true, stack.itemChecker, context.getMachineController())) {
123+
return CraftCheck.success();
124+
}
125+
} else if (ItemUtils.consumeFromInventory(handler, copiedStack, true, copiedStack.getTagCompound())) {
105126
return CraftCheck.success();
106127
}
107128
break;
108129
}
109130
case ORE_DICT: {
110131
int amt = Math.round(RecipeModifier.applyModifiers(context, this, stack.count, false)) * parallelism;
111-
if (ItemUtils.consumeFromInventoryOreDict(handler, stack.oreDictName, amt,true, stack.tag)) {
132+
133+
if (stack.itemChecker != null) {
134+
if (ItemUtils.consumeFromInventoryOreDict(handler, stack.oreDictName, amt, true, stack.itemChecker, context.getMachineController())) {
135+
return CraftCheck.success();
136+
}
137+
} else if (ItemUtils.consumeFromInventoryOreDict(handler, stack.oreDictName, amt, true, stack.tag)) {
112138
return CraftCheck.success();
113139
}
114140
break;
@@ -123,6 +149,8 @@ public CraftCheck canStartCrafting(ProcessingComponent<?> component, RecipeCraft
123149
public ComponentRequirement<ItemStack, RequirementTypeIngredientArray> deepCopy() {
124150
RequirementIngredientArray copied = new RequirementIngredientArray(this.itemArray);
125151
copied.parallelizeUnaffected = this.parallelizeUnaffected;
152+
copied.triggerTime = this.triggerTime;
153+
copied.triggerRepeatable = this.triggerRepeatable;
126154
return copied;
127155
}
128156

@@ -186,7 +214,7 @@ public int maxParallelism(ProcessingComponent<?> component, RecipeCraftingContex
186214
switch (ingredientStack.ingredientType) {
187215
case ITEMSTACK: {
188216
ItemStack stack = ItemUtils.copyStackWithSize(ingredientStack.itemStack, ingredientStack.count);
189-
stack.setCount(Math.round(stack.getCount() * parallelism));
217+
stack.setCount(stack.getCount() * parallelism);
190218
return ItemUtils.maxInputParallelism(handler, stack, maxParallelism, ingredientStack.tag);
191219
}
192220
case ORE_DICT: {

0 commit comments

Comments
 (0)