Search in sources :

Example 66 with ItemStack

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

the class ItemStackValueMapping method matches.

@Override
protected Optional<PointType> matches(Collection<ItemStack> a, Collection<ItemStack> b, PointType matchPoints) {
    if (a.size() != b.size()) {
        return Optional.empty();
    }
    int factor = Integer.MAX_VALUE;
    for (ItemStack aStack : a) {
        boolean matchFound = false;
        for (ItemStack bStack : b) {
            if (ItemComparisonUtil.isSimilar(aStack, bStack)) {
                matchFound = true;
                factor = Math.min(factor, aStack.getQuantity() / bStack.getQuantity());
                break;
            }
        }
        if (!matchFound) {
            return Optional.empty();
        }
    }
    return Optional.of(pointTypeMulti.apply(pointTypeFromInt.apply(factor), matchPoints));
}
Also used : ItemStack(org.spongepowered.api.item.inventory.ItemStack)

Example 67 with ItemStack

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

the class ItemStackValueMapping method collect.

@Override
protected Collection<ItemStack> collect(Collection<ItemStack> satisfiers, PointType amt) {
    List<ItemStack> itemStacks = new ArrayList<>();
    for (ItemStack satisfier : satisfiers) {
        PointType baseQuantity = pointTypeFromInt.apply(satisfier.getQuantity());
        PointType maxQuantity = pointTypeFromInt.apply(satisfier.getMaxStackQuantity());
        PointType total = pointTypeMulti.apply(baseQuantity, amt);
        while (total.compareTo(zeroValue) > 0) {
            PointType increment = min(total, maxQuantity);
            total = pointTypeSub.apply(total, increment);
            itemStacks.add(ItemStackFactory.newItemStack(satisfier, pointTypeToInt.apply(increment)));
        }
    }
    return itemStacks;
}
Also used : ArrayList(java.util.ArrayList) ItemStack(org.spongepowered.api.item.inventory.ItemStack)

Example 68 with ItemStack

use of org.spongepowered.api.item.inventory.ItemStack in project HuskyCrates-Sponge by codeHusky.

the class KeyAll method execute.

@Override
public CommandResult execute(CommandSource commandSource, CommandContext commandContext) throws CommandException {
    if (commandContext.getOne("type").isPresent()) {
        String type = commandContext.<String>getOne("type").get();
        VirtualCrate virtualCrate = HuskyCrates.instance.getCrateUtilities().getVirtualCrate(type);
        int quantity = commandContext.getOne("quantity").isPresent() ? commandContext.<Integer>getOne("quantity").get() : 1;
        if (virtualCrate == null) {
            commandSource.sendMessage(Text.of("Invalid crate id: " + type + ". Please check your config."));
            return CommandResult.empty();
        }
        for (Player player : Sponge.getServer().getOnlinePlayers()) {
            ItemStack keyItemStack = virtualCrate.getCrateKey(quantity);
            InventoryTransactionResult.Type mainInventory = player.getInventory().offer(keyItemStack.copy()).getType();
            if (!mainInventory.equals(InventoryTransactionResult.Type.SUCCESS)) {
                InventoryTransactionResult.Type enderInventory = player.getEnderChestInventory().offer(keyItemStack.copy()).getType();
                if (!enderInventory.equals(InventoryTransactionResult.Type.SUCCESS)) {
                    commandSource.sendMessage(Text.of("Couldn't give key to " + player.getName() + " because of a full inventory and enderchest"));
                    HuskyCrates.instance.logger.info("Couldn't give key to " + player.getName() + " because of a full inventory and enderchest");
                } else {
                    player.sendMessage(Text.of("You have been given 1 or more ", TextSerializers.FORMATTING_CODE.deserialize(virtualCrate.displayName), " key(s), but some have been placed in your Ender Chest."));
                }
            }
        }
    } else {
        commandSource.sendMessage(Text.of("Usage: /crate keyall <id> [count]"));
    }
    return CommandResult.success();
}
Also used : VirtualCrate(pw.codehusky.huskycrates.crate.VirtualCrate) Player(org.spongepowered.api.entity.living.player.Player) ItemStack(org.spongepowered.api.item.inventory.ItemStack) InventoryTransactionResult(org.spongepowered.api.item.inventory.transaction.InventoryTransactionResult)

Example 69 with ItemStack

use of org.spongepowered.api.item.inventory.ItemStack in project HuskyCrates-Sponge by codeHusky.

the class Wand method execute.

@Override
public CommandResult execute(CommandSource commandSource, CommandContext commandContext) throws CommandException {
    if (commandContext.getOne("type").isPresent() && commandSource instanceof Player) {
        String type = commandContext.<String>getOne("type").get();
        Player player = (Player) commandSource;
        VirtualCrate virtualCrate = HuskyCrates.instance.getCrateUtilities().getVirtualCrate(type);
        if (virtualCrate == null) {
            commandSource.sendMessage(Text.of("Invalid crate id: " + type + ". Please check your config."));
            return CommandResult.empty();
        }
        ItemStack keyItemStack = virtualCrate.getCrateWand();
        InventoryTransactionResult.Type mainInventory = player.getInventory().offer(keyItemStack.copy()).getType();
        if (!mainInventory.equals(InventoryTransactionResult.Type.SUCCESS)) {
            InventoryTransactionResult.Type enderInventory = player.getEnderChestInventory().offer(keyItemStack.copy()).getType();
            if (!enderInventory.equals(InventoryTransactionResult.Type.SUCCESS)) {
                commandSource.sendMessage(Text.of("Couldn't give wand to " + player.getName() + " because of a full inventory and enderchest"));
                HuskyCrates.instance.logger.info("Couldn't give wand to " + player.getName() + " because of a full inventory and enderchest");
            } else {
                player.sendMessage(Text.of("You have been given a ", TextSerializers.FORMATTING_CODE.deserialize(virtualCrate.displayName), " wand, but it has been placed in your Ender Chest."));
            }
        }
    } else {
        commandSource.sendMessage(Text.of("Usage: /crate wand <id>"));
    }
    return CommandResult.success();
}
Also used : VirtualCrate(pw.codehusky.huskycrates.crate.VirtualCrate) Player(org.spongepowered.api.entity.living.player.Player) ItemStack(org.spongepowered.api.item.inventory.ItemStack) InventoryTransactionResult(org.spongepowered.api.item.inventory.transaction.InventoryTransactionResult)

Example 70 with ItemStack

use of org.spongepowered.api.item.inventory.ItemStack in project HuskyCrates-Sponge by codeHusky.

the class VirtualCrate method getCrateKey.

/***
     * Retrieve the crate item
     * @since 0.10.2
     * @param quantity the quantity of keys you want.
     * @return the ItemStack with the keys.
     */
public ItemStack getCrateKey(int quantity) {
    ItemStack key = ItemStack.builder().itemType(keyType).quantity(quantity).add(Keys.DISPLAY_NAME, TextSerializers.FORMATTING_CODE.deserialize(displayName + " Key")).build();
    ArrayList<Text> itemLore = new ArrayList<>();
    itemLore.add(Text.of(TextColors.WHITE, "A key for a ", TextSerializers.FORMATTING_CODE.deserialize(displayName), TextColors.WHITE, "."));
    itemLore.add(Text.of(TextColors.DARK_GRAY, "HuskyCrates"));
    key.offer(Keys.ITEM_LORE, itemLore);
    if (keyDamage != null) {
        key = ItemStack.builder().fromContainer(key.toContainer().set(DataQuery.of("UnsafeDamage"), keyDamage)).build();
    }
    //
    return ItemStack.builder().fromContainer(key.toContainer().set(DataQuery.of("UnsafeData", "crateID"), id)).build();
}
Also used : ArrayList(java.util.ArrayList) Text(org.spongepowered.api.text.Text) ItemStack(org.spongepowered.api.item.inventory.ItemStack)

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