Search in sources :

Example 1 with StackType

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

the class ConversionManager method convertChunkEntities.

/**
 * Runs the chunk data conversion for any conversion handlers
 *
 * @param chunkEntities The entities in the chunk to convert
 */
public void convertChunkEntities(List<Entity> chunkEntities) {
    Set<StackType> requiredStackTypes = new HashSet<>();
    for (ConversionHandler conversionHandler : this.conversionHandlers) requiredStackTypes.add(conversionHandler.getRequiredDataStackType());
    Map<StackType, Set<ConversionData>> conversionData = this.dataManager.getConversionData(chunkEntities, requiredStackTypes);
    Set<Stack<?>> convertedStacks = new HashSet<>();
    for (ConversionHandler conversionHandler : this.conversionHandlers) {
        Set<ConversionData> data;
        if (conversionHandler.shouldUseChunkEntities()) {
            data = new HashSet<>();
            switch(conversionHandler.getRequiredDataStackType()) {
                case ENTITY:
                    for (Entity entity : chunkEntities) if (entity.getType() != EntityType.DROPPED_ITEM)
                        data.add(new ConversionData(entity));
                    break;
                case ITEM:
                    for (Entity entity : chunkEntities) if (entity.getType() == EntityType.DROPPED_ITEM)
                        data.add(new ConversionData(entity));
                    break;
                default:
                    break;
            }
        } else {
            data = conversionData.get(conversionHandler.getRequiredDataStackType());
        }
        if (data.isEmpty())
            continue;
        convertedStacks.addAll(conversionHandler.handleConversion(data));
    }
    // Update nametags synchronously
    if (!convertedStacks.isEmpty())
        Bukkit.getScheduler().runTask(this.rosePlugin, () -> convertedStacks.forEach(Stack::updateDisplay));
}
Also used : Entity(org.bukkit.entity.Entity) StackType(dev.rosewood.rosestacker.stack.StackType) HashSet(java.util.HashSet) Set(java.util.Set) ConversionHandler(dev.rosewood.rosestacker.conversion.handler.ConversionHandler) ConversionData(dev.rosewood.rosestacker.conversion.ConversionData) HashSet(java.util.HashSet) Stack(dev.rosewood.rosestacker.stack.Stack)

Example 2 with StackType

use of dev.rosewood.rosestacker.stack.StackType 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)

Aggregations

ConversionData (dev.rosewood.rosestacker.conversion.ConversionData)2 StackType (dev.rosewood.rosestacker.stack.StackType)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 DatabaseConnector (com.songoda.ultimatestacker.core.database.DatabaseConnector)1 ConversionHandler (dev.rosewood.rosestacker.conversion.handler.ConversionHandler)1 DataManager (dev.rosewood.rosestacker.manager.DataManager)1 StackManager (dev.rosewood.rosestacker.manager.StackManager)1 Stack (dev.rosewood.rosestacker.stack.Stack)1 StackedBlock (dev.rosewood.rosestacker.stack.StackedBlock)1 StackedSpawner (dev.rosewood.rosestacker.stack.StackedSpawner)1 ResultSet (java.sql.ResultSet)1 Statement (java.sql.Statement)1 HashMap (java.util.HashMap)1 UUID (java.util.UUID)1 Location (org.bukkit.Location)1 World (org.bukkit.World)1 Block (org.bukkit.block.Block)1 Entity (org.bukkit.entity.Entity)1