Skip to content

Commit

Permalink
Expand InventoryHelper & Update PlayerUtil.findItem().
Browse files Browse the repository at this point in the history
Expand InventoryHelper with methods to find a specific inventory, and all registered inventories excluding certain ones.  Together these allow a method to check inventories in a specific order, but still be able to check all registered inventories.

Update PlayerUtil.findItem() to use the update InventoryHelper.  This is needed to allow it to check Curios and any add-on mod inventories.  Decided to check Curios before the Main Inventory, but after the Off-Hand slot.
  • Loading branch information
VT-14 committed Oct 3, 2023
1 parent e573e39 commit 4cb22c0
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 8 deletions.
38 changes: 33 additions & 5 deletions src/main/java/wayoftime/bloodmagic/core/util/PlayerUtil.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package wayoftime.bloodmagic.core.util;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

import com.google.common.collect.Multimap;

import net.minecraft.core.NonNullList;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.ai.attributes.Attribute;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import wayoftime.bloodmagic.BloodMagic;
import wayoftime.bloodmagic.common.item.ExpandedArmor;
import wayoftime.bloodmagic.core.living.LivingUtil;
import wayoftime.bloodmagic.util.helper.InventoryHelper;

public class PlayerUtil
{
Expand All @@ -22,13 +27,36 @@ public static ItemStack findItem(Player player, Predicate<ItemStack> requirement
ItemStack offHand = player.getOffhandItem();
if (requirements.test(offHand))
return offHand;
List<String> checkedInventories = new ArrayList<>();
checkedInventories.add("offHandInventory");

// Check inventory next
for (int slot = 0; slot < player.getInventory().getContainerSize(); slot++)
// Check Curios next, if available.
if (BloodMagic.curiosLoaded)
{
ItemStack foundStack = player.getInventory().getItem(slot);
if (!foundStack.isEmpty() && requirements.test(foundStack))
return foundStack;
NonNullList<ItemStack> curiosInventory = InventoryHelper.getInventory(player, "curiosInventory");
for (ItemStack item : curiosInventory)
{
if (requirements.test(item))
return item;
}
checkedInventories.add("curiosInventory");
}

// Check Main Inventory next.
NonNullList<ItemStack> mainInventory = InventoryHelper.getInventory(player, "mainInventory");
for (ItemStack item : mainInventory)
{
if (requirements.test(item))
return item;
}
checkedInventories.add("mainInventory");

// Check all remaining registered inventories. Armor and Add-ons.
NonNullList<ItemStack> remainingInventories = InventoryHelper.getAllInventoriesExcluding(player, checkedInventories);
for (ItemStack item : remainingInventories)
{
if (requirements.test(item))
return item;
}

return ItemStack.EMPTY;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package wayoftime.bloodmagic.util.helper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

Expand All @@ -11,6 +13,21 @@

public class InventoryHelper
{
private static Map<String, Function<Player, NonNullList<ItemStack>>> inventoryProvider = BloodMagicAPI.INSTANCE.getInventoryProvider();

/**
* Gets all items from the specified inventory.
*
* @param player - The player who's inventories to check.
* @param inventoryKey - inventory's name. See BloodMagicCorePlugin for the vanilla ones.
* @return - NonNullList<ItemStack> of all items in those inventories.
*/
public static NonNullList<ItemStack> getInventory(Player player, String inventoryKey)
{
NonNullList<ItemStack> inventory = NonNullList.create();
inventory.addAll(inventoryProvider.get(inventoryKey).apply(player));
return inventory;
}

/**
* Gets all items from all registered inventories.
Expand All @@ -20,14 +37,29 @@ public class InventoryHelper
*/
public static NonNullList<ItemStack> getAllInventories(Player player)
{
Map<String, Function<Player, NonNullList<ItemStack>>> inventoryProvider = BloodMagicAPI.INSTANCE.getInventoryProvider();
NonNullList<ItemStack> inventory = NonNullList.create();

inventoryProvider.forEach((identifier, provider) -> inventory.addAll(provider.apply(player)));

return inventory;
}

/**
* Gets all items from all inventories, excluding the listed inventories
*
* @param player - The player who's inventories to check.
* @param excludedInventoryKeys - inventory keys to exclude. See BloodMagicCorePlugin for the vanilla ones.
* @return - NonNullList<ItemStack> of all items in those inventories.
*/
public static NonNullList<ItemStack> getAllInventoriesExcluding(Player player, List<String> excludedInventoryKeys)
{
NonNullList<ItemStack> inventory = NonNullList.create();

inventoryProvider.forEach((identifier, provider) -> {if (!identifier.equals(excludedInventoryKeys)) {inventory.addAll(provider.apply(player));}});

return inventory;
}

/**
* Gets all items from all registered inventories marked as active as well as
* main and off hand
Expand All @@ -37,10 +69,10 @@ public static NonNullList<ItemStack> getAllInventories(Player player)
*/
public static NonNullList<ItemStack> getActiveInventories(Player player)
{
Map<String, Function<Player, NonNullList<ItemStack>>> inventoryProviders = BloodMagicAPI.INSTANCE.getActiveInventoryProvider();
Map<String, Function<Player, NonNullList<ItemStack>>> activeInventoryProvider = BloodMagicAPI.INSTANCE.getActiveInventoryProvider();
NonNullList<ItemStack> inventories = NonNullList.create();

inventoryProviders.forEach((identifier, provider) -> inventories.addAll(provider.apply(player)));
activeInventoryProvider.forEach((identifier, provider) -> inventories.addAll(provider.apply(player)));

inventories.add(player.getItemInHand(InteractionHand.MAIN_HAND));
inventories.add(player.getItemInHand(InteractionHand.OFF_HAND));
Expand Down

0 comments on commit 4cb22c0

Please sign in to comment.