diff options
author | snowleo <schneeleo@gmail.com> | 2011-08-27 16:19:23 +0200 |
---|---|---|
committer | snowleo <schneeleo@gmail.com> | 2011-08-27 16:19:23 +0200 |
commit | 14030deb164f82978a2778daa330a74af82309dc (patch) | |
tree | 7e6cce95529253f897a1b008ad00826a08183347 | |
parent | 0543a8f8ba60548376bf4cff6f0dc673a98ecb73 (diff) | |
download | Essentials-14030deb164f82978a2778daa330a74af82309dc.tar Essentials-14030deb164f82978a2778daa330a74af82309dc.tar.gz Essentials-14030deb164f82978a2778daa330a74af82309dc.tar.lz Essentials-14030deb164f82978a2778daa330a74af82309dc.tar.xz Essentials-14030deb164f82978a2778daa330a74af82309dc.zip |
Cleanup of repair command and allow different charges for each item.
-rw-r--r-- | Essentials/src/com/earth2me/essentials/commands/Commandrepair.java | 91 |
1 files changed, 57 insertions, 34 deletions
diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandrepair.java b/Essentials/src/com/earth2me/essentials/commands/Commandrepair.java index df0509f94..ea1d1fc9e 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandrepair.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandrepair.java @@ -1,21 +1,17 @@ -/* - * To change this template, choose Tools | Templates - * and open the template in the editor. - */ package com.earth2me.essentials.commands; +import com.earth2me.essentials.ChargeException; +import com.earth2me.essentials.IUser; +import com.earth2me.essentials.Trade; import com.earth2me.essentials.User; import com.earth2me.essentials.Util; +import java.util.ArrayList; +import java.util.List; 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() @@ -24,7 +20,7 @@ public class Commandrepair extends EssentialsCommand } @Override - public void run(Server server, User user, String commandLabel, String[] args) throws Exception + public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception { if (args.length < 1) { @@ -33,7 +29,20 @@ public class Commandrepair extends EssentialsCommand if (args[0].equalsIgnoreCase("hand")) { - ItemStack item = user.getItemInHand(); + final ItemStack item = user.getItemInHand(); + final String itemName = item.getType().toString().toLowerCase(); + final Trade charge = new Trade("repair-" + itemName.replace('_', '-'), ess); + + try + { + charge.isAffordableFor(user); + } + catch (Exception ex) + { + user.sendMessage(ex.getMessage()); + return; + } + try { repairItem(item); @@ -43,26 +52,26 @@ public class Commandrepair extends EssentialsCommand user.sendMessage(e.getMessage()); return; } + charge.charge(user); - String itemName = item.getType().toString().toLowerCase().replace('_', ' '); charge(user); - user.sendMessage(Util.format("repair", itemName)); + user.sendMessage(Util.format("repair", itemName.replace('_', ' '))); } else if (args[0].equalsIgnoreCase("all")) { - StringBuilder itemList = new StringBuilder(); - itemList.append(repairItems(user.getInventory().getContents())); + final List<String> repaired = new ArrayList<String>(); + repairItems(user.getInventory().getContents(), user, repaired); - String armor = repairItems(user.getInventory().getArmorContents()); + repairItems(user.getInventory().getArmorContents(), user, repaired); - if (itemList.length() == 0) + if (repaired.isEmpty()) { user.sendMessage(Util.format("repairNone")); } else { charge(user); - user.sendMessage(Util.format("repair", Util.joinList(itemList))); + user.sendMessage(Util.format("repair", Util.joinList(repaired))); } } @@ -72,10 +81,9 @@ public class Commandrepair extends EssentialsCommand } } - private void repairItem(ItemStack item) throws Exception + private void repairItem(final ItemStack item) throws Exception { - Material material = Material.getMaterial(item.getTypeId()); - String error = null; + final Material material = Material.getMaterial(item.getTypeId()); if (material.isBlock() || material.getMaxDurability() < 0) { throw new Exception(Util.i18n("repairInvalidType")); @@ -89,28 +97,43 @@ public class Commandrepair extends EssentialsCommand item.setDurability((short)0); } - private String repairItems(ItemStack[] items) + private void repairItems(final ItemStack[] items, final IUser user, final List<String> repaired) { - StringBuilder itemList = new StringBuilder(); for (ItemStack item : items) { + final String itemName = item.getType().toString().toLowerCase(); + final Trade charge = new Trade("repair-" + itemName.replace('_', '-'), ess); + boolean canBeRepaired = true; try { - repairItem(item); - if (itemList.length() > 0) - { - itemList.append(", "); - } - - String itemName = item.getType().toString().toLowerCase().replace('_', ' '); - itemList.append(itemName); + charge.isAffordableFor(user); } - catch (Exception e) + catch (ChargeException ex) { + canBeRepaired = false; } - } + if (canBeRepaired) + { + try + { + repairItem(item); + } + catch (Exception e) + { + continue; + } + try + { + charge.charge(user); + } + catch (ChargeException ex) + { + user.sendMessage(ex.getMessage()); + } + repaired.add(itemName.replace('_', ' ')); + } - return itemList.toString(); + } } } |