Permalink
Browse files

Optimised the ranged collector for multi-item collections.

  • Loading branch information...
me4502 committed Aug 19, 2017
1 parent 52bf6d5 commit 6213cd1a27d116fe93a4a19accbcde826528df3f
@@ -1,5 +1,6 @@
package com.sk89q.craftbook.mechanics.ic.gates.world.items;
import com.google.common.collect.Lists;
import com.sk89q.craftbook.ChangedSign;
import com.sk89q.craftbook.bukkit.util.BukkitUtil;
import com.sk89q.craftbook.mechanics.ic.AbstractICFactory;
@@ -21,6 +22,7 @@
import org.bukkit.Server;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.Chest;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Item;
import org.bukkit.inventory.InventoryHolder;
@@ -104,6 +106,8 @@ public boolean collect() {
boolean collected = false;
List<Item> itemsForChest = Lists.newArrayList();
for (Entity entity : LocationUtil.getNearbyEntities(centre, radius)) {
if (entity.isValid() && entity instanceof Item) {
ItemStack stack = ((Item) entity).getItemStack();
@@ -145,22 +149,33 @@ public boolean collect() {
return true;
}
if(!InventoryUtil.doesBlockHaveInventory(chest))
return false;
itemsForChest.add((Item) entity);
}
}
if (!itemsForChest.isEmpty()) {
if(!InventoryUtil.doesBlockHaveInventory(chest))
return false;
// Add the items to a container, and destroy them.
List<ItemStack> leftovers = InventoryUtil.addItemsToInventory((InventoryHolder)chest.getState(), stack);
if(leftovers.isEmpty()) {
Chest chestState = (Chest) chest.getState();
// Add the items to a container, and destroy them.
for (Item entity : itemsForChest) {
ItemStack stack = entity.getItemStack();
List<ItemStack> leftovers = InventoryUtil.addItemsToInventory(chestState, false, stack);
if (leftovers.isEmpty()) {
entity.remove();
return true;
} else {
if(ItemUtil.areItemsIdentical(leftovers.get(0), stack) && leftovers.get(0).getAmount() != stack.getAmount()) {
((Item) entity).setItemStack(leftovers.get(0));
return true;
if (ItemUtil.areItemsIdentical(leftovers.get(0), stack) && leftovers.get(0).getAmount() != stack.getAmount()) {
entity.setItemStack(leftovers.get(0));
}
}
collected = true;
}
if (collected) {
chestState.update();
}
}
return collected;
@@ -30,14 +30,25 @@
* @return The stacks that could not be added.
*/
public static ArrayList<ItemStack> addItemsToInventory(InventoryHolder container, ItemStack ... stacks) {
return addItemsToInventory(container, true, stacks);
}
/**
* Adds items to an inventory, returning the leftovers.
*
* @param container The InventoryHolder to add the items to.
* @param stacks The stacks to add to the inventory.
* @return The stacks that could not be added.
*/
public static ArrayList<ItemStack> addItemsToInventory(InventoryHolder container, boolean update, ItemStack ... stacks) {
if(container instanceof Furnace) {
return addItemsToFurnace((Furnace) container, stacks);
} else if(container instanceof BrewingStand) {
return addItemsToBrewingStand((BrewingStand) container, stacks);
} else { //Basic inventories like chests, dispensers, storage carts, etc.
ArrayList<ItemStack> leftovers = new ArrayList<>(container.getInventory().addItem(stacks).values());
if(container instanceof BlockState)
if(container instanceof BlockState && update)
((BlockState) container).update();
return leftovers;
}

0 comments on commit 6213cd1

Please sign in to comment.