Search in sources :

Example 1 with LootResult

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

the class LootTableManager method callEvent.

/**
 * Calls the PostLootGenerateEvent for the given LootResult.
 * May modify the given LootResult or return a new one entirely.
 *
 * @param lootResult The LootResult to call the event with
 * @return The LootResult after the event has been called
 */
private LootResult callEvent(LootResult lootResult) {
    PostLootGenerateEvent event = new PostLootGenerateEvent(lootResult);
    Bukkit.getPluginManager().callEvent(event);
    if (event.isCancelled())
        return new LootResult(lootResult.getLootContext(), new LootContents(lootResult.getLootContext()), OverwriteExisting.NONE);
    if (!event.shouldDropItems())
        lootResult.getLootContents().removeItems();
    if (!event.shouldDropExperience())
        lootResult.getLootContents().removeExperience();
    if (!event.shouldTriggerExtras())
        lootResult.getLootContents().removeExtraTriggers();
    lootResult.setOverwriteExisting(event.getOverwriteExisting());
    return lootResult;
}
Also used : LootResult(dev.rosewood.roseloot.loot.LootResult) LootContents(dev.rosewood.roseloot.loot.LootContents) PostLootGenerateEvent(dev.rosewood.roseloot.event.PostLootGenerateEvent)

Example 2 with LootResult

use of dev.rosewood.roseloot.loot.LootResult 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 LootResult

use of dev.rosewood.roseloot.loot.LootResult 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 LootResult

use of dev.rosewood.roseloot.loot.LootResult 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 LootResult

use of dev.rosewood.roseloot.loot.LootResult 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

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