2016-03-02 19:28:01 +00:00
|
|
|
package me.totalfreedom.totalfreedommod.command;
|
2012-11-24 19:57:18 +00:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2016-03-06 15:56:15 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.rank.Rank;
|
2015-10-19 17:43:46 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.util.FUtil;
|
2019-07-31 16:19:23 +00:00
|
|
|
import org.apache.commons.lang.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;
|
|
|
|
|
2016-03-06 15:56:15 +00:00
|
|
|
@CommandPermissions(level = Rank.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>")
|
2015-10-19 17:43:46 +00:00
|
|
|
public class Command_dispfill extends FreedomCommand
|
2012-11-24 19:57:18 +00:00
|
|
|
{
|
2015-11-22 18:26:47 +00:00
|
|
|
|
2020-12-25 19:46:43 +00:00
|
|
|
private static void setDispenserContents(final Block targetBlock, final ItemStack[] items)
|
|
|
|
{
|
|
|
|
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
|
|
|
@Override
|
2015-11-22 18:26:47 +00:00
|
|
|
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])));
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2016-03-02 19:28:01 +00:00
|
|
|
final List<ItemStack> items = new ArrayList<>();
|
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);
|
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
|
|
|
|
2020-12-25 19:46:43 +00:00
|
|
|
final ItemStack[] itemsArray = items.toArray(new ItemStack[0]);
|
2012-11-24 19:57:18 +00:00
|
|
|
|
|
|
|
int affected = 0;
|
2015-11-22 18:26:47 +00:00
|
|
|
final Location centerLocation = playerSender.getLocation();
|
2014-07-20 01:02:00 +00:00
|
|
|
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
|
|
|
{
|
2015-10-19 17:43:46 +00:00
|
|
|
sender.sendMessage("Filling dispenser @ " + FUtil.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;
|
|
|
|
}
|
|
|
|
}
|