Search in sources :

Example 1 with ConversionData

use of dev.rosewood.rosestacker.conversion.ConversionData in project RoseStacker by Rosewood-Development.

the class UltimateStackerConversionHandler method handleConversion.

@Override
public Set<Stack<?>> handleConversion(Set<ConversionData> conversionData) {
    Set<Stack<?>> stacks = new HashSet<>();
    for (ConversionData data : conversionData) {
        Item item = data.getItem();
        if (item == null)
            continue;
        int stackSize = this.getItemAmount(item);
        StackedItem stackedItem = new StackedItem(stackSize, data.getItem());
        this.stackManager.addItemStack(stackedItem);
        stacks.add(stackedItem);
    }
    return stacks;
}
Also used : Item(org.bukkit.entity.Item) StackedItem(dev.rosewood.rosestacker.stack.StackedItem) ConversionData(dev.rosewood.rosestacker.conversion.ConversionData) StackedItem(dev.rosewood.rosestacker.stack.StackedItem) Stack(dev.rosewood.rosestacker.stack.Stack) ItemStack(org.bukkit.inventory.ItemStack) HashSet(java.util.HashSet)

Example 2 with ConversionData

use of dev.rosewood.rosestacker.conversion.ConversionData in project RoseStacker by Rosewood-Development.

the class DataManager method getConversionData.

private Set<ConversionData> getConversionData(List<Entity> entities, String tableName, Connection connection) throws SQLException {
    Set<ConversionData> conversionData = new HashSet<>();
    Map<UUID, Entity> entityMap = new HashMap<>();
    for (Entity entity : entities) entityMap.put(entity.getUniqueId(), entity);
    if (entityMap.isEmpty())
        return conversionData;
    String entityUniqueIdsString = entityMap.keySet().stream().map(UUID::toString).map(x -> "'" + x + "'").collect(Collectors.joining(","));
    // Get data
    try (Statement statement = connection.createStatement()) {
        String query = "SELECT entity_uuid, stack_size FROM " + this.getTablePrefix() + "convert_stacked_" + tableName + " WHERE entity_uuid IN (" + entityUniqueIdsString + ")";
        ResultSet result = statement.executeQuery(query);
        while (result.next()) {
            UUID uuid = UUID.fromString(result.getString("entity_uuid"));
            Entity entity = entityMap.get(uuid);
            int stackSize = result.getInt("stack_size");
            conversionData.add(new ConversionData(entity, stackSize));
        }
    }
    // Delete data
    try (Statement statement = connection.createStatement()) {
        String query = "DELETE FROM " + this.getTablePrefix() + "convert_stacked_" + tableName + " WHERE entity_uuid IN (" + entityUniqueIdsString + ")";
        statement.executeUpdate(query);
    }
    return conversionData;
}
Also used : ConversionData(dev.rosewood.rosestacker.conversion.ConversionData) Arrays(java.util.Arrays) Connection(java.sql.Connection) dev.rosewood.rosestacker.database.migrations._4_Alter_Spawner_Table_Player_Placed(dev.rosewood.rosestacker.database.migrations._4_Alter_Spawner_Table_Player_Placed) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) dev.rosewood.rosestacker.database.migrations._1_Create_Tables_Stacks(dev.rosewood.rosestacker.database.migrations._1_Create_Tables_Stacks) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) Map(java.util.Map) SQLiteConnector(dev.rosewood.rosegarden.database.SQLiteConnector) StackType(dev.rosewood.rosestacker.stack.StackType) EnumSet(java.util.EnumSet) Entity(org.bukkit.entity.Entity) Set(java.util.Set) DataMigration(dev.rosewood.rosegarden.database.DataMigration) UUID(java.util.UUID) PreparedStatement(java.sql.PreparedStatement) dev.rosewood.rosestacker.database.migrations._3_Create_Tables_Translation_Locales(dev.rosewood.rosestacker.database.migrations._3_Create_Tables_Translation_Locales) Collectors(java.util.stream.Collectors) RosePlugin(dev.rosewood.rosegarden.RosePlugin) ConverterType(dev.rosewood.rosestacker.conversion.ConverterType) List(java.util.List) dev.rosewood.rosestacker.database.migrations._2_Create_Tables_Convert_Stacks(dev.rosewood.rosestacker.database.migrations._2_Create_Tables_Convert_Stacks) Statement(java.sql.Statement) AbstractDataManager(dev.rosewood.rosegarden.manager.AbstractDataManager) Entity(org.bukkit.entity.Entity) HashMap(java.util.HashMap) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) ConversionData(dev.rosewood.rosestacker.conversion.ConversionData) UUID(java.util.UUID) HashSet(java.util.HashSet)

Example 3 with ConversionData

use of dev.rosewood.rosestacker.conversion.ConversionData in project RoseStacker by Rosewood-Development.

the class EntityConversionHandler method handleConversion.

@Override
public Set<Stack<?>> handleConversion(Set<ConversionData> conversionData) {
    Set<Stack<?>> stacks = new HashSet<>();
    for (ConversionData data : conversionData) {
        LivingEntity entity = data.getEntity();
        // This could cause data loss if the entity actually has a custom name, but we have to remove the stack tag
        entity.setCustomName(null);
        entity.setCustomNameVisible(false);
        StackedEntity stackedEntity = new StackedEntity(data.getEntity(), this.createEntityStackNBT(entity.getType(), data.getStackSize(), entity.getLocation()));
        this.stackManager.addEntityStack(stackedEntity);
        stacks.add(stackedEntity);
    }
    return stacks;
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) StackedEntity(dev.rosewood.rosestacker.stack.StackedEntity) ConversionData(dev.rosewood.rosestacker.conversion.ConversionData) Stack(dev.rosewood.rosestacker.stack.Stack) HashSet(java.util.HashSet)

Example 4 with ConversionData

use of dev.rosewood.rosestacker.conversion.ConversionData in project RoseStacker by Rosewood-Development.

the class StackMobConversionHandler method handleConversion.

@Override
public Set<Stack<?>> handleConversion(Set<ConversionData> conversionData) {
    Set<LivingEntity> entities = conversionData.stream().map(ConversionData::getEntity).filter(Objects::nonNull).collect(Collectors.toSet());
    Set<Stack<?>> stacks = new HashSet<>();
    for (LivingEntity entity : entities) {
        PersistentDataContainer dataContainer = entity.getPersistentDataContainer();
        if (dataContainer.has(PersistentDataUtils.CONVERTED_KEY, PersistentDataType.INTEGER))
            continue;
        int stackSize = dataContainer.getOrDefault(STACK_KEY, PersistentDataType.INTEGER, -1);
        if (stackSize == -1)
            continue;
        dataContainer.set(PersistentDataUtils.CONVERTED_KEY, PersistentDataType.INTEGER, 1);
        StackedEntity stackedEntity = new StackedEntity(entity, this.createEntityStackNBT(entity.getType(), stackSize, entity.getLocation()));
        this.stackManager.addEntityStack(stackedEntity);
        stacks.add(stackedEntity);
    }
    return stacks;
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) StackedEntity(dev.rosewood.rosestacker.stack.StackedEntity) ConversionData(dev.rosewood.rosestacker.conversion.ConversionData) Stack(dev.rosewood.rosestacker.stack.Stack) HashSet(java.util.HashSet) PersistentDataContainer(org.bukkit.persistence.PersistentDataContainer)

Example 5 with ConversionData

use of dev.rosewood.rosestacker.conversion.ConversionData 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)

Aggregations

ConversionData (dev.rosewood.rosestacker.conversion.ConversionData)10 HashSet (java.util.HashSet)9 Stack (dev.rosewood.rosestacker.stack.Stack)7 StackType (dev.rosewood.rosestacker.stack.StackType)3 StackedEntity (dev.rosewood.rosestacker.stack.StackedEntity)3 StackedItem (dev.rosewood.rosestacker.stack.StackedItem)3 Set (java.util.Set)3 LivingEntity (org.bukkit.entity.LivingEntity)3 PersistentDataContainer (org.bukkit.persistence.PersistentDataContainer)3 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 Statement (java.sql.Statement)2 HashMap (java.util.HashMap)2 UUID (java.util.UUID)2 Entity (org.bukkit.entity.Entity)2 Item (org.bukkit.entity.Item)2 DatabaseConnector (com.songoda.ultimatestacker.core.database.DatabaseConnector)1 RosePlugin (dev.rosewood.rosegarden.RosePlugin)1 DataMigration (dev.rosewood.rosegarden.database.DataMigration)1 SQLiteConnector (dev.rosewood.rosegarden.database.SQLiteConnector)1