Search in sources :

Example 21 with StackManager

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

the class StackedEntity method decreaseStackSize.

/**
 * Unstacks the visible entity from the stack and moves the next in line to the front
 *
 * @return The new StackedEntity of size 1 that was just created
 */
public StackedEntity decreaseStackSize() {
    if (this.serializedStackedEntities.isEmpty())
        throw new IllegalStateException();
    StackManager stackManager = RoseStacker.getInstance().getManager(StackManager.class);
    LivingEntity oldEntity = this.entity;
    stackManager.setEntityStackingTemporarilyDisabled(true);
    this.entity = NMSAdapter.getHandler().createEntityFromNBT(this.serializedStackedEntities.pop(), oldEntity.getLocation(), true, oldEntity.getType());
    stackManager.setEntityStackingTemporarilyDisabled(false);
    this.stackSettings.applyUnstackProperties(this.entity, oldEntity);
    stackManager.updateStackedEntityKey(oldEntity, this.entity);
    // Nudge the entity to unstack it from the old entity
    this.entity.setVelocity(this.entity.getVelocity().add(Vector.getRandom().multiply(0.01)));
    // Attempt to prevent adult entities from going into walls when a baby entity gets unstacked
    if (oldEntity instanceof Ageable) {
        Ageable ageable1 = (Ageable) oldEntity;
        Ageable ageable2 = (Ageable) this.entity;
        if (!ageable1.isAdult() && ageable2.isAdult()) {
            Location centered = ageable1.getLocation();
            centered.setX(centered.getBlockX() + 0.5);
            centered.setZ(centered.getBlockZ() + 0.5);
            ageable2.teleport(centered);
        }
    }
    this.updateDisplay();
    PersistentDataUtils.applyDisabledAi(this.entity);
    DataUtils.clearStackedEntityData(oldEntity);
    return new StackedEntity(oldEntity, NMSAdapter.getHandler().createEntityDataStorage(oldEntity));
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) StackManager(dev.rosewood.rosestacker.manager.StackManager) Ageable(org.bukkit.entity.Ageable) Location(org.bukkit.Location)

Example 22 with StackManager

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

the class MobSpawningMethod method spawn.

@Override
public void spawn(StackedSpawner stackedSpawner) {
    StackedSpawnerTile spawnerTile = stackedSpawner.getSpawnerTile();
    SpawnerStackSettings stackSettings = stackedSpawner.getStackSettings();
    // Mob spawning logic
    List<ConditionTag> spawnRequirements = new ArrayList<>(stackSettings.getSpawnRequirements());
    // Check general spawner conditions
    List<ConditionTag> perSpawnConditions = spawnRequirements.stream().filter(ConditionTag::isRequiredPerSpawn).collect(Collectors.toList());
    spawnRequirements.removeAll(perSpawnConditions);
    Set<ConditionTag> invalidSpawnConditions = spawnRequirements.stream().filter(x -> !x.check(stackedSpawner, stackedSpawner.getBlock())).collect(Collectors.toSet());
    if (Setting.SPAWNER_SPAWN_ONLY_PLAYER_PLACED.getBoolean() && !stackedSpawner.isPlacedByPlayer())
        invalidSpawnConditions.add(NotPlayerPlacedConditionTag.INSTANCE);
    boolean passedSpawnerChecks = invalidSpawnConditions.isEmpty();
    // Will be removed when they pass
    invalidSpawnConditions.addAll(perSpawnConditions);
    // Spawn the mobs
    int spawnAmount;
    if (Setting.SPAWNER_SPAWN_COUNT_STACK_SIZE_RANDOMIZED.getBoolean()) {
        if (stackSettings.getSpawnCountStackSizeMultiplier() != -1) {
            int spawnerSpawnCount = Math.max(spawnerTile.getSpawnCount(), 0);
            spawnAmount = StackerUtils.randomInRange(stackedSpawner.getStackSize(), spawnerSpawnCount);
        } else {
            spawnAmount = this.random.nextInt(spawnerTile.getSpawnCount()) + 1;
        }
    } else {
        spawnAmount = spawnerTile.getSpawnCount();
    }
    EntityCacheManager entityCacheManager = RoseStacker.getInstance().getManager(EntityCacheManager.class);
    StackManager stackManager = RoseStacker.getInstance().getManager(StackManager.class);
    Bukkit.getScheduler().runTaskAsynchronously(RoseStacker.getInstance(), () -> {
        Set<Location> spawnLocations = new HashSet<>();
        int spawnRange = spawnerTile.getSpawnRange();
        for (int i = 0; i < spawnAmount; i++) {
            int attempts = 0;
            while (attempts < Setting.SPAWNER_MAX_FAILED_SPAWN_ATTEMPTS.getInt()) {
                int xOffset = this.random.nextInt(spawnRange * 2 + 1) - spawnRange;
                int yOffset = !Setting.SPAWNER_USE_VERTICAL_SPAWN_RANGE.getBoolean() ? this.random.nextInt(3) - 1 : this.random.nextInt(spawnRange * 2 + 1) - spawnRange;
                int zOffset = this.random.nextInt(spawnRange * 2 + 1) - spawnRange;
                Location spawnLocation = stackedSpawner.getLocation().clone().add(xOffset + 0.5, yOffset, zOffset + 0.5);
                Block target = stackedSpawner.getLocation().clone().add(xOffset, yOffset, zOffset).getBlock();
                boolean invalid = false;
                for (ConditionTag conditionTag : perSpawnConditions) {
                    if (!conditionTag.check(stackedSpawner, target)) {
                        invalid = true;
                    } else {
                        invalidSpawnConditions.remove(conditionTag);
                    }
                }
                if (invalid) {
                    attempts++;
                    continue;
                }
                if (!passedSpawnerChecks)
                    break;
                spawnLocations.add(spawnLocation);
                break;
            }
        }
        EntityType entityType = stackedSpawner.getSpawnerTile().getSpawnedType();
        Predicate<Entity> predicate = entity -> entity.getType() == entityType;
        Collection<Entity> nearbyEntities = entityCacheManager.getNearbyEntities(stackedSpawner.getLocation(), stackSettings.getSpawnRange(), predicate);
        List<StackedEntity> nearbyStackedEntities = new ArrayList<>();
        for (Entity entity : nearbyEntities) {
            StackedEntity stackedEntity = stackManager.getStackedEntity((LivingEntity) entity);
            if (stackedEntity != null)
                nearbyStackedEntities.add(stackedEntity);
        }
        int successfulSpawns = this.spawnEntitiesIntoNearbyStacks(stackedSpawner, spawnAmount, spawnLocations, nearbyStackedEntities, stackManager);
        stackedSpawner.getLastInvalidConditions().clear();
        if (successfulSpawns <= 0) {
            if (invalidSpawnConditions.isEmpty()) {
                stackedSpawner.getLastInvalidConditions().add(NoneConditionTag.class);
            } else {
                List<Class<? extends ConditionTag>> invalidSpawnConditionClasses = new ArrayList<>();
                for (ConditionTag conditionTag : invalidSpawnConditions) invalidSpawnConditionClasses.add(conditionTag.getClass());
                stackedSpawner.getLastInvalidConditions().addAll(invalidSpawnConditionClasses);
            }
            // Spawn particles indicating the spawn did not occur
            stackedSpawner.getWorld().spawnParticle(Particle.SMOKE_NORMAL, stackedSpawner.getLocation().clone().add(0.5, 0.5, 0.5), 50, 0.5, 0.5, 0.5, 0);
        } else {
            // Spawn particles indicating the spawn occurred
            stackedSpawner.getWorld().spawnParticle(Particle.FLAME, stackedSpawner.getLocation().clone().add(0.5, 0.5, 0.5), 50, 0.5, 0.5, 0.5, 0);
            Bukkit.getScheduler().runTask(RoseStacker.getInstance(), () -> {
                if (stackedSpawner.getBlock().getType() == Material.SPAWNER)
                    PersistentDataUtils.increaseSpawnCount(spawnerTile, successfulSpawns);
            });
        }
    });
}
Also used : 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) SpawnerStackSettings(dev.rosewood.rosestacker.stack.settings.SpawnerStackSettings) StackedEntity(dev.rosewood.rosestacker.stack.StackedEntity) Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) StackedEntity(dev.rosewood.rosestacker.stack.StackedEntity) NoneConditionTag(dev.rosewood.rosestacker.spawner.conditions.tags.NoneConditionTag) NotPlayerPlacedConditionTag(dev.rosewood.rosestacker.spawner.conditions.tags.NotPlayerPlacedConditionTag) ConditionTag(dev.rosewood.rosestacker.spawner.conditions.ConditionTag) StackManager(dev.rosewood.rosestacker.manager.StackManager) ArrayList(java.util.ArrayList) EntityCacheManager(dev.rosewood.rosestacker.manager.EntityCacheManager) StackedSpawnerTile(dev.rosewood.rosestacker.nms.spawner.StackedSpawnerTile) EntityType(org.bukkit.entity.EntityType) Block(org.bukkit.block.Block) Location(org.bukkit.Location) HashSet(java.util.HashSet)

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