Search in sources :

Example 6 with StackedBlock

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

the class UltimateStackerPluginConverter method convert.

@Override
public void convert() {
    // If EpicSpawners is installed, spawner stacking functionality is handled by that plugin instead
    if (Bukkit.getPluginManager().isPluginEnabled("EpicSpawners"))
        return;
    DataManager dataManager = this.rosePlugin.getManager(DataManager.class);
    // Force save loaded data
    this.ultimateStacker.getDataManager().bulkUpdateSpawners(this.ultimateStacker.getSpawnerStackManager().getStacks());
    // Go through the database to be able to load all spawner information
    DatabaseConnector connector = this.ultimateStacker.getDatabaseConnector();
    connector.connect(connection -> {
        // Load entities
        try (Statement statement = connection.createStatement()) {
            String query = "SELECT he.uuid AS uuid, COUNT(se.host) + 1 AS stackAmount FROM ultimatestacker_host_entities he " + "JOIN ultimatestacker_stacked_entities se ON he.id = se.host " + "GROUP BY se.host";
            ResultSet result = statement.executeQuery(query);
            Set<ConversionData> entityConversionData = new HashSet<>();
            while (result.next()) {
                UUID uuid = UUID.fromString(result.getString("uuid"));
                int amount = result.getInt("stackAmount");
                entityConversionData.add(new ConversionData(uuid, amount));
            }
            Map<StackType, Set<ConversionData>> conversionData = new HashMap<>();
            conversionData.put(StackType.ENTITY, entityConversionData);
            dataManager.setConversionData(conversionData);
        }
        // Load blocks
        Set<StackedBlock> stackedBlocks = new HashSet<>();
        try (Statement statement = connection.createStatement()) {
            ResultSet result = statement.executeQuery("SELECT amount, world, x, y, z FROM ultimatestacker_blocks");
            while (result.next()) {
                World world = Bukkit.getWorld(result.getString("world"));
                if (world == null)
                    continue;
                int amount = result.getInt("amount");
                if (amount == 1)
                    continue;
                Block block = world.getBlockAt(result.getInt("x"), result.getInt("y"), result.getInt("z"));
                stackedBlocks.add(new StackedBlock(amount, block));
            }
        }
        // Load spawners
        Set<StackedSpawner> stackedSpawners = new HashSet<>();
        try (Statement statement = connection.createStatement()) {
            ResultSet result = statement.executeQuery("SELECT amount, world, x, y, z FROM ultimatestacker_spawners");
            while (result.next()) {
                World world = Bukkit.getWorld(result.getString("world"));
                if (world == null)
                    continue;
                int x = result.getInt("x");
                int y = result.getInt("y");
                int z = result.getInt("z");
                Location location = new Location(world, x, y, z);
                int amount = result.getInt("amount");
                stackedSpawners.add(new StackedSpawner(amount, location));
            }
        }
        if (!stackedBlocks.isEmpty() || !stackedSpawners.isEmpty()) {
            StackManager stackManager = this.rosePlugin.getManager(StackManager.class);
            Bukkit.getScheduler().runTask(this.rosePlugin, () -> {
                for (StackedBlock stackedBlock : stackedBlocks) stackManager.createBlockStack(stackedBlock.getLocation().getBlock(), stackedBlock.getStackSize());
                for (StackedSpawner stackedSpawner : stackedSpawners) stackManager.createSpawnerStack(stackedSpawner.getLocation().getBlock(), stackedSpawner.getStackSize(), false);
            });
        }
    });
}
Also used : DatabaseConnector(com.songoda.ultimatestacker.core.database.DatabaseConnector) Set(java.util.Set) HashSet(java.util.HashSet) ResultSet(java.sql.ResultSet) StackedSpawner(dev.rosewood.rosestacker.stack.StackedSpawner) HashMap(java.util.HashMap) Statement(java.sql.Statement) StackManager(dev.rosewood.rosestacker.manager.StackManager) DataManager(dev.rosewood.rosestacker.manager.DataManager) World(org.bukkit.World) StackType(dev.rosewood.rosestacker.stack.StackType) StackedBlock(dev.rosewood.rosestacker.stack.StackedBlock) ResultSet(java.sql.ResultSet) Block(org.bukkit.block.Block) StackedBlock(dev.rosewood.rosestacker.stack.StackedBlock) ConversionData(dev.rosewood.rosestacker.conversion.ConversionData) UUID(java.util.UUID) HashSet(java.util.HashSet) Location(org.bukkit.Location)

Example 7 with StackedBlock

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

the class BlockListener method onBlockBreak.

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onBlockBreak(BlockBreakEvent event) {
    StackManager stackManager = this.rosePlugin.getManager(StackManager.class);
    if (stackManager.isWorldDisabled(event.getPlayer().getWorld()))
        return;
    Block block = event.getBlock();
    boolean isStacked = this.isBlockOrSpawnerStack(stackManager, block);
    boolean isSpawner = block.getType() == Material.SPAWNER;
    if (!isStacked && !isSpawner)
        return;
    Player player = event.getPlayer();
    Location dropLocation = block.getLocation().clone();
    if (isSpawner) {
        if (!stackManager.isSpawnerStackingEnabled())
            return;
        StackedSpawner stackedSpawner = stackManager.getStackedSpawner(block);
        if (stackedSpawner == null)
            stackedSpawner = stackManager.createSpawnerStack(block, 1, false);
        EntityType entityType = stackedSpawner.getSpawnerTile().getSpawnedType();
        boolean breakEverything = Setting.SPAWNER_BREAK_ENTIRE_STACK_WHILE_SNEAKING.getBoolean() && player.isSneaking();
        int breakAmount = breakEverything ? stackedSpawner.getStackSize() : 1;
        SpawnerUnstackEvent spawnerUnstackEvent = new SpawnerUnstackEvent(player, stackedSpawner, breakAmount);
        Bukkit.getPluginManager().callEvent(spawnerUnstackEvent);
        if (spawnerUnstackEvent.isCancelled()) {
            event.setCancelled(true);
            return;
        }
        breakAmount = spawnerUnstackEvent.getDecreaseAmount();
        if (this.tryDropSpawners(player, dropLocation, entityType, breakAmount, stackedSpawner.isPlacedByPlayer())) {
            BlockLoggingHook.recordBlockBreak(player, block);
            if (breakAmount == stackedSpawner.getStackSize()) {
                stackedSpawner.setStackSize(0);
                Bukkit.getScheduler().runTask(this.rosePlugin, () -> block.setType(Material.AIR));
            } else {
                stackedSpawner.increaseStackSize(-breakAmount);
            }
            if (stackedSpawner.getStackSize() <= 0) {
                stackManager.removeSpawnerStack(stackedSpawner);
                return;
            }
        } else {
            event.setCancelled(true);
            return;
        }
    } else {
        if (!stackManager.isBlockStackingEnabled())
            return;
        StackedBlock stackedBlock = stackManager.getStackedBlock(block);
        if (stackedBlock == null)
            return;
        if (stackedBlock.isLocked()) {
            event.setCancelled(true);
            return;
        }
        boolean breakEverything = Setting.BLOCK_BREAK_ENTIRE_STACK_WHILE_SNEAKING.getBoolean() && player.isSneaking();
        int breakAmount = breakEverything ? stackedBlock.getStackSize() : 1;
        BlockUnstackEvent blockUnstackEvent = new BlockUnstackEvent(player, stackedBlock, breakAmount);
        Bukkit.getPluginManager().callEvent(blockUnstackEvent);
        if (blockUnstackEvent.isCancelled()) {
            event.setCancelled(true);
            return;
        }
        breakAmount = blockUnstackEvent.getDecreaseAmount();
        if (player.getGameMode() != GameMode.CREATIVE) {
            List<ItemStack> items;
            if (Setting.BLOCK_BREAK_ENTIRE_STACK_INTO_SEPARATE.getBoolean()) {
                items = GuiUtil.getMaterialAmountAsItemStacks(block.getType(), breakAmount);
            } else {
                items = Collections.singletonList(ItemUtils.getBlockAsStackedItemStack(block.getType(), breakAmount));
            }
            if (Setting.BLOCK_DROP_TO_INVENTORY.getBoolean()) {
                ItemUtils.dropItemsToPlayer(player, items);
            } else {
                stackManager.preStackItems(items, dropLocation);
            }
        }
        BlockLoggingHook.recordBlockBreak(player, block);
        if (breakAmount == stackedBlock.getStackSize()) {
            stackedBlock.setStackSize(0);
            Bukkit.getScheduler().runTask(this.rosePlugin, () -> block.setType(Material.AIR));
        } else {
            stackedBlock.increaseStackSize(-1);
        }
        if (stackedBlock.getStackSize() <= 1)
            stackManager.removeBlockStack(stackedBlock);
    }
    this.damageTool(player);
    event.setCancelled(true);
}
Also used : SpawnerUnstackEvent(dev.rosewood.rosestacker.event.SpawnerUnstackEvent) Player(org.bukkit.entity.Player) StackedSpawner(dev.rosewood.rosestacker.stack.StackedSpawner) StackManager(dev.rosewood.rosestacker.manager.StackManager) BlockUnstackEvent(dev.rosewood.rosestacker.event.BlockUnstackEvent) EntityType(org.bukkit.entity.EntityType) StackedBlock(dev.rosewood.rosestacker.stack.StackedBlock) Block(org.bukkit.block.Block) StackedBlock(dev.rosewood.rosestacker.stack.StackedBlock) ItemStack(org.bukkit.inventory.ItemStack) Location(org.bukkit.Location) EventHandler(org.bukkit.event.EventHandler)

Example 8 with StackedBlock

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

the class BlockListener method handleExplosion.

private void handleExplosion(Location location, List<Block> blockList) {
    StackManager stackManager = this.rosePlugin.getManager(StackManager.class);
    if (stackManager.isWorldDisabled(location.getWorld()))
        return;
    boolean stackedBlockProtection = Setting.BLOCK_EXPLOSION_PROTECTION.getBoolean() && stackManager.isBlockStackingEnabled();
    boolean stackedSpawnerProtection = Setting.SPAWNER_EXPLOSION_PROTECTION.getBoolean() && stackManager.isSpawnerStackingEnabled();
    if (stackedSpawnerProtection)
        blockList.removeIf(stackManager::isSpawnerStacked);
    if (stackedBlockProtection)
        blockList.removeIf(stackManager::isBlockStacked);
    for (Block block : new ArrayList<>(blockList)) {
        if (stackManager.isBlockStacked(block)) {
            blockList.remove(block);
            if (!StackerUtils.passesChance(Setting.BLOCK_EXPLOSION_DESTROY_CHANCE.getDouble() / 100))
                continue;
            StackedBlock stackedBlock = stackManager.getStackedBlock(block);
            stackedBlock.kickOutGuiViewers();
            int destroyAmountFixed = Setting.BLOCK_EXPLOSION_DESTROY_AMOUNT_FIXED.getInt();
            int destroyAmount;
            if (destroyAmountFixed != -1) {
                destroyAmount = destroyAmountFixed;
            } else {
                destroyAmount = stackedBlock.getStackSize() - (int) Math.ceil(stackedBlock.getStackSize() * (Setting.BLOCK_EXPLOSION_DESTROY_AMOUNT_PERCENTAGE.getDouble() / 100));
            }
            BlockUnstackEvent blockUnstackEvent = new BlockUnstackEvent(null, stackedBlock, destroyAmount);
            Bukkit.getPluginManager().callEvent(blockUnstackEvent);
            if (blockUnstackEvent.isCancelled())
                continue;
            destroyAmount = blockUnstackEvent.getDecreaseAmount();
            int newStackSize = stackedBlock.getStackSize() - destroyAmount;
            if (newStackSize <= 0) {
                block.setType(Material.AIR);
                stackedBlock.setStackSize(0);
                stackManager.removeBlockStack(stackedBlock);
                continue;
            }
            if (Setting.BLOCK_EXPLOSION_DECREASE_STACK_SIZE_ONLY.getBoolean()) {
                stackedBlock.setStackSize(newStackSize);
                if (newStackSize <= 1)
                    stackManager.removeBlockStack(stackedBlock);
            } else {
                stackedBlock.setStackSize(0);
                stackManager.removeBlockStack(stackedBlock);
                Material type = block.getType();
                block.setType(Material.AIR);
                Bukkit.getScheduler().runTask(this.rosePlugin, () -> {
                    List<ItemStack> items;
                    if (Setting.BLOCK_BREAK_ENTIRE_STACK_INTO_SEPARATE.getBoolean()) {
                        items = GuiUtil.getMaterialAmountAsItemStacks(type, newStackSize);
                    } else {
                        items = Collections.singletonList(ItemUtils.getBlockAsStackedItemStack(type, newStackSize));
                    }
                    stackManager.preStackItems(items, block.getLocation().clone().add(0.5, 0.5, 0.5));
                });
            }
        } else if (stackManager.isSpawnerStacked(block)) {
            blockList.remove(block);
            if (!StackerUtils.passesChance(Setting.SPAWNER_EXPLOSION_DESTROY_CHANCE.getDouble() / 100))
                continue;
            StackedSpawner stackedSpawner = stackManager.getStackedSpawner(block);
            int destroyAmountFixed = Setting.SPAWNER_EXPLOSION_DESTROY_AMOUNT_FIXED.getInt();
            int destroyAmount;
            if (destroyAmountFixed != -1) {
                destroyAmount = destroyAmountFixed;
            } else {
                destroyAmount = stackedSpawner.getStackSize() - (int) Math.ceil(stackedSpawner.getStackSize() * (Setting.SPAWNER_EXPLOSION_DESTROY_AMOUNT_PERCENTAGE.getDouble() / 100));
            }
            SpawnerUnstackEvent spawnerUnstackEvent = new SpawnerUnstackEvent(null, stackedSpawner, destroyAmount);
            Bukkit.getPluginManager().callEvent(spawnerUnstackEvent);
            if (spawnerUnstackEvent.isCancelled())
                continue;
            destroyAmount = spawnerUnstackEvent.getDecreaseAmount();
            int newStackSize = stackedSpawner.getStackSize() - destroyAmount;
            if (newStackSize <= 0) {
                block.setType(Material.AIR);
                stackedSpawner.setStackSize(0);
                stackManager.removeSpawnerStack(stackedSpawner);
                continue;
            }
            if (Setting.SPAWNER_EXPLOSION_DECREASE_STACK_SIZE_ONLY.getBoolean()) {
                stackedSpawner.setStackSize(newStackSize);
            } else {
                stackedSpawner.setStackSize(0);
                stackManager.removeSpawnerStack(stackedSpawner);
                EntityType spawnedType = stackedSpawner.getSpawnerTile().getSpawnedType();
                block.setType(Material.AIR);
                Bukkit.getScheduler().runTask(this.rosePlugin, () -> {
                    List<ItemStack> items;
                    if (Setting.SPAWNER_BREAK_ENTIRE_STACK_INTO_SEPARATE.getBoolean()) {
                        items = new ArrayList<>();
                        for (int i = 0; i < newStackSize; i++) items.add(ItemUtils.getSpawnerAsStackedItemStack(spawnedType, 1));
                    } else {
                        items = Collections.singletonList(ItemUtils.getSpawnerAsStackedItemStack(spawnedType, newStackSize));
                    }
                    stackManager.preStackItems(items, block.getLocation().clone());
                });
            }
        }
    }
}
Also used : SpawnerUnstackEvent(dev.rosewood.rosestacker.event.SpawnerUnstackEvent) StackedSpawner(dev.rosewood.rosestacker.stack.StackedSpawner) StackManager(dev.rosewood.rosestacker.manager.StackManager) ArrayList(java.util.ArrayList) Material(org.bukkit.Material) BlockUnstackEvent(dev.rosewood.rosestacker.event.BlockUnstackEvent) EntityType(org.bukkit.entity.EntityType) StackedBlock(dev.rosewood.rosestacker.stack.StackedBlock) Block(org.bukkit.block.Block) StackedBlock(dev.rosewood.rosestacker.stack.StackedBlock) List(java.util.List) ArrayList(java.util.ArrayList) ItemStack(org.bukkit.inventory.ItemStack)

Example 9 with StackedBlock

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

the class WildStackerPluginConverter method convert.

@Override
public void convert() {
    StackManager stackManager = this.rosePlugin.getManager(StackManager.class);
    // Force save loaded data
    SystemManager systemHandler = WildStackerAPI.getWildStacker().getSystemManager();
    systemHandler.performCacheSave();
    // Go through the database to be able to load all information
    DatabaseConnector connector = new SQLiteConnector(this.wildStacker, "database");
    connector.connect(connection -> {
        // Load barrels (blocks)
        Set<StackedBlock> stackedBlocks = new HashSet<>();
        try (Statement statement = connection.createStatement()) {
            ResultSet result = statement.executeQuery("SELECT location, stackAmount FROM barrels");
            while (result.next()) {
                Location location = this.parseLocation(result.getString("location"), ',');
                if (location == null)
                    continue;
                int amount = result.getInt("stackAmount");
                Material type = systemHandler.getStackedSnapshot(location.getChunk()).getStackedBarrelItem(location).getValue().getType();
                Block block = location.getBlock();
                // Remove hologram thingy
                StackedBarrel barrel = systemHandler.getStackedBarrel(block);
                if (barrel != null)
                    barrel.removeDisplayBlock();
                // Set the block type to the stack type since we just removed the hologram thingy
                block.setType(type);
                // Stacks of 1 aren't really stacks
                if (amount == 1)
                    continue;
                stackedBlocks.add(new StackedBlock(amount, block));
            }
        }
        // Load spawners
        Set<StackedSpawner> stackedSpawners = new HashSet<>();
        try (Statement statement = connection.createStatement()) {
            ResultSet result = statement.executeQuery("SELECT location, stackAmount FROM spawners");
            while (result.next()) {
                Location location = this.parseLocation(result.getString("location"), ',');
                if (location == null)
                    continue;
                Block block = location.getBlock();
                BlockState blockState = block.getState();
                if (!(blockState instanceof CreatureSpawner))
                    continue;
                int amount = result.getInt("stackAmount");
                stackedSpawners.add(new StackedSpawner(amount, location));
            }
        }
        if (!stackedBlocks.isEmpty() || !stackedSpawners.isEmpty()) {
            Bukkit.getScheduler().runTask(this.rosePlugin, () -> {
                for (StackedBlock stackedBlock : stackedBlocks) stackManager.createBlockStack(stackedBlock.getLocation().getBlock(), stackedBlock.getStackSize());
                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) StackManager(dev.rosewood.rosestacker.manager.StackManager) Statement(java.sql.Statement) SystemManager(com.bgsoftware.wildstacker.api.handlers.SystemManager) Material(org.bukkit.Material) CreatureSpawner(org.bukkit.block.CreatureSpawner) BlockState(org.bukkit.block.BlockState) StackedBlock(dev.rosewood.rosestacker.stack.StackedBlock) SQLiteConnector(dev.rosewood.rosegarden.database.SQLiteConnector) ResultSet(java.sql.ResultSet) StackedBarrel(com.bgsoftware.wildstacker.api.objects.StackedBarrel) Block(org.bukkit.block.Block) StackedBlock(dev.rosewood.rosestacker.stack.StackedBlock) HashSet(java.util.HashSet) Location(org.bukkit.Location)

Example 10 with StackedBlock

use of dev.rosewood.rosestacker.stack.StackedBlock in project IridiumSkyblock by Iridium-Development.

the class RoseStackerSupport method getExtraBlocks.

@Override
public int getExtraBlocks(Island island, XMaterial material) {
    IslandBlocks islandBlocks = IridiumSkyblock.getInstance().getIslandManager().getIslandBlock(island, material);
    int stackedBlocks = 0;
    for (StackedBlock stackedBlock : RoseStackerAPI.getInstance().getStackedBlocks().values()) {
        if (!island.isInIsland(stackedBlock.getLocation()))
            continue;
        if (material != XMaterial.matchXMaterial(stackedBlock.getBlock().getType()))
            continue;
        stackedBlocks += (stackedBlock.getStackSize() - 1);
    }
    islandBlocks.setExtraAmount(stackedBlocks);
    return stackedBlocks;
}
Also used : StackedBlock(dev.rosewood.rosestacker.stack.StackedBlock) IslandBlocks(com.iridium.iridiumskyblock.database.IslandBlocks)

Aggregations

StackedBlock (dev.rosewood.rosestacker.stack.StackedBlock)10 StackedSpawner (dev.rosewood.rosestacker.stack.StackedSpawner)7 Block (org.bukkit.block.Block)7 StackManager (dev.rosewood.rosestacker.manager.StackManager)6 ItemStack (org.bukkit.inventory.ItemStack)5 Location (org.bukkit.Location)4 EventHandler (org.bukkit.event.EventHandler)4 ArrayList (java.util.ArrayList)3 EntityType (org.bukkit.entity.EntityType)3 Player (org.bukkit.entity.Player)3 BlockUnstackEvent (dev.rosewood.rosestacker.event.BlockUnstackEvent)2 SpawnerUnstackEvent (dev.rosewood.rosestacker.event.SpawnerUnstackEvent)2 ResultSet (java.sql.ResultSet)2 Statement (java.sql.Statement)2 HashSet (java.util.HashSet)2 Material (org.bukkit.Material)2 CreatureSpawner (org.bukkit.block.CreatureSpawner)2 PersistentDataContainer (org.bukkit.persistence.PersistentDataContainer)2 SystemManager (com.bgsoftware.wildstacker.api.handlers.SystemManager)1 StackedBarrel (com.bgsoftware.wildstacker.api.objects.StackedBarrel)1