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

69 lines
1.8 KiB
Java
Raw Normal View History

2014-02-05 12:16:50 +00:00
package com.earth2me.essentials.commands;
import static com.earth2me.essentials.I18n.tl;
2014-02-05 12:16:50 +00:00
import com.earth2me.essentials.User;
import com.earth2me.essentials.craftbukkit.InventoryWorkaround;
2014-02-05 12:16:50 +00:00
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta;
public class Commandskull extends EssentialsCommand
{
public Commandskull()
{
super("skull");
}
@Override
protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception
{
String owner;
if (args.length > 0 && user.isAuthorized("essentials.skull.others")) {
2014-02-05 12:16:50 +00:00
if (!args[0].matches("^[A-Za-z0-9_]+$")) {
throw new IllegalArgumentException(tl("alphaNames"));
2014-02-05 12:16:50 +00:00
}
owner = args[0];
}
else {
owner = user.getName();
}
2014-02-05 12:16:50 +00:00
ItemStack itemSkull = user.getBase().getItemInHand();
SkullMeta metaSkull = null;
boolean spawn = false;
if (itemSkull != null && itemSkull.getType() == Material.SKULL_ITEM && itemSkull.getDurability() == 3) {
metaSkull = (SkullMeta) itemSkull.getItemMeta();
}
else if (user.isAuthorized("essentials.skull.spawn"))
{
itemSkull = new ItemStack(Material.SKULL_ITEM, 1, (byte) 3);
metaSkull = (SkullMeta) itemSkull.getItemMeta();
spawn = true;
}
else {
throw new Exception(tl("invalidSkull"));
}
2014-02-05 12:16:50 +00:00
if (metaSkull.hasOwner() && !user.isAuthorized("essentials.skull.modify"))
{
throw new Exception(tl("noPermissionSkull"));
}
2014-02-05 12:16:50 +00:00
metaSkull.setDisplayName("§fSkull of " + owner);
metaSkull.setOwner(owner);
2014-02-05 12:16:50 +00:00
itemSkull.setItemMeta(metaSkull);
2014-02-05 12:16:50 +00:00
if (spawn) {
InventoryWorkaround.addItems(user.getBase().getInventory(), itemSkull);
user.sendMessage(tl("givenSkull", owner));
2014-02-05 12:16:50 +00:00
}
else {
user.sendMessage(tl("skullChanged", owner));
2014-02-05 12:16:50 +00:00
}
}
2014-02-05 12:16:50 +00:00
}