TotalFreedomMod/src/main/java/me/totalfreedom/totalfreedommod/commands/Command_dispfill.java

113 lines
4.0 KiB
Java
Raw Normal View History

package me.totalfreedom.totalfreedommod.commands;
2012-11-24 19:57:18 +00:00
import me.totalfreedom.totalfreedommod.rank.PlayerRank;
2012-11-24 19:57:18 +00:00
import java.util.ArrayList;
import java.util.List;
import me.totalfreedom.totalfreedommod.util.DepreciationAggregator;
import me.totalfreedom.totalfreedommod.util.FUtil;
import org.apache.commons.lang3.StringUtils;
2012-11-24 19:57:18 +00:00
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.Dispenser;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
@CommandPermissions(level = PlayerRank.OP, source = SourceType.ONLY_IN_GAME)
@CommandParameters(description = "Fill nearby dispensers with a set of items of your choice.", usage = "/<command> <radius> <comma,separated,items>")
public class Command_dispfill extends FreedomCommand
2012-11-24 19:57:18 +00:00
{
2012-11-24 19:57:18 +00:00
@Override
public boolean run(CommandSender sender, Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole)
2012-11-24 19:57:18 +00:00
{
if (args.length == 2)
{
int radius;
try
{
radius = Math.max(5, Math.min(25, Integer.parseInt(args[0])));
}
catch (NumberFormatException ex)
2012-11-24 19:57:18 +00:00
{
sender.sendMessage("Invalid radius.");
return true;
}
final List<ItemStack> items = new ArrayList<ItemStack>();
2012-11-24 19:57:18 +00:00
final String[] itemsRaw = StringUtils.split(args[1], ",");
for (final String searchItem : itemsRaw)
{
Material material = Material.matchMaterial(searchItem);
if (material == null)
2012-11-24 19:57:18 +00:00
{
try
{
material = DepreciationAggregator.getMaterial(Integer.parseInt(searchItem));
2012-11-24 19:57:18 +00:00
}
catch (NumberFormatException ex)
2012-11-24 19:57:18 +00:00
{
}
}
if (material != null)
2012-11-24 19:57:18 +00:00
{
items.add(new ItemStack(material, 64));
2012-11-24 19:57:18 +00:00
}
else
{
sender.sendMessage("Skipping invalid item: " + searchItem);
2012-11-24 19:57:18 +00:00
}
}
final ItemStack[] itemsArray = items.toArray(new ItemStack[items.size()]);
2012-11-24 19:57:18 +00:00
int affected = 0;
final Location centerLocation = playerSender.getLocation();
final Block centerBlock = centerLocation.getBlock();
for (int xOffset = -radius; xOffset <= radius; xOffset++)
2012-11-24 19:57:18 +00:00
{
for (int yOffset = -radius; yOffset <= radius; yOffset++)
2012-11-24 19:57:18 +00:00
{
for (int zOffset = -radius; zOffset <= radius; zOffset++)
2012-11-24 19:57:18 +00:00
{
final Block targetBlock = centerBlock.getRelative(xOffset, yOffset, zOffset);
if (targetBlock.getLocation().distanceSquared(centerLocation) < (radius * radius))
2012-11-24 19:57:18 +00:00
{
2013-08-14 14:55:37 +00:00
if (targetBlock.getType().equals(Material.DISPENSER))
2012-11-24 19:57:18 +00:00
{
sender.sendMessage("Filling dispenser @ " + FUtil.formatLocation(targetBlock.getLocation()));
setDispenserContents(targetBlock, itemsArray);
2012-11-24 19:57:18 +00:00
affected++;
}
}
}
}
}
sender.sendMessage("Done. " + affected + " dispenser(s) filled.");
}
else
{
return false;
}
return true;
}
private static void setDispenserContents(final Block targetBlock, final ItemStack[] items)
2012-11-24 19:57:18 +00:00
{
if (targetBlock.getType() == Material.DISPENSER)
{
final Inventory dispenserInv = ((Dispenser) targetBlock.getState()).getInventory();
dispenserInv.clear();
dispenserInv.addItem(items);
}
2012-11-24 19:57:18 +00:00
}
}