Search in sources :

Example 21 with ItemStack

use of org.spongepowered.api.item.inventory.ItemStack in project Skree by Skelril.

the class ButterFingersCurse method accept.

@Override
public void accept(Player player) {
    List<ItemStack> drops = new ArrayList<>();
    while (true) {
        Optional<ItemStack> optDrop = player.getInventory().poll();
        if (!optDrop.isPresent()) {
            break;
        }
        drops.add(optDrop.get());
    }
    Collections.shuffle(drops);
    new ItemDropper(player.getLocation()).dropStacks(drops, SpawnTypes.DROPPED_ITEM);
}
Also used : ItemDropper(com.skelril.nitro.item.ItemDropper) ArrayList(java.util.ArrayList) ItemStack(org.spongepowered.api.item.inventory.ItemStack)

Example 22 with ItemStack

use of org.spongepowered.api.item.inventory.ItemStack in project Skree by Skelril.

the class WildernessWorldWrapper method onEntityDeath.

@Listener
public void onEntityDeath(DestructEntityEvent.Death event) {
    Entity entity = event.getTargetEntity();
    Location<World> loc = entity.getLocation();
    Optional<Integer> optLevel = getLevel(loc);
    if (!optLevel.isPresent()) {
        return;
    }
    int level = optLevel.get();
    if (entity instanceof Monster) {
        DropTable dropTable;
        if (entity.getLocation().getExtent().getDimension() == DimensionTypes.NETHER || entity instanceof Wither) {
            dropTable = netherMobDropTable;
        } else {
            dropTable = commonDropTable;
        }
        Optional<EntityDamageSource> optDamageSource = event.getCause().first(EntityDamageSource.class);
        if (optDamageSource.isPresent()) {
            Entity srcEntity;
            if (optDamageSource.get() instanceof IndirectEntityDamageSource) {
                srcEntity = ((IndirectEntityDamageSource) optDamageSource.get()).getIndirectSource();
            } else {
                srcEntity = optDamageSource.get().getSource();
            }
            int dropTier = level;
            if (srcEntity instanceof Player) {
                Optional<ItemStack> optHeldItem = ((Player) srcEntity).getItemInHand(HandTypes.MAIN_HAND);
                if (optHeldItem.isPresent()) {
                    Optional<ItemEnchantment> optLooting = EnchantmentUtil.getHighestEnchantment(optHeldItem.get(), Enchantments.LOOTING);
                    if (optLooting.isPresent()) {
                        dropTier += optLooting.get().getLevel();
                    }
                }
                dropTier = getDropTier(dropTier);
                Collection<ItemStack> drops = dropTable.getDrops((entity instanceof Boss ? 5 : 1) * dropTier, getDropMod(dropTier, ((Monster) entity).getHealthData().maxHealth().get(), entity.getType()));
                int times = 1;
                Optional<ModifierService> optService = Sponge.getServiceManager().provide(ModifierService.class);
                if (optService.isPresent()) {
                    ModifierService service = optService.get();
                    if (service.isActive(Modifiers.DOUBLE_WILD_DROPS)) {
                        times *= 2;
                    }
                }
                ItemDropper dropper = new ItemDropper(loc);
                for (int i = 0; i < times; ++i) {
                    dropper.dropStacks(drops, SpawnTypes.DROPPED_ITEM);
                }
            }
        }
        if (entity.getType() == EntityTypes.ENDERMITE && Probability.getChance(20)) {
            entity.getWorld().triggerExplosion(Explosion.builder().location(entity.getLocation()).shouldBreakBlocks(true).radius(4F).build(), Cause.source(SkreePlugin.container()).build());
        }
    }
    GRAVE_STONE.createGraveFromDeath(event);
}
Also used : Wither(org.spongepowered.api.entity.living.monster.Wither) Player(org.spongepowered.api.entity.living.player.Player) ItemDropper(com.skelril.nitro.item.ItemDropper) ModifierService(com.skelril.skree.service.ModifierService) World(org.spongepowered.api.world.World) DropTable(com.skelril.nitro.droptable.DropTable) MasterDropTable(com.skelril.nitro.droptable.MasterDropTable) EntityDamageSource(org.spongepowered.api.event.cause.entity.damage.source.EntityDamageSource) IndirectEntityDamageSource(org.spongepowered.api.event.cause.entity.damage.source.IndirectEntityDamageSource) WanderingBoss(com.skelril.skree.content.world.wilderness.wanderer.WanderingBoss) Boss(org.spongepowered.api.entity.living.monster.Boss) ItemEnchantment(org.spongepowered.api.data.meta.ItemEnchantment) Monster(org.spongepowered.api.entity.living.monster.Monster) IndirectEntityDamageSource(org.spongepowered.api.event.cause.entity.damage.source.IndirectEntityDamageSource) ItemStack(org.spongepowered.api.item.inventory.ItemStack) ItemStackFactory.newItemStack(com.skelril.nitro.item.ItemStackFactory.newItemStack) Listener(org.spongepowered.api.event.Listener)

Example 23 with ItemStack

use of org.spongepowered.api.item.inventory.ItemStack in project Skree by Skelril.

the class WildernessWorldWrapper method getPoolItemDrop.

private ItemStackSnapshot getPoolItemDrop(ItemStackSnapshot snapshot) {
    Map<String, QuantityBoundedItemStackConfig> replacementMapping = config.getDropAmplificationConfig().getItemReplacementMapping();
    ItemStackConfig replacementItem = replacementMapping.get(snapshot.getType().getId());
    if (replacementItem != null) {
        return ((ItemStack) (Object) replacementItem.toNSMStack()).createSnapshot();
    }
    return snapshot;
}
Also used : QuantityBoundedItemStackConfig(com.skelril.nitro.registry.dynamic.QuantityBoundedItemStackConfig) QuantityBoundedItemStackConfig(com.skelril.nitro.registry.dynamic.QuantityBoundedItemStackConfig) ItemStackConfig(com.skelril.nitro.registry.dynamic.ItemStackConfig) ItemStack(org.spongepowered.api.item.inventory.ItemStack) ItemStackFactory.newItemStack(com.skelril.nitro.item.ItemStackFactory.newItemStack)

Example 24 with ItemStack

use of org.spongepowered.api.item.inventory.ItemStack in project Skree by Skelril.

the class MarketBuyCommand method execute.

@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
    if (!(src instanceof Player)) {
        src.sendMessage(Text.of("You must be a player to use this command!"));
        return CommandResult.empty();
    }
    Optional<MarketService> optService = Sponge.getServiceManager().provide(MarketService.class);
    if (!optService.isPresent()) {
        src.sendMessage(Text.of(TextColors.DARK_RED, "The market service is not currently running."));
        return CommandResult.empty();
    }
    MarketService service = optService.get();
    Player player = (Player) src;
    if (!canBuyOrSell(player)) {
        player.sendMessage(Text.of(TextColors.RED, "You cannot buy or sell from this area."));
        return CommandResult.empty();
    }
    String itemName = args.<String>getOne("item").get();
    List<String> targetItems;
    if (itemName.endsWith("#armor")) {
        String armorType = itemName.replace("#armor", " ");
        targetItems = Lists.newArrayList(armorType + "helmet", armorType + "chestplate", armorType + "leggings", armorType + "boots");
    } else {
        targetItems = Lists.newArrayList(itemName);
    }
    BigDecimal price = BigDecimal.ZERO;
    for (String anItem : targetItems) {
        Optional<BigDecimal> optPrice = service.getPrice(anItem);
        if (!optPrice.isPresent()) {
            src.sendMessage(Text.of(TextColors.RED, "That item is not available for purchase."));
            return CommandResult.empty();
        }
        price = price.add(optPrice.get());
    }
    Optional<Integer> optAmt = args.getOne("amount");
    int amt = Math.max(1, optAmt.isPresent() ? optAmt.get() : 0);
    price = price.multiply(BigDecimal.valueOf(amt));
    BigDecimal funds = MarketImplUtil.getMoney(player);
    BigDecimal newBalance = funds.subtract(price);
    if (newBalance.compareTo(BigDecimal.ZERO) < 0) {
        BigDecimal neededAmt = newBalance.multiply(BigDecimal.valueOf(-1));
        src.sendMessage(Text.of(TextColors.RED, "You do not have enough money to purchase that item(s)."));
        src.sendMessage(Text.of(TextColors.RED, "You need an additional ", TextColors.WHITE, format(neededAmt), TextColors.RED, "."));
        return CommandResult.empty();
    }
    List<Clause<String, Integer>> newStocks = new ArrayList<>();
    for (String anItem : targetItems) {
        Optional<Integer> optStock = service.getStock(anItem);
        if (optStock.orElse(0) < amt) {
            src.sendMessage(Text.of(TextColors.RED, "There is not enough stock to satisfy your order."));
            return CommandResult.empty();
        }
        newStocks.add(new Clause<>(anItem, optStock.get() - amt));
    }
    // Accumulate items
    List<ItemStack> itemStacks = new ArrayList<>(targetItems.size());
    for (String anItem : targetItems) {
        Optional<ItemStack> stack = service.getItem(anItem);
        if (!stack.isPresent()) {
            // TODO Auto reporting
            src.sendMessage(Text.of(TextColors.RED, "An item stack could not be resolved, please report this!"));
            return CommandResult.empty();
        }
        int total = amt;
        while (total > 0) {
            int increment = Math.min(total, stack.get().getMaxStackQuantity());
            total -= increment;
            itemStacks.add(newItemStack(stack.get(), increment));
        }
    }
    // Alright, all items have been found
    if (!MarketImplUtil.setBalanceTo(player, newBalance, Cause.source(SkreePlugin.container()).build())) {
        // TODO Auto reporting
        src.sendMessage(Text.of(TextColors.RED, "Failed to adjust your balance, please report this!"));
        return CommandResult.empty();
    }
    Clause<Boolean, List<Clause<ItemStack, Integer>>> transactions = MarketImplUtil.giveItems(player, itemStacks, Cause.source(SkreePlugin.container()).build());
    if (!transactions.getKey()) {
        // TODO Auto reporting
        src.sendMessage(Text.of(TextColors.RED, "Failed to give all items, please report this!"));
        return CommandResult.empty();
    }
    // Items have been given process stocks
    for (Clause<String, Integer> stock : newStocks) {
        service.setStock(stock.getKey(), stock.getValue());
    }
    if (!service.logTransactionByStack(player.getUniqueId(), transactions.getValue())) {
        // TODO Auto reporting
        // Not critical, continue
        src.sendMessage(Text.of(TextColors.RED, "Failed to log transactions, please report this!"));
    }
    player.sendMessage(Text.of(TextColors.YELLOW, "Item(s) purchased for ", TextColors.WHITE, format(price), TextColors.YELLOW, "!"));
    return CommandResult.success();
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal) ArrayList(java.util.ArrayList) List(java.util.List) Clause(com.skelril.nitro.Clause) ItemStack(org.spongepowered.api.item.inventory.ItemStack) ItemStackFactory.newItemStack(com.skelril.nitro.item.ItemStackFactory.newItemStack) MarketService(com.skelril.skree.service.MarketService)

Example 25 with ItemStack

use of org.spongepowered.api.item.inventory.ItemStack in project Skree by Skelril.

the class MarketSellCommand method execute.

@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
    if (!(src instanceof Player)) {
        src.sendMessage(Text.of("You must be a player to use this command!"));
        return CommandResult.empty();
    }
    Optional<MarketService> optService = Sponge.getServiceManager().provide(MarketService.class);
    if (!optService.isPresent()) {
        src.sendMessage(Text.of(TextColors.DARK_RED, "The market service is not currently running."));
        return CommandResult.empty();
    }
    MarketService service = optService.get();
    Player player = (Player) src;
    if (!canBuyOrSell(player)) {
        player.sendMessage(Text.of(TextColors.DARK_RED, "You cannot buy or sell from this area."));
        return CommandResult.empty();
    }
    Optional<ItemStack> filter = Optional.empty();
    QueryMode mode = QueryMode.HELD;
    if (args.hasAny("a")) {
        mode = QueryMode.EVERYTHING;
    } else if (args.hasAny("h")) {
        mode = QueryMode.HOT_BAR;
    }
    if (mode != QueryMode.HELD && !args.hasAny("u")) {
        filter = player.getItemInHand(HandTypes.MAIN_HAND);
        if (!filter.isPresent()) {
            src.sendMessage(Text.of(TextColors.RED, "You're not holding an item to filter with!"));
            return CommandResult.empty();
        }
    }
    Clause<BigDecimal, List<Integer>> changes = MarketImplUtil.getChanges(player, service, mode, filter.orElse(null));
    if (changes.getValue().isEmpty()) {
        src.sendMessage(Text.of(TextColors.RED, "No sellable items found" + (filter.isPresent() ? " that matched the filter" : "") + "!"));
        return CommandResult.empty();
    }
    List<Clause<ItemStack, Integer>> transactions = MarketImplUtil.removeAtPos(player, changes.getValue());
    if (!service.logTransactionByStack(player.getUniqueId(), transactions)) {
        // TODO Auto reporting
        // Not critical, continue
        src.sendMessage(Text.of(TextColors.DARK_RED, "Failed to log transactions, please report this!"));
    }
    BigDecimal newBalance = changes.getKey().add(MarketImplUtil.getMoney(player));
    if (!MarketImplUtil.setBalanceTo(player, newBalance, Cause.source(SkreePlugin.container()).build())) {
        // TODO Auto reporting
        src.sendMessage(Text.of(TextColors.DARK_RED, "Failed to adjust your balance, please report this!"));
        return CommandResult.empty();
    }
    // Process new stocks
    for (Clause<ItemStack, Integer> transaction : transactions) {
        service.setStock(transaction.getKey(), service.getStock(transaction.getKey()).orElse(0) + -(transaction.getValue()));
    }
    player.sendMessage(Text.of(TextColors.YELLOW, "Item(s) sold for: ", TextColors.WHITE, MarketImplUtil.format(changes.getKey()), TextColors.YELLOW, "!"));
    return CommandResult.success();
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) BigDecimal(java.math.BigDecimal) QueryMode(com.skelril.skree.content.market.MarketImplUtil.QueryMode) List(java.util.List) Clause(com.skelril.nitro.Clause) ItemStack(org.spongepowered.api.item.inventory.ItemStack) MarketService(com.skelril.skree.service.MarketService)

Aggregations

ItemStack (org.spongepowered.api.item.inventory.ItemStack)76 Player (org.spongepowered.api.entity.living.player.Player)30 ItemStackFactory.newItemStack (com.skelril.nitro.item.ItemStackFactory.newItemStack)29 Listener (org.spongepowered.api.event.Listener)17 ArrayList (java.util.ArrayList)12 Text (org.spongepowered.api.text.Text)10 ItemDropper (com.skelril.nitro.item.ItemDropper)9 MarketService (com.skelril.skree.service.MarketService)9 World (org.spongepowered.api.world.World)9 VirtualCrate (pw.codehusky.huskycrates.crate.VirtualCrate)9 BigDecimal (java.math.BigDecimal)8 Entity (org.spongepowered.api.entity.Entity)8 Vector3d (com.flowpowered.math.vector.Vector3d)5 Clause (com.skelril.nitro.Clause)5 InventoryTransactionResult (org.spongepowered.api.item.inventory.transaction.InventoryTransactionResult)5 DamageEntityEvent (org.spongepowered.api.event.entity.DamageEntityEvent)4 Inventory (org.spongepowered.api.item.inventory.Inventory)4 IOException (java.io.IOException)3 BigInteger (java.math.BigInteger)3 EntityDamageSource (org.spongepowered.api.event.cause.entity.damage.source.EntityDamageSource)3