Search in sources :

Example 6 with StackedSpawner

use of dev.rosewood.rosestacker.stack.StackedSpawner 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 7 with StackedSpawner

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

the class BlockListener method onBlockClicked.

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onBlockClicked(PlayerInteractEvent event) {
    Block block = event.getClickedBlock();
    if (block == null)
        return;
    StackManager stackManager = this.rosePlugin.getManager(StackManager.class);
    if (stackManager.isWorldDisabled(event.getPlayer().getWorld()))
        return;
    // Check for igniting stacked TNT
    ItemStack item = event.getItem();
    if (item != null && stackManager.isBlockStackingEnabled() && block.getType() == Material.TNT && stackManager.isBlockStacked(block) && (item.getType() == Material.FLINT_AND_STEEL || item.getType() == Material.FIRE_CHARGE)) {
        event.setUseInteractedBlock(Event.Result.DENY);
        return;
    }
    if (event.getPlayer().isSneaking() && event.getAction() == Action.RIGHT_CLICK_BLOCK) {
        if (stackManager.isBlockStackingEnabled() && Setting.BLOCK_GUI_ENABLED.getBoolean()) {
            StackedBlock stackedBlock = stackManager.getStackedBlock(block);
            if (stackedBlock != null) {
                stackedBlock.openGui(event.getPlayer());
                event.setCancelled(true);
                return;
            }
        }
        if (stackManager.isSpawnerStackingEnabled() && block.getType() == Material.SPAWNER && Setting.SPAWNER_GUI_ENABLED.getBoolean()) {
            StackedSpawner stackedSpawner = stackManager.getStackedSpawner(block);
            if (stackedSpawner == null)
                // Doesn't exist, need it to in order to open the GUI
                stackManager.createSpawnerStack(block, 1, false);
            if (stackedSpawner != null) {
                stackedSpawner.openGui(event.getPlayer());
                event.setCancelled(true);
            }
        }
    }
}
Also used : StackedSpawner(dev.rosewood.rosestacker.stack.StackedSpawner) StackManager(dev.rosewood.rosestacker.manager.StackManager) StackedBlock(dev.rosewood.rosestacker.stack.StackedBlock) Block(org.bukkit.block.Block) StackedBlock(dev.rosewood.rosestacker.stack.StackedBlock) ItemStack(org.bukkit.inventory.ItemStack) EventHandler(org.bukkit.event.EventHandler)

Example 8 with StackedSpawner

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

the class InteractListener method onInteract.

@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onInteract(PlayerInteractEvent event) {
    Block clickedBlock = event.getClickedBlock();
    ItemStack item = event.getItem();
    if (item == null || event.getAction() != Action.RIGHT_CLICK_BLOCK || clickedBlock == null)
        return;
    StackManager stackManager = this.rosePlugin.getManager(StackManager.class);
    if (stackManager.isWorldDisabled(event.getPlayer().getWorld()))
        return;
    if (stackManager.isSpawnerStackingEnabled()) {
        // Handle spawner conversion before we try to spawn entities
        if (clickedBlock.getType() == Material.SPAWNER && ItemUtils.isSpawnEgg(item.getType()) && ItemUtils.getStackedItemStackAmount(item) == 1) {
            StackedSpawner stackedSpawner = stackManager.getStackedSpawner(clickedBlock);
            if (stackedSpawner == null)
                stackedSpawner = stackManager.createSpawnerStack(clickedBlock, 1, false);
            EntityStackSettings stackSettings = this.rosePlugin.getManager(StackSettingManager.class).getEntityStackSettings(item.getType());
            if (stackSettings != null && stackSettings.getEntityType() == stackedSpawner.getSpawnerTile().getSpawnedType()) {
                // Don't allow converting spawners if it's the exact same type... that just wastes spawn eggs
                event.setCancelled(true);
                return;
            }
            if (!event.getPlayer().hasPermission("rosestacker.spawnerconvert")) {
                event.setCancelled(true);
                return;
            }
            if (Setting.SPAWNER_CONVERT_REQUIRE_SAME_AMOUNT.getBoolean() && item.getAmount() < stackedSpawner.getStackSize() && event.getPlayer().getGameMode() != GameMode.CREATIVE) {
                event.setCancelled(true);
                this.rosePlugin.getManager(LocaleManager.class).sendMessage(event.getPlayer(), "spawner-convert-not-enough");
                return;
            }
            StackedSpawner finalStackedSpawner = stackedSpawner;
            Bukkit.getScheduler().runTask(this.rosePlugin, () -> {
                // Make sure spawners convert and update their display properly
                finalStackedSpawner.updateSpawnerProperties(false);
                finalStackedSpawner.updateDisplay();
                if (finalStackedSpawner.getStackSize() != 1 && event.getPlayer().getGameMode() != GameMode.CREATIVE)
                    item.setAmount(item.getAmount() - finalStackedSpawner.getStackSize() + 1);
            });
            return;
        }
    }
    Location spawnLocation = clickedBlock.getRelative(event.getBlockFace()).getLocation();
    // Center on block
    spawnLocation.add(0.5, 0, 0.5);
    if (this.spawnEntities(null, spawnLocation, item) && clickedBlock.getType() != Material.SPAWNER) {
        ItemUtils.takeOneItem(event.getPlayer(), event.getHand());
        event.setCancelled(true);
    }
}
Also used : EntityStackSettings(dev.rosewood.rosestacker.stack.settings.EntityStackSettings) StackedSpawner(dev.rosewood.rosestacker.stack.StackedSpawner) StackManager(dev.rosewood.rosestacker.manager.StackManager) Block(org.bukkit.block.Block) StackSettingManager(dev.rosewood.rosestacker.manager.StackSettingManager) LocaleManager(dev.rosewood.rosestacker.manager.LocaleManager) ItemStack(org.bukkit.inventory.ItemStack) Location(org.bukkit.Location) EventHandler(org.bukkit.event.EventHandler)

Example 9 with StackedSpawner

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

the class StackToolListener method onInteract.

/**
 * Handles Shift Left Click for StackedItems
 *
 * @param event The PlayerInteractEvent
 */
@EventHandler(priority = EventPriority.LOWEST)
public void onInteract(PlayerInteractEvent event) {
    Player player = event.getPlayer();
    ItemStack tool = player.getInventory().getItemInMainHand();
    if (event.getAction() == Action.PHYSICAL || !ItemUtils.isStackingTool(tool))
        return;
    event.setCancelled(true);
    if (!player.isSneaking() || event.getHand() != EquipmentSlot.HAND)
        return;
    if (!player.hasPermission("rosestacker.stacktool")) {
        this.localeManager.sendMessage(player, "command-stacktool-no-permission");
        return;
    }
    if (event.getAction() == Action.LEFT_CLICK_AIR || event.getAction() == Action.LEFT_CLICK_BLOCK) {
        for (Entity entity : player.getNearbyEntities(3, 3, 3)) {
            if (entity.getType() != EntityType.DROPPED_ITEM)
                continue;
            Item item = (Item) entity;
            if (!EntityUtils.isLookingAtItem(player, item))
                continue;
            StackedItem stackedItem = this.stackManager.getStackedItem(item);
            if (stackedItem == null)
                continue;
            ItemMeta itemMeta = item.getItemStack().getItemMeta();
            this.localeManager.sendMessage(player, "command-stacktool-info");
            this.localeManager.sendSimpleMessage(player, "command-stacktool-info-uuid", StringPlaceholders.single("uuid", item.getUniqueId().toString()));
            this.localeManager.sendSimpleMessage(player, "command-stacktool-info-entity-id", StringPlaceholders.single("id", item.getEntityId()));
            this.localeManager.sendSimpleMessage(player, "command-stacktool-info-item-type", StringPlaceholders.single("type", item.getItemStack().getType().name()));
            this.localeManager.sendSimpleMessage(player, "command-stacktool-info-stack-size", StringPlaceholders.single("amount", stackedItem.getStackSize()));
            if (itemMeta != null && itemMeta.hasDisplayName())
                this.localeManager.sendSimpleMessage(player, "command-stacktool-info-custom-name", StringPlaceholders.single("name", itemMeta.getDisplayName()));
            this.localeManager.sendSimpleMessage(player, "command-stacktool-info-location", StringPlaceholders.builder("x", item.getLocation().getBlockX()).addPlaceholder("y", item.getLocation().getBlockY()).addPlaceholder("z", item.getLocation().getBlockZ()).addPlaceholder("world", item.getWorld().getName()).build());
            this.localeManager.sendSimpleMessage(player, "command-stacktool-info-chunk", StringPlaceholders.builder("x", item.getLocation().getChunk().getX()).addPlaceholder("z", item.getLocation().getChunk().getZ()).build());
            return;
        }
    }
    if (event.getAction() == Action.LEFT_CLICK_BLOCK) {
        Block clickedBlock = event.getClickedBlock();
        if (clickedBlock == null)
            return;
        if (clickedBlock.getType() != Material.SPAWNER) {
            StackedBlock stackedBlock = this.stackManager.getStackedBlock(clickedBlock);
            if (stackedBlock == null)
                return;
            this.localeManager.sendMessage(player, "command-stacktool-info");
            this.localeManager.sendSimpleMessage(player, "command-stacktool-info-block-type", StringPlaceholders.single("type", clickedBlock.getType().name()));
            this.localeManager.sendSimpleMessage(player, "command-stacktool-info-stack-size", StringPlaceholders.single("amount", stackedBlock.getStackSize()));
            this.localeManager.sendSimpleMessage(player, "command-stacktool-info-location", StringPlaceholders.builder("x", clickedBlock.getX()).addPlaceholder("y", clickedBlock.getY()).addPlaceholder("z", clickedBlock.getZ()).addPlaceholder("world", clickedBlock.getWorld().getName()).build());
            this.localeManager.sendSimpleMessage(player, "command-stacktool-info-chunk", StringPlaceholders.builder("x", clickedBlock.getChunk().getX()).addPlaceholder("z", clickedBlock.getChunk().getZ()).build());
        } else {
            StackedSpawner stackedSpawner = this.stackManager.getStackedSpawner(clickedBlock);
            if (stackedSpawner == null)
                return;
            this.localeManager.sendMessage(player, "command-stacktool-info");
            this.localeManager.sendSimpleMessage(player, "command-stacktool-info-spawner-type", StringPlaceholders.single("type", stackedSpawner.getSpawnerTile().getSpawnedType().name()));
            this.localeManager.sendSimpleMessage(player, "command-stacktool-info-stack-size", StringPlaceholders.single("amount", stackedSpawner.getStackSize()));
            this.localeManager.sendSimpleMessage(player, "command-stacktool-info-location", StringPlaceholders.builder("x", clickedBlock.getX()).addPlaceholder("y", clickedBlock.getY()).addPlaceholder("z", clickedBlock.getZ()).addPlaceholder("world", clickedBlock.getWorld().getName()).build());
            this.localeManager.sendSimpleMessage(player, "command-stacktool-info-chunk", StringPlaceholders.builder("x", clickedBlock.getChunk().getX()).addPlaceholder("z", clickedBlock.getChunk().getZ()).build());
        }
    }
    if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
        Block clickedBlock = event.getClickedBlock();
        if (clickedBlock == null || clickedBlock.getType() != Material.SPAWNER)
            return;
        StackedSpawner stackedSpawner = this.stackManager.getStackedSpawner(clickedBlock);
        if (stackedSpawner == null) {
            CreatureSpawner creatureSpawner = (CreatureSpawner) clickedBlock.getState();
            creatureSpawner.setDelay(5);
            creatureSpawner.update();
        } else {
            stackedSpawner.getSpawnerTile().setDelay(5);
        }
        int points = 50;
        for (int i = 0; i < points; i++) {
            double dx = Math.cos(Math.PI * 2 * ((double) i / points)) * 0.25;
            double dy = 0.5;
            double dz = Math.sin(Math.PI * 2 * ((double) i / points)) * 0.25;
            double angle = Math.atan2(dz, dx);
            double xAng = Math.cos(angle);
            double zAng = Math.sin(angle);
            clickedBlock.getWorld().spawnParticle(Particle.END_ROD, clickedBlock.getLocation().add(0.5 + dx, dy, 0.5 + dz), 0, xAng, 0, zAng, 0.15);
        }
    }
    // Make the item bob a little bit
    player.updateInventory();
}
Also used : StackedEntity(dev.rosewood.rosestacker.stack.StackedEntity) Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) Item(org.bukkit.entity.Item) StackedItem(dev.rosewood.rosestacker.stack.StackedItem) Player(org.bukkit.entity.Player) StackedSpawner(dev.rosewood.rosestacker.stack.StackedSpawner) StackedBlock(dev.rosewood.rosestacker.stack.StackedBlock) Block(org.bukkit.block.Block) StackedBlock(dev.rosewood.rosestacker.stack.StackedBlock) ItemStack(org.bukkit.inventory.ItemStack) StackedItem(dev.rosewood.rosestacker.stack.StackedItem) ItemMeta(org.bukkit.inventory.meta.ItemMeta) CreatureSpawner(org.bukkit.block.CreatureSpawner) EventHandler(org.bukkit.event.EventHandler)

Example 10 with StackedSpawner

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

the class MobSpawningMethod method spawnEntitiesIntoNearbyStacks.

private int spawnEntitiesIntoNearbyStacks(StackedSpawner stackedSpawner, int spawnAmount, Set<Location> locations, List<StackedEntity> nearbyEntities, StackManager stackManager) {
    EntityStackSettings entityStackSettings = RoseStacker.getInstance().getManager(StackSettingManager.class).getEntityStackSettings(this.entityType);
    List<StackedEntity> stackedEntities = new ArrayList<>(nearbyEntities);
    List<Location> possibleLocations = new ArrayList<>(locations);
    if (this.entityType.getEntityClass() == null)
        return 0;
    boolean ageable = Ageable.class.isAssignableFrom(this.entityType.getEntityClass());
    int successfulSpawns = 0;
    if (stackManager.isEntityStackingEnabled() && entityStackSettings.isStackingEnabled() && Setting.SPAWNER_SPAWN_INTO_NEARBY_STACKS.getBoolean()) {
        List<StackedEntity> newStacks = new ArrayList<>();
        NMSHandler nmsHandler = NMSAdapter.getHandler();
        for (int i = 0; i < spawnAmount; i++) {
            if (possibleLocations.isEmpty())
                break;
            Location location = possibleLocations.get(this.random.nextInt(possibleLocations.size()));
            LivingEntity entity = nmsHandler.createNewEntityUnspawned(this.entityType, location, CreatureSpawnEvent.SpawnReason.SPAWNER);
            SpawnerFlagPersistenceHook.flagSpawnerSpawned(entity);
            if (ageable)
                ((Ageable) entity).setAdult();
            if ((stackedSpawner.getStackSettings().isMobAIDisabled() && (!Setting.SPAWNER_DISABLE_MOB_AI_ONLY_PLAYER_PLACED.getBoolean() || stackedSpawner.isPlacedByPlayer())) || Setting.ENTITY_DISABLE_ALL_MOB_AI.getBoolean())
                PersistentDataUtils.removeEntityAi(entity);
            PersistentDataUtils.tagSpawnedFromSpawner(entity);
            entityStackSettings.applySpawnerSpawnedProperties(entity);
            StackedEntity newStack = new StackedEntity(entity);
            Optional<StackedEntity> matchingEntity = stackedEntities.stream().filter(x -> WorldGuardHook.testLocation(x.getLocation()) && entityStackSettings.testCanStackWith(x, newStack, false, true)).findFirst();
            if (matchingEntity.isPresent()) {
                matchingEntity.get().increaseStackSize(entity);
            } else {
                stackedEntities.add(newStack);
                newStacks.add(newStack);
                possibleLocations.remove(location);
            }
            successfulSpawns++;
        }
        Bukkit.getScheduler().runTask(RoseStacker.getInstance(), () -> {
            stackManager.setEntityStackingTemporarilyDisabled(true);
            for (StackedEntity stackedEntity : newStacks) {
                LivingEntity entity = stackedEntity.getEntity();
                SpawnerSpawnEvent spawnerSpawnEvent = new SpawnerSpawnEvent(entity, stackedSpawner.getSpawner());
                Bukkit.getPluginManager().callEvent(spawnerSpawnEvent);
                if (spawnerSpawnEvent.isCancelled())
                    continue;
                nmsHandler.spawnExistingEntity(stackedEntity.getEntity(), CreatureSpawnEvent.SpawnReason.SPAWNER, Setting.SPAWNER_BYPASS_REGION_SPAWNING_RULES.getBoolean());
                entity.setVelocity(Vector.getRandom().multiply(0.01));
                stackManager.addEntityStack(stackedEntity);
            }
            stackManager.setEntityStackingTemporarilyDisabled(false);
            // Spawn particles for new entities and update nametags
            for (StackedEntity entity : newStacks) {
                entity.updateDisplay();
                World world = entity.getLocation().getWorld();
                if (world != null)
                    world.spawnParticle(Particle.EXPLOSION_NORMAL, entity.getLocation().clone().add(0, 0.75, 0), 5, 0.25, 0.25, 0.25, 0.01);
            }
        });
    } else {
        successfulSpawns = Math.min(spawnAmount, possibleLocations.size());
        Bukkit.getScheduler().runTask(RoseStacker.getInstance(), () -> {
            NMSHandler nmsHandler = NMSAdapter.getHandler();
            for (int i = 0; i < spawnAmount; i++) {
                if (possibleLocations.isEmpty())
                    break;
                Location location = possibleLocations.remove(this.random.nextInt(possibleLocations.size()));
                World world = location.getWorld();
                if (world == null)
                    continue;
                LivingEntity entity = nmsHandler.spawnEntityWithReason(this.entityType, location, CreatureSpawnEvent.SpawnReason.SPAWNER, Setting.SPAWNER_BYPASS_REGION_SPAWNING_RULES.getBoolean());
                entityStackSettings.applySpawnerSpawnedProperties(entity);
                SpawnerFlagPersistenceHook.flagSpawnerSpawned(entity);
                SpawnerSpawnEvent spawnerSpawnEvent = new SpawnerSpawnEvent(entity, stackedSpawner.getSpawner());
                Bukkit.getPluginManager().callEvent(spawnerSpawnEvent);
                if (spawnerSpawnEvent.isCancelled()) {
                    entity.remove();
                    continue;
                }
                // Spawn Particles
                if (entity.isValid())
                    entity.getWorld().spawnParticle(Particle.EXPLOSION_NORMAL, entity.getLocation().clone().add(0, 0.75, 0), 5, 0.25, 0.25, 0.25, 0.01);
            }
        });
    }
    return successfulSpawns;
}
Also used : EntityStackSettings(dev.rosewood.rosestacker.stack.settings.EntityStackSettings) RoseStacker(dev.rosewood.rosestacker.RoseStacker) WorldGuardHook(dev.rosewood.rosestacker.hook.WorldGuardHook) StackedEntity(dev.rosewood.rosestacker.stack.StackedEntity) NoneConditionTag(dev.rosewood.rosestacker.spawner.conditions.tags.NoneConditionTag) Random(java.util.Random) SpawnerSpawnEvent(org.bukkit.event.entity.SpawnerSpawnEvent) StackedSpawner(dev.rosewood.rosestacker.stack.StackedSpawner) ArrayList(java.util.ArrayList) EntityStackSettings(dev.rosewood.rosestacker.stack.settings.EntityStackSettings) HashSet(java.util.HashSet) StackSettingManager(dev.rosewood.rosestacker.manager.StackSettingManager) Block(org.bukkit.block.Block) Location(org.bukkit.Location) NotPlayerPlacedConditionTag(dev.rosewood.rosestacker.spawner.conditions.tags.NotPlayerPlacedConditionTag) World(org.bukkit.World) NMSHandler(dev.rosewood.rosestacker.nms.NMSHandler) StackedSpawnerTile(dev.rosewood.rosestacker.nms.spawner.StackedSpawnerTile) PersistentDataUtils(dev.rosewood.rosestacker.utils.PersistentDataUtils) Material(org.bukkit.Material) Ageable(org.bukkit.entity.Ageable) Bukkit(org.bukkit.Bukkit) Setting(dev.rosewood.rosestacker.manager.ConfigurationManager.Setting) ConditionTag(dev.rosewood.rosestacker.spawner.conditions.ConditionTag) Entity(org.bukkit.entity.Entity) Predicate(java.util.function.Predicate) CreatureSpawnEvent(org.bukkit.event.entity.CreatureSpawnEvent) Collection(java.util.Collection) Set(java.util.Set) EntityType(org.bukkit.entity.EntityType) LivingEntity(org.bukkit.entity.LivingEntity) EntityCacheManager(dev.rosewood.rosestacker.manager.EntityCacheManager) Collectors(java.util.stream.Collectors) Particle(org.bukkit.Particle) Vector(org.bukkit.util.Vector) StackManager(dev.rosewood.rosestacker.manager.StackManager) SpawnerFlagPersistenceHook(dev.rosewood.rosestacker.hook.SpawnerFlagPersistenceHook) List(java.util.List) Optional(java.util.Optional) StackerUtils(dev.rosewood.rosestacker.utils.StackerUtils) NMSAdapter(dev.rosewood.rosestacker.nms.NMSAdapter) SpawnerStackSettings(dev.rosewood.rosestacker.stack.settings.SpawnerStackSettings) StackedEntity(dev.rosewood.rosestacker.stack.StackedEntity) ArrayList(java.util.ArrayList) StackSettingManager(dev.rosewood.rosestacker.manager.StackSettingManager) SpawnerSpawnEvent(org.bukkit.event.entity.SpawnerSpawnEvent) World(org.bukkit.World) NMSHandler(dev.rosewood.rosestacker.nms.NMSHandler) LivingEntity(org.bukkit.entity.LivingEntity) Location(org.bukkit.Location)

Aggregations

StackedSpawner (dev.rosewood.rosestacker.stack.StackedSpawner)21 Block (org.bukkit.block.Block)17 StackManager (dev.rosewood.rosestacker.manager.StackManager)11 Location (org.bukkit.Location)9 StackedSpawnerTile (dev.rosewood.rosestacker.nms.spawner.StackedSpawnerTile)8 StackedBlock (dev.rosewood.rosestacker.stack.StackedBlock)8 ItemStack (org.bukkit.inventory.ItemStack)7 ArrayList (java.util.ArrayList)6 HashSet (java.util.HashSet)6 EventHandler (org.bukkit.event.EventHandler)6 SpawnerStackSettings (dev.rosewood.rosestacker.stack.settings.SpawnerStackSettings)5 EntityType (org.bukkit.entity.EntityType)5 Material (org.bukkit.Material)4 World (org.bukkit.World)4 LivingEntity (org.bukkit.entity.LivingEntity)4 RoseStacker (dev.rosewood.rosestacker.RoseStacker)3 Setting (dev.rosewood.rosestacker.manager.ConfigurationManager.Setting)3 StackSettingManager (dev.rosewood.rosestacker.manager.StackSettingManager)3 ConditionTag (dev.rosewood.rosestacker.spawner.conditions.ConditionTag)3 NoneConditionTag (dev.rosewood.rosestacker.spawner.conditions.tags.NoneConditionTag)3