Skip to content

Commit

Permalink
The inventory component will now inform the exact quantity of items g…
Browse files Browse the repository at this point in the history
…iven, and the exact quantity of items that have been skipped, this fixes CMDBOOK-2392
  • Loading branch information
DarkArc committed Jun 27, 2014
1 parent fdfde24 commit 739b257
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
5 changes: 3 additions & 2 deletions src/main/java/com/sk89q/commandbook/InventoryComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.bukkit.inventory.ItemStack;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;

Expand Down Expand Up @@ -128,7 +129,7 @@ public class Commands {
public void item(CommandContext args, CommandSender sender) throws CommandException {
ItemStack item = null;
int amt = config.defaultItemStackSize;
Iterable<Player> targets = null;
Collection<Player> targets = null;

// How this command handles parameters depends on how many there
// are, so the following code splits the incoming input
Expand Down Expand Up @@ -171,7 +172,7 @@ public void item(CommandContext args, CommandSender sender) throws CommandExcept
public void give(CommandContext args, CommandSender sender) throws CommandException {
ItemStack item = null;
int amt = config.defaultItemStackSize;
Iterable<Player> targets = null;
Collection<Player> targets = null;

// How this command handles parameters depends on how many there
// are, so the following code splits the incoming input
Expand Down
51 changes: 33 additions & 18 deletions src/main/java/com/sk89q/commandbook/util/item/InventoryUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import java.util.Collection;

public class InventoryUtil {

/**
Expand All @@ -24,10 +26,10 @@ public class InventoryUtil {
*/
@SuppressWarnings("deprecation")
public static void giveItem(CommandSender sender, ItemStack item, int amt,
Iterable<Player> targets, InventoryComponent component, boolean drop, boolean overrideStackSize)
Collection<Player> targets, InventoryComponent component, boolean drop, boolean overrideStackSize)
throws CommandException {

boolean included = false; // Is the command sender also receiving items?
boolean infinite = false; // Is the stack infinite?

int maxStackSize = overrideStackSize ? 64 : item.getType().getMaxStackSize();

Expand All @@ -39,6 +41,7 @@ public static void giveItem(CommandSender sender, ItemStack item, int amt,
} else if (amt == -1) {
// Check to see if the player can give infinite items
CommandBook.inst().checkPermission(sender, "commandbook.give.infinite");
infinite = true;
} else if (overrideStackSize) {
CommandBook.inst().checkPermission(sender, "commandbook.override.maxstacksize");
} else if (amt > maxStackSize * 5) {
Expand All @@ -51,17 +54,22 @@ public static void giveItem(CommandSender sender, ItemStack item, int amt,
CommandBook.inst().checkPermission(sender, "commandbook.give.stacks");
}

if(amt > 2240 && !drop) amt = 2240;
int targetQuantity = targets.size();
// Send the message ahead of time so that we can follow up with any errors
if (targetQuantity > 1 || !targets.contains(sender)) {
sender.sendMessage(ChatColor.YELLOW.toString() + targetQuantity + " player(s)"
+ " have been given " + getAmountText(false, infinite, amt)
+ ' ' + ItemUtil.toItemName(item.getTypeId()) + '.');
}

// Get a nice amount name
String amtText = amt == -1 ? "an infinite stack of" : String.valueOf(amt);

for (Player player : targets) {
int left = amt;

// Give individual stacks
while (left > 0 || amt == -1) {
while (left > 0 || infinite) {
int givenAmt = Math.min(maxStackSize, left);
item = item.clone(); // This prevents the possibility of a linked ItemStack issue
item.setAmount(givenAmt);
left -= givenAmt;

Expand All @@ -70,7 +78,19 @@ public static void giveItem(CommandSender sender, ItemStack item, int amt,
if (drop) {
player.getWorld().dropItemNaturally(player.getLocation(), item);
} else {
player.getInventory().addItem(item);
Collection<ItemStack> result = player.getInventory().addItem(item).values();
// Check for items that couldn't be added
if (!result.isEmpty()) {
for (ItemStack stack : result) {
left += stack.getAmount();
sender.sendMessage(ChatColor.RED + getAmountText(true, infinite, left)
+ ' ' + ItemUtil.toItemName(stack.getTypeId())
+ " could not be given to "
+ player.getName() + " (their inventory is probably full)!");
}
// End the loop so we don't waste time, seeing as the item cannot be added
break;
}
}

if (amt == -1) {
Expand All @@ -81,28 +101,23 @@ public static void giveItem(CommandSender sender, ItemStack item, int amt,
// workaround for having inventory open while giving items (eg TMI mod)
player.updateInventory();

String amtString = getAmountText(false, infinite, amt - left);
// Tell the user about the given item
if (player.equals(sender)) {
player.sendMessage(ChatColor.YELLOW + "You've been given " + amtText + " "
player.sendMessage(ChatColor.YELLOW + "You've been given " + amtString + " "
+ ItemUtil.toItemName(item.getTypeId()) + ".");

// Keep track of this
included = true;
} else {
player.sendMessage(ChatColor.YELLOW + "Given from "
+ ChatUtil.toColoredName(sender, ChatColor.YELLOW) + ": "
+ amtText + " "
+ amtString + " "
+ ItemUtil.toItemName(item.getTypeId()) + ".");

}
}
}

// The player didn't receive any items, then we need to send the
// user a message so s/he know that something is indeed working
if (!included) {
sender.sendMessage(ChatColor.YELLOW.toString() + amtText + " "
+ ItemUtil.toItemName(item.getTypeId()) + " has been given.");
}
private static String getAmountText(boolean sentenceStart, boolean infinite, int amount) {
return infinite ? (sentenceStart ? "An" : "an") + " infinite stack of" : String.valueOf(amount);
}

/**
Expand Down

0 comments on commit 739b257

Please sign in to comment.