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));
}
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;
}
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();
}
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();
}
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();
}
Aggregations