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

117 lines
2.3 KiB
Java
Raw Normal View History

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.earth2me.essentials.commands;
import com.earth2me.essentials.User;
import com.earth2me.essentials.Util;
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.MaterialData;
/**
*
* @author Seiji
*/
public class Commandrepair extends EssentialsCommand
{
public Commandrepair()
{
super("repair");
}
@Override
public void run(Server server, User user, String commandLabel, String[] args) throws Exception
{
if (args.length < 1)
{
throw new NotEnoughArgumentsException();
}
if (args[0].equalsIgnoreCase("hand"))
{
ItemStack item = user.getItemInHand();
try
{
repairItem(item);
}
catch (Exception e)
{
user.sendMessage(e.getMessage());
return;
}
String itemName = item.getType().toString().toLowerCase().replace('_', ' ');
2011-08-20 20:23:44 +00:00
charge(user);
user.sendMessage(Util.format("repair", itemName));
}
else if (args[0].equalsIgnoreCase("all"))
{
StringBuilder itemList = new StringBuilder();
itemList.append(repairItems(user.getInventory().getContents()));
2011-08-20 20:23:44 +00:00
String armor = repairItems(user.getInventory().getArmorContents());
2011-08-20 20:23:44 +00:00
if (itemList.length() == 0)
{
user.sendMessage(Util.format("repairNone"));
}
else
{
2011-08-20 20:23:44 +00:00
charge(user);
user.sendMessage(Util.format("repair", Util.joinList(itemList)));
}
}
else
{
throw new NotEnoughArgumentsException();
}
}
private void repairItem(ItemStack item) throws Exception
{
Material material = Material.getMaterial(item.getTypeId());
String error = null;
if (material.isBlock() || material.getMaxDurability() < 0)
{
throw new Exception(Util.i18n("repairInvalidType"));
}
2011-08-20 20:23:44 +00:00
if (item.getDurability() == 0)
{
throw new Exception(Util.i18n("repairAlreadyFixed"));
}
2011-08-20 20:23:44 +00:00
item.setDurability((short)0);
}
2011-08-20 20:23:44 +00:00
private String repairItems(ItemStack[] items)
{
StringBuilder itemList = new StringBuilder();
for (ItemStack item : items)
{
try
{
repairItem(item);
if (itemList.length() > 0)
{
itemList.append(", ");
}
String itemName = item.getType().toString().toLowerCase().replace('_', ' ');
itemList.append(itemName);
}
catch (Exception e)
{
}
}
return itemList.toString();
}
}