2013-10-27 16:48:43 +01:00
|
|
|
package com.Acrobot.ChestShop.Commands;
|
|
|
|
|
2013-10-27 16:49:14 +01:00
|
|
|
import com.Acrobot.Breeze.Utils.InventoryUtil;
|
|
|
|
import com.Acrobot.Breeze.Utils.MaterialUtil;
|
|
|
|
import com.Acrobot.Breeze.Utils.NumberUtil;
|
|
|
|
import com.Acrobot.ChestShop.Configuration.Messages;
|
2019-01-17 19:50:47 +01:00
|
|
|
import com.Acrobot.ChestShop.Events.ItemParseEvent;
|
2021-01-03 01:55:01 +01:00
|
|
|
import com.Acrobot.ChestShop.Utils.ItemUtil;
|
2013-10-27 16:49:14 +01:00
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.command.Command;
|
|
|
|
import org.bukkit.command.CommandExecutor;
|
|
|
|
import org.bukkit.command.CommandSender;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
|
2020-09-14 15:36:10 +02:00
|
|
|
import java.util.HashSet;
|
2013-10-27 16:49:14 +01:00
|
|
|
import java.util.List;
|
2020-09-14 15:36:10 +02:00
|
|
|
import java.util.Set;
|
2013-10-27 16:49:14 +01:00
|
|
|
|
2013-10-27 16:48:43 +01:00
|
|
|
/**
|
|
|
|
* @author Acrobot
|
|
|
|
*/
|
2013-10-27 16:49:14 +01:00
|
|
|
public class Give implements CommandExecutor {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
2013-10-29 17:12:37 +01:00
|
|
|
if (args.length < 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-27 16:49:14 +01:00
|
|
|
Player receiver = (sender instanceof Player ? (Player) sender : null);
|
|
|
|
int quantity = 1;
|
|
|
|
|
2020-09-14 15:36:10 +02:00
|
|
|
Set<Integer> disregardedIndexes = new HashSet<>();
|
2013-10-27 16:49:14 +01:00
|
|
|
|
|
|
|
if (args.length > 1) {
|
2013-10-29 17:12:37 +01:00
|
|
|
for (int index = args.length - 1; index >= 0; --index) {
|
2013-10-27 16:49:14 +01:00
|
|
|
Player target = Bukkit.getPlayer(args[index]);
|
|
|
|
|
|
|
|
if (target == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
receiver = target;
|
|
|
|
disregardedIndexes.add(index);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-10-29 17:12:37 +01:00
|
|
|
for (int index = args.length - 1; index >= 0; --index) {
|
2020-09-14 15:36:10 +02:00
|
|
|
if (disregardedIndexes.contains(index) || !NumberUtil.isInteger(args[index]) || Integer.parseInt(args[index]) < 0) {
|
2013-10-27 16:49:14 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
quantity = Integer.parseInt(args[index]);
|
|
|
|
disregardedIndexes.add(index);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (receiver == null) {
|
2020-06-28 19:00:09 +02:00
|
|
|
Messages.PLAYER_NOT_FOUND.sendWithPrefix(sender);
|
2013-10-27 16:49:14 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ItemStack item = getItem(args, disregardedIndexes);
|
|
|
|
|
2013-10-29 17:12:37 +01:00
|
|
|
if (MaterialUtil.isEmpty(item)) {
|
2020-06-28 19:00:09 +02:00
|
|
|
Messages.INCORRECT_ITEM_ID.sendWithPrefix(sender);
|
2013-10-27 16:49:14 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
item.setAmount(quantity);
|
|
|
|
InventoryUtil.add(item, receiver.getInventory());
|
|
|
|
|
2021-01-03 01:55:01 +01:00
|
|
|
Messages.ITEM_GIVEN.send(sender, "item", ItemUtil.getName(item), "player", receiver.getName());
|
2013-10-27 16:49:14 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-09-14 15:36:10 +02:00
|
|
|
private static ItemStack getItem(String[] arguments, Set<Integer> disregardedElements) {
|
2013-10-27 16:49:14 +01:00
|
|
|
StringBuilder builder = new StringBuilder(arguments.length * 5);
|
|
|
|
|
|
|
|
for (int index = 0; index < arguments.length; ++index) {
|
|
|
|
if (disregardedElements.contains(index)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
builder.append(arguments[index]).append(' ');
|
|
|
|
}
|
|
|
|
|
2019-01-17 19:50:47 +01:00
|
|
|
ItemParseEvent parseEvent = new ItemParseEvent(builder.toString());
|
|
|
|
Bukkit.getPluginManager().callEvent(parseEvent);
|
|
|
|
return parseEvent.getItem();
|
2013-10-27 16:49:14 +01:00
|
|
|
}
|
2013-10-27 16:48:43 +01:00
|
|
|
}
|