2011-03-19 22:39:51 +00:00
|
|
|
package com.earth2me.essentials.commands;
|
|
|
|
|
2013-01-12 14:12:12 +00:00
|
|
|
import com.earth2me.essentials.MetaItemStack;
|
2011-03-19 22:39:51 +00:00
|
|
|
import com.earth2me.essentials.User;
|
2012-09-08 13:55:37 +00:00
|
|
|
import com.earth2me.essentials.craftbukkit.InventoryWorkaround;
|
2017-06-11 00:17:43 +00:00
|
|
|
import com.google.common.collect.Lists;
|
2011-04-21 10:08:44 +00:00
|
|
|
import org.bukkit.Material;
|
2011-11-18 17:42:26 +00:00
|
|
|
import org.bukkit.Server;
|
2013-01-12 17:05:05 +00:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
2011-03-19 22:39:51 +00:00
|
|
|
|
2017-06-11 00:17:43 +00:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
2015-04-15 04:06:16 +00:00
|
|
|
import java.util.Locale;
|
|
|
|
|
|
|
|
import static com.earth2me.essentials.I18n.tl;
|
|
|
|
|
|
|
|
|
|
|
|
public class Commanditem extends EssentialsCommand {
|
|
|
|
public Commanditem() {
|
|
|
|
super("item");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception {
|
|
|
|
if (args.length < 1) {
|
|
|
|
throw new NotEnoughArgumentsException();
|
|
|
|
}
|
2017-12-13 06:06:25 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
ItemStack stack = ess.getItemDb().get(args[0]);
|
2011-03-19 22:39:51 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
final String itemname = stack.getType().toString().toLowerCase(Locale.ENGLISH).replace("_", "");
|
2018-10-15 12:16:10 +00:00
|
|
|
if (ess.getSettings().permissionBasedItemSpawn() ? (!user.isAuthorized("essentials.itemspawn.item-all") && !user.isAuthorized("essentials.itemspawn.item-" + itemname)) : (!user.isAuthorized("essentials.itemspawn.exempt") && !user.canSpawnItem(stack.getType()))) {
|
2015-04-15 04:06:16 +00:00
|
|
|
throw new Exception(tl("cantSpawnItem", itemname));
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
if (args.length > 1 && Integer.parseInt(args[1]) > 0) {
|
|
|
|
stack.setAmount(Integer.parseInt(args[1]));
|
|
|
|
} else if (ess.getSettings().getDefaultStackSize() > 0) {
|
|
|
|
stack.setAmount(ess.getSettings().getDefaultStackSize());
|
|
|
|
} else if (ess.getSettings().getOversizedStackSize() > 0 && user.isAuthorized("essentials.oversizedstacks")) {
|
|
|
|
stack.setAmount(ess.getSettings().getOversizedStackSize());
|
|
|
|
}
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
throw new NotEnoughArgumentsException();
|
|
|
|
}
|
2015-06-04 17:18:47 +00:00
|
|
|
|
|
|
|
MetaItemStack metaStack = new MetaItemStack(stack);
|
|
|
|
if (!metaStack.canSpawn(ess)) {
|
|
|
|
throw new Exception(tl("unableToSpawnItem", itemname));
|
|
|
|
}
|
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
if (args.length > 2) {
|
|
|
|
final boolean allowUnsafe = ess.getSettings().allowUnsafeEnchantments() && user.isAuthorized("essentials.enchantments.allowunsafe");
|
2013-01-06 18:28:24 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
metaStack.parseStringMeta(user.getSource(), allowUnsafe, args, 2, ess);
|
2013-01-06 18:28:24 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
stack = metaStack.getItemStack();
|
|
|
|
}
|
2013-01-06 18:28:24 +00:00
|
|
|
|
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
if (stack.getType() == Material.AIR) {
|
|
|
|
throw new Exception(tl("cantSpawnItem", "Air"));
|
|
|
|
}
|
2013-01-06 18:28:24 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
final String displayName = stack.getType().toString().toLowerCase(Locale.ENGLISH).replace('_', ' ');
|
|
|
|
user.sendMessage(tl("itemSpawn", stack.getAmount(), displayName));
|
|
|
|
if (user.isAuthorized("essentials.oversizedstacks")) {
|
|
|
|
InventoryWorkaround.addOversizedItems(user.getBase().getInventory(), ess.getSettings().getOversizedStackSize(), stack);
|
|
|
|
} else {
|
|
|
|
InventoryWorkaround.addItems(user.getBase().getInventory(), stack);
|
|
|
|
}
|
|
|
|
user.getBase().updateInventory();
|
|
|
|
}
|
2017-06-11 00:17:43 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected List<String> getTabCompleteOptions(final Server server, final User user, final String commandLabel, final String[] args) {
|
|
|
|
if (args.length == 1) {
|
|
|
|
return getItems();
|
|
|
|
} else if (args.length == 2) {
|
|
|
|
return Lists.newArrayList("1", "64"); // TODO: get actual max size
|
|
|
|
} else if (args.length == 3) {
|
|
|
|
return Lists.newArrayList("0");
|
|
|
|
} else {
|
|
|
|
return Collections.emptyList();
|
|
|
|
}
|
|
|
|
}
|
2011-03-19 22:39:51 +00:00
|
|
|
}
|