From ea79efe693594e5dac874a9d2d77515782d87afc Mon Sep 17 00:00:00 2001 From: sk89q Date: Sat, 2 Apr 2011 01:36:05 -0700 Subject: [PATCH] /stack re-added. --- .../bukkit/commands/GeneralCommands.java | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/src/com/sk89q/worldguard/bukkit/commands/GeneralCommands.java b/src/com/sk89q/worldguard/bukkit/commands/GeneralCommands.java index 41a7fa03..951fe3ad 100644 --- a/src/com/sk89q/worldguard/bukkit/commands/GeneralCommands.java +++ b/src/com/sk89q/worldguard/bukkit/commands/GeneralCommands.java @@ -22,12 +22,14 @@ import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; import com.sk89q.minecraft.util.commands.Command; import com.sk89q.minecraft.util.commands.CommandContext; import com.sk89q.minecraft.util.commands.CommandException; import com.sk89q.minecraft.util.commands.CommandPermissions; import com.sk89q.worldguard.bukkit.ConfigurationManager; import com.sk89q.worldguard.bukkit.WorldGuardPlugin; +import com.sk89q.worldedit.blocks.ItemType; public class GeneralCommands { @@ -238,4 +240,76 @@ public static void locate(CommandContext args, WorldGuardPlugin plugin, sender.sendMessage(ChatColor.YELLOW.toString() + "Compass repointed."); } } + + @Command(aliases = {"stack"}, + usage = "", + desc = "Stack items", + flags = "", min = 0, max = 0) + @CommandPermissions({"worldguard.stack"}) + public static void stack(CommandContext args, WorldGuardPlugin plugin, + CommandSender sender) throws CommandException { + + Player player = plugin.checkPlayer(sender); + + ItemStack[] items = player.getInventory().getContents(); + int len = items.length; + + int affected = 0; + + for (int i = 0; i < len; i++) { + ItemStack item = items[i]; + + // Avoid infinite stacks and stacks with durability + if (item == null || item.getAmount() <= 0 + || ItemType.shouldNotStack(item.getTypeId())) { + continue; + } + + // Ignore buckets + if (item.getTypeId() >= 325 && item.getTypeId() <= 327) { + continue; + } + + if (item.getAmount() < 64) { + int needed = 64 - item.getAmount(); // Number of needed items until 64 + + // Find another stack of the same type + for (int j = i + 1; j < len; j++) { + ItemStack item2 = items[j]; + + // Avoid infinite stacks and stacks with durability + if (item2 == null || item2.getAmount() <= 0 + || ItemType.shouldNotStack(item.getTypeId())) { + continue; + } + + // Same type? + // Blocks store their color in the damage value + if (item2.getTypeId() == item.getTypeId() && + (!ItemType.usesDamageValue(item.getTypeId()) + || item.getDurability() == item2.getDurability())) { + // This stack won't fit in the parent stack + if (item2.getAmount() > needed) { + item.setAmount(64); + item2.setAmount(item2.getAmount() - needed); + break; + // This stack will + } else { + items[j] = null; + item.setAmount(item.getAmount() + item2.getAmount()); + needed = 64 - item.getAmount(); + } + + affected++; + } + } + } + } + + if (affected > 0) { + player.getInventory().setContents(items); + } + + player.sendMessage(ChatColor.YELLOW + "Items compacted into stacks!"); + } }