Search in sources :

Example 1 with StackedEntityDataStorage

use of dev.rosewood.rosestacker.nms.storage.StackedEntityDataStorage in project RoseStacker by Rosewood-Development.

the class ConversionHandler method createEntityStackNBT.

/**
 * Used to fill in the missing entity stack nbt data
 *
 * @param entityType The type of entity
 * @param amount The amount of nbt entries to create
 * @param location The location of the main entity
 * @return A list of nbt data
 */
protected StackedEntityDataStorage createEntityStackNBT(EntityType entityType, int amount, Location location) {
    NMSHandler nmsHandler = NMSAdapter.getHandler();
    StackedEntityDataStorage stackedEntityDataStorage = nmsHandler.createEntityDataStorage(nmsHandler.createNewEntityUnspawned(entityType, location, CreatureSpawnEvent.SpawnReason.CUSTOM));
    for (int i = 0; i < amount - 1; i++) stackedEntityDataStorage.addFirst(nmsHandler.createNewEntityUnspawned(entityType, location, CreatureSpawnEvent.SpawnReason.CUSTOM));
    return stackedEntityDataStorage;
}
Also used : StackedEntityDataStorage(dev.rosewood.rosestacker.nms.storage.StackedEntityDataStorage) NMSHandler(dev.rosewood.rosestacker.nms.NMSHandler)

Example 2 with StackedEntityDataStorage

use of dev.rosewood.rosestacker.nms.storage.StackedEntityDataStorage in project RoseStacker by Rosewood-Development.

the class StackerUtils method reconstructStackedEntities.

public static void reconstructStackedEntities(StackedEntity stackedEntity, List<? extends LivingEntity> livingEntities) {
    StackedEntityDataStorage stackedEntityDataStorage = NMSAdapter.getHandler().createEntityDataStorage(stackedEntity.getEntity());
    for (LivingEntity livingEntity : livingEntities) stackedEntityDataStorage.addLast(livingEntity);
    stackedEntity.setStackedEntityNBT(stackedEntityDataStorage);
}
Also used : StackedEntityDataStorage(dev.rosewood.rosestacker.nms.storage.StackedEntityDataStorage) LivingEntity(org.bukkit.entity.LivingEntity)

Example 3 with StackedEntityDataStorage

use of dev.rosewood.rosestacker.nms.storage.StackedEntityDataStorage in project RoseStacker by Rosewood-Development.

the class StackingThread method stackEntities.

private void stackEntities() {
    boolean itemStackingEnabled = this.stackManager.isItemStackingEnabled();
    boolean entityStackingEnabled = this.stackManager.isEntityStackingEnabled();
    if (!entityStackingEnabled)
        return;
    // Auto unstack entities
    if (!this.stackManager.isEntityUnstackingTemporarilyDisabled()) {
        boolean minSplitIfLower = Setting.ENTITY_MIN_SPLIT_IF_LOWER.getBoolean();
        for (StackedEntity stackedEntity : this.stackedEntities.values()) {
            if (!stackedEntity.shouldStayStacked() && stackedEntity.getEntity().isValid()) {
                Bukkit.getScheduler().runTask(this.rosePlugin, () -> {
                    if (stackedEntity.getStackSize() > 1)
                        this.splitEntityStack(stackedEntity);
                });
            } else if (minSplitIfLower && stackedEntity.getStackSize() < stackedEntity.getStackSettings().getMinStackSize()) {
                NMSHandler nmsHandler = NMSAdapter.getHandler();
                StackedEntityDataStorage nbt = stackedEntity.getStackedEntityNBT();
                stackedEntity.setStackedEntityNBT(nmsHandler.createEntityDataStorage(stackedEntity.getEntity()));
                Bukkit.getScheduler().runTask(this.rosePlugin, () -> {
                    for (StackedEntityDataEntry<?> stackedEntityDataEntry : nbt.getAll()) nmsHandler.createEntityFromNBT(stackedEntityDataEntry, stackedEntity.getLocation(), true, stackedEntity.getEntity().getType());
                });
            }
        }
    }
    // Auto stack entities
    if (this.entityStackSwitch) {
        for (StackedEntity stackedEntity : this.stackedEntities.values()) {
            LivingEntity livingEntity = stackedEntity.getEntity();
            if (this.isRemoved(livingEntity)) {
                this.removeEntityStack(stackedEntity);
                continue;
            }
            this.tryStackEntity(stackedEntity);
        }
    }
    // Run entity stacking half as often as the unstacking
    this.entityStackSwitch = !this.entityStackSwitch;
    // Cleans up entities/items that aren't stacked
    this.cleanupTimer++;
    if (this.cleanupTimer >= CLEANUP_TIMER_TARGET) {
        Bukkit.getScheduler().runTask(this.rosePlugin, () -> {
            for (Entity entity : this.targetWorld.getEntities()) {
                if (this.isRemoved(entity))
                    continue;
                if (entity instanceof LivingEntity && entity.getType() != EntityType.ARMOR_STAND && entity.getType() != EntityType.PLAYER) {
                    LivingEntity livingEntity = (LivingEntity) entity;
                    if (!this.isEntityStacked(livingEntity))
                        this.createEntityStack(livingEntity, false);
                } else if (itemStackingEnabled && entity.getType() == EntityType.DROPPED_ITEM) {
                    Item item = (Item) entity;
                    if (!this.isItemStacked(item))
                        this.createItemStack(item, false);
                }
            }
        });
        this.cleanupTimer = 0;
    }
}
Also used : StackedEntityDataStorage(dev.rosewood.rosestacker.nms.storage.StackedEntityDataStorage) LivingEntity(org.bukkit.entity.LivingEntity) Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) Item(org.bukkit.entity.Item) StackedEntityDataEntry(dev.rosewood.rosestacker.nms.storage.StackedEntityDataEntry) NMSHandler(dev.rosewood.rosestacker.nms.NMSHandler)

Aggregations

StackedEntityDataStorage (dev.rosewood.rosestacker.nms.storage.StackedEntityDataStorage)3 NMSHandler (dev.rosewood.rosestacker.nms.NMSHandler)2 LivingEntity (org.bukkit.entity.LivingEntity)2 StackedEntityDataEntry (dev.rosewood.rosestacker.nms.storage.StackedEntityDataEntry)1 Entity (org.bukkit.entity.Entity)1 Item (org.bukkit.entity.Item)1