Skip to content

Commit

Permalink
Add method to check if inv has enough space to fit given items (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
zefir-git committed Aug 25, 2023
2 parents 734d2f4 + 78959e0 commit f62e975
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/main/java/pro/cloudnode/smp/bankaccounts/BankAccounts.java
Expand Up @@ -10,6 +10,9 @@
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand;
import org.bukkit.event.Listener;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -323,6 +326,32 @@ public static Optional<String> checkForUpdates() {
return Optional.empty();
}

/**
* Check if an inventory can fit items
*
* @param inventory The inventory that you want to hold the items
* @param items The items to check if they can fit in the inventory
* @return A HashMap containing items that didn't fit.
*/
public static @NotNull HashMap<@NotNull Integer, @NotNull ItemStack> canFit(final @NotNull Inventory inventory, final @NotNull ItemStack... items) {
final @NotNull Inventory inv = getInstance().getServer().createInventory(null, inventory.getSize());
inv.setContents(inventory.getContents());
final @NotNull HashMap<@NotNull Integer, @NotNull ItemStack> didNotFit = inv.addItem(items);
inv.close();
return didNotFit;
}

/**
* Check if entity's inventory can fit items
*
* @param entity The entity that you want to hold the items
* @param items The items to check if they can fit in the inventory
* @return A HashMap containing items that didn't fit.
*/
public static @NotNull HashMap<@NotNull Integer, @NotNull ItemStack> canFit(final @NotNull InventoryHolder entity, final @NotNull ItemStack... items) {
return canFit(entity.getInventory(), items);
}

public static final class Key {
public final static @NotNull NamespacedKey INSTRUMENT_ACCOUNT = namespacedKey("instrument-account");
public final static @NotNull NamespacedKey POS_OWNER_GUI = namespacedKey("pos-owner-gui");
Expand Down

0 comments on commit f62e975

Please sign in to comment.