Search in sources :

Example 1 with Source

use of com.archyx.aureliumskills.source.Source in project AureliumSkills by Archy-X.

the class LootHandler method selectLoot.

@Nullable
protected Loot selectLoot(LootPool pool, @Nullable Source source) {
    List<Loot> lootList = new ArrayList<>();
    // Add applicable loot to list for selection
    for (Loot loot : pool.getLoot()) {
        if (loot.getSources().size() > 0 && source != null) {
            for (Source lootSource : loot.getSources()) {
                if (lootSource.equals(source)) {
                    lootList.add(loot);
                    break;
                }
            }
        } else {
            lootList.add(loot);
        }
    }
    // Loot selected based on weight
    int totalWeight = 0;
    for (Loot loot : lootList) {
        totalWeight += loot.getWeight();
    }
    if (totalWeight == 0) {
        // Don't attempt selection if no loot entries are applicable
        return null;
    }
    int selected = random.nextInt(totalWeight);
    int currentWeight = 0;
    Loot selectedLoot = null;
    for (Loot loot : lootList) {
        if (selected >= currentWeight && selected < currentWeight + loot.getWeight()) {
            selectedLoot = loot;
            break;
        }
        currentWeight += loot.getWeight();
    }
    return selectedLoot;
}
Also used : ArrayList(java.util.ArrayList) Loot(com.archyx.aureliumskills.loot.Loot) CommandLoot(com.archyx.aureliumskills.loot.type.CommandLoot) ItemLoot(com.archyx.aureliumskills.loot.type.ItemLoot) Source(com.archyx.aureliumskills.source.Source) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with Source

use of com.archyx.aureliumskills.source.Source in project AureliumSkills by Archy-X.

the class LootParser method parseSources.

@NotNull
protected Set<Source> parseSources(Map<?, ?> map) {
    if (map.containsKey("sources")) {
        Set<Source> sources = new HashSet<>();
        for (String entry : getStringList(map, "sources")) {
            // Try get source name
            Source source = plugin.getSourceRegistry().valueOf(entry);
            if (source != null) {
                sources.add(source);
            } else {
                // Try to get tag if not found
                SourceTag tag = SourceTag.valueOf(entry.toUpperCase(Locale.ROOT));
                // All all sources in tag
                sources.addAll(plugin.getSourceManager().getTag(tag));
            }
        }
        return sources;
    } else {
        return new HashSet<>();
    }
}
Also used : SourceTag(com.archyx.aureliumskills.source.SourceTag) Source(com.archyx.aureliumskills.source.Source) HashSet(java.util.HashSet) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with Source

use of com.archyx.aureliumskills.source.Source in project AureliumSkills by Archy-X.

the class BlockLootHandler method onBreak.

@EventHandler(priority = EventPriority.HIGH)
public void onBreak(BlockBreakEvent event) {
    if (!OptionL.isEnabled(skill))
        return;
    if (event.isCancelled())
        return;
    Block block = event.getBlock();
    if (getSource(block) == null)
        return;
    Player player = event.getPlayer();
    if (blockAbility(player))
        return;
    if (player.getGameMode() != GameMode.SURVIVAL) {
        // Only drop loot in survival mode
        return;
    }
    PlayerData playerData = plugin.getPlayerManager().getPlayerData(player);
    if (playerData == null)
        return;
    Source originalSource = getSource(block);
    LootTable table = plugin.getLootTableManager().getLootTable(skill);
    if (table == null)
        return;
    for (LootPool pool : table.getPools()) {
        // Calculate chance for pool
        double chance = getChance(pool, playerData);
        LootDropCause cause = getCause(pool);
        // Select pool and give loot
        if (selectBlockLoot(pool, player, chance, originalSource, event, cause)) {
            break;
        }
    }
}
Also used : LootTable(com.archyx.aureliumskills.loot.LootTable) Player(org.bukkit.entity.Player) LootDropCause(com.archyx.aureliumskills.api.event.LootDropCause) Block(org.bukkit.block.Block) LootPool(com.archyx.aureliumskills.loot.LootPool) PlayerData(com.archyx.aureliumskills.data.PlayerData) Source(com.archyx.aureliumskills.source.Source) EventHandler(org.bukkit.event.EventHandler)

Example 4 with Source

use of com.archyx.aureliumskills.source.Source in project AureliumSkills by Archy-X.

the class FishingLootHandler method onFish.

@EventHandler(priority = EventPriority.HIGH)
public void onFish(PlayerFishEvent event) {
    if (!OptionL.isEnabled(Skills.FISHING))
        return;
    Player player = event.getPlayer();
    if (blockAbility(player))
        return;
    if (plugin.getWorldManager().isInBlockedWorld(player.getLocation())) {
        return;
    }
    if (plugin.isWorldGuardEnabled()) {
        if (plugin.getWorldGuardSupport().isInBlockedRegion(player.getLocation())) {
            return;
        } else // Check if blocked by flags
        if (plugin.getWorldGuardSupport().blockedByFlag(player.getLocation(), player, WorldGuardFlags.FlagKey.XP_GAIN)) {
            return;
        }
    }
    if (!(event.getCaught() instanceof Item))
        return;
    if (!event.getState().equals(PlayerFishEvent.State.CAUGHT_FISH))
        return;
    if (event.getExpToDrop() == 0)
        return;
    PlayerData playerData = plugin.getPlayerManager().getPlayerData(player);
    if (playerData == null)
        return;
    ItemStack originalItem = ((Item) event.getCaught()).getItemStack();
    FishingSource originalSource = FishingSource.valueOf(originalItem);
    LootTable table = plugin.getLootTableManager().getLootTable(Skills.FISHING);
    if (table == null)
        return;
    for (LootPool pool : table.getPools()) {
        // Calculate chance for pool
        Source source;
        double chance = getCommonChance(pool, playerData);
        LootDropCause cause = LootDropCause.FISHING_OTHER_LOOT;
        if (pool.getName().equals("rare") && plugin.getAbilityManager().isEnabled(Ability.TREASURE_HUNTER)) {
            chance += (getValue(Ability.TREASURE_HUNTER, playerData) / 100);
            source = FishingSource.RARE;
            cause = LootDropCause.TREASURE_HUNTER;
        } else if (pool.getName().equals("epic") && plugin.getAbilityManager().isEnabled(Ability.EPIC_CATCH)) {
            chance += (getValue(Ability.EPIC_CATCH, playerData) / 100);
            source = FishingSource.EPIC;
            cause = LootDropCause.EPIC_CATCH;
        } else {
            source = originalSource;
        }
        if (random.nextDouble() < chance) {
            // Pool is selected
            Loot selectedLoot = selectLoot(pool, originalSource);
            // Give loot
            if (selectedLoot != null) {
                if (selectedLoot instanceof ItemLoot) {
                    ItemLoot itemLoot = (ItemLoot) selectedLoot;
                    giveFishingItemLoot(player, itemLoot, event, source, cause);
                } else if (selectedLoot instanceof CommandLoot) {
                    CommandLoot commandLoot = (CommandLoot) selectedLoot;
                    giveCommandLoot(player, commandLoot, source);
                }
                break;
            }
        }
    }
}
Also used : LootTable(com.archyx.aureliumskills.loot.LootTable) Player(org.bukkit.entity.Player) Loot(com.archyx.aureliumskills.loot.Loot) CommandLoot(com.archyx.aureliumskills.loot.type.CommandLoot) ItemLoot(com.archyx.aureliumskills.loot.type.ItemLoot) Source(com.archyx.aureliumskills.source.Source) Item(org.bukkit.entity.Item) LootDropCause(com.archyx.aureliumskills.api.event.LootDropCause) LootPool(com.archyx.aureliumskills.loot.LootPool) ItemStack(org.bukkit.inventory.ItemStack) PlayerData(com.archyx.aureliumskills.data.PlayerData) ItemLoot(com.archyx.aureliumskills.loot.type.ItemLoot) CommandLoot(com.archyx.aureliumskills.loot.type.CommandLoot) EventHandler(org.bukkit.event.EventHandler)

Aggregations

Source (com.archyx.aureliumskills.source.Source)4 LootDropCause (com.archyx.aureliumskills.api.event.LootDropCause)2 PlayerData (com.archyx.aureliumskills.data.PlayerData)2 Loot (com.archyx.aureliumskills.loot.Loot)2 LootPool (com.archyx.aureliumskills.loot.LootPool)2 LootTable (com.archyx.aureliumskills.loot.LootTable)2 CommandLoot (com.archyx.aureliumskills.loot.type.CommandLoot)2 ItemLoot (com.archyx.aureliumskills.loot.type.ItemLoot)2 Player (org.bukkit.entity.Player)2 EventHandler (org.bukkit.event.EventHandler)2 SourceTag (com.archyx.aureliumskills.source.SourceTag)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Block (org.bukkit.block.Block)1 Item (org.bukkit.entity.Item)1 ItemStack (org.bukkit.inventory.ItemStack)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1