Search in sources :

Example 1 with PreDropStackedItemsEvent

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);
}
Also used : 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) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Item(org.bukkit.entity.Item) StackedEntityDataEntry(dev.rosewood.rosestacker.nms.storage.StackedEntityDataEntry) Entry(java.util.Map.Entry) ItemStack(org.bukkit.inventory.ItemStack) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) PreDropStackedItemsEvent(dev.rosewood.rosestacker.event.PreDropStackedItemsEvent)

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