|
1 | 1 | package cat.nyaa.nyaautils.repair;
|
2 | 2 |
|
| 3 | +import cat.nyaa.nyaautils.I18n; |
3 | 4 | import cat.nyaa.nyaautils.NyaaUtils;
|
4 | 5 | import cat.nyaa.utils.CommandReceiver;
|
5 | 6 | import cat.nyaa.utils.Internationalization;
|
| 7 | +import cat.nyaa.utils.Message; |
6 | 8 | import org.bukkit.Material;
|
7 | 9 | import org.bukkit.command.CommandSender;
|
| 10 | +import org.bukkit.entity.Player; |
| 11 | +import org.bukkit.inventory.ItemStack; |
| 12 | +import org.bukkit.inventory.meta.Repairable; |
| 13 | + |
| 14 | +import static cat.nyaa.nyaautils.repair.RepairInstance.RepairStat.REPAIRABLE; |
| 15 | +import static cat.nyaa.nyaautils.repair.RepairInstance.RepairStat.UNREPAIRABLE; |
8 | 16 |
|
9 | 17 | public class RepairCommands extends CommandReceiver<NyaaUtils> {
|
10 | 18 | private final NyaaUtils plugin;
|
@@ -36,4 +44,68 @@ public void addRepairItem(CommandSender sender, Arguments args) {
|
36 | 44 | plugin.cfg.repair.addItem(tool, item);
|
37 | 45 | msg(sender, "user.repair.item_added");
|
38 | 46 | }
|
| 47 | + |
| 48 | + @SubCommand(value = "info", permission = "nu.repair") |
| 49 | + public void repairInfo(CommandSender sender, Arguments args) { |
| 50 | + ItemStack item = getItemInHand(sender); |
| 51 | + RepairInstance info = new RepairInstance(item, plugin.cfg.repair); |
| 52 | + new Message(I18n._("user.repair.info_1")).append(item).send(asPlayer(sender)); |
| 53 | + msg(sender, "user.repair.info_2", item.getType().name()); |
| 54 | + if (info.stat != REPAIRABLE) { |
| 55 | + msg(sender, "user.repair.info_3", I18n._("user.repair.unrepairable." + info.stat.name())); |
| 56 | + } |
| 57 | + if (info.stat == UNREPAIRABLE) return; |
| 58 | + int fullDur = item.getType().getMaxDurability(); |
| 59 | + int currDur = fullDur - item.getDurability(); |
| 60 | + msg(sender, "user.repair.info_4", currDur, fullDur, (double) currDur / (double) fullDur * 100); |
| 61 | + new Message(I18n._("user.repair.info_5")).append(new ItemStack(info.repairMaterial)).send(asPlayer(sender)); |
| 62 | + msg(sender, "user.repair.info_6", info.expConsumption); |
| 63 | + msg(sender, "user.repair.info_7", info.durRecovered, (double) info.durRecovered / (double) fullDur * 100); |
| 64 | + if (info.repairLimit <= 0) { |
| 65 | + msg(sender, "user.repair.info_8"); |
| 66 | + } else { |
| 67 | + int repairTime = ((Repairable) item.getItemMeta()).getRepairCost(); |
| 68 | + msg(sender, "user.repair.info_9", repairTime, info.repairLimit); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + @SubCommand(value = "hand", permission = "nu.repair") |
| 73 | + public void repairHand(CommandSender sender, Arguments args) { |
| 74 | + ItemStack item = getItemInHand(sender); |
| 75 | + ItemStack material = getItemInOffHand(sender); |
| 76 | + RepairInstance info = new RepairInstance(item, plugin.cfg.repair); |
| 77 | + if (info.stat != REPAIRABLE) { |
| 78 | + msg(sender, "user.repair.info_3", I18n._("user.repair.unrepairable." + info.stat.name())); |
| 79 | + return; |
| 80 | + } |
| 81 | + if (material.getType() != info.repairMaterial || material.getAmount() <= 0) { |
| 82 | + msg(sender, "user.repair.material_mismatch"); |
| 83 | + return; |
| 84 | + } |
| 85 | + Player p = asPlayer(sender); |
| 86 | + if (p.getTotalExperience() < info.expConsumption) { |
| 87 | + msg(sender, "user.repair.no_enough_exp"); |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + // TODO: elegant exp handling |
| 92 | + int tmp = p.getTotalExperience() - info.expConsumption; |
| 93 | + p.setTotalExperience(0); |
| 94 | + p.setLevel(0); |
| 95 | + p.setExp(0); |
| 96 | + p.giveExp(tmp); |
| 97 | + |
| 98 | + int dur = item.getDurability(); |
| 99 | + dur -= info.durRecovered; |
| 100 | + if (dur < 0) dur = 0; |
| 101 | + item.setDurability((short) dur); |
| 102 | + p.getInventory().setItemInMainHand(item); |
| 103 | + int count = material.getAmount(); |
| 104 | + if (count <= 1) { |
| 105 | + p.getInventory().setItemInOffHand(new ItemStack(Material.AIR)); |
| 106 | + } else { |
| 107 | + material.setAmount(count - 1); |
| 108 | + p.getInventory().setItemInOffHand(material); |
| 109 | + } |
| 110 | + } |
39 | 111 | }
|
0 commit comments