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