TFM-4.3-Reloaded/src/me/StevenLawson/TotalFreedomMod/Commands/Command_enchant.java
Steven Lawson d60963cc7c Added enchant interface
Other misc stuff
2011-11-29 00:41:47 -05:00

131 lines
3.7 KiB
Java

package me.StevenLawson.TotalFreedomMod.Commands;
import me.StevenLawson.TotalFreedomMod.TotalFreedomMod;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
public class Command_enchant extends TFM_Command
{
@Override
public boolean run(CommandSender sender, Player sender_p, Command cmd, String commandLabel, String[] args, boolean senderIsConsole)
{
if (senderIsConsole)
{
sender.sendMessage(TotalFreedomMod.NOT_FROM_CONSOLE);
return true;
}
if (!sender.isOp())
{
sender.sendMessage(TotalFreedomMod.MSG_NO_PERMS);
return true;
}
if (args.length < 1)
{
return false;
}
ItemStack itemInHand = sender_p.getItemInHand();
if (itemInHand == null)
{
sender.sendMessage("You are holding an invalid item.");
return true;
}
if (args[0].equalsIgnoreCase("list"))
{
boolean has_enchantments = false;
StringBuilder possible_ench = new StringBuilder("Possible enchantments for held item: ");
for (Enchantment ench : Enchantment.values())
{
if (ench.canEnchantItem(itemInHand))
{
has_enchantments = true;
possible_ench.append(ench.getName()).append(", ");
}
}
if (has_enchantments)
{
sender.sendMessage(possible_ench.toString());
}
else
{
sender.sendMessage("The held item has no enchantments.");
}
}
else if (args[0].equalsIgnoreCase("addall"))
{
for (Enchantment ench : Enchantment.values())
{
if (ench.canEnchantItem(itemInHand))
{
itemInHand.addEnchantment(ench, ench.getMaxLevel());
}
}
sender.sendMessage("Added all possible enchantments for this item.");
}
else if (args[0].equalsIgnoreCase("reset"))
{
for (Enchantment ench : itemInHand.getEnchantments().keySet())
{
itemInHand.removeEnchantment(ench);
}
sender.sendMessage("Removed all enchantments.");
}
else
{
if (args.length < 2)
{
return false;
}
Enchantment ench = null;
try
{
ench = Enchantment.getByName(args[1]);
}
catch (Exception ex)
{
}
if (ench == null)
{
sender.sendMessage(args[1] + " is an invalid enchantment for the held item. Type \"/enchant list\" for valid enchantments for this item.");
return true;
}
if (args[0].equalsIgnoreCase("add"))
{
if (ench.canEnchantItem(itemInHand))
{
itemInHand.addEnchantment(ench, ench.getMaxLevel());
sender.sendMessage("Added enchantment: " + ench.getName());
}
else
{
sender.sendMessage("Can't use this enchantment on held item.");
}
}
else if (args[0].equalsIgnoreCase("remove"))
{
itemInHand.removeEnchantment(ench);
sender.sendMessage("Removed enchantment: " + ench.getName());
}
}
return true;
}
}