Search in sources :

Example 11 with NMSHandler

use of dev.rosewood.rosestacker.nms.NMSHandler 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 12 with NMSHandler

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

the class ItemUtils method getEntityAsStackedItemStack.

public static ItemStack getEntityAsStackedItemStack(EntityType entityType, int amount) {
    EntityStackSettings stackSettings = RoseStacker.getInstance().getManager(StackSettingManager.class).getEntityStackSettings(entityType);
    Material spawnEggMaterial = stackSettings.getEntityTypeData().getSpawnEggMaterial();
    if (spawnEggMaterial == null)
        return null;
    ItemStack itemStack = new ItemStack(spawnEggMaterial);
    if (amount == 1)
        return itemStack;
    ItemMeta itemMeta = itemStack.getItemMeta();
    if (itemMeta == null)
        return itemStack;
    StringPlaceholders placeholders = StringPlaceholders.builder("amount", amount).addPlaceholder("name", stackSettings.getDisplayName()).build();
    String displayString = RoseStacker.getInstance().getManager(LocaleManager.class).getLocaleMessage("entity-stack-display-spawn-egg", placeholders);
    itemMeta.setDisplayName(displayString);
    // Set the lore, if defined
    List<String> lore = RoseStacker.getInstance().getManager(LocaleManager.class).getLocaleMessages("stack-item-lore-entity", placeholders);
    if (!lore.isEmpty())
        itemMeta.setLore(lore);
    itemStack.setItemMeta(itemMeta);
    // Set stack size
    NMSHandler nmsHandler = NMSAdapter.getHandler();
    itemStack = nmsHandler.setItemStackNBT(itemStack, "StackSize", amount);
    return itemStack;
}
Also used : EntityStackSettings(dev.rosewood.rosestacker.stack.settings.EntityStackSettings) StringPlaceholders(dev.rosewood.rosegarden.utils.StringPlaceholders) StackSettingManager(dev.rosewood.rosestacker.manager.StackSettingManager) Material(org.bukkit.Material) LocaleManager(dev.rosewood.rosestacker.manager.LocaleManager) ItemStack(org.bukkit.inventory.ItemStack) NMSHandler(dev.rosewood.rosestacker.nms.NMSHandler) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Example 13 with NMSHandler

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

the class ItemUtils method getBlockAsStackedItemStack.

public static ItemStack getBlockAsStackedItemStack(Material material, int amount) {
    ItemStack itemStack = new ItemStack(material);
    if (amount == 1)
        return itemStack;
    ItemMeta itemMeta = itemStack.getItemMeta();
    if (itemMeta == null)
        return itemStack;
    BlockStackSettings stackSettings = RoseStacker.getInstance().getManager(StackSettingManager.class).getBlockStackSettings(material);
    StringPlaceholders placeholders = StringPlaceholders.builder("amount", amount).addPlaceholder("name", stackSettings.getDisplayName()).build();
    String displayString = RoseStacker.getInstance().getManager(LocaleManager.class).getLocaleMessage("block-stack-display", placeholders);
    itemMeta.setDisplayName(displayString);
    // Set the lore, if defined
    List<String> lore = RoseStacker.getInstance().getManager(LocaleManager.class).getLocaleMessages("stack-item-lore-block", placeholders);
    if (!lore.isEmpty())
        itemMeta.setLore(lore);
    itemStack.setItemMeta(itemMeta);
    // Set stack size
    NMSHandler nmsHandler = NMSAdapter.getHandler();
    itemStack = nmsHandler.setItemStackNBT(itemStack, "StackSize", amount);
    return itemStack;
}
Also used : StringPlaceholders(dev.rosewood.rosegarden.utils.StringPlaceholders) StackSettingManager(dev.rosewood.rosestacker.manager.StackSettingManager) LocaleManager(dev.rosewood.rosestacker.manager.LocaleManager) ItemStack(org.bukkit.inventory.ItemStack) BlockStackSettings(dev.rosewood.rosestacker.stack.settings.BlockStackSettings) NMSHandler(dev.rosewood.rosestacker.nms.NMSHandler) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Example 14 with NMSHandler

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

the class EntityListener method handleEntityTransformation.

private void handleEntityTransformation(EntityTransformEvent event) {
    if (this.stackManager.isWorldDisabled(event.getEntity().getWorld()))
        return;
    if (!this.stackManager.isEntityStackingEnabled())
        return;
    if (event.getEntity() instanceof Slime) {
        if (PersistentDataUtils.isAiDisabled((LivingEntity) event.getEntity()))
            event.getTransformedEntities().stream().map(x -> (Slime) x).forEach(PersistentDataUtils::removeEntityAi);
        return;
    }
    if (!(event.getEntity() instanceof LivingEntity) || !(event.getTransformedEntity() instanceof LivingEntity) || event.getEntity().getType() == event.getTransformedEntity().getType() || !this.stackManager.isEntityStacked((LivingEntity) event.getEntity()))
        return;
    StackedEntity stackedEntity = this.stackManager.getStackedEntity((LivingEntity) event.getEntity());
    if (stackedEntity.getStackSize() == 1)
        return;
    LivingEntity transformedEntity = (LivingEntity) event.getTransformedEntity();
    if (Setting.ENTITY_TRANSFORM_ENTIRE_STACK.getBoolean()) {
        NMSHandler nmsHandler = NMSAdapter.getHandler();
        StackedEntityDataEntry<?> serialized = nmsHandler.getEntityAsNBT(transformedEntity);
        event.setCancelled(true);
        // Handle mooshroom shearing
        if (event.getEntityType() == EntityType.MUSHROOM_COW) {
            MushroomCowStackSettings stackSettings = (MushroomCowStackSettings) stackedEntity.getStackSettings();
            int mushroomsDropped = 5;
            if (stackSettings.shouldDropAdditionalMushroomsPerCowInStack())
                mushroomsDropped += (stackedEntity.getStackSize() - 1) * stackSettings.getExtraMushroomsPerCowInStack();
            Material dropType = ((MushroomCow) event.getEntity()).getVariant() == Variant.BROWN ? Material.BROWN_MUSHROOM : Material.RED_MUSHROOM;
            this.stackManager.preStackItems(GuiUtil.getMaterialAmountAsItemStacks(dropType, mushroomsDropped), event.getEntity().getLocation());
        }
        boolean aiDisabled = PersistentDataUtils.isAiDisabled((LivingEntity) event.getEntity());
        event.getEntity().remove();
        Bukkit.getScheduler().scheduleSyncDelayedTask(this.rosePlugin, () -> {
            this.stackManager.setEntityStackingTemporarilyDisabled(true);
            LivingEntity newEntity = nmsHandler.createEntityFromNBT(serialized, transformedEntity.getLocation(), true, transformedEntity.getType());
            if (aiDisabled)
                PersistentDataUtils.removeEntityAi(newEntity);
            StackedEntity newStack = this.stackManager.createEntityStack(newEntity, false);
            this.stackManager.setEntityStackingTemporarilyDisabled(false);
            if (newStack == null)
                return;
            for (StackedEntityDataEntry<?> serializedEntity : stackedEntity.getStackedEntityNBT().getAll()) {
                LivingEntity entity = nmsHandler.createEntityFromNBT(serializedEntity, transformedEntity.getLocation(), false, transformedEntity.getType());
                if (aiDisabled)
                    PersistentDataUtils.removeEntityAi(entity);
                newStack.increaseStackSize(entity);
            }
        });
    } else {
        // Make sure disabled AI gets transferred
        if (PersistentDataUtils.isAiDisabled((LivingEntity) event.getEntity()))
            PersistentDataUtils.removeEntityAi((LivingEntity) event.getTransformedEntity());
        if (event.getTransformReason() == TransformReason.LIGHTNING) {
            // Wait for lightning to disappear
            Bukkit.getScheduler().scheduleSyncDelayedTask(this.rosePlugin, stackedEntity::decreaseStackSize, 20);
        } else {
            Bukkit.getScheduler().runTask(this.rosePlugin, stackedEntity::decreaseStackSize);
        }
    }
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) MushroomCowStackSettings(dev.rosewood.rosestacker.stack.settings.entity.MushroomCowStackSettings) StackedEntity(dev.rosewood.rosestacker.stack.StackedEntity) MushroomCow(org.bukkit.entity.MushroomCow) Material(org.bukkit.Material) Slime(org.bukkit.entity.Slime) NMSHandler(dev.rosewood.rosestacker.nms.NMSHandler)

Example 15 with NMSHandler

use of dev.rosewood.rosestacker.nms.NMSHandler 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)19 LivingEntity (org.bukkit.entity.LivingEntity)10 StackSettingManager (dev.rosewood.rosestacker.manager.StackSettingManager)6 ArrayList (java.util.ArrayList)6 ItemStack (org.bukkit.inventory.ItemStack)6 StackManager (dev.rosewood.rosestacker.manager.StackManager)4 StackedEntityDataEntry (dev.rosewood.rosestacker.nms.storage.StackedEntityDataEntry)4 StackedEntityDataStorage (dev.rosewood.rosestacker.nms.storage.StackedEntityDataStorage)4 EntityStackSettings (dev.rosewood.rosestacker.stack.settings.EntityStackSettings)4 Collection (java.util.Collection)4 Material (org.bukkit.Material)4 StringPlaceholders (dev.rosewood.rosegarden.utils.StringPlaceholders)3 WorldGuardHook (dev.rosewood.rosestacker.hook.WorldGuardHook)3 Setting (dev.rosewood.rosestacker.manager.ConfigurationManager.Setting)3 EntityCacheManager (dev.rosewood.rosestacker.manager.EntityCacheManager)3 LocaleManager (dev.rosewood.rosestacker.manager.LocaleManager)3 NMSAdapter (dev.rosewood.rosestacker.nms.NMSAdapter)3 StackedEntity (dev.rosewood.rosestacker.stack.StackedEntity)3 PersistentDataUtils (dev.rosewood.rosestacker.utils.PersistentDataUtils)3 StackerUtils (dev.rosewood.rosestacker.utils.StackerUtils)3