Search in sources :

Example 11 with SoliniaItemException

use of com.solinia.solinia.Exceptions.SoliniaItemException in project solinia3-core by mixxit.

the class CommandCreateArmourSet method onCommand.

@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    if (!(sender instanceof Player) && !(sender instanceof CommandSender))
        return false;
    try {
        if (sender instanceof Player) {
            Player player = (Player) sender;
            if (!player.isOp()) {
                player.sendMessage("This is an operator only command");
                return false;
            }
        }
        if (args.length < 2) {
            return false;
        }
        // args
        // classid
        // armourtier
        // suffixname
        int classid = Integer.parseInt(args[0]);
        int armourtier = Integer.parseInt(args[1]);
        ISoliniaClass classtype = StateManager.getInstance().getConfigurationManager().getClassObj(classid);
        if (classtype == null) {
            sender.sendMessage("Class ID does not exist");
            return true;
        }
        String partialname = "";
        int count = 0;
        for (String entry : args) {
            if (count < 2) {
                count++;
                continue;
            }
            partialname += entry + " ";
            count++;
        }
        partialname = partialname.trim();
        if (partialname.equals("")) {
            sender.sendMessage("Blank suffix name not allowed when creating armour set");
            return false;
        }
        List<Integer> items = SoliniaItemFactory.CreateClassItemSet(classtype, armourtier, partialname, false, sender.isOp());
        String itemscreated = "";
        for (Integer item : items) {
            itemscreated += item + " ";
        }
        sender.sendMessage("Created items as IDs: " + itemscreated);
    } catch (CoreStateInitException e) {
        sender.sendMessage(e.getMessage());
    } catch (SoliniaItemException e) {
        // TODO Auto-generated catch block
        sender.sendMessage(e.getMessage());
    }
    return true;
}
Also used : ISoliniaClass(com.solinia.solinia.Interfaces.ISoliniaClass) Player(org.bukkit.entity.Player) CoreStateInitException(com.solinia.solinia.Exceptions.CoreStateInitException) CommandSender(org.bukkit.command.CommandSender) ConsoleCommandSender(org.bukkit.command.ConsoleCommandSender) SoliniaItemException(com.solinia.solinia.Exceptions.SoliniaItemException)

Example 12 with SoliniaItemException

use of com.solinia.solinia.Exceptions.SoliniaItemException in project solinia3-core by mixxit.

the class CommandCraft method onCommand.

@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    if (!(sender instanceof Player)) {
        return false;
    }
    Player player = (Player) sender;
    ItemStack primaryItem = player.getInventory().getItemInMainHand();
    if (primaryItem.getType().equals(Material.AIR)) {
        player.sendMessage(ChatColor.GRAY + "Empty item in primary hand. You must hold the item you want to use in your crafting recipe");
        return false;
    }
    ItemStack secondaryItem = player.getInventory().getItemInOffHand();
    if (secondaryItem.getType().equals(Material.AIR)) {
        player.sendMessage(ChatColor.GRAY + "Empty item in offhand. You must hold the item you want to use in your crafting recipe");
        return false;
    }
    if (primaryItem.getAmount() > 1) {
        player.sendMessage(ChatColor.GRAY + "Stack size in primary hand is too high (max 1)");
        return false;
    }
    if (secondaryItem.getAmount() > 1) {
        player.sendMessage(ChatColor.GRAY + "Stack size in secondary hand is too high (max 1)");
        return false;
    }
    if (!Utils.IsSoliniaItem(primaryItem)) {
        player.sendMessage("You can only create a new item from solinia items, not minecraft items");
        return true;
    }
    if (!Utils.IsSoliniaItem(secondaryItem)) {
        player.sendMessage("You can only create a new item from solinia items, not minecraft items");
        return true;
    }
    try {
        ISoliniaPlayer solPlayer = SoliniaPlayerAdapter.Adapt(player);
        ISoliniaItem primarysolItem = SoliniaItemAdapter.Adapt(primaryItem);
        ISoliniaItem secondarysolItem = SoliniaItemAdapter.Adapt(secondaryItem);
        List<SoliniaCraft> craft = StateManager.getInstance().getConfigurationManager().getCrafts(primarysolItem.getId(), secondarysolItem.getId());
        if (craft.size() < 1) {
            player.sendMessage("You do not seem to know how to make anything with these items");
            return true;
        }
        int createCount = 0;
        for (SoliniaCraft craftEntry : craft) {
            if (craftEntry.getClassId() > 0) {
                if (solPlayer.getClassObj() == null) {
                    player.sendMessage("You do not seem to know how to make anything with these items");
                    continue;
                }
                if (solPlayer.getClassObj().getId() != craftEntry.getClassId()) {
                    player.sendMessage("You do not seem to know how to make anything with these items");
                    continue;
                }
            }
            if (craftEntry.getSkilltype() != SkillType.None) {
                if (craftEntry.getMinSkill() > 0) {
                    SoliniaPlayerSkill skill = solPlayer.getSkill(craftEntry.getSkilltype().name().toUpperCase());
                    if (skill == null) {
                        player.sendMessage("You do not seem to know how to make anything with these items");
                        continue;
                    }
                    if (skill.getValue() < craftEntry.getMinSkill()) {
                        player.sendMessage("You do not seem to know how to make anything with these items");
                        continue;
                    }
                }
            }
            ISoliniaItem outputItem = StateManager.getInstance().getConfigurationManager().getItem(craftEntry.getOutputItem());
            if (outputItem != null) {
                player.getWorld().dropItemNaturally(player.getLocation(), outputItem.asItemStack());
                player.sendMessage("You fashion the items together to make something new!");
                createCount++;
                if (craftEntry.getSkilltype() != SkillType.None) {
                    solPlayer.tryIncreaseSkill(craftEntry.getSkilltype().name().toUpperCase(), 1);
                }
            }
        }
        if (createCount > 0) {
            player.getInventory().setItemInMainHand(null);
            player.getInventory().setItemInOffHand(null);
            player.updateInventory();
        }
    } catch (CoreStateInitException e) {
    } catch (SoliniaItemException e) {
        player.sendMessage("Item no longer exists");
    }
    return true;
}
Also used : Player(org.bukkit.entity.Player) ISoliniaPlayer(com.solinia.solinia.Interfaces.ISoliniaPlayer) ISoliniaItem(com.solinia.solinia.Interfaces.ISoliniaItem) SoliniaPlayerSkill(com.solinia.solinia.Models.SoliniaPlayerSkill) CoreStateInitException(com.solinia.solinia.Exceptions.CoreStateInitException) SoliniaCraft(com.solinia.solinia.Models.SoliniaCraft) ISoliniaPlayer(com.solinia.solinia.Interfaces.ISoliniaPlayer) ItemStack(org.bukkit.inventory.ItemStack) SoliniaItemException(com.solinia.solinia.Exceptions.SoliniaItemException)

Example 13 with SoliniaItemException

use of com.solinia.solinia.Exceptions.SoliniaItemException in project solinia3-core by mixxit.

the class SoliniaNPCEventHandler method awardPlayer.

@Override
public void awardPlayer(Player triggerentity) {
    try {
        ISoliniaPlayer player = SoliniaPlayerAdapter.Adapt(triggerentity);
        if (getAwardsQuest() > 0) {
            boolean foundQuest = false;
            for (PlayerQuest playerQuest : player.getPlayerQuests()) {
                if (playerQuest.getQuestId() == getAwardsQuest()) {
                    foundQuest = true;
                }
            }
            if (foundQuest == false)
                player.addPlayerQuest(getAwardsQuest());
        }
        if (getAwardsQuestFlag() != null && !getAwardsQuestFlag().equals("")) {
            boolean foundQuestFlag = false;
            for (String playerQuestFlag : player.getPlayerQuestFlags()) {
                if (playerQuestFlag.equals(getAwardsQuestFlag())) {
                    foundQuestFlag = true;
                }
            }
            if (foundQuestFlag == false) {
                player.addPlayerQuestFlag(getAwardsQuestFlag());
                // All item awards must be accompanied with a quest flag else they will repeat the item return over and over
                if (getAwardsItem() > 0) {
                    System.out.println("Awarding item with awardquestflag: " + getAwardsQuestFlag());
                    ISoliniaItem item = StateManager.getInstance().getConfigurationManager().getItem(getAwardsItem());
                    final int awarditemid = getAwardsItem();
                    final UUID uuid = player.getBukkitPlayer().getUniqueId();
                    if (item != null) {
                        Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Bukkit.getPluginManager().getPlugin("Solinia3Core"), new Runnable() {

                            public void run() {
                                try {
                                    ItemStack itemStack = item.asItemStack();
                                    ISoliniaItem item = StateManager.getInstance().getConfigurationManager().getItem(awarditemid);
                                    Bukkit.getPlayer(uuid).getWorld().dropItem(Bukkit.getPlayer(uuid).getLocation(), itemStack);
                                    System.out.println("Awarded item: " + item.getDisplayname());
                                } catch (CoreStateInitException e) {
                                // skip
                                }
                            }
                        });
                    }
                }
                if (this.isAwardsTitle() == true) {
                    if (this.getTitle() != null) {
                        if (!this.getTitle().equals("")) {
                            player.grantTitle(this.getTitle());
                        }
                    }
                }
                if (isAwardsRandomisedGear() == true) {
                    String suffix = "of Randomisation";
                    if (getRandomisedGearSuffix() != null) {
                        if (!getRandomisedGearSuffix().equals("")) {
                            suffix = getRandomisedGearSuffix();
                        }
                    }
                    System.out.println("Awarding randomisedgear with awardquestflag: " + getAwardsQuestFlag());
                    int playertier = 1;
                    if (player.getLevel() >= 1 && player.getLevel() < 11)
                        playertier = 1;
                    if (player.getLevel() >= 11 && player.getLevel() < 21)
                        playertier = 2;
                    if (player.getLevel() >= 21 && player.getLevel() < 31)
                        playertier = 3;
                    if (player.getLevel() >= 31 && player.getLevel() < 41)
                        playertier = 4;
                    if (player.getLevel() >= 41 && player.getLevel() < 51)
                        playertier = 5;
                    if (player.getLevel() >= 51 && player.getLevel() < 61)
                        playertier = 6;
                    try {
                        // always give the next tier up and then we will reset the player requirements ot current level
                        // this ability is for special seasonal rewards only
                        playertier += 1;
                        List<Integer> items = SoliniaItemFactory.CreateClassItemSet(player.getClassObj(), playertier, suffix, false, true);
                        for (int itemid : items) {
                            ISoliniaItem item = StateManager.getInstance().getConfigurationManager().getItem(itemid);
                            final String playerName = player.getBukkitPlayer().getName();
                            final int minLevel = player.getLevel();
                            final int finalitemid = itemid;
                            if (item != null) {
                                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Bukkit.getPluginManager().getPlugin("Solinia3Core"), new Runnable() {

                                    public void run() {
                                        try {
                                            ISoliniaItem item = StateManager.getInstance().getConfigurationManager().getItem(finalitemid);
                                            item.setMinLevel(minLevel);
                                            SoliniaAccountClaim claim = new SoliniaAccountClaim();
                                            claim.setId(StateManager.getInstance().getConfigurationManager().getNextAccountClaimId());
                                            claim.setMcname(playerName);
                                            claim.setItemid(finalitemid);
                                            claim.setClaimed(false);
                                            Player claimPlayer = Bukkit.getPlayer(playerName);
                                            if (claimPlayer != null) {
                                                claimPlayer.sendMessage(ChatColor.GOLD + "You have been awarded with a claim item! See /claim");
                                            }
                                            StateManager.getInstance().getConfigurationManager().addAccountClaim(claim);
                                            System.out.println("Awarded Claim: " + item.getDisplayname() + " to " + playerName);
                                        } catch (CoreStateInitException e) {
                                        // skip
                                        }
                                    }
                                });
                            }
                        }
                    } catch (SoliniaItemException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }
            }
        }
    } catch (CoreStateInitException e) {
        System.out.println(e.getMessage());
        return;
    }
}
Also used : Player(org.bukkit.entity.Player) ISoliniaPlayer(com.solinia.solinia.Interfaces.ISoliniaPlayer) ISoliniaItem(com.solinia.solinia.Interfaces.ISoliniaItem) SoliniaItemException(com.solinia.solinia.Exceptions.SoliniaItemException) CoreStateInitException(com.solinia.solinia.Exceptions.CoreStateInitException) ISoliniaPlayer(com.solinia.solinia.Interfaces.ISoliniaPlayer) UUID(java.util.UUID) ItemStack(org.bukkit.inventory.ItemStack)

Example 14 with SoliniaItemException

use of com.solinia.solinia.Exceptions.SoliniaItemException in project solinia3-core by mixxit.

the class SoliniaLivingEntity method Attack.

@Override
public boolean Attack(ISoliniaLivingEntity defender, EntityDamageEvent event, boolean arrowHit, Solinia3CorePlugin plugin) {
    int baseDamage = (int) event.getDamage(DamageModifier.BASE);
    try {
        if (defender.isPlayer() && isPlayer()) {
            ISoliniaPlayer defenderPlayer = SoliniaPlayerAdapter.Adapt((Player) defender.getBukkitLivingEntity());
            ISoliniaPlayer attackerPlayer = SoliniaPlayerAdapter.Adapt((Player) this.getBukkitLivingEntity());
            if (defenderPlayer.getGroup() != null && attackerPlayer.getGroup() != null) {
                if (defenderPlayer.getGroup().getId().equals(attackerPlayer.getGroup().getId())) {
                    Utils.CancelEvent(event);
                    ;
                    return false;
                }
            }
        }
    } catch (CoreStateInitException e) {
    // ignore
    }
    if (isPlayer()) {
        Player player = (Player) this.getBukkitLivingEntity();
        if (player.isSneaking()) {
            try {
                ISoliniaPlayer solplayer = SoliniaPlayerAdapter.Adapt(player);
                if (solplayer.getClassObj() != null) {
                    if (solplayer.getClassObj().isSneakFromCrouch()) {
                        player.sendMessage("You cannot concentrate on combat while meditating or sneaking");
                        Utils.CancelEvent(event);
                        ;
                        return false;
                    }
                }
            } catch (CoreStateInitException e) {
            // do nothing
            }
        }
    }
    if (usingValidWeapon() == false) {
        Utils.CancelEvent(event);
        ;
        return false;
    } else {
        if (Utils.IsSoliniaItem(getBukkitLivingEntity().getEquipment().getItemInMainHand())) {
            try {
                ISoliniaItem soliniaitem = StateManager.getInstance().getConfigurationManager().getItem(getBukkitLivingEntity().getEquipment().getItemInMainHand());
                // TODO move this
                if (soliniaitem.getBaneUndead() > 0 && defender.isUndead())
                    baseDamage += soliniaitem.getBaneUndead();
            } catch (CoreStateInitException e) {
                Utils.CancelEvent(event);
                ;
                return false;
            }
        }
    }
    if (baseDamage < 1)
        baseDamage = 1;
    if (defender == null) {
        Utils.CancelEvent(event);
        ;
        return false;
    }
    if (defender.getBukkitLivingEntity().isDead() || this.getBukkitLivingEntity().isDead() || this.getBukkitLivingEntity().getHealth() < 0) {
        Utils.CancelEvent(event);
        ;
        return false;
    }
    if (isInulvnerable()) {
        Utils.CancelEvent(event);
        ;
        return false;
    }
    if (isFeigned()) {
        Utils.CancelEvent(event);
        ;
        return false;
    }
    ItemStack weapon = this.getBukkitLivingEntity().getEquipment().getItemInHand();
    DamageHitInfo my_hit = new DamageHitInfo();
    my_hit.skill = Utils.getSkillForMaterial(weapon.getType().toString()).getSkillname();
    if (arrowHit) {
        my_hit.skill = "ARCHERY";
    }
    // Now figure out damage
    my_hit.damage_done = 1;
    my_hit.min_damage = 0;
    int mylevel = getLevel();
    int hate = 0;
    my_hit.base_damage = baseDamage;
    // amount of hate is based on the damage done
    if (hate == 0 && my_hit.base_damage > 1)
        hate = my_hit.base_damage;
    if (my_hit.base_damage > 0) {
        my_hit.base_damage = getDamageCaps(my_hit.base_damage);
        tryIncreaseSkill(my_hit.skill, 1);
        tryIncreaseSkill("OFFENSE", 1);
        int ucDamageBonus = 0;
        if (getClassObj() != null && getClassObj().isWarriorClass() && getLevel() >= 28) {
            ucDamageBonus = getWeaponDamageBonus(weapon);
            my_hit.min_damage = ucDamageBonus;
            hate += ucDamageBonus;
        }
        // TODO Sinister Strikes
        int hit_chance_bonus = 0;
        // we need this a few times
        my_hit.offense = getOffense(my_hit.skill);
        my_hit.tohit = getTotalToHit(my_hit.skill, hit_chance_bonus);
        doAttack(plugin, defender, my_hit);
    }
    defender.addToHateList(getBukkitLivingEntity().getUniqueId(), hate);
    if (getBukkitLivingEntity().isDead()) {
        Utils.CancelEvent(event);
        ;
        return false;
    }
    if (my_hit.damage_done > 0) {
        triggerDefensiveProcs(defender, my_hit.damage_done, arrowHit);
        try {
            event.setDamage(DamageModifier.ABSORPTION, 0);
        } catch (UnsupportedOperationException e) {
        }
        try {
            event.setDamage(DamageModifier.ARMOR, 0);
        } catch (UnsupportedOperationException e) {
        }
        try {
            event.setDamage(DamageModifier.BASE, my_hit.damage_done);
        } catch (UnsupportedOperationException e) {
        }
        try {
            event.setDamage(DamageModifier.BLOCKING, 0);
        } catch (UnsupportedOperationException e) {
        }
        try {
            event.setDamage(DamageModifier.HARD_HAT, 0);
        } catch (UnsupportedOperationException e) {
        }
        try {
            event.setDamage(DamageModifier.MAGIC, 0);
        } catch (UnsupportedOperationException e) {
        }
        try {
            event.setDamage(DamageModifier.RESISTANCE, 0);
        } catch (UnsupportedOperationException e) {
        }
        DecimalFormat df = new DecimalFormat();
        df.setMaximumFractionDigits(2);
        if (getBukkitLivingEntity() instanceof Player) {
            String name = defender.getBukkitLivingEntity().getName();
            if (defender.getBukkitLivingEntity().getCustomName() != null)
                name = defender.getBukkitLivingEntity().getCustomName();
            if (defender.isPlayer())
                System.out.println("Detected player " + getBukkitLivingEntity().getName() + " vs player " + defender.getName());
            ((Player) getBukkitLivingEntity()).spigot().sendMessage(ChatMessageType.ACTION_BAR, new TextComponent("You hit " + name + " for " + df.format(event.getDamage()) + " " + df.format(defender.getBukkitLivingEntity().getHealth() - event.getDamage()) + "/" + df.format(defender.getBukkitLivingEntity().getMaxHealth()) + " " + my_hit.skill + " damage"));
            if (defender.isNPC()) {
                ISoliniaNPC npc;
                try {
                    npc = StateManager.getInstance().getConfigurationManager().getNPC(defender.getNpcid());
                } catch (CoreStateInitException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            // Only players get this
            if (getDoubleAttackCheck()) {
                if (getBukkitLivingEntity() instanceof Player) {
                    ((Player) getBukkitLivingEntity()).sendMessage(ChatColor.GRAY + "* You double attack!");
                    tryIncreaseSkill("DOUBLEATTACK", 1);
                }
                defender.damage(plugin, my_hit.damage_done, this.getBukkitLivingEntity());
            }
            try {
                if (Utils.IsSoliniaItem(getBukkitLivingEntity().getEquipment().getItemInMainHand())) {
                    try {
                        ISoliniaItem soliniaitem = SoliniaItemAdapter.Adapt(getBukkitLivingEntity().getEquipment().getItemInMainHand());
                        if (soliniaitem != null) {
                            // Check if item has any proc effects
                            if (soliniaitem.getWeaponabilityid() > 0 && event.getCause().equals(DamageCause.ENTITY_ATTACK)) {
                                ISoliniaSpell procSpell = StateManager.getInstance().getConfigurationManager().getSpell(soliniaitem.getWeaponabilityid());
                                if (procSpell != null) {
                                    // Chance to proc
                                    int procChance = getProcChancePct();
                                    int roll = Utils.RandomBetween(0, 100);
                                    if (roll < procChance) {
                                        // TODO - For now apply self and group to attacker, else attach to target
                                        switch(Utils.getSpellTargetType(procSpell.getTargettype())) {
                                            case Self:
                                                procSpell.tryApplyOnEntity(plugin, this.getBukkitLivingEntity(), this.getBukkitLivingEntity());
                                                break;
                                            case Group:
                                                procSpell.tryApplyOnEntity(plugin, this.getBukkitLivingEntity(), this.getBukkitLivingEntity());
                                                break;
                                            default:
                                                procSpell.tryApplyOnEntity(plugin, this.getBukkitLivingEntity(), defender.getBukkitLivingEntity());
                                        }
                                    }
                                }
                            }
                        }
                    } catch (SoliniaItemException e) {
                    // skip
                    }
                }
                // Check if attacker has any WeaponProc effects
                SoliniaEntitySpells effects = StateManager.getInstance().getEntityManager().getActiveEntitySpells(this.getBukkitLivingEntity());
                if (effects != null) {
                    for (SoliniaActiveSpell activeSpell : effects.getActiveSpells()) {
                        ISoliniaSpell spell = StateManager.getInstance().getConfigurationManager().getSpell(activeSpell.getSpellId());
                        if (spell == null)
                            continue;
                        if (!spell.isWeaponProc())
                            continue;
                        for (ActiveSpellEffect spelleffect : activeSpell.getActiveSpellEffects()) {
                            if (spelleffect.getSpellEffectType().equals(SpellEffectType.WeaponProc)) {
                                if (spelleffect.getBase() < 0)
                                    continue;
                                ISoliniaSpell procSpell = StateManager.getInstance().getConfigurationManager().getSpell(spelleffect.getBase());
                                if (spell == null)
                                    continue;
                                // Chance to proc
                                int procChance = getProcChancePct();
                                int roll = Utils.RandomBetween(0, 100);
                                if (roll < procChance) {
                                    boolean itemUseSuccess = procSpell.tryApplyOnEntity(plugin, this.getBukkitLivingEntity(), defender.getBukkitLivingEntity());
                                    if (procSpell.getActSpellCost(this) > 0)
                                        if (itemUseSuccess) {
                                            if (getBukkitLivingEntity() instanceof Player) {
                                                SoliniaPlayerAdapter.Adapt((Player) getBukkitLivingEntity()).reducePlayerMana(procSpell.getActSpellCost(this));
                                            }
                                        }
                                }
                            }
                        }
                    }
                }
            } catch (CoreStateInitException e) {
            }
        }
        if (defender.getBukkitLivingEntity() instanceof Player) {
            ((Player) defender.getBukkitLivingEntity()).spigot().sendMessage(ChatMessageType.ACTION_BAR, new TextComponent("You were hit by " + getBukkitLivingEntity().getCustomName() + " for " + df.format(event.getDamage()) + " " + my_hit.skill + " damage"));
        }
        if (event.getDamage() > getBukkitLivingEntity().getHealth() && hasDeathSave() > 0) {
            Utils.CancelEvent(event);
            removeDeathSaves(plugin);
            getBukkitLivingEntity().sendMessage("* Your death save boon has saved you from death!");
            return false;
        }
        defender.damageHook(event.getDamage(), getBukkitLivingEntity());
        return true;
    } else {
        Utils.CancelEvent(event);
        return false;
    }
}
Also used : TextComponent(net.md_5.bungee.api.chat.TextComponent) Player(org.bukkit.entity.Player) ISoliniaPlayer(com.solinia.solinia.Interfaces.ISoliniaPlayer) ISoliniaItem(com.solinia.solinia.Interfaces.ISoliniaItem) ISoliniaSpell(com.solinia.solinia.Interfaces.ISoliniaSpell) DecimalFormat(java.text.DecimalFormat) SoliniaItemException(com.solinia.solinia.Exceptions.SoliniaItemException) CoreStateInitException(com.solinia.solinia.Exceptions.CoreStateInitException) ISoliniaNPC(com.solinia.solinia.Interfaces.ISoliniaNPC) ISoliniaPlayer(com.solinia.solinia.Interfaces.ISoliniaPlayer) ItemStack(org.bukkit.inventory.ItemStack)

Example 15 with SoliniaItemException

use of com.solinia.solinia.Exceptions.SoliniaItemException in project solinia3-core by mixxit.

the class Utils method removeItemsFromInventory.

public static int removeItemsFromInventory(Player player, int itemid, int count) {
    int removed = 0;
    int remaining = count;
    for (int i = 0; i < 36; i++) {
        ItemStack itemstack = player.getInventory().getItem(i);
        if (itemstack == null)
            continue;
        if (itemstack.getType().equals(Material.AIR))
            continue;
        if (!Utils.IsSoliniaItem(itemstack))
            continue;
        int tmpitemid = itemstack.getEnchantmentLevel(Enchantment.DURABILITY);
        // covers cases of negative tmp ids
        if (tmpitemid >= 0 && tmpitemid < 1000)
            continue;
        try {
            tmpitemid = SoliniaItemAdapter.Adapt(itemstack).getId();
        } catch (SoliniaItemException e) {
            continue;
        } catch (CoreStateInitException e) {
            continue;
        }
        if (remaining < 1)
            break;
        if (tmpitemid != itemid)
            continue;
        if (remaining <= itemstack.getAmount()) {
            removed = removed + remaining;
            itemstack.setAmount(itemstack.getAmount() - remaining);
            remaining = 0;
            break;
        }
        if (remaining > 64) {
            if (itemstack.getAmount() < 64) {
                removed = removed + itemstack.getAmount();
                remaining = remaining - itemstack.getAmount();
                itemstack.setAmount(0);
            } else {
                removed = removed + 64;
                remaining = remaining - 64;
                itemstack.setAmount(itemstack.getAmount() - 64);
            }
        } else {
            removed = removed + itemstack.getAmount();
            remaining = remaining - itemstack.getAmount();
            itemstack.setAmount(0);
        }
    }
    player.updateInventory();
    return removed;
}
Also used : CoreStateInitException(com.solinia.solinia.Exceptions.CoreStateInitException) CraftItemStack(org.bukkit.craftbukkit.v1_12_R1.inventory.CraftItemStack) ItemStack(org.bukkit.inventory.ItemStack) SoliniaItemException(com.solinia.solinia.Exceptions.SoliniaItemException)

Aggregations

CoreStateInitException (com.solinia.solinia.Exceptions.CoreStateInitException)15 SoliniaItemException (com.solinia.solinia.Exceptions.SoliniaItemException)15 ISoliniaItem (com.solinia.solinia.Interfaces.ISoliniaItem)11 Player (org.bukkit.entity.Player)9 ItemStack (org.bukkit.inventory.ItemStack)9 ISoliniaPlayer (com.solinia.solinia.Interfaces.ISoliniaPlayer)8 TextComponent (net.md_5.bungee.api.chat.TextComponent)3 CommandSender (org.bukkit.command.CommandSender)3 EventHandler (org.bukkit.event.EventHandler)3 ISoliniaClass (com.solinia.solinia.Interfaces.ISoliniaClass)2 ISoliniaLivingEntity (com.solinia.solinia.Interfaces.ISoliniaLivingEntity)2 DecimalFormat (java.text.DecimalFormat)2 ArrayList (java.util.ArrayList)2 UUID (java.util.UUID)2 ConsoleCommandSender (org.bukkit.command.ConsoleCommandSender)2 CraftItemStack (org.bukkit.craftbukkit.v1_12_R1.inventory.CraftItemStack)2 Entity (org.bukkit.entity.Entity)2 LivingEntity (org.bukkit.entity.LivingEntity)2 ISoliniaLootDrop (com.solinia.solinia.Interfaces.ISoliniaLootDrop)1 ISoliniaNPC (com.solinia.solinia.Interfaces.ISoliniaNPC)1