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

151 lines
3.6 KiB
Java
Raw Normal View History

package com.earth2me.essentials.commands;
2013-06-08 22:31:19 +01:00
import com.earth2me.essentials.utils.StringUtil;
import static com.earth2me.essentials.I18n._;
2011-11-18 17:42:26 +00:00
import com.earth2me.essentials.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.inventory.ItemStack;
public class Commandrepair extends EssentialsCommand
{
public Commandrepair()
{
super("repair");
}
@Override
public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception
{
if (args.length < 1 || args[0].equalsIgnoreCase("hand") || !user.isAuthorized("essentials.repair.all"))
{
repairHand(user);
}
else if (args[0].equalsIgnoreCase("all"))
{
final Trade charge = new Trade("repair-all", ess);
charge.isAffordableFor(user);
repairAll(user);
charge.charge(user);
}
else
{
throw new NotEnoughArgumentsException();
}
}
public void repairHand(User user) throws Exception
{
final ItemStack item = user.getItemInHand();
if (item == null || item.getType().isBlock() || item.getDurability() == 0)
{
throw new Exception(_("repairInvalidType"));
}
if (!item.getEnchantments().isEmpty()
&& !ess.getSettings().getRepairEnchanted()
&& !user.isAuthorized("essentials.repair.enchanted"))
{
throw new Exception(_("repairEnchanted"));
}
final String itemName = item.getType().toString().toLowerCase(Locale.ENGLISH);
final Trade charge = new Trade("repair-" + itemName.replace('_', '-'), new Trade("repair-" + item.getTypeId(), new Trade("repair-item", ess), ess), ess);
charge.isAffordableFor(user);
repairItem(item);
charge.charge(user);
2013-06-27 02:12:07 +01:00
user.updateInventory();
user.sendMessage(_("repair", itemName.replace('_', ' ')));
}
public void repairAll(User user) throws Exception
{
final List<String> repaired = new ArrayList<String>();
repairItems(user.getInventory().getContents(), user, repaired);
if (user.isAuthorized("essentials.repair.armor"))
{
repairItems(user.getInventory().getArmorContents(), user, repaired);
}
2013-06-27 02:12:07 +01:00
user.updateInventory();
if (repaired.isEmpty())
{
throw new Exception(_("repairNone"));
}
else
{
2013-06-08 22:31:19 +01:00
user.sendMessage(_("repair", StringUtil.joinList(repaired)));
}
}
private void repairItem(final ItemStack item) throws Exception
{
final Material material = Material.getMaterial(item.getTypeId());
if (material.isBlock() || material.getMaxDurability() < 1)
{
throw new Exception(_("repairInvalidType"));
}
2011-08-20 21:23:44 +01:00
if (item.getDurability() == 0)
{
throw new Exception(_("repairAlreadyFixed"));
}
2011-08-20 21:23:44 +01:00
item.setDurability((short)0);
}
2011-08-20 21:23:44 +01:00
private void repairItems(final ItemStack[] items, final IUser user, final List<String> repaired)
{
for (ItemStack item : items)
{
if (item == null || item.getType().isBlock() || item.getDurability() == 0)
2011-10-08 19:41:41 +02:00
{
continue;
}
final String itemName = item.getType().toString().toLowerCase(Locale.ENGLISH);
final Trade charge = new Trade("repair-" + itemName.replace('_', '-'), new Trade("repair-" + item.getTypeId(), new Trade("repair-item", ess), ess), ess);
try
{
charge.isAffordableFor(user);
}
catch (ChargeException ex)
{
2011-08-27 16:21:29 +02:00
user.sendMessage(ex.getMessage());
continue;
}
if (!item.getEnchantments().isEmpty()
&& !ess.getSettings().getRepairEnchanted()
&& !user.isAuthorized("essentials.repair.enchanted"))
{
continue;
}
2011-08-27 16:21:29 +02:00
try
{
2011-08-27 16:21:29 +02:00
repairItem(item);
}
2011-08-27 16:21:29 +02:00
catch (Exception e)
{
continue;
}
try
{
charge.charge(user);
}
catch (ChargeException ex)
{
user.sendMessage(ex.getMessage());
}
repaired.add(itemName.replace('_', ' '));
}
}
}