-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
50 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package es.roobre.chestorganizer; | ||
|
||
import org.bukkit.Material; | ||
import org.bukkit.block.BlockState; | ||
import org.bukkit.block.Container; | ||
import org.bukkit.inventory.Inventory; | ||
import org.bukkit.inventory.InventoryHolder; | ||
import org.bukkit.inventory.ItemStack; | ||
|
||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class Util { | ||
/** | ||
* Removes the specified number of items matching a given type from an inventory. | ||
* Stacks of items will be removed until the specified amount has been reached, or until there are no more items to remove. | ||
* Smaller stacks of items are removed first, in an attempt to keep the inventory as tidy as possible. | ||
* | ||
* @param src Inventory to remove items from | ||
* @param type Material to target | ||
* @param amount Maximum amount of items to remove | ||
* @return The number of items that could not be removed, if amount was higher than the total number of items in the inventory | ||
*/ | ||
protected static int removeItems(Inventory src, Material type, int amount) { | ||
List<ItemStack> stackList = src.all(type).values().stream() | ||
.sorted(Comparator.comparingInt(ItemStack::getAmount)) | ||
.collect(Collectors.toList()); | ||
|
||
for (ItemStack stack : stackList) { | ||
int toRemove = Math.min(amount, stack.getAmount()); | ||
stack.setAmount(stack.getAmount() - toRemove); | ||
amount -= toRemove; | ||
|
||
if (amount == 0) { | ||
break; // We are done removing | ||
} | ||
} | ||
|
||
return amount; | ||
} | ||
} |