use of dev.rosewood.rosestacker.event.ItemStackEvent in project RoseStacker by Rosewood-Development.
the class StackingThread method tryStackItem.
/**
* Tries to stack a StackedItem with all other StackedItems
*
* @param stackedItem the StackedItem to try to stack
*/
private void tryStackItem(StackedItem stackedItem) {
ItemStackSettings stackSettings = stackedItem.getStackSettings();
if (stackSettings == null || !stackSettings.isStackingEnabled() || stackedItem.getItem().getPickupDelay() > 40)
return;
Item item = stackedItem.getItem();
if (this.isRemoved(item))
return;
Predicate<Entity> predicate = x -> x.getType() == EntityType.DROPPED_ITEM;
Set<Item> nearbyItems = this.entityCacheManager.getNearbyEntities(stackedItem.getLocation(), Setting.ITEM_MERGE_RADIUS.getDouble(), predicate).stream().map(x -> (Item) x).collect(Collectors.toSet());
Set<StackedItem> targetItems = new HashSet<>();
for (Item otherItem : nearbyItems) {
if (item == otherItem || otherItem.getPickupDelay() > 40 || !item.getItemStack().isSimilar(otherItem.getItemStack()) || this.isRemoved(otherItem))
continue;
StackedItem other = this.stackedItems.get(otherItem.getUniqueId());
if (other != null)
targetItems.add(other);
}
int totalSize = stackedItem.getStackSize();
Set<StackedItem> removable = new HashSet<>();
for (StackedItem target : targetItems) {
if (totalSize + target.getStackSize() <= stackSettings.getMaxStackSize()) {
totalSize += target.getStackSize();
removable.add(target);
}
}
StackedItem headStack = stackedItem;
for (StackedItem other : removable) {
StackedItem increased = headStack.compareTo(other) > 0 ? headStack : other;
StackedItem removed = increased == headStack ? other : headStack;
headStack = increased;
ItemStackEvent itemStackEvent = new ItemStackEvent(removed, increased);
Bukkit.getPluginManager().callEvent(itemStackEvent);
if (itemStackEvent.isCancelled())
continue;
increased.increaseStackSize(removed.getStackSize(), true);
if (Setting.ITEM_RESET_DESPAWN_TIMER_ON_MERGE.getBoolean())
// Reset the 5 minute pickup timer
increased.getItem().setTicksLived(1);
// Don't allow the item we just merged to get picked up or stacked
removed.getItem().setPickupDelay(100);
increased.getItem().setPickupDelay(5);
Runnable removeTask = () -> removed.getItem().remove();
if (Bukkit.isPrimaryThread()) {
removeTask.run();
} else {
Bukkit.getScheduler().runTask(this.rosePlugin, removeTask);
}
this.removeItemStack(removed);
}
}
Aggregations