Skip to content

Commit 2a2e24e

Browse files
authored
Merge pull request #178 from ProperSAMA/feat-consumeDurability
add RecipePrimer: consumeDurability
2 parents dceccd8 + 94b3ea1 commit 2a2e24e

File tree

5 files changed

+196
-5
lines changed

5 files changed

+196
-5
lines changed

build.gradle.kts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ plugins {
99
id("maven-publish")
1010
id("org.jetbrains.gradle.plugin.idea-ext") version "1.1.7"
1111
id("eclipse")
12-
id("com.gtnewhorizons.retrofuturagradle") version "1.3.19"
12+
id("com.gtnewhorizons.retrofuturagradle") version "1.4.0"
1313
}
1414

1515
// Project properties
@@ -156,8 +156,7 @@ repositories {
156156
}
157157
maven {
158158
name = "GTNH Maven"
159-
url = uri("http://jenkins.usrv.eu:8081/nexus/content/groups/public/")
160-
isAllowInsecureProtocol = true
159+
url = uri("https://nexus.gtnewhorizons.com/repository/public/")
161160
}
162161
}
163162

settings.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ pluginManagement {
1313
maven {
1414
// RetroFuturaGradle
1515
name = "GTNH Maven"
16-
url = uri("http://jenkins.usrv.eu:8081/nexus/content/groups/public/")
16+
url = uri("https://nexus.gtnewhorizons.com/repository/public/")
1717
isAllowInsecureProtocol = true
1818
mavenContent {
19+
includeGroupByRegex("com\\.gtnewhorizons\\..+")
1920
includeGroup("com.gtnewhorizons")
20-
includeGroup("com.gtnewhorizons.retrofuturagradle")
2121
}
2222
}
2323
gradlePluginPortal()

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ public class RequirementItem extends ComponentRequirement.MultiCompParallelizabl
7878
public int minAmount = 1;
7979
public int maxAmount = 1;
8080

81+
public int consumeDurability = 0;
82+
8183
public RequirementItem(IOType ioType, ItemStack item) {
8284
super(RequirementTypesMM.REQUIREMENT_ITEM, ioType);
8385
this.requirementType = ItemRequirementType.ITEMSTACKS;
@@ -118,6 +120,18 @@ public void addItemModifier(AdvancedItemModifier itemModifier) {
118120
this.itemModifierList.add(itemModifier);
119121
}
120122

123+
public void setConsumeDurability(final int durability) {
124+
this.consumeDurability = Math.max(0, durability);
125+
}
126+
127+
public boolean supportsDurability() {
128+
return switch (this.requirementType) {
129+
case ITEMSTACKS -> !this.required.isEmpty() && this.required.isItemStackDamageable();
130+
case OREDICT -> ItemUtils.hasDamageableEntry(this.oreDictName);
131+
default -> false;
132+
};
133+
}
134+
121135
@Override
122136
public int getSortingWeight() {
123137
return PRIORITY_WEIGHT_ITEM;
@@ -156,6 +170,7 @@ public RequirementItem deepCopyModified(List<RecipeModifier> modifiers) {
156170
if (this.previewDisplayTag != null) {
157171
item.previewDisplayTag = this.previewDisplayTag.copy();
158172
}
173+
item.consumeDurability = this.consumeDurability;
159174
return item;
160175
}
161176

@@ -371,6 +386,54 @@ public int consumeAllItems(final List<IItemHandlerModifiable> handlers,
371386

372387
final ItemStack finalStack = stack;
373388
final int finalMaxConsume = maxConsume;
389+
if (consumeDurability > 0 && getActionType() == IOType.INPUT) {
390+
final ItemStack referenceStack = finalStack.copy();
391+
switch (this.requirementType) {
392+
case ITEMSTACKS -> {
393+
for (final IItemHandlerModifiable handler : handlers) {
394+
Sync.executeSyncIfPresent(handler, () -> {
395+
final int remaining = finalMaxConsume - consumed.get();
396+
if (remaining <= 0) {
397+
return;
398+
}
399+
final int damaged;
400+
if (itemChecker != null) {
401+
damaged = ItemUtils.damageAll(handler, referenceStack, remaining, consumeDurability, itemChecker, context.getMachineController());
402+
} else {
403+
damaged = ItemUtils.damageAll(handler, referenceStack, remaining, consumeDurability, tag);
404+
}
405+
consumed.addAndGet(damaged);
406+
});
407+
if (consumed.get() >= maxConsume) {
408+
break;
409+
}
410+
}
411+
}
412+
case OREDICT -> {
413+
for (final IItemHandlerModifiable handler : handlers) {
414+
Sync.executeSyncIfPresent(handler, () -> {
415+
final int remaining = finalMaxConsume - consumed.get();
416+
if (remaining <= 0) {
417+
return;
418+
}
419+
final int damaged;
420+
if (itemChecker != null) {
421+
damaged = ItemUtils.damageAll(handler, oreDictName, remaining, consumeDurability, itemChecker, context.getMachineController());
422+
} else {
423+
damaged = ItemUtils.damageAll(handler, oreDictName, remaining, consumeDurability, tag);
424+
}
425+
consumed.addAndGet(damaged);
426+
});
427+
if (consumed.get() >= maxConsume) {
428+
break;
429+
}
430+
}
431+
}
432+
default -> {
433+
}
434+
}
435+
return consumed.get() / toConsume;
436+
}
374437
switch (this.requirementType) {
375438
case ITEMSTACKS -> {
376439
for (final IItemHandlerModifiable handler : handlers) {

src/main/java/hellfirepvp/modularmachinery/common/integration/crafttweaker/RecipePrimer.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,30 @@ public RecipePrimer setMinMaxAmount(int min, int max) {
195195
return this;
196196
}
197197

198+
@ZenMethod
199+
public RecipePrimer consumeDurability(int durability) {
200+
if (lastComponent instanceof RequirementItem reqItem) {
201+
if (reqItem.getActionType() != IOType.INPUT) {
202+
CraftTweakerAPI.logWarning("[ModularMachinery] consumeDurability(int) only can be applied to item inputs!");
203+
return this;
204+
}
205+
int safeDurability = durability;
206+
if (durability < 0) {
207+
CraftTweakerAPI.logWarning("[ModularMachinery] consumeDurability(int) requires a non-negative durability value!");
208+
safeDurability = 0;
209+
}
210+
if (safeDurability > 0 && !reqItem.supportsDurability()) {
211+
CraftTweakerAPI.logWarning("[ModularMachinery] consumeDurability(int) only applies to items with durability; a component will be ignored.");
212+
reqItem.setConsumeDurability(0);
213+
return this;
214+
}
215+
reqItem.setConsumeDurability(safeDurability);
216+
} else {
217+
CraftTweakerAPI.logWarning("[ModularMachinery] consumeDurability(int) only can be applied to `Item`!");
218+
}
219+
return this;
220+
}
221+
198222
/**
199223
* <p>为某个输入或输出设置特定触发时间。</p>
200224
* <p>注意:如果设置了触发时间,则配方在其他时间不会触发任何消耗或产出动作。</p>

src/main/java/hellfirepvp/modularmachinery/common/util/ItemUtils.java

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import net.minecraft.item.ItemStack;
2222
import net.minecraft.nbt.NBTTagCompound;
2323
import net.minecraft.tileentity.TileEntityFurnace;
24+
import net.minecraft.util.NonNullList;
2425
import net.minecraftforge.common.ForgeHooks;
2526
import net.minecraftforge.items.IItemHandler;
2627
import net.minecraftforge.items.IItemHandlerModifiable;
@@ -196,6 +197,75 @@ public static int consumeAll(IItemHandlerModifiable handler, String oreName, int
196197
return consumeAllInternal(handler, contents, amount);
197198
}
198199

200+
public static int damageAll(IItemHandlerModifiable handler, ItemStack toDamage, int amount, int damagePerUse, AdvancedItemChecker itemChecker, TileMultiblockMachineController controller) {
201+
if (amount <= 0 || damagePerUse <= 0) {
202+
return 0;
203+
}
204+
Int2ObjectMap<ItemStack> contents = findItemsIndexedInInventory(handler, toDamage, false, itemChecker, controller);
205+
if (contents.isEmpty()) {
206+
return 0;
207+
}
208+
return damageAllInternal(handler, contents, amount, damagePerUse);
209+
}
210+
211+
public static int damageAll(IItemHandlerModifiable handler, ItemStack toDamage, int amount, int damagePerUse, @Nullable NBTTagCompound matchNBTTag) {
212+
if (amount <= 0 || damagePerUse <= 0) {
213+
return 0;
214+
}
215+
Int2ObjectMap<ItemStack> contents = findItemsIndexedInInventory(handler, toDamage, false, matchNBTTag);
216+
if (contents.isEmpty()) {
217+
return 0;
218+
}
219+
return damageAllInternal(handler, contents, amount, damagePerUse);
220+
}
221+
222+
public static int damageAll(IItemHandlerModifiable handler, String oreName, int amount, int damagePerUse, AdvancedItemChecker itemChecker, TileMultiblockMachineController controller) {
223+
if (amount <= 0 || damagePerUse <= 0) {
224+
return 0;
225+
}
226+
Int2ObjectMap<ItemStack> contents = findItemsIndexedInInventoryOreDict(handler, oreName, itemChecker, controller);
227+
if (contents.isEmpty()) {
228+
return 0;
229+
}
230+
return damageAllInternal(handler, contents, amount, damagePerUse);
231+
}
232+
233+
public static int damageAll(IItemHandlerModifiable handler, String oreName, int amount, int damagePerUse, @Nullable NBTTagCompound matchNBTTag) {
234+
if (amount <= 0 || damagePerUse <= 0) {
235+
return 0;
236+
}
237+
Int2ObjectMap<ItemStack> contents = findItemsIndexedInInventoryOreDict(handler, oreName, matchNBTTag);
238+
if (contents.isEmpty()) {
239+
return 0;
240+
}
241+
return damageAllInternal(handler, contents, amount, damagePerUse);
242+
}
243+
244+
public static boolean hasDamageableEntry(final String oreDictName) {
245+
if (oreDictName == null || oreDictName.isEmpty()) {
246+
return false;
247+
}
248+
NonNullList<ItemStack> entries = OreDictionary.getOres(oreDictName);
249+
for (ItemStack entry : entries) {
250+
if (entry.isEmpty()) {
251+
continue;
252+
}
253+
if (entry.isItemStackDamageable()) {
254+
return true;
255+
}
256+
if (entry.getItemDamage() == OreDictionary.WILDCARD_VALUE && entry.getItem().getCreativeTab() != null) {
257+
NonNullList<ItemStack> subItems = NonNullList.create();
258+
entry.getItem().getSubItems(entry.getItem().getCreativeTab(), subItems);
259+
for (ItemStack subEntry : subItems) {
260+
if (!subEntry.isEmpty() && subEntry.isItemStackDamageable()) {
261+
return true;
262+
}
263+
}
264+
}
265+
}
266+
return false;
267+
}
268+
199269
public static int insertAll(@Nonnull ItemStack stack, IItemHandlerModifiable handler, int maxInsert) {
200270
if (stack.getCount() <= 0) {
201271
return 0;
@@ -254,6 +324,41 @@ private static int consumeAllInternal(IItemHandlerModifiable handler, Int2Object
254324
return cAmt;
255325
}
256326

327+
private static int damageAllInternal(IItemHandlerModifiable handler, Int2ObjectMap<ItemStack> contents, int maxOperations, int damagePerUse) {
328+
int operations = 0;
329+
if (damagePerUse <= 0) {
330+
return 0;
331+
}
332+
for (final Int2ObjectMap.Entry<ItemStack> content : contents.int2ObjectEntrySet()) {
333+
int slot = content.getIntKey();
334+
ItemStack stack = content.getValue();
335+
if (stack.isEmpty() || !stack.isItemStackDamageable() || stack.getMaxDamage() <= 0) {
336+
continue;
337+
}
338+
339+
while (operations < maxOperations && !stack.isEmpty()) {
340+
int newDamage = stack.getItemDamage() + damagePerUse;
341+
if (newDamage >= stack.getMaxDamage()) {
342+
stack.shrink(1);
343+
if (!stack.isEmpty()) {
344+
stack.setItemDamage(0);
345+
}
346+
} else {
347+
stack.setItemDamage(newDamage);
348+
}
349+
operations++;
350+
}
351+
352+
handler.setStackInSlot(slot, stack);
353+
354+
if (operations >= maxOperations) {
355+
break;
356+
}
357+
}
358+
359+
return operations;
360+
}
361+
257362
public static boolean stackEqualsNonNBT(@Nonnull ItemStack stack, @Nonnull ItemStack other) {
258363
if (stack.isEmpty() && other.isEmpty()) {
259364
return true;

0 commit comments

Comments
 (0)