Search in sources :

Example 6 with StackedEntity

use of dev.rosewood.rosestacker.stack.StackedEntity 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 7 with StackedEntity

use of dev.rosewood.rosestacker.stack.StackedEntity 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 8 with StackedEntity

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

the class MaxNearbyEntityConditionTag method check.

@Override
public boolean check(StackedSpawner stackedSpawner, Block spawnBlock) {
    if (this.stackManager == null || this.entityCacheManager == null) {
        this.stackManager = RoseStacker.getInstance().getManager(StackManager.class);
        this.entityCacheManager = RoseStacker.getInstance().getManager(EntityCacheManager.class);
    }
    SpawnerStackSettings stackSettings = stackedSpawner.getStackSettings();
    int detectionRange = stackSettings.getEntitySearchRange() == -1 ? stackedSpawner.getSpawnerTile().getSpawnRange() : stackSettings.getEntitySearchRange();
    Block block = stackedSpawner.getBlock();
    EntityType entityType = stackedSpawner.getSpawnerTile().getSpawnedType();
    Collection<Entity> nearbyEntities = this.entityCacheManager.getNearbyEntities(block.getLocation().clone().add(0.5, 0.5, 0.5), detectionRange, entity -> entity.getType() == entityType);
    if (Setting.SPAWNER_MAX_NEARBY_ENTITIES_INCLUDE_STACKS.getBoolean()) {
        return nearbyEntities.stream().mapToInt(x -> {
            StackedEntity stackedEntity = this.stackManager.getStackedEntity((LivingEntity) x);
            return stackedEntity == null ? 1 : stackedEntity.getStackSize();
        }).sum() < this.maxNearbyEntities;
    } else {
        return nearbyEntities.size() < this.maxNearbyEntities;
    }
}
Also used : EntityType(org.bukkit.entity.EntityType) SpawnerStackSettings(dev.rosewood.rosestacker.stack.settings.SpawnerStackSettings) Entity(org.bukkit.entity.Entity) StackedEntity(dev.rosewood.rosestacker.stack.StackedEntity) LivingEntity(org.bukkit.entity.LivingEntity) StackedEntity(dev.rosewood.rosestacker.stack.StackedEntity) StackManager(dev.rosewood.rosestacker.manager.StackManager) EntityCacheManager(dev.rosewood.rosestacker.manager.EntityCacheManager) Block(org.bukkit.block.Block)

Example 9 with StackedEntity

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

the class DataUtils method readStackedEntity.

public static StackedEntity readStackedEntity(LivingEntity entity) {
    PersistentDataContainer pdc = entity.getPersistentDataContainer();
    NMSHandler nmsHandler = NMSAdapter.getHandler();
    byte[] data = pdc.get(ENTITY_KEY, PersistentDataType.BYTE_ARRAY);
    if (data == null)
        return new StackedEntity(entity, nmsHandler.createEntityDataStorage(entity));
    try (ByteArrayInputStream inputStream = new ByteArrayInputStream(data);
        ObjectInputStream dataInput = new ObjectInputStream(new GZIPInputStream(inputStream))) {
        int dataVersion = dataInput.readInt();
        if (dataVersion == 1) {
            int length = dataInput.readInt();
            byte[] nbt = new byte[length];
            for (int i = 0; i < length; i++) nbt[i] = dataInput.readByte();
            return new StackedEntity(entity, nmsHandler.deserializeEntityDataStorage(nbt));
        }
    } catch (Exception e) {
        e.printStackTrace();
        pdc.remove(ENTITY_KEY);
    }
    return null;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) StackedEntity(dev.rosewood.rosestacker.stack.StackedEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) NMSHandler(dev.rosewood.rosestacker.nms.NMSHandler) PersistentDataContainer(org.bukkit.persistence.PersistentDataContainer) ObjectInputStream(java.io.ObjectInputStream)

Example 10 with StackedEntity

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

StackedEntity (dev.rosewood.rosestacker.stack.StackedEntity)19 LivingEntity (org.bukkit.entity.LivingEntity)14 EventHandler (org.bukkit.event.EventHandler)10 StackManager (dev.rosewood.rosestacker.manager.StackManager)8 Player (org.bukkit.entity.Player)8 Entity (org.bukkit.entity.Entity)7 ItemStack (org.bukkit.inventory.ItemStack)7 NMSHandler (dev.rosewood.rosestacker.nms.NMSHandler)6 Setting (dev.rosewood.rosestacker.manager.ConfigurationManager.Setting)5 EntityCacheManager (dev.rosewood.rosestacker.manager.EntityCacheManager)5 SpawnerStackSettings (dev.rosewood.rosestacker.stack.settings.SpawnerStackSettings)5 PersistentDataUtils (dev.rosewood.rosestacker.utils.PersistentDataUtils)5 StackerUtils (dev.rosewood.rosestacker.utils.StackerUtils)5 ArrayList (java.util.ArrayList)5 HashSet (java.util.HashSet)5 Bukkit (org.bukkit.Bukkit)5 Material (org.bukkit.Material)5 EntityType (org.bukkit.entity.EntityType)5 RoseStacker (dev.rosewood.rosestacker.RoseStacker)4 StackSettingManager (dev.rosewood.rosestacker.manager.StackSettingManager)4