Search in sources :

Example 6 with StackManager

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

the class StackedBlockGui method updateStackedBlock.

/**
 * Updates the StackedBlock with the changed item contents
 *
 * @param player The Player that finalized the change
 * @param items The Items that are inside the StackedBlock
 */
private void updateStackedBlock(Player player, List<ItemStack> items) {
    StackManager stackManager = this.rosePlugin.getManager(StackManager.class);
    // No longer any players viewing
    this.guiContainer = null;
    int newStackSize = items.stream().mapToInt(ItemStack::getAmount).sum();
    if (newStackSize == this.stackedBlock.getStackSize())
        return;
    int difference = newStackSize - this.stackedBlock.getStackSize();
    if (newStackSize > this.stackedBlock.getStackSize()) {
        BlockStackEvent blockStackEvent = new BlockStackEvent(player, this.stackedBlock, difference, false);
        Bukkit.getPluginManager().callEvent(blockStackEvent);
        if (blockStackEvent.isCancelled()) {
            ItemUtils.dropItemsToPlayer(player, GuiUtil.getMaterialAmountAsItemStacks(this.stackedBlock.getBlock().getType(), difference));
            return;
        }
        newStackSize = this.stackedBlock.getStackSize() + blockStackEvent.getIncreaseAmount();
    } else {
        BlockUnstackEvent blockUnstackEvent = new BlockUnstackEvent(player, this.stackedBlock, -difference);
        Bukkit.getPluginManager().callEvent(blockUnstackEvent);
        if (blockUnstackEvent.isCancelled()) {
            this.takeFromPlayer(player, -difference);
            return;
        }
        newStackSize = this.stackedBlock.getStackSize() - blockUnstackEvent.getDecreaseAmount();
    }
    this.stackedBlock.setStackSize(newStackSize);
    int maxStackSize = this.stackedBlock.getStackSettings().getMaxStackSize();
    if (newStackSize == 1) {
        stackManager.removeBlockStack(this.stackedBlock);
    } else if (newStackSize == 0) {
        stackManager.removeBlockStack(this.stackedBlock);
        this.stackedBlock.getBlock().setType(Material.AIR);
    } else if (newStackSize > maxStackSize) {
        List<ItemStack> overflowItems = GuiUtil.getMaterialAmountAsItemStacks(this.stackedBlock.getBlock().getType(), newStackSize - maxStackSize);
        ItemUtils.dropItemsToPlayer(player, overflowItems);
        this.stackedBlock.setStackSize(maxStackSize);
    }
}
Also used : StackManager(dev.rosewood.rosestacker.manager.StackManager) ItemStack(org.bukkit.inventory.ItemStack) BlockStackEvent(dev.rosewood.rosestacker.event.BlockStackEvent) BlockUnstackEvent(dev.rosewood.rosestacker.event.BlockUnstackEvent)

Example 7 with StackManager

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

the class StackedBlockGui method destroyStackedBlock.

/**
 * Destroys the StackedBlock and drops all its items either onto the ground or a player inventory
 *
 * @param player The Player that destroyed the stack
 */
private void destroyStackedBlock(Player player) {
    this.kickOutViewers();
    StackManager stackManager = this.rosePlugin.getManager(StackManager.class);
    Bukkit.getScheduler().runTask(this.rosePlugin, () -> {
        BlockUnstackEvent blockUnstackEvent = new BlockUnstackEvent(player, this.stackedBlock, this.stackedBlock.getStackSize());
        Bukkit.getPluginManager().callEvent(blockUnstackEvent);
        if (blockUnstackEvent.isCancelled())
            return;
        List<ItemStack> itemsToDrop;
        if (Setting.BLOCK_BREAK_ENTIRE_STACK_INTO_SEPARATE.getBoolean()) {
            itemsToDrop = GuiUtil.getMaterialAmountAsItemStacks(this.stackedBlock.getBlock().getType(), blockUnstackEvent.getDecreaseAmount());
        } else {
            itemsToDrop = Collections.singletonList(ItemUtils.getBlockAsStackedItemStack(this.stackedBlock.getBlock().getType(), blockUnstackEvent.getDecreaseAmount()));
        }
        if (Setting.BLOCK_DROP_TO_INVENTORY.getBoolean()) {
            ItemUtils.dropItemsToPlayer(player, itemsToDrop);
        } else {
            stackManager.preStackItems(itemsToDrop, this.stackedBlock.getLocation());
        }
        this.stackedBlock.setStackSize(this.stackedBlock.getStackSize() - blockUnstackEvent.getDecreaseAmount());
        if (this.stackedBlock.getStackSize() <= 0) {
            stackManager.removeBlockStack(this.stackedBlock);
            this.stackedBlock.getBlock().setType(Material.AIR);
            this.stackedBlock.getBlock().getWorld().playSound(this.stackedBlock.getBlock().getLocation(), Sound.BLOCK_ANVIL_LAND, 0.1F, 0.01F);
        }
    });
}
Also used : StackManager(dev.rosewood.rosestacker.manager.StackManager) ItemStack(org.bukkit.inventory.ItemStack) BlockUnstackEvent(dev.rosewood.rosestacker.event.BlockUnstackEvent)

Example 8 with StackManager

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

Example 9 with StackManager

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

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

the class BeeListener method onBeeEnterHive.

@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onBeeEnterHive(EntityEnterBlockEvent event) {
    if (event.getEntityType() != EntityType.BEE)
        return;
    StackManager stackManager = this.rosePlugin.getManager(StackManager.class);
    if (stackManager.isWorldDisabled(event.getBlock().getWorld()))
        return;
    if (!stackManager.isEntityStackingEnabled())
        return;
    Bee beeEntity = (Bee) event.getEntity();
    StackedEntity stackedEntity = stackManager.getStackedEntity(beeEntity);
    if (stackedEntity == null)
        return;
    if (stackedEntity.getStackSize() == 1) {
        stackManager.removeEntityStack(stackedEntity);
        return;
    }
    Bukkit.getScheduler().runTask(this.rosePlugin, stackedEntity::decreaseStackSize);
}
Also used : StackedEntity(dev.rosewood.rosestacker.stack.StackedEntity) Bee(org.bukkit.entity.Bee) StackManager(dev.rosewood.rosestacker.manager.StackManager) 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