use of dev.rosewood.rosestacker.event.PreDropStackedItemsEvent in project RoseStacker by Rosewood-Development.
the class StackingThread method preStackItems.
@Override
public void preStackItems(Collection<ItemStack> items, Location location) {
if (location.getWorld() == null)
return;
// Merge items and store their amounts
Map<ItemStack, Integer> itemStackAmounts = new HashMap<>();
for (ItemStack itemStack : items) {
if (itemStack == null || itemStack.getType() == Material.AIR)
continue;
Optional<Map.Entry<ItemStack, Integer>> similar = itemStackAmounts.entrySet().stream().filter(x -> x.getKey().isSimilar(itemStack)).findFirst();
if (similar.isPresent()) {
similar.get().setValue(similar.get().getValue() + itemStack.getAmount());
} else {
ItemStack clone = itemStack.clone();
clone.setAmount(1);
itemStackAmounts.put(clone, itemStack.getAmount());
}
}
// Fire the event to allow other plugins to manipulate the items before we stack and drop them
PreDropStackedItemsEvent event = new PreDropStackedItemsEvent(itemStackAmounts, location);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled())
return;
if (itemStackAmounts.isEmpty())
return;
// If stacking is disabled, drop the items separated by their max stack size
if (!this.stackManager.isItemStackingEnabled()) {
for (Map.Entry<ItemStack, Integer> entry : itemStackAmounts.entrySet()) {
ItemStack itemStack = entry.getKey();
int amount = entry.getValue();
while (amount > 0) {
int maxStackSize = itemStack.getMaxStackSize();
int stackSize = Math.min(amount, maxStackSize);
amount -= stackSize;
ItemStack toDrop = itemStack.clone();
toDrop.setAmount(stackSize);
location.getWorld().dropItemNaturally(location, toDrop);
}
}
return;
}
// Drop all the items stacked with the correct amounts
this.stackManager.setEntityStackingTemporarilyDisabled(true);
for (Map.Entry<ItemStack, Integer> entry : itemStackAmounts.entrySet()) {
if (entry.getValue() <= 0)
continue;
Item item = location.getWorld().dropItemNaturally(location, entry.getKey());
StackedItem stackedItem = new StackedItem(entry.getValue(), item);
this.addItemStack(stackedItem);
stackedItem.updateDisplay();
}
this.stackManager.setEntityStackingTemporarilyDisabled(false);
}
Aggregations