Search in sources :

Example 1 with LootContext

use of dev.rosewood.roseloot.loot.context.LootContext in project RoseLoot by Rosewood-Development.

the class ExperienceLootItem method create.

@Override
public Integer create(LootContext context) {
    int amount = this.amounts.stream().mapToInt(NumberProvider::getInteger).sum();
    Optional<LivingEntity> lootedEntity = context.get(LootContextParams.LOOTED_ENTITY);
    if (lootedEntity.isPresent() && !this.equipmentBonuses.isEmpty()) {
        EntityEquipment equipment = lootedEntity.get().getEquipment();
        if (equipment != null) {
            long equipmentAmount = Arrays.stream(EquipmentSlot.values()).filter(x -> equipment.getItem(x).getType() != Material.AIR).count();
            for (int i = 0; i < equipmentAmount; i++) for (NumberProvider equipmentBonus : this.equipmentBonuses) amount += equipmentBonus.getInteger();
        }
    }
    context.getPlaceholders().add("experience_amount", amount);
    return amount;
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) LootContextParams(dev.rosewood.roseloot.loot.context.LootContextParams) Arrays(java.util.Arrays) NumberProvider(dev.rosewood.roseloot.util.NumberProvider) ConfigurationSection(org.bukkit.configuration.ConfigurationSection) LivingEntity(org.bukkit.entity.LivingEntity) ArrayList(java.util.ArrayList) List(java.util.List) LootContext(dev.rosewood.roseloot.loot.context.LootContext) Optional(java.util.Optional) EntityEquipment(org.bukkit.inventory.EntityEquipment) Collections(java.util.Collections) Material(org.bukkit.Material) EquipmentSlot(org.bukkit.inventory.EquipmentSlot) EntityEquipment(org.bukkit.inventory.EntityEquipment) NumberProvider(dev.rosewood.roseloot.util.NumberProvider)

Example 2 with LootContext

use of dev.rosewood.roseloot.loot.context.LootContext in project RoseLoot by Rosewood-Development.

the class LootGenerateListener method onLootGenerate.

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onLootGenerate(LootGenerateEvent event) {
    if (!(event.getInventoryHolder() instanceof Container))
        return;
    Block block = ((Container) event.getInventoryHolder()).getBlock();
    if (Setting.DISABLED_WORLDS.getStringList().stream().anyMatch(x -> x.equalsIgnoreCase(block.getWorld().getName())))
        return;
    LivingEntity looter = null;
    if (event.getEntity() instanceof LivingEntity)
        looter = (LivingEntity) event.getEntity();
    LootContext lootContext = LootContext.builder(LootUtils.getEntityLuck(looter)).put(LootContextParams.ORIGIN, block.getLocation()).put(LootContextParams.LOOTER, looter).put(LootContextParams.LOOTED_BLOCK, block).put(LootContextParams.VANILLA_LOOT_TABLE_KEY, event.getLootTable().getKey()).build();
    LootResult lootResult = this.lootTableManager.getLoot(LootTableTypes.CONTAINER, lootContext);
    LootContents lootContents = lootResult.getLootContents();
    // Overwrite existing loot if applicable
    if (lootResult.shouldOverwriteItems())
        event.getLoot().clear();
    // Set items and drop experience
    event.getLoot().addAll(lootResult.getLootContents().getItems());
    int experience = lootContents.getExperience();
    if (experience > 0) {
        Location location = looter == null ? block.getLocation() : looter.getLocation();
        block.getWorld().spawn(location, ExperienceOrb.class, x -> x.setExperience(experience));
    }
    lootContents.triggerExtras(block.getLocation());
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) Container(org.bukkit.block.Container) LootResult(dev.rosewood.roseloot.loot.LootResult) LootContext(dev.rosewood.roseloot.loot.context.LootContext) LootContents(dev.rosewood.roseloot.loot.LootContents) Block(org.bukkit.block.Block) Location(org.bukkit.Location) EventHandler(org.bukkit.event.EventHandler)

Example 3 with LootContext

use of dev.rosewood.roseloot.loot.context.LootContext in project RoseLoot by Rosewood-Development.

the class RoseStackerEntityDeathListener method onEntityStackMultipleDeath.

@EventHandler
public void onEntityStackMultipleDeath(EntityStackMultipleDeathEvent event) {
    LivingEntity mainEntity = event.getStack().getEntity();
    if (ConfigurationManager.Setting.DISABLED_WORLDS.getStringList().stream().anyMatch(x -> x.equalsIgnoreCase(mainEntity.getWorld().getName())))
        return;
    Entity looter = null;
    if (mainEntity.getLastDamageCause() instanceof EntityDamageByEntityEvent)
        looter = ((EntityDamageByEntityEvent) mainEntity.getLastDamageCause()).getDamager();
    List<LootContents> extras = new ArrayList<>();
    for (Map.Entry<LivingEntity, EntityStackMultipleDeathEvent.EntityDrops> entry : event.getEntityDrops().entrySet()) {
        LivingEntity entity = entry.getKey();
        EntityStackMultipleDeathEvent.EntityDrops drops = entry.getValue();
        LootContext lootContext = LootContext.builder(LootUtils.getEntityLuck(looter)).put(LootContextParams.ORIGIN, entity.getLocation()).put(LootContextParams.LOOTER, looter).put(LootContextParams.LOOTED_ENTITY, entity).put(LootContextParams.EXPLOSION_TYPE, LootUtils.getDeathExplosionType(entity)).put(LootContextParams.HAS_EXISTING_ITEMS, !drops.getDrops().isEmpty()).build();
        LootResult lootResult = this.lootTableManager.getLoot(LootTableTypes.ENTITY, lootContext);
        LootContents lootContents = lootResult.getLootContents();
        // Overwrite existing drops if applicable
        if (lootResult.shouldOverwriteItems())
            drops.getDrops().clear();
        if (lootResult.shouldOverwriteExperience())
            drops.setExperience(0);
        // Add items to drops and adjust experience
        drops.getDrops().addAll(lootContents.getItems());
        drops.setExperience(drops.getExperience() + lootContents.getExperience());
        extras.add(lootContents);
    }
    Runnable task = () -> extras.forEach(x -> x.triggerExtras(mainEntity.getLocation()));
    if (!Bukkit.isPrimaryThread()) {
        Bukkit.getScheduler().runTask(this.rosePlugin, task);
    } else {
        task.run();
    }
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) EntityStackMultipleDeathEvent(dev.rosewood.rosestacker.event.EntityStackMultipleDeathEvent) LootResult(dev.rosewood.roseloot.loot.LootResult) LootContext(dev.rosewood.roseloot.loot.context.LootContext) LootContents(dev.rosewood.roseloot.loot.LootContents) ArrayList(java.util.ArrayList) LivingEntity(org.bukkit.entity.LivingEntity) EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) Map(java.util.Map) EventHandler(org.bukkit.event.EventHandler)

Example 4 with LootContext

use of dev.rosewood.roseloot.loot.context.LootContext in project RoseLoot by Rosewood-Development.

the class EntityListener method onEntityDeath.

@EventHandler
public void onEntityDeath(EntityDeathEvent event) {
    LivingEntity entity = event.getEntity();
    if (Setting.DISABLED_WORLDS.getStringList().stream().anyMatch(x -> x.equalsIgnoreCase(entity.getWorld().getName())))
        return;
    // Handle RoseStacker entity stack deaths in a different listener
    if (RoseStackerHook.useCustomEntityDeathHandling() && RoseStackerHook.isEntireEntityStackDying(entity))
        return;
    Entity looter = null;
    if (entity.getLastDamageCause() instanceof EntityDamageByEntityEvent)
        looter = ((EntityDamageByEntityEvent) entity.getLastDamageCause()).getDamager();
    LootContext lootContext = LootContext.builder(LootUtils.getEntityLuck(looter)).put(LootContextParams.ORIGIN, entity.getLocation()).put(LootContextParams.LOOTER, looter).put(LootContextParams.LOOTED_ENTITY, entity).put(LootContextParams.EXPLOSION_TYPE, LootUtils.getDeathExplosionType(entity)).put(LootContextParams.HAS_EXISTING_ITEMS, !event.getDrops().isEmpty()).build();
    LootResult lootResult = this.lootTableManager.getLoot(LootTableTypes.ENTITY, lootContext);
    LootContents lootContents = lootResult.getLootContents();
    // Overwrite existing drops if applicable
    if (lootResult.shouldOverwriteItems())
        event.getDrops().clear();
    if (lootResult.shouldOverwriteExperience())
        event.setDroppedExp(0);
    // Add items to drops and adjust experience
    event.getDrops().addAll(lootContents.getItems());
    event.setDroppedExp(event.getDroppedExp() + lootContents.getExperience());
    lootContents.triggerExtras(entity.getLocation());
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) LootResult(dev.rosewood.roseloot.loot.LootResult) LootContext(dev.rosewood.roseloot.loot.context.LootContext) LootContents(dev.rosewood.roseloot.loot.LootContents) EventHandler(org.bukkit.event.EventHandler)

Example 5 with LootContext

use of dev.rosewood.roseloot.loot.context.LootContext in project RoseLoot by Rosewood-Development.

the class EntityListener method onEntityDropItem.

@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onEntityDropItem(EntityDropItemEvent event) {
    if (!(event.getEntity() instanceof LivingEntity))
        return;
    LivingEntity entity = (LivingEntity) event.getEntity();
    if (Setting.DISABLED_WORLDS.getStringList().stream().anyMatch(x -> x.equalsIgnoreCase(entity.getWorld().getName())))
        return;
    Player shearer;
    switch(entity.getType()) {
        case SHEEP:
        case SNOWMAN:
        case MUSHROOM_COW:
            shearer = this.lastShearer.get();
            break;
        default:
            shearer = null;
            break;
    }
    LootContext lootContext = LootContext.builder(LootUtils.getEntityLuck(shearer)).put(LootContextParams.ORIGIN, entity.getLocation()).put(LootContextParams.LOOTER, shearer).put(LootContextParams.LOOTED_ENTITY, entity).put(LootContextParams.INPUT_ITEM, event.getItemDrop().getItemStack()).put(LootContextParams.HAS_EXISTING_ITEMS, true).build();
    LootResult lootResult = this.lootTableManager.getLoot(LootTableTypes.ENTITY_DROP_ITEM, lootContext);
    LootContents lootContents = lootResult.getLootContents();
    Location dropLocation = event.getItemDrop().getLocation();
    // Overwrite existing drops if applicable
    if (lootResult.shouldOverwriteItems())
        event.setCancelled(true);
    // Add items to drops and spawn experience
    lootContents.getItems().forEach(x -> entity.getWorld().dropItemNaturally(dropLocation, x));
    int experience = lootContents.getExperience();
    if (experience > 0)
        entity.getWorld().spawn(entity.getLocation(), ExperienceOrb.class, x -> x.setExperience(experience));
    lootContents.triggerExtras(dropLocation);
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) LootContextParams(dev.rosewood.roseloot.loot.context.LootContextParams) LootTableManager(dev.rosewood.roseloot.manager.LootTableManager) SpawnerSpawnEvent(org.bukkit.event.entity.SpawnerSpawnEvent) Player(org.bukkit.entity.Player) EventHandler(org.bukkit.event.EventHandler) EntityDeathEvent(org.bukkit.event.entity.EntityDeathEvent) LootContext(dev.rosewood.roseloot.loot.context.LootContext) LootTableTypes(dev.rosewood.roseloot.loot.table.LootTableTypes) Location(org.bukkit.Location) RoseStackerHook(dev.rosewood.roseloot.hook.RoseStackerHook) WeakReference(java.lang.ref.WeakReference) LootUtils(dev.rosewood.roseloot.util.LootUtils) EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) Listener(org.bukkit.event.Listener) Entity(org.bukkit.entity.Entity) LootContents(dev.rosewood.roseloot.loot.LootContents) CreatureSpawnEvent(org.bukkit.event.entity.CreatureSpawnEvent) PlayerShearEntityEvent(org.bukkit.event.player.PlayerShearEntityEvent) LivingEntity(org.bukkit.entity.LivingEntity) RosePlugin(dev.rosewood.rosegarden.RosePlugin) LootResult(dev.rosewood.roseloot.loot.LootResult) Reference(java.lang.ref.Reference) ExperienceOrb(org.bukkit.entity.ExperienceOrb) EventPriority(org.bukkit.event.EventPriority) BlockShearEntityEvent(org.bukkit.event.block.BlockShearEntityEvent) Setting(dev.rosewood.roseloot.manager.ConfigurationManager.Setting) EntityDropItemEvent(org.bukkit.event.entity.EntityDropItemEvent) Player(org.bukkit.entity.Player) LootResult(dev.rosewood.roseloot.loot.LootResult) LootContext(dev.rosewood.roseloot.loot.context.LootContext) LootContents(dev.rosewood.roseloot.loot.LootContents) ExperienceOrb(org.bukkit.entity.ExperienceOrb) Location(org.bukkit.Location) EventHandler(org.bukkit.event.EventHandler)

Aggregations

LootContext (dev.rosewood.roseloot.loot.context.LootContext)17 LootResult (dev.rosewood.roseloot.loot.LootResult)12 EventHandler (org.bukkit.event.EventHandler)11 LootContents (dev.rosewood.roseloot.loot.LootContents)9 Player (org.bukkit.entity.Player)9 LootContextParams (dev.rosewood.roseloot.loot.context.LootContextParams)7 LootTableManager (dev.rosewood.roseloot.manager.LootTableManager)7 Location (org.bukkit.Location)7 RosePlugin (dev.rosewood.rosegarden.RosePlugin)6 ItemStack (org.bukkit.inventory.ItemStack)6 LootTableTypes (dev.rosewood.roseloot.loot.table.LootTableTypes)5 Setting (dev.rosewood.roseloot.manager.ConfigurationManager.Setting)5 LootUtils (dev.rosewood.roseloot.util.LootUtils)5 Block (org.bukkit.block.Block)5 ExperienceOrb (org.bukkit.entity.ExperienceOrb)5 LivingEntity (org.bukkit.entity.LivingEntity)5 EventPriority (org.bukkit.event.EventPriority)5 Listener (org.bukkit.event.Listener)5 Entity (org.bukkit.entity.Entity)4 NumberProvider (dev.rosewood.roseloot.util.NumberProvider)3