Search in sources :

Example 1 with StackedEntity

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

the class EntityListener method onChickenLayEgg.

@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onChickenLayEgg(EntityDropItemEvent event) {
    if (event.getEntityType() != EntityType.CHICKEN || event.getItemDrop().getItemStack().getType() != Material.EGG)
        return;
    if (this.stackManager.isWorldDisabled(event.getEntity().getWorld()))
        return;
    if (!this.stackManager.isEntityStackingEnabled())
        return;
    Chicken chickenEntity = (Chicken) event.getEntity();
    StackedEntity stackedEntity = this.stackManager.getStackedEntity(chickenEntity);
    if (stackedEntity == null || stackedEntity.getStackSize() == 1)
        return;
    ChickenStackSettings chickenStackSettings = (ChickenStackSettings) stackedEntity.getStackSettings();
    if (!chickenStackSettings.shouldMultiplyEggDropsByStackSize())
        return;
    event.getItemDrop().remove();
    List<ItemStack> items = GuiUtil.getMaterialAmountAsItemStacks(Material.EGG, stackedEntity.getStackSize());
    this.stackManager.preStackItems(items, event.getEntity().getLocation());
}
Also used : StackedEntity(dev.rosewood.rosestacker.stack.StackedEntity) ChickenStackSettings(dev.rosewood.rosestacker.stack.settings.entity.ChickenStackSettings) Chicken(org.bukkit.entity.Chicken) ItemStack(org.bukkit.inventory.ItemStack) EventHandler(org.bukkit.event.EventHandler)

Example 2 with StackedEntity

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

the class EntityListener method onSheepRegrowWool.

@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onSheepRegrowWool(SheepRegrowWoolEvent event) {
    if (this.stackManager.isWorldDisabled(event.getEntity().getWorld()))
        return;
    if (!this.stackManager.isEntityStackingEnabled())
        return;
    Sheep sheepEntity = event.getEntity();
    StackedEntity stackedEntity = this.stackManager.getStackedEntity(sheepEntity);
    if (stackedEntity == null || stackedEntity.getStackSize() == 1)
        return;
    SheepStackSettings sheepStackSettings = (SheepStackSettings) stackedEntity.getStackSettings();
    double regrowPercentage = sheepStackSettings.getPercentageOfWoolToRegrowPerGrassEaten() / 100D;
    int regrowAmount = Math.max(1, (int) Math.round(stackedEntity.getStackSize() * regrowPercentage));
    if (sheepEntity.isSheared()) {
        sheepEntity.setSheared(false);
        regrowAmount--;
    }
    if (regrowAmount < 1)
        return;
    int fRegrowAmount = regrowAmount;
    Bukkit.getScheduler().runTaskAsynchronously(this.rosePlugin, () -> {
        int remaining = fRegrowAmount;
        List<Sheep> sheepList = StackerUtils.deconstructStackedEntities(stackedEntity).stream().map(x -> (Sheep) x).collect(Collectors.toList());
        for (Sheep sheep : sheepList) {
            if (sheep.isSheared()) {
                sheep.setSheared(false);
                remaining--;
                if (remaining <= 0)
                    break;
            }
        }
        StackerUtils.reconstructStackedEntities(stackedEntity, sheepList);
    });
}
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) Sheep(org.bukkit.entity.Sheep) EventHandler(org.bukkit.event.EventHandler)

Example 3 with StackedEntity

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

the class EntityListener method handleEntityDeath.

private void handleEntityDeath(EntityEvent event, LivingEntity entity, boolean useLastDamageCause) {
    if (this.stackManager.isWorldDisabled(entity.getWorld()))
        return;
    if (!this.stackManager.isEntityStackingEnabled())
        return;
    StackedEntity stackedEntity = this.stackManager.getStackedEntity(entity);
    if (stackedEntity == null)
        return;
    if (stackedEntity.getStackSize() == 1) {
        this.stackManager.removeEntityStack(stackedEntity);
        return;
    }
    // Should we kill the entire stack at once?
    EntityDamageEvent lastDamageCause = entity.getLastDamageCause();
    if (useLastDamageCause && (stackedEntity.getStackSettings().shouldKillEntireStackOnDeath() || (Setting.SPAWNER_DISABLE_MOB_AI_OPTIONS_KILL_ENTIRE_STACK_ON_DEATH.getBoolean() && PersistentDataUtils.isAiDisabled(entity)) || (lastDamageCause != null && Setting.ENTITY_KILL_ENTIRE_STACK_CONDITIONS.getStringList().stream().anyMatch(x -> x.equalsIgnoreCase(lastDamageCause.getCause().name()))) || (entity.getKiller() != null && entity.getKiller().hasPermission("rosestacker.killentirestack")))) {
        if (Setting.ENTITY_DROP_ACCURATE_ITEMS.getBoolean()) {
            if (entity instanceof Slime)
                ((Slime) entity).setSize(1);
            if (event instanceof EntityDeathEvent) {
                EntityDeathEvent deathEvent = (EntityDeathEvent) event;
                stackedEntity.dropStackLoot(new ArrayList<>(deathEvent.getDrops()), deathEvent.getDroppedExp());
                deathEvent.getDrops().clear();
            } else {
                stackedEntity.dropStackLoot(null, 0);
            }
        } else if (Setting.ENTITY_DROP_ACCURATE_EXP.getBoolean() && event instanceof EntityDeathEvent) {
            EntityDeathEvent deathEvent = (EntityDeathEvent) event;
            deathEvent.setDroppedExp(deathEvent.getDroppedExp() * stackedEntity.getStackSize());
        }
        Player killer = entity.getKiller();
        if (killer != null && stackedEntity.getStackSize() - 1 > 0)
            killer.incrementStatistic(Statistic.KILL_ENTITY, entity.getType(), stackedEntity.getStackSize() - 1);
        this.stackManager.removeEntityStack(stackedEntity);
        return;
    }
    // Decrease stack size by 1
    stackedEntity.updateDisplay();
    stackedEntity.decreaseStackSize();
    stackedEntity.getEntity().setVelocity(new Vector());
    if (Setting.ENTITY_KILL_TRANSFER_VELOCITY.getBoolean()) {
        stackedEntity.getEntity().setVelocity(entity.getVelocity());
        entity.setVelocity(new Vector());
    }
    if (!Setting.ENTITY_DISPLAY_CORPSE.getBoolean())
        event.getEntity().remove();
}
Also used : StackedEntity(dev.rosewood.rosestacker.stack.StackedEntity) Player(org.bukkit.entity.Player) EntityDeathEvent(org.bukkit.event.entity.EntityDeathEvent) AsyncEntityDeathEvent(dev.rosewood.rosestacker.event.AsyncEntityDeathEvent) Slime(org.bukkit.entity.Slime) Vector(org.bukkit.util.Vector) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent)

Example 4 with StackedEntity

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

the class StackToolListener method onRightClick.

/**
 * Handles Right Clicking for StackedEntities.
 * Handles Shift Right Clicking for StackedEntities.
 *
 * @param event The PlayerInteractAtEntityEvent
 */
@EventHandler(priority = EventPriority.LOWEST)
public void onRightClick(PlayerInteractAtEntityEvent event) {
    Player player = event.getPlayer();
    ItemStack tool = player.getInventory().getItemInMainHand();
    if (!(event.getRightClicked() instanceof LivingEntity) || event.getHand() != EquipmentSlot.HAND || !ItemUtils.isStackingTool(tool))
        return;
    event.setCancelled(true);
    if (!player.hasPermission("rosestacker.stacktool")) {
        this.localeManager.sendMessage(player, "command-stacktool-no-permission");
        return;
    }
    LivingEntity entity = (LivingEntity) event.getRightClicked();
    StackedEntity stackedEntity = this.stackManager.getStackedEntity(entity);
    if (stackedEntity == null) {
        this.localeManager.sendMessage(player, "command-stacktool-invalid-entity");
        return;
    }
    if (!player.isSneaking()) {
        boolean stackable = !PersistentDataUtils.isUnstackable(entity);
        PersistentDataUtils.setUnstackable(entity, stackable);
        String stackableStr = !stackable ? "stackable" : "unstackable";
        this.localeManager.sendMessage(player, "command-stacktool-marked-" + stackableStr, StringPlaceholders.single("type", stackedEntity.getStackSettings().getDisplayName()));
    } else {
        PersistentDataUtils.setUnstackable(entity, true);
        List<LivingEntity> stackEntities = StackerUtils.deconstructStackedEntities(stackedEntity);
        for (LivingEntity stackEntity : stackEntities) PersistentDataUtils.setUnstackable(stackEntity, true);
        StackerUtils.reconstructStackedEntities(stackedEntity, stackEntities);
        this.localeManager.sendMessage(player, "command-stacktool-marked-all-unstackable", StringPlaceholders.single("type", stackedEntity.getStackSettings().getDisplayName()));
    }
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) StackedEntity(dev.rosewood.rosestacker.stack.StackedEntity) Player(org.bukkit.entity.Player) ItemStack(org.bukkit.inventory.ItemStack) EventHandler(org.bukkit.event.EventHandler)

Example 5 with StackedEntity

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

the class EntityConversionHandler method handleConversion.

@Override
public Set<Stack<?>> handleConversion(Set<ConversionData> conversionData) {
    Set<Stack<?>> stacks = new HashSet<>();
    for (ConversionData data : conversionData) {
        LivingEntity entity = data.getEntity();
        // This could cause data loss if the entity actually has a custom name, but we have to remove the stack tag
        entity.setCustomName(null);
        entity.setCustomNameVisible(false);
        StackedEntity stackedEntity = new StackedEntity(data.getEntity(), this.createEntityStackNBT(entity.getType(), data.getStackSize(), entity.getLocation()));
        this.stackManager.addEntityStack(stackedEntity);
        stacks.add(stackedEntity);
    }
    return stacks;
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) StackedEntity(dev.rosewood.rosestacker.stack.StackedEntity) ConversionData(dev.rosewood.rosestacker.conversion.ConversionData) Stack(dev.rosewood.rosestacker.stack.Stack) HashSet(java.util.HashSet)

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