2012-11-24 19:57:18 +00:00
|
|
|
package me.StevenLawson.TotalFreedomMod.Commands;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import me.StevenLawson.TotalFreedomMod.TFM_Util;
|
2014-11-29 19:16:00 +00:00
|
|
|
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;
|
|
|
|
|
2013-03-19 22:05:20 +00:00
|
|
|
@CommandPermissions(level = AdminLevel.OP, source = SourceType.ONLY_IN_GAME)
|
2013-04-10 02:05:24 +00:00
|
|
|
@CommandParameters(description = "Fill nearby dispensers with a set of items of your choice.", usage = "/<command> <radius> <comma,separated,items>")
|
2012-11-24 19:57:18 +00:00
|
|
|
public class Command_dispfill extends TFM_Command
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public boolean run(CommandSender sender, Player sender_p, Command cmd, String commandLabel, String[] args, boolean senderIsConsole)
|
|
|
|
{
|
|
|
|
if (args.length == 2)
|
|
|
|
{
|
|
|
|
int radius;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
radius = Math.max(5, Math.min(25, Integer.parseInt(args[0])));
|
|
|
|
}
|
2013-08-14 13:28:19 +00:00
|
|
|
catch (NumberFormatException ex)
|
2012-11-24 19:57:18 +00:00
|
|
|
{
|
|
|
|
sender.sendMessage("Invalid radius.");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-20 01:02:00 +00:00
|
|
|
final List<ItemStack> items = new ArrayList<ItemStack>();
|
2012-11-24 19:57:18 +00:00
|
|
|
|
2014-07-20 01:02:00 +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
|
|
|
|
{
|
2014-07-20 01:02:00 +00:00
|
|
|
material = me.StevenLawson.TotalFreedomMod.TFM_DepreciationAggregator.getMaterial(Integer.parseInt(searchItem));
|
2012-11-24 19:57:18 +00:00
|
|
|
}
|
2013-08-14 13:28:19 +00:00
|
|
|
catch (NumberFormatException ex)
|
2012-11-24 19:57:18 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-20 01:02:00 +00:00
|
|
|
if (material != null)
|
2012-11-24 19:57:18 +00:00
|
|
|
{
|
2014-07-20 01:02:00 +00:00
|
|
|
items.add(new ItemStack(material, 64));
|
2012-11-24 19:57:18 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-20 01:02:00 +00:00
|
|
|
sender.sendMessage("Skipping invalid item: " + searchItem);
|
2012-11-24 19:57:18 +00:00
|
|
|
}
|
|
|
|
}
|
2014-07-20 01:02:00 +00:00
|
|
|
|
|
|
|
final ItemStack[] itemsArray = items.toArray(new ItemStack[items.size()]);
|
2012-11-24 19:57:18 +00:00
|
|
|
|
|
|
|
int affected = 0;
|
2014-07-20 01:02:00 +00:00
|
|
|
final Location centerLocation = sender_p.getLocation();
|
|
|
|
final Block centerBlock = centerLocation.getBlock();
|
|
|
|
for (int xOffset = -radius; xOffset <= radius; xOffset++)
|
2012-11-24 19:57:18 +00:00
|
|
|
{
|
2014-07-20 01:02:00 +00:00
|
|
|
for (int yOffset = -radius; yOffset <= radius; yOffset++)
|
2012-11-24 19:57:18 +00:00
|
|
|
{
|
2014-07-20 01:02:00 +00:00
|
|
|
for (int zOffset = -radius; zOffset <= radius; zOffset++)
|
2012-11-24 19:57:18 +00:00
|
|
|
{
|
2014-07-20 01:02:00 +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
|
|
|
{
|
2013-08-14 14:55:37 +00:00
|
|
|
sender.sendMessage("Filling dispenser @ " + TFM_Util.formatLocation(targetBlock.getLocation()));
|
2014-07-20 01:02:00 +00:00
|
|
|
setDispenserContents(targetBlock, itemsArray);
|
2012-11-24 19:57:18 +00:00
|
|
|
affected++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sender.sendMessage("Done. " + affected + " dispenser(s) filled.");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-20 01:02:00 +00:00
|
|
|
private static void setDispenserContents(final Block targetBlock, final ItemStack[] items)
|
2012-11-24 19:57:18 +00:00
|
|
|
{
|
2014-07-20 01:02:00 +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
|
|
|
}
|
|
|
|
}
|