Search in sources :

Example 1 with StackManager

use of dev.rosewood.rosestacker.manager.StackManager in project RoseStacker by Rosewood-Development.

the class ClearlagListener method onClear.

@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onClear(EntityRemoveEvent event) {
    StackManager stackManager = this.rosePlugin.getManager(StackManager.class);
    StackingThread stackingThread = stackManager.getStackingThread(event.getWorld());
    if (stackingThread == null)
        return;
    if (Setting.MISC_CLEARLAG_CLEAR_ENTITIES.getBoolean() && stackManager.isEntityStackingEnabled())
        stackingThread.removeAllEntityStacks();
    if (Setting.MISC_CLEARLAG_CLEAR_ITEMS.getBoolean() && stackManager.isItemStackingEnabled())
        stackingThread.removeAllItemStacks();
}
Also used : StackingThread(dev.rosewood.rosestacker.stack.StackingThread) StackManager(dev.rosewood.rosestacker.manager.StackManager) EventHandler(org.bukkit.event.EventHandler)

Example 2 with StackManager

use of dev.rosewood.rosestacker.manager.StackManager in project RoseStacker by Rosewood-Development.

the class EpicSpawnersPluginConverter method convert.

@Override
public void convert() {
    // Query their database ourselves
    DatabaseConnector connector = new SQLiteConnector(this.epicSpawners);
    connector.connect(connection -> {
        Set<StackedSpawner> stackedSpawners = new HashSet<>();
        String tablePrefix = this.epicSpawners.getDataManager().getTablePrefix();
        try (Statement statement = connection.createStatement()) {
            String query = "SELECT amount, world, x, y, z FROM " + tablePrefix + "placed_spawners ps JOIN " + tablePrefix + "spawner_stacks ss ON ps.id = ss.spawner_id";
            ResultSet result = statement.executeQuery(query);
            while (result.next()) {
                World world = Bukkit.getWorld(result.getString("world"));
                if (world == null)
                    continue;
                int amount = result.getInt("amount");
                double x = result.getDouble("x");
                double y = result.getDouble("y");
                double z = result.getDouble("z");
                Location location = new Location(world, x, y, z);
                stackedSpawners.add(new StackedSpawner(amount, location));
            }
        }
        if (!stackedSpawners.isEmpty()) {
            Bukkit.getScheduler().runTask(this.rosePlugin, () -> {
                StackManager stackManager = this.rosePlugin.getManager(StackManager.class);
                for (StackedSpawner stackedSpawner : stackedSpawners) stackManager.createSpawnerStack(stackedSpawner.getLocation().getBlock(), stackedSpawner.getStackSize(), false);
            });
        }
    });
}
Also used : DatabaseConnector(dev.rosewood.rosegarden.database.DatabaseConnector) StackedSpawner(dev.rosewood.rosestacker.stack.StackedSpawner) Statement(java.sql.Statement) StackManager(dev.rosewood.rosestacker.manager.StackManager) World(org.bukkit.World) SQLiteConnector(dev.rosewood.rosegarden.database.SQLiteConnector) ResultSet(java.sql.ResultSet) HashSet(java.util.HashSet) Location(org.bukkit.Location)

Example 3 with StackManager

use of dev.rosewood.rosestacker.manager.StackManager 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 4 with StackManager

use of dev.rosewood.rosestacker.manager.StackManager 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 5 with StackManager

use of dev.rosewood.rosestacker.manager.StackManager 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)

Aggregations

StackManager (dev.rosewood.rosestacker.manager.StackManager)22 StackedSpawner (dev.rosewood.rosestacker.stack.StackedSpawner)12 EventHandler (org.bukkit.event.EventHandler)10 ItemStack (org.bukkit.inventory.ItemStack)10 Location (org.bukkit.Location)9 Block (org.bukkit.block.Block)9 EntityType (org.bukkit.entity.EntityType)7 StackSettingManager (dev.rosewood.rosestacker.manager.StackSettingManager)6 StackedBlock (dev.rosewood.rosestacker.stack.StackedBlock)6 StackedEntity (dev.rosewood.rosestacker.stack.StackedEntity)6 EntityStackSettings (dev.rosewood.rosestacker.stack.settings.EntityStackSettings)5 SpawnerStackSettings (dev.rosewood.rosestacker.stack.settings.SpawnerStackSettings)5 ArrayList (java.util.ArrayList)5 HashSet (java.util.HashSet)5 LivingEntity (org.bukkit.entity.LivingEntity)5 BlockUnstackEvent (dev.rosewood.rosestacker.event.BlockUnstackEvent)4 Setting (dev.rosewood.rosestacker.manager.ConfigurationManager.Setting)4 LocaleManager (dev.rosewood.rosestacker.manager.LocaleManager)4 PersistentDataUtils (dev.rosewood.rosestacker.utils.PersistentDataUtils)4 StackerUtils (dev.rosewood.rosestacker.utils.StackerUtils)4