Search in sources :

Example 1 with StackedEntityDataEntry

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

the class StackerUtils method deconstructStackedEntities.

public static List<LivingEntity> deconstructStackedEntities(StackedEntity stackedEntity) {
    List<StackedEntityDataEntry<?>> stackedEntityDataEntry = stackedEntity.getStackedEntityNBT().getAll();
    List<LivingEntity> livingEntities = new ArrayList<>(stackedEntityDataEntry.size());
    Location location = stackedEntity.getLocation();
    NMSHandler nmsHandler = NMSAdapter.getHandler();
    for (StackedEntityDataEntry<?> nbt : stackedEntityDataEntry) livingEntities.add(nmsHandler.createEntityFromNBT(nbt, location, false, stackedEntity.getEntity().getType()));
    return livingEntities;
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) ArrayList(java.util.ArrayList) StackedEntityDataEntry(dev.rosewood.rosestacker.nms.storage.StackedEntityDataEntry) NMSHandler(dev.rosewood.rosestacker.nms.NMSHandler) Location(org.bukkit.Location)

Example 2 with StackedEntityDataEntry

use of dev.rosewood.rosestacker.nms.storage.StackedEntityDataEntry 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

NMSHandler (dev.rosewood.rosestacker.nms.NMSHandler)2 StackedEntityDataEntry (dev.rosewood.rosestacker.nms.storage.StackedEntityDataEntry)2 LivingEntity (org.bukkit.entity.LivingEntity)2 StackedEntityDataStorage (dev.rosewood.rosestacker.nms.storage.StackedEntityDataStorage)1 ArrayList (java.util.ArrayList)1 Location (org.bukkit.Location)1 Entity (org.bukkit.entity.Entity)1 Item (org.bukkit.entity.Item)1