Search in sources :

Example 1 with MushroomCowStackSettings

use of dev.rosewood.rosestacker.stack.settings.entity.MushroomCowStackSettings 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)

Aggregations

NMSHandler (dev.rosewood.rosestacker.nms.NMSHandler)1 StackedEntity (dev.rosewood.rosestacker.stack.StackedEntity)1 MushroomCowStackSettings (dev.rosewood.rosestacker.stack.settings.entity.MushroomCowStackSettings)1 Material (org.bukkit.Material)1 LivingEntity (org.bukkit.entity.LivingEntity)1 MushroomCow (org.bukkit.entity.MushroomCow)1 Slime (org.bukkit.entity.Slime)1