|
21 | 21 | import net.minecraft.item.ItemStack; |
22 | 22 | import net.minecraft.nbt.NBTTagCompound; |
23 | 23 | import net.minecraft.tileentity.TileEntityFurnace; |
| 24 | +import net.minecraft.util.NonNullList; |
24 | 25 | import net.minecraftforge.common.ForgeHooks; |
25 | 26 | import net.minecraftforge.items.IItemHandler; |
26 | 27 | import net.minecraftforge.items.IItemHandlerModifiable; |
@@ -196,6 +197,75 @@ public static int consumeAll(IItemHandlerModifiable handler, String oreName, int |
196 | 197 | return consumeAllInternal(handler, contents, amount); |
197 | 198 | } |
198 | 199 |
|
| 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 | + |
199 | 269 | public static int insertAll(@Nonnull ItemStack stack, IItemHandlerModifiable handler, int maxInsert) { |
200 | 270 | if (stack.getCount() <= 0) { |
201 | 271 | return 0; |
@@ -254,6 +324,41 @@ private static int consumeAllInternal(IItemHandlerModifiable handler, Int2Object |
254 | 324 | return cAmt; |
255 | 325 | } |
256 | 326 |
|
| 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 | + |
257 | 362 | public static boolean stackEqualsNonNBT(@Nonnull ItemStack stack, @Nonnull ItemStack other) { |
258 | 363 | if (stack.isEmpty() && other.isEmpty()) { |
259 | 364 | return true; |
|
0 commit comments