TFM-4.3-Reloaded/src/main/java/me/StevenLawson/TotalFreedomMod/commands/Command_dispfill.java

105 lines
3.7 KiB
Java
Raw Normal View History

2022-03-20 12:35:43 +00:00
package me.StevenLawson.TotalFreedomMod.commands;
2012-11-24 19:57:18 +00:00
2022-03-20 12:35:43 +00:00
import me.StevenLawson.TotalFreedomMod.util.DeprecationUtil;
import me.StevenLawson.TotalFreedomMod.util.Utilities;
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.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
2022-03-20 12:35:43 +00:00
import java.util.ArrayList;
import java.util.List;
@CommandPermissions(level = AdminLevel.OP, source = SourceType.ONLY_IN_GAME)
2022-03-20 12:35:43 +00:00
public class Command_dispfill extends FreedomCommand {
2012-11-24 19:57:18 +00:00
@Override
2022-03-20 12:35:43 +00:00
public boolean run(CommandSender sender, org.bukkit.entity.Player sender_p, Command cmd, String commandLabel, String[] args, boolean senderIsConsole) {
if (args.length == 2) {
2012-11-24 19:57:18 +00:00
int radius;
2022-03-20 12:35:43 +00:00
try {
2012-11-24 19:57:18 +00:00
radius = Math.max(5, Math.min(25, Integer.parseInt(args[0])));
2022-03-20 12:35:43 +00:00
} 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
{
2022-03-20 12:35:43 +00:00
material = DeprecationUtil.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 = sender_p.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
{
2022-03-20 12:35:43 +00:00
sender.sendMessage("Filling dispenser @ " + Utilities.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
}
}