TF-EssentialsX/Essentials/src/com/earth2me/essentials/commands/Commanditem.java

88 lines
3.5 KiB
Java
Raw Normal View History

package com.earth2me.essentials.commands;
2013-01-12 14:12:12 +00:00
import com.earth2me.essentials.MetaItemStack;
import com.earth2me.essentials.User;
import com.earth2me.essentials.craftbukkit.InventoryWorkaround;
import com.google.common.collect.Lists;
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;
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();
}
2015-04-15 04:06:16 +00:00
ItemStack stack = ess.getItemDb().get(args[0]);
2015-04-15 04:06:16 +00:00
final String itemname = stack.getType().toString().toLowerCase(Locale.ENGLISH).replace("_", "");
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();
}
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");
2015-04-15 04:06:16 +00:00
metaStack.parseStringMeta(user.getSource(), allowUnsafe, args, 2, ess);
2015-04-15 04:06:16 +00:00
stack = metaStack.getItemStack();
}
2015-04-15 04:06:16 +00:00
if (stack.getType() == Material.AIR) {
throw new Exception(tl("cantSpawnItem", "Air"));
}
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();
}
@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();
}
}
}