Search in sources :

Example 1 with BlockStackEvent

use of dev.rosewood.rosestacker.event.BlockStackEvent in project RoseStacker by Rosewood-Development.

the class BlockListener method onBlockPlace.

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onBlockPlace(BlockPlaceEvent event) {
    StackManager stackManager = this.rosePlugin.getManager(StackManager.class);
    if (stackManager.isWorldDisabled(event.getPlayer().getWorld()))
        return;
    Player player = event.getPlayer();
    Block block = event.getBlock();
    if (block.getType() == Material.SPAWNER) {
        if (!stackManager.isSpawnerStackingEnabled() || !stackManager.isSpawnerTypeStackable(((CreatureSpawner) block.getState()).getSpawnedType()))
            return;
    } else {
        if (!stackManager.isBlockStackingEnabled() || !stackManager.isBlockTypeStackable(block))
            return;
    }
    Block against = event.getBlockAgainst();
    if (against.equals(block))
        against = against.getRelative(BlockFace.DOWN);
    // Get the block in the player's hand that's being placed
    ItemStack placedItem = player.getInventory().getItemInMainHand();
    boolean isOffHand = false;
    if (placedItem.getType() == Material.AIR || !placedItem.getType().isBlock()) {
        placedItem = player.getInventory().getItemInOffHand();
        isOffHand = true;
    }
    // Will be true if we are adding to an existing stack (including a stack of 1), or false if we are creating a new one from an itemstack with a stack value
    boolean isDistanceStack = false;
    boolean isAdditiveStack = against.getType() == block.getType();
    EntityType entityType = placedItem.getType() == Material.SPAWNER ? ItemUtils.getStackedItemEntityType(placedItem) : null;
    int stackAmount = ItemUtils.getStackedItemStackAmount(placedItem);
    // See if we can stack the spawner (if applicable) into one nearby
    int autoStackRange = Setting.SPAWNER_AUTO_STACK_RANGE.getInt();
    boolean autoStackChunk = Setting.SPAWNER_AUTO_STACK_CHUNK.getBoolean();
    boolean useAutoStack = autoStackRange > 0 || autoStackChunk;
    if (useAutoStack && block.getType() == Material.SPAWNER) {
        StackedSpawner nearest = null;
        boolean anyNearby = false;
        List<StackedSpawner> spawners = new ArrayList<>(stackManager.getStackingThread(block.getWorld()).getStackedSpawners().values());
        if (!autoStackChunk) {
            double closestDistance = autoStackRange * autoStackRange;
            for (StackedSpawner spawner : spawners) {
                double distance = spawner.getLocation().distanceSquared(block.getLocation());
                if (distance < closestDistance) {
                    boolean sameType = spawner.getSpawnerTile().getSpawnedType() == entityType;
                    anyNearby = Setting.SPAWNER_AUTO_STACK_PREVENT_MULTIPLE_IN_RANGE.getBoolean() || sameType;
                    if (sameType && spawner.getStackSize() + stackAmount <= spawner.getStackSettings().getMaxStackSize()) {
                        closestDistance = distance;
                        nearest = spawner;
                    }
                }
            }
        } else {
            Chunk chunk = block.getChunk();
            for (StackedSpawner spawner : spawners) {
                if (chunk == spawner.getBlock().getChunk()) {
                    boolean sameType = spawner.getSpawnerTile().getSpawnedType() == entityType;
                    anyNearby = Setting.SPAWNER_AUTO_STACK_PREVENT_MULTIPLE_IN_RANGE.getBoolean() || sameType;
                    if (sameType && spawner.getStackSize() + stackAmount <= spawner.getStackSettings().getMaxStackSize()) {
                        nearest = spawner;
                        break;
                    }
                }
            }
        }
        if (nearest != null) {
            against = nearest.getBlock();
            isAdditiveStack = true;
            isDistanceStack = true;
        } else if (anyNearby) {
            event.setCancelled(true);
            return;
        }
    }
    // Don't allow placing if they don't have permission
    if (entityType != null && Setting.SPAWNER_ADVANCED_PERMISSIONS.getBoolean() && !player.hasPermission("rosestacker.spawnerplace." + entityType.name().toLowerCase())) {
        this.rosePlugin.getManager(LocaleManager.class).sendMessage(player, "spawner-advanced-place-no-permission");
        event.setCancelled(true);
        return;
    }
    if (isAdditiveStack && against.getType() == Material.SPAWNER)
        isAdditiveStack = ((CreatureSpawner) against.getState()).getSpawnedType() == entityType;
    if (isAdditiveStack && (!player.isSneaking() || isDistanceStack)) {
        if (block.getType() == Material.SPAWNER) {
            if (!stackManager.isSpawnerTypeStackable(entityType))
                return;
            // Handle spawner stacking
            StackedSpawner stackedSpawner = stackManager.getStackedSpawner(against);
            if (stackedSpawner != null && stackedSpawner.getStackSize() + stackAmount > stackedSpawner.getStackSettings().getMaxStackSize()) {
                event.setCancelled(true);
                return;
            }
            if (stackedSpawner != null) {
                SpawnerStackEvent spawnerStackEvent = new SpawnerStackEvent(player, stackedSpawner, stackAmount, false);
                Bukkit.getPluginManager().callEvent(spawnerStackEvent);
                if (spawnerStackEvent.isCancelled()) {
                    event.setCancelled(true);
                    return;
                }
                stackAmount = spawnerStackEvent.getIncreaseAmount();
            } else {
                stackedSpawner = stackManager.createSpawnerStack(against, 1, false);
                SpawnerStackEvent spawnerStackEvent = new SpawnerStackEvent(player, stackedSpawner, stackAmount, true);
                Bukkit.getPluginManager().callEvent(spawnerStackEvent);
                if (spawnerStackEvent.isCancelled()) {
                    event.setCancelled(true);
                    return;
                }
                if (stackedSpawner.getStackSize() + stackAmount > stackedSpawner.getStackSettings().getMaxStackSize()) {
                    event.setCancelled(true);
                    return;
                }
            }
            stackedSpawner.increaseStackSize(stackAmount);
            // Fling particles from the attempted place location to the actual place location
            if (isDistanceStack && Setting.SPAWNER_AUTO_STACK_PARTICLES.getBoolean()) {
                for (int i = 0; i < 50; i++) {
                    Vector offset = Vector.getRandom();
                    Location startLoc = block.getLocation().clone().add(offset);
                    Vector start = startLoc.toVector();
                    Vector end = against.getLocation().toVector().add(offset).add(new Vector(0.0, 0.1, 0.0));
                    Vector angle = end.clone().subtract(start);
                    double length = angle.length() * 0.09;
                    angle.normalize();
                    player.spawnParticle(Particle.END_ROD, startLoc, 0, angle.getX(), angle.getY(), angle.getZ(), length);
                }
            }
        } else {
            if (!stackManager.isBlockTypeStackable(against))
                return;
            // Handle normal block stacking
            StackedBlock stackedBlock = stackManager.getStackedBlock(against);
            if (stackedBlock != null) {
                if (stackedBlock.isLocked()) {
                    event.setCancelled(true);
                    return;
                }
                if (stackedBlock.getStackSize() + stackAmount > stackedBlock.getStackSettings().getMaxStackSize()) {
                    event.setCancelled(true);
                    return;
                }
                BlockStackEvent blockStackEvent = new BlockStackEvent(player, stackedBlock, stackAmount, false);
                Bukkit.getPluginManager().callEvent(blockStackEvent);
                if (blockStackEvent.isCancelled()) {
                    event.setCancelled(true);
                    return;
                }
                stackAmount = blockStackEvent.getIncreaseAmount();
            } else {
                stackedBlock = stackManager.createBlockStack(against, 1);
                BlockStackEvent blockStackEvent = new BlockStackEvent(player, stackedBlock, stackAmount, false);
                Bukkit.getPluginManager().callEvent(blockStackEvent);
                if (blockStackEvent.isCancelled()) {
                    event.setCancelled(true);
                    return;
                }
                if (stackedBlock.getStackSize() + stackAmount > stackedBlock.getStackSettings().getMaxStackSize()) {
                    event.setCancelled(true);
                    return;
                }
            }
            stackedBlock.increaseStackSize(stackAmount);
        }
        event.setCancelled(true);
        BlockLoggingHook.recordBlockPlace(player, against);
    } else {
        // Handle singular items that have a stack multiplier
        // Set the spawner type
        StackSettingManager stackSettingManager = this.rosePlugin.getManager(StackSettingManager.class);
        if (placedItem.getType() == Material.SPAWNER) {
            EntityType spawnedType = ItemUtils.getStackedItemEntityType(placedItem);
            if (spawnedType == null)
                return;
            SpawnerStackSettings spawnerStackSettings = stackSettingManager.getSpawnerStackSettings(spawnedType);
            if (spawnerStackSettings == null)
                return;
            if (stackAmount <= 0)
                return;
            if (stackAmount > spawnerStackSettings.getMaxStackSize()) {
                event.setCancelled(true);
                return;
            }
            StackedSpawner tempStackedSpawner = new StackedSpawner(0, block, true);
            SpawnerStackEvent spawnerStackEvent = new SpawnerStackEvent(player, tempStackedSpawner, stackAmount, true);
            Bukkit.getPluginManager().callEvent(spawnerStackEvent);
            if (spawnerStackEvent.isCancelled()) {
                tempStackedSpawner.setStackSize(0);
                event.setCancelled(true);
                return;
            }
            stackAmount = spawnerStackEvent.getIncreaseAmount();
            StackedSpawner stackedSpawner = stackManager.createSpawnerStack(block, stackAmount, true);
            if (stackedSpawner != null) {
                stackedSpawner.getSpawnerTile().setSpawnedType(spawnedType);
                stackedSpawner.updateSpawnerProperties(true);
            }
        } else {
            if (stackAmount <= 1)
                return;
            BlockStackSettings blockStackSettings = stackSettingManager.getBlockStackSettings(block);
            if (blockStackSettings == null)
                return;
            if (stackAmount > blockStackSettings.getMaxStackSize()) {
                event.setCancelled(true);
                return;
            }
            StackedBlock tempStackedBlock = new StackedBlock(0, block);
            BlockStackEvent blockStackEvent = new BlockStackEvent(player, tempStackedBlock, stackAmount, true);
            Bukkit.getPluginManager().callEvent(blockStackEvent);
            if (blockStackEvent.isCancelled()) {
                tempStackedBlock.setStackSize(0);
                event.setCancelled(true);
                return;
            }
            stackManager.createBlockStack(block, stackAmount);
        }
    }
    // Take an item from the player's hand
    ItemUtils.takeOneItem(player, isOffHand ? EquipmentSlot.OFF_HAND : EquipmentSlot.HAND);
}
Also used : SpawnerStackSettings(dev.rosewood.rosestacker.stack.settings.SpawnerStackSettings) Player(org.bukkit.entity.Player) StackedSpawner(dev.rosewood.rosestacker.stack.StackedSpawner) StackManager(dev.rosewood.rosestacker.manager.StackManager) ArrayList(java.util.ArrayList) StackSettingManager(dev.rosewood.rosestacker.manager.StackSettingManager) Chunk(org.bukkit.Chunk) BlockStackSettings(dev.rosewood.rosestacker.stack.settings.BlockStackSettings) EntityType(org.bukkit.entity.EntityType) StackedBlock(dev.rosewood.rosestacker.stack.StackedBlock) Block(org.bukkit.block.Block) StackedBlock(dev.rosewood.rosestacker.stack.StackedBlock) LocaleManager(dev.rosewood.rosestacker.manager.LocaleManager) SpawnerStackEvent(dev.rosewood.rosestacker.event.SpawnerStackEvent) ItemStack(org.bukkit.inventory.ItemStack) Vector(org.bukkit.util.Vector) BlockStackEvent(dev.rosewood.rosestacker.event.BlockStackEvent) Location(org.bukkit.Location) EventHandler(org.bukkit.event.EventHandler)

Example 2 with BlockStackEvent

use of dev.rosewood.rosestacker.event.BlockStackEvent in project RoseStacker by Rosewood-Development.

the class StackedBlockGui method updateStackedBlock.

/**
 * Updates the StackedBlock with the changed item contents
 *
 * @param player The Player that finalized the change
 * @param items The Items that are inside the StackedBlock
 */
private void updateStackedBlock(Player player, List<ItemStack> items) {
    StackManager stackManager = this.rosePlugin.getManager(StackManager.class);
    // No longer any players viewing
    this.guiContainer = null;
    int newStackSize = items.stream().mapToInt(ItemStack::getAmount).sum();
    if (newStackSize == this.stackedBlock.getStackSize())
        return;
    int difference = newStackSize - this.stackedBlock.getStackSize();
    if (newStackSize > this.stackedBlock.getStackSize()) {
        BlockStackEvent blockStackEvent = new BlockStackEvent(player, this.stackedBlock, difference, false);
        Bukkit.getPluginManager().callEvent(blockStackEvent);
        if (blockStackEvent.isCancelled()) {
            ItemUtils.dropItemsToPlayer(player, GuiUtil.getMaterialAmountAsItemStacks(this.stackedBlock.getBlock().getType(), difference));
            return;
        }
        newStackSize = this.stackedBlock.getStackSize() + blockStackEvent.getIncreaseAmount();
    } else {
        BlockUnstackEvent blockUnstackEvent = new BlockUnstackEvent(player, this.stackedBlock, -difference);
        Bukkit.getPluginManager().callEvent(blockUnstackEvent);
        if (blockUnstackEvent.isCancelled()) {
            this.takeFromPlayer(player, -difference);
            return;
        }
        newStackSize = this.stackedBlock.getStackSize() - blockUnstackEvent.getDecreaseAmount();
    }
    this.stackedBlock.setStackSize(newStackSize);
    int maxStackSize = this.stackedBlock.getStackSettings().getMaxStackSize();
    if (newStackSize == 1) {
        stackManager.removeBlockStack(this.stackedBlock);
    } else if (newStackSize == 0) {
        stackManager.removeBlockStack(this.stackedBlock);
        this.stackedBlock.getBlock().setType(Material.AIR);
    } else if (newStackSize > maxStackSize) {
        List<ItemStack> overflowItems = GuiUtil.getMaterialAmountAsItemStacks(this.stackedBlock.getBlock().getType(), newStackSize - maxStackSize);
        ItemUtils.dropItemsToPlayer(player, overflowItems);
        this.stackedBlock.setStackSize(maxStackSize);
    }
}
Also used : StackManager(dev.rosewood.rosestacker.manager.StackManager) ItemStack(org.bukkit.inventory.ItemStack) BlockStackEvent(dev.rosewood.rosestacker.event.BlockStackEvent) BlockUnstackEvent(dev.rosewood.rosestacker.event.BlockUnstackEvent)

Aggregations

BlockStackEvent (dev.rosewood.rosestacker.event.BlockStackEvent)2 StackManager (dev.rosewood.rosestacker.manager.StackManager)2 ItemStack (org.bukkit.inventory.ItemStack)2 BlockUnstackEvent (dev.rosewood.rosestacker.event.BlockUnstackEvent)1 SpawnerStackEvent (dev.rosewood.rosestacker.event.SpawnerStackEvent)1 LocaleManager (dev.rosewood.rosestacker.manager.LocaleManager)1 StackSettingManager (dev.rosewood.rosestacker.manager.StackSettingManager)1 StackedBlock (dev.rosewood.rosestacker.stack.StackedBlock)1 StackedSpawner (dev.rosewood.rosestacker.stack.StackedSpawner)1 BlockStackSettings (dev.rosewood.rosestacker.stack.settings.BlockStackSettings)1 SpawnerStackSettings (dev.rosewood.rosestacker.stack.settings.SpawnerStackSettings)1 ArrayList (java.util.ArrayList)1 Chunk (org.bukkit.Chunk)1 Location (org.bukkit.Location)1 Block (org.bukkit.block.Block)1 EntityType (org.bukkit.entity.EntityType)1 Player (org.bukkit.entity.Player)1 EventHandler (org.bukkit.event.EventHandler)1 Vector (org.bukkit.util.Vector)1