Search in sources :

Example 1 with ItemStackEvent

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);
    }
}
Also used : ItemStackSettings(dev.rosewood.rosestacker.stack.settings.ItemStackSettings) StackedEntityDataEntry(dev.rosewood.rosestacker.nms.storage.StackedEntityDataEntry) EntityUtils(dev.rosewood.rosestacker.utils.EntityUtils) Player(org.bukkit.entity.Player) ItemUtils(dev.rosewood.rosestacker.utils.ItemUtils) PreDropStackedItemsEvent(dev.rosewood.rosestacker.event.PreDropStackedItemsEvent) EntityStackSettings(dev.rosewood.rosestacker.stack.settings.EntityStackSettings) StackSettingManager(dev.rosewood.rosestacker.manager.StackSettingManager) Block(org.bukkit.block.Block) Location(org.bukkit.Location) World(org.bukkit.World) Map(java.util.Map) PersistentDataUtils(dev.rosewood.rosestacker.utils.PersistentDataUtils) Chunk(org.bukkit.Chunk) ItemStackSettings(dev.rosewood.rosestacker.stack.settings.ItemStackSettings) Material(org.bukkit.Material) Bukkit(org.bukkit.Bukkit) Setting(dev.rosewood.rosestacker.manager.ConfigurationManager.Setting) Entity(org.bukkit.entity.Entity) Predicate(java.util.function.Predicate) ItemStackClearEvent(dev.rosewood.rosestacker.event.ItemStackClearEvent) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) UUID(java.util.UUID) EntityType(org.bukkit.entity.EntityType) LivingEntity(org.bukkit.entity.LivingEntity) EntityCacheManager(dev.rosewood.rosestacker.manager.EntityCacheManager) Collectors(java.util.stream.Collectors) RosePlugin(dev.rosewood.rosegarden.RosePlugin) EntityUnstackEvent(dev.rosewood.rosestacker.event.EntityUnstackEvent) StackedEntityDataStorage(dev.rosewood.rosestacker.nms.storage.StackedEntityDataStorage) ItemStack(org.bukkit.inventory.ItemStack) Objects(java.util.Objects) StackManager(dev.rosewood.rosestacker.manager.StackManager) List(java.util.List) Entry(java.util.Map.Entry) BukkitTask(org.bukkit.scheduler.BukkitTask) Optional(java.util.Optional) StackerUtils(dev.rosewood.rosestacker.utils.StackerUtils) NPCsHook(dev.rosewood.rosestacker.hook.NPCsHook) DustOptions(org.bukkit.Particle.DustOptions) Item(org.bukkit.entity.Item) WorldGuardHook(dev.rosewood.rosestacker.hook.WorldGuardHook) HashMap(java.util.HashMap) FixedMetadataValue(org.bukkit.metadata.FixedMetadataValue) DataUtils(dev.rosewood.rosestacker.utils.DataUtils) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ItemStackEvent(dev.rosewood.rosestacker.event.ItemStackEvent) NMSHandler(dev.rosewood.rosestacker.nms.NMSHandler) SpawnReason(org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason) CreatureSpawner(org.bukkit.block.CreatureSpawner) Particle(org.bukkit.Particle) Vector(org.bukkit.util.Vector) EntityStackEvent(dev.rosewood.rosestacker.event.EntityStackEvent) EntityStackClearEvent(dev.rosewood.rosestacker.event.EntityStackClearEvent) ArmorStand(org.bukkit.entity.ArmorStand) NMSAdapter(dev.rosewood.rosestacker.nms.NMSAdapter) Item(org.bukkit.entity.Item) Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) HashSet(java.util.HashSet) ItemStackEvent(dev.rosewood.rosestacker.event.ItemStackEvent)

Aggregations

RosePlugin (dev.rosewood.rosegarden.RosePlugin)1 EntityStackClearEvent (dev.rosewood.rosestacker.event.EntityStackClearEvent)1 EntityStackEvent (dev.rosewood.rosestacker.event.EntityStackEvent)1 EntityUnstackEvent (dev.rosewood.rosestacker.event.EntityUnstackEvent)1 ItemStackClearEvent (dev.rosewood.rosestacker.event.ItemStackClearEvent)1 ItemStackEvent (dev.rosewood.rosestacker.event.ItemStackEvent)1 PreDropStackedItemsEvent (dev.rosewood.rosestacker.event.PreDropStackedItemsEvent)1 NPCsHook (dev.rosewood.rosestacker.hook.NPCsHook)1 WorldGuardHook (dev.rosewood.rosestacker.hook.WorldGuardHook)1 Setting (dev.rosewood.rosestacker.manager.ConfigurationManager.Setting)1 EntityCacheManager (dev.rosewood.rosestacker.manager.EntityCacheManager)1 StackManager (dev.rosewood.rosestacker.manager.StackManager)1 StackSettingManager (dev.rosewood.rosestacker.manager.StackSettingManager)1 NMSAdapter (dev.rosewood.rosestacker.nms.NMSAdapter)1 NMSHandler (dev.rosewood.rosestacker.nms.NMSHandler)1 StackedEntityDataEntry (dev.rosewood.rosestacker.nms.storage.StackedEntityDataEntry)1 StackedEntityDataStorage (dev.rosewood.rosestacker.nms.storage.StackedEntityDataStorage)1 EntityStackSettings (dev.rosewood.rosestacker.stack.settings.EntityStackSettings)1 ItemStackSettings (dev.rosewood.rosestacker.stack.settings.ItemStackSettings)1 DataUtils (dev.rosewood.rosestacker.utils.DataUtils)1