Search in sources :

Example 11 with StackedEntity

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

use of dev.rosewood.rosestacker.stack.StackedEntity in project RoseStacker by Rosewood-Development.

the class EntityListener method onEntityTeleport.

@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onEntityTeleport(EntityPortalEvent event) {
    if (event.getTo() == null || event.getFrom().getWorld() == event.getTo().getWorld())
        return;
    if (this.stackManager.isWorldDisabled(event.getEntity().getWorld()))
        return;
    Entity entity = event.getEntity();
    if (entity instanceof LivingEntity) {
        if (!this.stackManager.isEntityStackingEnabled())
            return;
        LivingEntity livingEntity = (LivingEntity) entity;
        StackedEntity stackedEntity = this.stackManager.getStackedEntity(livingEntity);
        if (stackedEntity != null) {
            Bukkit.getScheduler().runTask(this.rosePlugin, () -> {
                this.stackManager.changeStackingThread(livingEntity.getUniqueId(), stackedEntity, event.getFrom().getWorld(), event.getTo().getWorld());
                stackedEntity.updateDisplay();
            });
        }
    } else if (entity instanceof Item) {
        if (!this.stackManager.isItemStackingEnabled())
            return;
        Item item = (Item) entity;
        StackedItem stackedItem = this.stackManager.getStackedItem(item);
        if (stackedItem != null)
            event.setCancelled(true);
    }
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) StackedEntity(dev.rosewood.rosestacker.stack.StackedEntity) StackedEntity(dev.rosewood.rosestacker.stack.StackedEntity) Item(org.bukkit.entity.Item) StackedItem(dev.rosewood.rosestacker.stack.StackedItem) StackedItem(dev.rosewood.rosestacker.stack.StackedItem) EventHandler(org.bukkit.event.EventHandler)

Example 13 with StackedEntity

use of dev.rosewood.rosestacker.stack.StackedEntity in project RoseStacker by Rosewood-Development.

the class EntityListener method onEntityDamage.

@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onEntityDamage(EntityDamageEvent event) {
    if (!(event.getEntity() instanceof LivingEntity) || event.getEntity().getType() == EntityType.ARMOR_STAND || event.getEntity().getType() == EntityType.PLAYER)
        return;
    LivingEntity entity = (LivingEntity) event.getEntity();
    if (this.stackManager.isWorldDisabled(entity.getWorld()))
        return;
    if (!this.stackManager.isEntityStackingEnabled())
        return;
    StackedEntity stackedEntity = this.stackManager.getStackedEntity(entity);
    if (stackedEntity == null || stackedEntity.getStackSize() == 1)
        return;
    if (!Setting.ENTITY_SHARE_DAMAGE_CONDITIONS.getStringList().contains(event.getCause().name()))
        return;
    double damage = event.getFinalDamage();
    List<LivingEntity> killedEntities = new ArrayList<>();
    List<LivingEntity> internalEntities = StackerUtils.deconstructStackedEntities(stackedEntity);
    for (LivingEntity internal : internalEntities) {
        double health = internal.getHealth();
        if (health - damage <= 0) {
            killedEntities.add(internal);
        } else {
            internal.setHealth(health - damage);
        }
    }
    // Only try dropping loot if something actually died
    if (!killedEntities.isEmpty()) {
        internalEntities.removeIf(killedEntities::contains);
        // Pick a random amount of exp to drop based on the entity type
        // This is only an incredibly rough estimate and isn't 1:1 with vanilla
        int experience;
        Class<? extends Entity> type = stackedEntity.getStackSettings().getEntityType().getEntityClass();
        if (type == null || NPC.class.isAssignableFrom(type) || Golem.class.isAssignableFrom(type) || type == Bat.class) {
            experience = 0;
        } else if (Animals.class.isAssignableFrom(type)) {
            experience = StackerUtils.randomInRange(1, 3);
        } else if (type == Wither.class) {
            experience = 50;
        } else if (type == Blaze.class || Guardian.class.isAssignableFrom(type)) {
            experience = 10;
        } else {
            experience = 5;
        }
        stackedEntity.dropPartialStackLoot(killedEntities, new ArrayList<>(), experience);
        Player killer = entity.getKiller();
        if (killer != null && killedEntities.size() - 1 > 0)
            killer.incrementStatistic(Statistic.KILL_ENTITY, entity.getType(), killedEntities.size() - 1);
    }
    StackerUtils.reconstructStackedEntities(stackedEntity, internalEntities);
}
Also used : NPC(org.bukkit.entity.NPC) StackedEntity(dev.rosewood.rosestacker.stack.StackedEntity) Player(org.bukkit.entity.Player) ArrayList(java.util.ArrayList) LivingEntity(org.bukkit.entity.LivingEntity) Animals(org.bukkit.entity.Animals) Bat(org.bukkit.entity.Bat) EventHandler(org.bukkit.event.EventHandler)

Example 14 with StackedEntity

use of dev.rosewood.rosestacker.stack.StackedEntity in project RoseStacker by Rosewood-Development.

the class EntityListener method handleSheepShear.

public static boolean handleSheepShear(RosePlugin rosePlugin, ItemStack shears, Entity entity) {
    if (entity.getType() != EntityType.SHEEP)
        return false;
    StackManager stackManager = rosePlugin.getManager(StackManager.class);
    if (stackManager.isWorldDisabled(entity.getWorld()))
        return false;
    if (!stackManager.isEntityStackingEnabled())
        return false;
    Sheep sheepEntity = (Sheep) entity;
    StackedEntity stackedEntity = stackManager.getStackedEntity(sheepEntity);
    if (stackedEntity == null || stackedEntity.getStackSize() == 1)
        return false;
    SheepStackSettings sheepStackSettings = (SheepStackSettings) stackedEntity.getStackSettings();
    if (!sheepStackSettings.shouldShearAllSheepInStack())
        return false;
    ItemUtils.damageTool(shears);
    ItemStack baseSheepWool = new ItemStack(ItemUtils.getWoolMaterial(sheepEntity.getColor()), getWoolDropAmount());
    sheepEntity.setSheared(true);
    List<ItemStack> drops = new ArrayList<>(Collections.singletonList(baseSheepWool));
    stackManager.setEntityUnstackingTemporarilyDisabled(true);
    Bukkit.getScheduler().runTaskAsynchronously(RoseStacker.getInstance(), () -> {
        try {
            List<Sheep> sheepList = StackerUtils.deconstructStackedEntities(stackedEntity).stream().map(x -> (Sheep) x).collect(Collectors.toList());
            for (Sheep sheep : sheepList) {
                if (!sheep.isSheared()) {
                    ItemStack sheepWool = new ItemStack(ItemUtils.getWoolMaterial(sheep.getColor()), getWoolDropAmount());
                    sheep.setSheared(true);
                    drops.add(sheepWool);
                }
            }
            StackerUtils.reconstructStackedEntities(stackedEntity, sheepList);
            Bukkit.getScheduler().runTask(RoseStacker.getInstance(), () -> stackManager.preStackItems(drops, sheepEntity.getLocation()));
        } finally {
            stackManager.setEntityUnstackingTemporarilyDisabled(false);
        }
    });
    return true;
}
Also used : StackedEntityDataEntry(dev.rosewood.rosestacker.nms.storage.StackedEntityDataEntry) TransformReason(org.bukkit.event.entity.EntityTransformEvent.TransformReason) SheepStackSettings(dev.rosewood.rosestacker.stack.settings.entity.SheepStackSettings) AttributeInstance(org.bukkit.attribute.AttributeInstance) Slime(org.bukkit.entity.Slime) RoseStacker(dev.rosewood.rosestacker.RoseStacker) Statistic(org.bukkit.Statistic) Animals(org.bukkit.entity.Animals) Projectile(org.bukkit.entity.Projectile) EntitySpawnEvent(org.bukkit.event.entity.EntitySpawnEvent) Player(org.bukkit.entity.Player) StackedSpawner(dev.rosewood.rosestacker.stack.StackedSpawner) ItemUtils(dev.rosewood.rosestacker.utils.ItemUtils) EventHandler(org.bukkit.event.EventHandler) StackSettingManager(dev.rosewood.rosestacker.manager.StackSettingManager) Enderman(org.bukkit.entity.Enderman) Sheep(org.bukkit.entity.Sheep) PigZapEvent(org.bukkit.event.entity.PigZapEvent) PersistentDataUtils(dev.rosewood.rosestacker.utils.PersistentDataUtils) ItemStackSettings(dev.rosewood.rosestacker.stack.settings.ItemStackSettings) Material(org.bukkit.Material) EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) Bukkit(org.bukkit.Bukkit) Setting(dev.rosewood.rosestacker.manager.ConfigurationManager.Setting) NPC(org.bukkit.entity.NPC) Entity(org.bukkit.entity.Entity) CreatureSpawnEvent(org.bukkit.event.entity.CreatureSpawnEvent) Blaze(org.bukkit.entity.Blaze) EntityEvent(org.bukkit.event.entity.EntityEvent) PlayerShearEntityEvent(org.bukkit.event.player.PlayerShearEntityEvent) 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) ChickenStackSettings(dev.rosewood.rosestacker.stack.settings.entity.ChickenStackSettings) ItemStack(org.bukkit.inventory.ItemStack) StackManager(dev.rosewood.rosestacker.manager.StackManager) List(java.util.List) Guardian(org.bukkit.entity.Guardian) EntityCombustByEntityEvent(org.bukkit.event.entity.EntityCombustByEntityEvent) StackerUtils(dev.rosewood.rosestacker.utils.StackerUtils) EntityExplodeEvent(org.bukkit.event.entity.EntityExplodeEvent) Item(org.bukkit.entity.Item) Variant(org.bukkit.entity.MushroomCow.Variant) Wither(org.bukkit.entity.Wither) StackedEntity(dev.rosewood.rosestacker.stack.StackedEntity) EntityTargetEvent(org.bukkit.event.entity.EntityTargetEvent) SpawnerSpawnEvent(org.bukkit.event.entity.SpawnerSpawnEvent) ArrayList(java.util.ArrayList) EntityDeathEvent(org.bukkit.event.entity.EntityDeathEvent) EntityTransformEvent(org.bukkit.event.entity.EntityTransformEvent) GameMode(org.bukkit.GameMode) GuiUtil(dev.rosewood.guiframework.framework.util.GuiUtil) AsyncEntityDeathEvent(dev.rosewood.rosestacker.event.AsyncEntityDeathEvent) NMSHandler(dev.rosewood.rosestacker.nms.NMSHandler) SheepRegrowWoolEvent(org.bukkit.event.entity.SheepRegrowWoolEvent) MushroomCow(org.bukkit.entity.MushroomCow) StackedItem(dev.rosewood.rosestacker.stack.StackedItem) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent) Listener(org.bukkit.event.Listener) Attribute(org.bukkit.attribute.Attribute) EntityCombustEvent(org.bukkit.event.entity.EntityCombustEvent) EntityPortalEvent(org.bukkit.event.entity.EntityPortalEvent) MushroomCowStackSettings(dev.rosewood.rosestacker.stack.settings.entity.MushroomCowStackSettings) EntityTeleportEvent(org.bukkit.event.entity.EntityTeleportEvent) Bat(org.bukkit.entity.Bat) Vector(org.bukkit.util.Vector) Chicken(org.bukkit.entity.Chicken) EventPriority(org.bukkit.event.EventPriority) EntityCombustByBlockEvent(org.bukkit.event.entity.EntityCombustByBlockEvent) EntityDropItemEvent(org.bukkit.event.entity.EntityDropItemEvent) Golem(org.bukkit.entity.Golem) NMSAdapter(dev.rosewood.rosestacker.nms.NMSAdapter) Collections(java.util.Collections) SpawnerStackSettings(dev.rosewood.rosestacker.stack.settings.SpawnerStackSettings) StackedEntity(dev.rosewood.rosestacker.stack.StackedEntity) SheepStackSettings(dev.rosewood.rosestacker.stack.settings.entity.SheepStackSettings) StackManager(dev.rosewood.rosestacker.manager.StackManager) ArrayList(java.util.ArrayList) Sheep(org.bukkit.entity.Sheep) ItemStack(org.bukkit.inventory.ItemStack)

Example 15 with StackedEntity

use of dev.rosewood.rosestacker.stack.StackedEntity in project RoseStacker by Rosewood-Development.

the class BreedingListener method onBreed.

@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onBreed(PlayerInteractEntityEvent event) {
    Entity entity = event.getRightClicked();
    if (!Setting.ENTITY_CUMULATIVE_BREEDING.getBoolean() || !(entity instanceof Animals))
        return;
    Animals animal = (Animals) entity;
    if (!animal.canBreed())
        return;
    StackManager stackManager = this.rosePlugin.getManager(StackManager.class);
    if (!stackManager.isEntityStackingEnabled())
        return;
    StackedEntity stackedEntity = stackManager.getStackedEntity(animal);
    if (stackedEntity == null)
        return;
    Player player = event.getPlayer();
    EntityStackSettings stackSettings = stackedEntity.getStackSettings();
    ItemStack breedingItem = player.getInventory().getItem(event.getHand());
    if (!stackSettings.getEntityTypeData().isValidBreedingMaterial(breedingItem.getType()) || (player.getGameMode() != GameMode.CREATIVE && breedingItem.getAmount() < 2))
        return;
    if (PersistentDataUtils.isAiDisabled(animal) && Setting.SPAWNER_DISABLE_MOB_AI_OPTIONS_DISABLE_BREEDING.getBoolean()) {
        event.setCancelled(true);
        return;
    }
    int stackSize = stackedEntity.getStackSize();
    if (stackSize < 2)
        return;
    Class<? extends Entity> entityClass = animal.getType().getEntityClass();
    if (entityClass == null)
        return;
    event.setCancelled(true);
    // Take the items for breeding
    int totalChildren;
    if (player.getGameMode() != GameMode.CREATIVE) {
        int requiredFood = Math.min(stackSize, breedingItem.getAmount());
        breedingItem.setAmount(breedingItem.getAmount() - requiredFood);
        totalChildren = requiredFood / 2;
    } else {
        // Creative mode should allow the entire stack to breed half as many babies as the total size
        totalChildren = stackSize / 2;
    }
    // Reset breeding timer and play the breeding effect
    animal.setAge(6000);
    animal.setBreedCause(player.getUniqueId());
    animal.playEffect(EntityEffect.LOVE_HEARTS);
    boolean disableAi = PersistentDataUtils.isAiDisabled(animal);
    // Drop experience and spawn entities a few ticks later
    int f_totalChildren = totalChildren;
    Bukkit.getScheduler().runTaskLater(this.rosePlugin, () -> {
        for (int i = 0; i < f_totalChildren; i++) animal.getWorld().spawn(animal.getLocation(), entityClass, x -> {
            Ageable baby = (Ageable) x;
            baby.setBaby();
            if (disableAi)
                PersistentDataUtils.removeEntityAi(baby);
        });
        StackerUtils.dropExperience(animal.getLocation(), totalChildren, 7 * totalChildren, totalChildren);
    }, 30);
}
Also used : EntityStackSettings(dev.rosewood.rosestacker.stack.settings.EntityStackSettings) Setting(dev.rosewood.rosestacker.manager.ConfigurationManager.Setting) EntityEffect(org.bukkit.EntityEffect) Entity(org.bukkit.entity.Entity) StackedEntity(dev.rosewood.rosestacker.stack.StackedEntity) Animals(org.bukkit.entity.Animals) Player(org.bukkit.entity.Player) PlayerInteractEntityEvent(org.bukkit.event.player.PlayerInteractEntityEvent) RosePlugin(dev.rosewood.rosegarden.RosePlugin) ItemStack(org.bukkit.inventory.ItemStack) EntityStackSettings(dev.rosewood.rosestacker.stack.settings.EntityStackSettings) EventHandler(org.bukkit.event.EventHandler) StackManager(dev.rosewood.rosestacker.manager.StackManager) GameMode(org.bukkit.GameMode) EventPriority(org.bukkit.event.EventPriority) PersistentDataUtils(dev.rosewood.rosestacker.utils.PersistentDataUtils) StackerUtils(dev.rosewood.rosestacker.utils.StackerUtils) Ageable(org.bukkit.entity.Ageable) Bukkit(org.bukkit.Bukkit) Listener(org.bukkit.event.Listener) Entity(org.bukkit.entity.Entity) StackedEntity(dev.rosewood.rosestacker.stack.StackedEntity) StackedEntity(dev.rosewood.rosestacker.stack.StackedEntity) Player(org.bukkit.entity.Player) Animals(org.bukkit.entity.Animals) StackManager(dev.rosewood.rosestacker.manager.StackManager) ItemStack(org.bukkit.inventory.ItemStack) Ageable(org.bukkit.entity.Ageable) EventHandler(org.bukkit.event.EventHandler)

Aggregations

StackedEntity (dev.rosewood.rosestacker.stack.StackedEntity)19 LivingEntity (org.bukkit.entity.LivingEntity)14 EventHandler (org.bukkit.event.EventHandler)10 StackManager (dev.rosewood.rosestacker.manager.StackManager)8 Player (org.bukkit.entity.Player)8 Entity (org.bukkit.entity.Entity)7 ItemStack (org.bukkit.inventory.ItemStack)7 NMSHandler (dev.rosewood.rosestacker.nms.NMSHandler)6 Setting (dev.rosewood.rosestacker.manager.ConfigurationManager.Setting)5 EntityCacheManager (dev.rosewood.rosestacker.manager.EntityCacheManager)5 SpawnerStackSettings (dev.rosewood.rosestacker.stack.settings.SpawnerStackSettings)5 PersistentDataUtils (dev.rosewood.rosestacker.utils.PersistentDataUtils)5 StackerUtils (dev.rosewood.rosestacker.utils.StackerUtils)5 ArrayList (java.util.ArrayList)5 HashSet (java.util.HashSet)5 Bukkit (org.bukkit.Bukkit)5 Material (org.bukkit.Material)5 EntityType (org.bukkit.entity.EntityType)5 RoseStacker (dev.rosewood.rosestacker.RoseStacker)4 StackSettingManager (dev.rosewood.rosestacker.manager.StackSettingManager)4