Search in sources :

Example 6 with StackedItem

use of dev.rosewood.rosestacker.stack.StackedItem in project RoseStacker by Rosewood-Development.

the class ItemListener method onItemPickup.

@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onItemPickup(EntityPickupItemEvent event) {
    if (this.stackManager.isWorldDisabled(event.getEntity().getWorld()))
        return;
    if (!this.stackManager.isItemStackingEnabled())
        return;
    StackedItem stackedItem = this.stackManager.getStackedItem(event.getItem());
    if (stackedItem == null)
        return;
    Inventory inventory;
    if (event.getEntity() instanceof Player) {
        Player player = (Player) event.getEntity();
        inventory = player.getInventory();
    } else if (event.getEntity() instanceof Villager) {
        Villager villager = (Villager) event.getEntity();
        inventory = villager.getInventory();
    } else if (event.getEntityType() == EntityType.DOLPHIN) {
        // Only stop the dolphin from picking up the item if it's larger than a normal item would be, otherwise
        // cancel the event, give the dolphin an item of max normal stack size, and reduce the stacked item size
        int maxStack = event.getItem().getItemStack().getType().getMaxStackSize();
        if (stackedItem.getStackSize() > maxStack) {
            ItemStack clone = event.getItem().getItemStack().clone();
            clone.setAmount(maxStack);
            stackedItem.setStackSize(stackedItem.getStackSize() - maxStack);
            EntityEquipment equipment = event.getEntity().getEquipment();
            if (equipment != null)
                equipment.setItemInMainHand(clone);
            // Stun the item temporarily to avoid it getting instantly stacked back into
            stackedItem.getItem().setPickupDelay(50);
            event.setCancelled(true);
        }
        return;
    } else {
        // Only pick up one item
        if (stackedItem.getStackSize() > 1) {
            this.stackManager.splitItemStack(stackedItem, 1);
            event.setCancelled(true);
        }
        return;
    }
    if (this.applyInventoryItemPickup(inventory, stackedItem, event.getEntity()))
        event.setCancelled(true);
}
Also used : Player(org.bukkit.entity.Player) EntityEquipment(org.bukkit.inventory.EntityEquipment) Villager(org.bukkit.entity.Villager) ItemStack(org.bukkit.inventory.ItemStack) StackedItem(dev.rosewood.rosestacker.stack.StackedItem) Inventory(org.bukkit.inventory.Inventory) PlayerInventory(org.bukkit.inventory.PlayerInventory) EventHandler(org.bukkit.event.EventHandler)

Example 7 with StackedItem

use of dev.rosewood.rosestacker.stack.StackedItem in project RoseStacker by Rosewood-Development.

the class ItemConversionHandler method handleConversion.

@Override
public Set<Stack<?>> handleConversion(Set<ConversionData> conversionData) {
    Set<Stack<?>> stacks = new HashSet<>();
    for (ConversionData data : conversionData) {
        StackedItem stackedItem = new StackedItem(data.getStackSize(), data.getItem());
        this.stackManager.addItemStack(stackedItem);
        stacks.add(stackedItem);
    }
    return stacks;
}
Also used : ConversionData(dev.rosewood.rosestacker.conversion.ConversionData) StackedItem(dev.rosewood.rosestacker.stack.StackedItem) Stack(dev.rosewood.rosestacker.stack.Stack) HashSet(java.util.HashSet)

Example 8 with StackedItem

use of dev.rosewood.rosestacker.stack.StackedItem in project RoseStacker by Rosewood-Development.

the class WildStackerItemConversionHandler method handleConversion.

@Override
public Set<Stack<?>> handleConversion(Set<ConversionData> conversionData) {
    Set<Item> items = conversionData.stream().map(ConversionData::getItem).filter(Objects::nonNull).collect(Collectors.toSet());
    Set<Stack<?>> stacks = new HashSet<>();
    for (Item item : items) {
        PersistentDataContainer dataContainer = item.getPersistentDataContainer();
        if (dataContainer.has(PersistentDataUtils.CONVERTED_KEY, PersistentDataType.INTEGER))
            continue;
        int stackSize = dataContainer.getOrDefault(STACK_KEY, PersistentDataType.INTEGER, -1);
        if (stackSize == -1)
            continue;
        dataContainer.set(PersistentDataUtils.CONVERTED_KEY, PersistentDataType.INTEGER, 1);
        StackedItem stackedItem = new StackedItem(stackSize, item);
        this.stackManager.addItemStack(stackedItem);
        stacks.add(stackedItem);
    }
    return stacks;
}
Also used : Item(org.bukkit.entity.Item) StackedItem(dev.rosewood.rosestacker.stack.StackedItem) ConversionData(dev.rosewood.rosestacker.conversion.ConversionData) StackedItem(dev.rosewood.rosestacker.stack.StackedItem) Stack(dev.rosewood.rosestacker.stack.Stack) HashSet(java.util.HashSet) PersistentDataContainer(org.bukkit.persistence.PersistentDataContainer)

Aggregations

StackedItem (dev.rosewood.rosestacker.stack.StackedItem)8 Item (org.bukkit.entity.Item)4 EventHandler (org.bukkit.event.EventHandler)4 ConversionData (dev.rosewood.rosestacker.conversion.ConversionData)3 Stack (dev.rosewood.rosestacker.stack.Stack)3 HashSet (java.util.HashSet)3 ItemStack (org.bukkit.inventory.ItemStack)3 StackedEntity (dev.rosewood.rosestacker.stack.StackedEntity)2 Entity (org.bukkit.entity.Entity)2 LivingEntity (org.bukkit.entity.LivingEntity)2 Player (org.bukkit.entity.Player)2 Inventory (org.bukkit.inventory.Inventory)2 PlayerInventory (org.bukkit.inventory.PlayerInventory)2 PersistentDataContainer (org.bukkit.persistence.PersistentDataContainer)2 StackedBlock (dev.rosewood.rosestacker.stack.StackedBlock)1 StackedSpawner (dev.rosewood.rosestacker.stack.StackedSpawner)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 Block (org.bukkit.block.Block)1 CreatureSpawner (org.bukkit.block.CreatureSpawner)1