Skip to content

Commit

Permalink
rewrite ItemUtils#removeItem method
Browse files Browse the repository at this point in the history
  • Loading branch information
shradinx committed Nov 27, 2024
1 parent 542b3d6 commit 0082a26
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,30 @@
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.vindicterra.vindicterralib.items.ItemRemoveResult;

public class ItemUtils {
/**
* Removes the specified amount of Bukkit material from the player's inventory
* @param player Player to remove item from
* @param material Bukkit material of item being removed
* @param player Not-null Player to remove item from
* @param material Not-null Bukkit material of item being removed
* @param amount Amount of item to be removed
*
* @return True if item was successfully removed, otherwise false
*/
public static boolean removeItem(Player player, Material material, int amount, boolean checkGamemode) {
public static ItemRemoveResult removeItem(@NotNull Player player, @NotNull Material material, int amount, boolean checkGamemode) {
if (checkGamemode && player.getGameMode().equals(GameMode.CREATIVE)) {
return false;
return ItemRemoveResult.SUCCESS;
}
int index = player.getInventory().first(material);
if (index == -1) return false;
ItemStack first = player.getInventory().getItem(index);
if (first == null) return false;
first.setAmount(first.getAmount() - amount);
player.getInventory().setItem(index, first);
return true;
if (!player.getInventory().contains(material)) {
return ItemRemoveResult.FAIL;
}
if (!player.getInventory().contains(material, amount)) {
return ItemRemoveResult.INVALID_AMOUNT;
}

player.getInventory().removeItem(new ItemStack(material, amount));
return ItemRemoveResult.SUCCESS;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.vindicterra.vindicterralib.items;

/**
* Result of removing an item from a player's inventory
*/
public enum ItemRemoveResult {
SUCCESS,
FAIL,
INVALID_AMOUNT
}

0 comments on commit 0082a26

Please sign in to comment.