Skip to content

Commit

Permalink
Moved unrelated items to Util class
Browse files Browse the repository at this point in the history
  • Loading branch information
nadiamoe committed Jul 6, 2019
1 parent b75fa0f commit 4bf786e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 39 deletions.
47 changes: 8 additions & 39 deletions src/main/java/es/roobre/chestorganizer/ChestOrganizer.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
package es.roobre.chestorganizer;

import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.BlockState;
import org.bukkit.block.Chest;
import org.bukkit.block.Container;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
Expand All @@ -23,6 +15,11 @@
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.Set;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public final class ChestOrganizer extends JavaPlugin implements Listener {
private Logger log = getLogger();

Expand Down Expand Up @@ -123,7 +120,7 @@ private void organize(Container container, ItemStack items) {
this,
() -> {
ItemStack notAdded = targetChest.getInventory().addItem(items.clone()).get(0);
int notRemoved = removeItems(container.getInventory(), items.getType(), items.getAmount() - (notAdded == null ? 0 : notAdded.getAmount()));
int notRemoved = Util.removeItems(container.getInventory(), items.getType(), items.getAmount() - (notAdded == null ? 0 : notAdded.getAmount()));

log.info("Moved " + items.getAmount() + " " + items.getType() + " from " + container.getBlock().getLocation() + " to " + targetChest.getBlock().getLocation());

Expand Down Expand Up @@ -165,42 +162,14 @@ private Container findSuitable(Location chestLocation, Material material) {
return bestCandidate;
}

/**
* 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
*/
private 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;
}

/**
* Checks if the given location contains an unlocked chest with at least one instance of the given item
*
* @param block Entity to check
* @param mat Item to look for
* @return A suitable chest (as InventoryHolder), or null if it wasn't suitable
*/
private static Container isSuitableReceiver(BlockState block, Material mat) {
private Container isSuitableReceiver(BlockState block, Material mat) {
// TODO: add array of accepted types of containers instead (just like we check it for the source)
if (block instanceof Container) {
Container container = (Container) block;
Expand All @@ -220,7 +189,7 @@ private static Container isSuitableReceiver(BlockState block, Material mat) {
* @param holder The holder to check
* @return Whether the chest is an organizer or not
*/
private static boolean isOrganizer(InventoryHolder holder) {
private boolean isOrganizer(InventoryHolder holder) {
// check if the holder is a container
if (holder instanceof Container) {
Container container = (Container) holder;
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/es/roobre/chestorganizer/Util.java
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;
}
}

0 comments on commit 4bf786e

Please sign in to comment.