use of org.spongepowered.api.item.inventory.ItemStack in project Skree by Skelril.
the class MarketAddAliasCommand 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();
Optional<ItemStack> held = ((Player) src).getItemInHand(HandTypes.MAIN_HAND);
if (!held.isPresent()) {
src.sendMessage(Text.of(TextColors.RED, "You are not holding an item."));
return CommandResult.empty();
}
ItemStack item = held.get();
String alias = args.<String>getOne("alias").get();
if (service.addAlias(alias, item)) {
src.sendMessage(Text.of(TextColors.YELLOW, alias + " added to the market."));
} else {
src.sendMessage(Text.of(TextColors.RED, "Your held item is not currently tracked, or the alias is already in use."));
return CommandResult.empty();
}
return CommandResult.success();
}
use of org.spongepowered.api.item.inventory.ItemStack in project Skree by Skelril.
the class MarketQuickAddCommand 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();
Optional<ItemStack> held = ((Player) src).getItemInHand(HandTypes.MAIN_HAND);
if (!held.isPresent()) {
src.sendMessage(Text.of(TextColors.RED, "You are not holding an item."));
return CommandResult.empty();
}
ItemStack item = held.get();
String alias = args.<String>getOne("alias").get();
BigDecimal price;
try {
price = new BigDecimal(args.<String>getOne("price").get());
} catch (NumberFormatException ex) {
src.sendMessage(Text.of(TextColors.RED, "Invalid price specified"));
return CommandResult.empty();
}
if (service.addItem(item)) {
if (service.addAlias(alias, item)) {
if (service.setBasePrice(alias, price)) {
if (service.setPrimaryAlias(alias)) {
src.sendMessage(Text.of(TextColors.YELLOW, alias + " added to the market with a price of " + format(price)));
return CommandResult.success();
}
// Same error, fall through
}
src.sendMessage(Text.of(TextColors.RED, alias + " is not a valid alias"));
return CommandResult.empty();
}
src.sendMessage(Text.of(TextColors.RED, "Your held item is not currently tracked, or the alias is already in use."));
return CommandResult.empty();
}
src.sendMessage(Text.of(TextColors.RED, "Your held item is already tracked."));
return CommandResult.empty();
}
use of org.spongepowered.api.item.inventory.ItemStack in project Skree by Skelril.
the class ItemCompactor method execute.
public Optional<ItemStack[]> execute(ItemStack[] input) {
ItemStack[] output = Arrays.copyOf(input, input.length);
int[] countBuckets = new int[valueMappings.size()];
BigInteger[] pointBuckets = new BigInteger[valueMappings.size()];
for (int i = 0; i < pointBuckets.length; ++i) {
pointBuckets[i] = BigInteger.ZERO;
}
for (int i = 0; i < input.length; ++i) {
Collection<ItemStack> stack = Collections.singleton(input[i]);
for (int k = 0; k < valueMappings.size(); ++k) {
Optional<BigInteger> value = valueMappings.get(k).getValue(stack);
if (value.isPresent()) {
output[i] = null;
countBuckets[k] += input[i].getQuantity();
pointBuckets[k] = pointBuckets[k].add(value.get());
break;
}
}
}
Queue<ItemStack> collectedStacks = new ArrayDeque<>();
boolean canMinimize = false;
for (int k = 0; k < valueMappings.size(); ++k) {
Collection<ItemStack> stacks = valueMappings.get(k).satisfy(pointBuckets[k]);
int minimumSum = 0;
for (ItemStack stack : stacks) {
minimumSum += stack.getQuantity();
}
if (minimumSum < countBuckets[k]) {
canMinimize = true;
}
collectedStacks.addAll(stacks);
}
if (canMinimize) {
for (int i = 0; i < output.length; ++i) {
if (collectedStacks.isEmpty()) {
break;
}
if (output[i] == null) {
output[i] = collectedStacks.poll();
}
}
// TODO do this better
return collectedStacks.isEmpty() ? Optional.of(output) : Optional.empty();
}
return Optional.empty();
}
use of org.spongepowered.api.item.inventory.ItemStack in project Skree by Skelril.
the class MarketImplUtil method setBalanceTo.
public static boolean setBalanceTo(Player player, BigDecimal decimal, Cause cause) {
EntityPlayer playerEnt = tf(player);
net.minecraft.item.ItemStack[] mainInv = playerEnt.inventory.mainInventory;
Collection<ItemStack> results = CofferValueMap.inst().satisfy(decimal.toBigInteger());
Iterator<ItemStack> resultIt = results.iterator();
// Loop through replacing empty slots and the old coffers with the new balance
for (int i = 0; i < mainInv.length; ++i) {
Optional<BigInteger> value = CofferValueMap.inst().getValue(Lists.newArrayList(tf(mainInv[i])));
if (value.isPresent()) {
mainInv[i] = null;
}
if (mainInv[i] == null && resultIt.hasNext()) {
mainInv[i] = tf(resultIt.next());
resultIt.remove();
}
}
// Add remaining currency
new ItemDropper(player.getLocation()).dropStacks(results, SpawnTypes.PLUGIN);
return true;
}
use of org.spongepowered.api.item.inventory.ItemStack in project Skree by Skelril.
the class MarketImplUtil method giveItems.
public static Clause<Boolean, List<Clause<ItemStack, Integer>>> giveItems(Player player, Collection<ItemStack> stacks, Cause cause) {
List<Clause<ItemStack, Integer>> transactions = new ArrayList<>(stacks.size());
List<ItemStackSnapshot> itemBuffer = new ArrayList<>();
itemBuffer.addAll(stacks.stream().map(ItemStack::createSnapshot).collect(Collectors.toList()));
PlayerInventory playerInventory = player.getInventory().query(PlayerInventory.class);
List<Inventory> inventories = new ArrayList<>();
inventories.add(playerInventory.getHotbar());
inventories.add(playerInventory.getMain());
// Loop through replacing empty space with the requested items
for (Inventory inventory : inventories) {
List<ItemStackSnapshot> newBuffer = new ArrayList<>();
for (ItemStackSnapshot snapshot : itemBuffer) {
ItemStack stack = snapshot.createStack();
InventoryTransactionResult result = inventory.offer(stack);
newBuffer.addAll(result.getRejectedItems());
transactions.add(new Clause<>(stack, stack.getQuantity()));
}
itemBuffer = newBuffer;
}
// Drop remaining items
new ItemDropper(player.getLocation()).dropStackSnapshots(itemBuffer, SpawnTypes.PLUGIN);
return new Clause<>(true, transactions);
}
Aggregations