Search in sources :

Example 1 with EntityStackEvent

use of dev.rosewood.rosestacker.event.EntityStackEvent in project RoseStacker by Rosewood-Development.

the class StackingThread method tryStackEntity.

/**
 * Tries to stack a StackedEntity with all other StackedEntities
 *
 * @param stackedEntity the StackedEntity to try to stack
 */
private void tryStackEntity(StackedEntity stackedEntity) {
    EntityStackSettings stackSettings = stackedEntity.getStackSettings();
    if (stackSettings == null)
        return;
    if (stackedEntity.checkNPC()) {
        this.removeEntityStack(stackedEntity);
        return;
    }
    LivingEntity entity = stackedEntity.getEntity();
    if (this.isRemoved(entity))
        return;
    if (!WorldGuardHook.testLocation(entity.getLocation()))
        return;
    Collection<Entity> nearbyEntities;
    Predicate<Entity> predicate = x -> x.getType() == entity.getType();
    if (!Setting.ENTITY_MERGE_ENTIRE_CHUNK.getBoolean()) {
        nearbyEntities = this.entityCacheManager.getNearbyEntities(entity.getLocation(), stackSettings.getMergeRadius(), predicate);
    } else {
        nearbyEntities = this.entityCacheManager.getEntitiesInChunk(entity.getLocation(), predicate);
    }
    Set<StackedEntity> targetEntities = new HashSet<>();
    targetEntities.add(stackedEntity);
    for (Entity otherEntity : nearbyEntities) {
        if (entity == otherEntity || this.isRemoved(otherEntity))
            continue;
        StackedEntity other = this.stackedEntities.get(otherEntity.getUniqueId());
        if (other == null)
            continue;
        if (stackSettings.testCanStackWith(stackedEntity, other, false) && (!Setting.ENTITY_REQUIRE_LINE_OF_SIGHT.getBoolean() || EntityUtils.hasLineOfSight(entity, otherEntity, 0.75, false)) && WorldGuardHook.testLocation(otherEntity.getLocation()))
            targetEntities.add(other);
    }
    StackedEntity increased;
    int totalSize;
    List<StackedEntity> removable = new ArrayList<>(targetEntities.size());
    if (!Setting.ENTITY_MIN_STACK_COUNT_ONLY_INDIVIDUALS.getBoolean()) {
        increased = targetEntities.stream().max(StackedEntity::compareTo).orElse(stackedEntity);
        targetEntities.remove(increased);
        totalSize = increased.getStackSize();
        for (StackedEntity target : targetEntities) {
            if (totalSize + target.getStackSize() <= stackSettings.getMaxStackSize()) {
                totalSize += target.getStackSize();
                removable.add(target);
            }
        }
    } else {
        increased = stackedEntity;
        targetEntities.remove(increased);
        totalSize = 1;
        int totalStackSize = increased.getStackSize();
        for (StackedEntity target : targetEntities) {
            if (totalStackSize + target.getStackSize() <= stackSettings.getMaxStackSize()) {
                totalSize++;
                totalStackSize += target.getStackSize();
                removable.add(target);
            }
        }
    }
    if (removable.isEmpty() || totalSize < stackSettings.getMinStackSize())
        return;
    EntityStackEvent entityStackEvent = new EntityStackEvent(removable, increased);
    Bukkit.getPluginManager().callEvent(entityStackEvent);
    if (entityStackEvent.isCancelled())
        return;
    for (StackedEntity toStack : removable) {
        stackSettings.applyStackProperties(toStack.getEntity(), increased.getEntity());
        increased.increaseStackSize(toStack.getEntity());
        increased.increaseStackSize(toStack.getStackedEntityNBT());
        this.removeEntityStack(toStack);
    }
    Runnable removeTask = () -> removable.stream().map(StackedEntity::getEntity).forEach(Entity::remove);
    if (Bukkit.isPrimaryThread()) {
        removeTask.run();
    } else {
        Bukkit.getScheduler().runTask(this.rosePlugin, removeTask);
    }
}
Also used : EntityStackSettings(dev.rosewood.rosestacker.stack.settings.EntityStackSettings) StackedEntityDataEntry(dev.rosewood.rosestacker.nms.storage.StackedEntityDataEntry) EntityUtils(dev.rosewood.rosestacker.utils.EntityUtils) Player(org.bukkit.entity.Player) ItemUtils(dev.rosewood.rosestacker.utils.ItemUtils) PreDropStackedItemsEvent(dev.rosewood.rosestacker.event.PreDropStackedItemsEvent) EntityStackSettings(dev.rosewood.rosestacker.stack.settings.EntityStackSettings) StackSettingManager(dev.rosewood.rosestacker.manager.StackSettingManager) Block(org.bukkit.block.Block) Location(org.bukkit.Location) World(org.bukkit.World) Map(java.util.Map) PersistentDataUtils(dev.rosewood.rosestacker.utils.PersistentDataUtils) Chunk(org.bukkit.Chunk) ItemStackSettings(dev.rosewood.rosestacker.stack.settings.ItemStackSettings) Material(org.bukkit.Material) Bukkit(org.bukkit.Bukkit) Setting(dev.rosewood.rosestacker.manager.ConfigurationManager.Setting) Entity(org.bukkit.entity.Entity) Predicate(java.util.function.Predicate) ItemStackClearEvent(dev.rosewood.rosestacker.event.ItemStackClearEvent) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) UUID(java.util.UUID) EntityType(org.bukkit.entity.EntityType) LivingEntity(org.bukkit.entity.LivingEntity) EntityCacheManager(dev.rosewood.rosestacker.manager.EntityCacheManager) Collectors(java.util.stream.Collectors) RosePlugin(dev.rosewood.rosegarden.RosePlugin) EntityUnstackEvent(dev.rosewood.rosestacker.event.EntityUnstackEvent) StackedEntityDataStorage(dev.rosewood.rosestacker.nms.storage.StackedEntityDataStorage) ItemStack(org.bukkit.inventory.ItemStack) Objects(java.util.Objects) StackManager(dev.rosewood.rosestacker.manager.StackManager) List(java.util.List) Entry(java.util.Map.Entry) BukkitTask(org.bukkit.scheduler.BukkitTask) Optional(java.util.Optional) StackerUtils(dev.rosewood.rosestacker.utils.StackerUtils) NPCsHook(dev.rosewood.rosestacker.hook.NPCsHook) DustOptions(org.bukkit.Particle.DustOptions) Item(org.bukkit.entity.Item) WorldGuardHook(dev.rosewood.rosestacker.hook.WorldGuardHook) HashMap(java.util.HashMap) FixedMetadataValue(org.bukkit.metadata.FixedMetadataValue) DataUtils(dev.rosewood.rosestacker.utils.DataUtils) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ItemStackEvent(dev.rosewood.rosestacker.event.ItemStackEvent) NMSHandler(dev.rosewood.rosestacker.nms.NMSHandler) SpawnReason(org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason) CreatureSpawner(org.bukkit.block.CreatureSpawner) Particle(org.bukkit.Particle) Vector(org.bukkit.util.Vector) EntityStackEvent(dev.rosewood.rosestacker.event.EntityStackEvent) EntityStackClearEvent(dev.rosewood.rosestacker.event.EntityStackClearEvent) ArmorStand(org.bukkit.entity.ArmorStand) NMSAdapter(dev.rosewood.rosestacker.nms.NMSAdapter) Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) EntityStackEvent(dev.rosewood.rosestacker.event.EntityStackEvent) ArrayList(java.util.ArrayList) LivingEntity(org.bukkit.entity.LivingEntity) HashSet(java.util.HashSet)

Aggregations

RosePlugin (dev.rosewood.rosegarden.RosePlugin)1 EntityStackClearEvent (dev.rosewood.rosestacker.event.EntityStackClearEvent)1 EntityStackEvent (dev.rosewood.rosestacker.event.EntityStackEvent)1 EntityUnstackEvent (dev.rosewood.rosestacker.event.EntityUnstackEvent)1 ItemStackClearEvent (dev.rosewood.rosestacker.event.ItemStackClearEvent)1 ItemStackEvent (dev.rosewood.rosestacker.event.ItemStackEvent)1 PreDropStackedItemsEvent (dev.rosewood.rosestacker.event.PreDropStackedItemsEvent)1 NPCsHook (dev.rosewood.rosestacker.hook.NPCsHook)1 WorldGuardHook (dev.rosewood.rosestacker.hook.WorldGuardHook)1 Setting (dev.rosewood.rosestacker.manager.ConfigurationManager.Setting)1 EntityCacheManager (dev.rosewood.rosestacker.manager.EntityCacheManager)1 StackManager (dev.rosewood.rosestacker.manager.StackManager)1 StackSettingManager (dev.rosewood.rosestacker.manager.StackSettingManager)1 NMSAdapter (dev.rosewood.rosestacker.nms.NMSAdapter)1 NMSHandler (dev.rosewood.rosestacker.nms.NMSHandler)1 StackedEntityDataEntry (dev.rosewood.rosestacker.nms.storage.StackedEntityDataEntry)1 StackedEntityDataStorage (dev.rosewood.rosestacker.nms.storage.StackedEntityDataStorage)1 EntityStackSettings (dev.rosewood.rosestacker.stack.settings.EntityStackSettings)1 ItemStackSettings (dev.rosewood.rosestacker.stack.settings.ItemStackSettings)1 DataUtils (dev.rosewood.rosestacker.utils.DataUtils)1