110 lines
4.2 KiB
Java
110 lines
4.2 KiB
Java
package me.totalfreedom.totalfreedommod.command;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
import me.totalfreedom.totalfreedommod.command.handling.CommandParameters;
|
|
import me.totalfreedom.totalfreedommod.command.handling.CommandPermissions;
|
|
import me.totalfreedom.totalfreedommod.command.handling.FreedomCommand;
|
|
import me.totalfreedom.totalfreedommod.command.handling.SourceType;
|
|
import me.totalfreedom.totalfreedommod.util.FUtil;
|
|
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
|
|
import org.apache.commons.lang.StringUtils;
|
|
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(permission = "dispfill", 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
|
|
{
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public boolean run(CommandSender sender, Player playerSender, 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])));
|
|
}
|
|
catch (NumberFormatException ex)
|
|
{
|
|
msgNew("<red>Invalid radius: <amount>", Placeholder.unparsed("amount", args[0]));
|
|
return true;
|
|
}
|
|
|
|
final List<ItemStack> items = new ArrayList<>();
|
|
|
|
final String[] itemsRaw = StringUtils.split(args[1], ",");
|
|
for (final String searchItem : itemsRaw)
|
|
{
|
|
Material material = Material.matchMaterial(searchItem);
|
|
|
|
if (material != null)
|
|
{
|
|
items.add(new ItemStack(material, 64));
|
|
}
|
|
else
|
|
{
|
|
msgNew("Skipping invalid item: <item>", Placeholder.unparsed("item", searchItem));
|
|
}
|
|
}
|
|
|
|
final ItemStack[] itemsArray = items.toArray(new ItemStack[0]);
|
|
|
|
int affected = 0;
|
|
final Location centerLocation = playerSender.getLocation();
|
|
final Block centerBlock = centerLocation.getBlock();
|
|
for (int xOffset = -radius; xOffset <= radius; xOffset++)
|
|
{
|
|
for (int yOffset = -radius; yOffset <= radius; yOffset++)
|
|
{
|
|
for (int zOffset = -radius; zOffset <= radius; zOffset++)
|
|
{
|
|
final Block targetBlock = centerBlock.getRelative(xOffset, yOffset, zOffset);
|
|
if (targetBlock.getLocation().distanceSquared(centerLocation) < (radius * radius) && targetBlock.getType().equals(Material.DISPENSER))
|
|
{
|
|
msgNew("Filling dispenser @ <location>", Placeholder.unparsed("location", FUtil.formatLocation(targetBlock.getLocation())));
|
|
|
|
if (plugin.cpb.isEnabled())
|
|
{
|
|
plugin.cpb.getCoreProtectAPI().logContainerTransaction(sender.getName(), targetBlock.getLocation());
|
|
}
|
|
|
|
setDispenserContents(targetBlock, itemsArray);
|
|
affected++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
msgNew("Done. <amount> dispenser(s) filled.", Placeholder.unparsed("amount", String.valueOf(affected)));
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|