Skip to content
This repository was archived by the owner on Mar 8, 2022. It is now read-only.

Commit 146d8a1

Browse files
committed
repair full
1 parent c4dcb80 commit 146d8a1

File tree

2 files changed

+71
-6
lines changed

2 files changed

+71
-6
lines changed

src/main/java/cat/nyaa/nyaautils/repair/RepairCommands.java

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ public void repairInfo(CommandSender sender, Arguments args) {
6767
int repairTime = ((Repairable) item.getItemMeta()).getRepairCost();
6868
msg(sender, "user.repair.info_9", repairTime, info.repairLimit);
6969
}
70+
if (info.stat == REPAIRABLE) {
71+
msg(sender, "user.repair.info_10", (int)Math.ceil(item.getDurability()/(double)info.durRecovered));
72+
}
7073
}
7174

7275
@SubCommand(value = "hand", permission = "nu.repair")
@@ -88,12 +91,7 @@ public void repairHand(CommandSender sender, Arguments args) {
8891
return;
8992
}
9093

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);
94+
addPlayerExperience(p, -info.expConsumption);
9795

9896
int dur = item.getDurability();
9997
dur -= info.durRecovered;
@@ -107,5 +105,70 @@ public void repairHand(CommandSender sender, Arguments args) {
107105
material.setAmount(count - 1);
108106
p.getInventory().setItemInOffHand(material);
109107
}
108+
msg(p, "user.repair.repaired");
109+
}
110+
111+
@SubCommand(value = "full", permission = "nu.repair")
112+
public void repairFull(CommandSender sender, Arguments args) {
113+
ItemStack item = getItemInHand(sender);
114+
ItemStack material = getItemInOffHand(sender);
115+
RepairInstance info = new RepairInstance(item, plugin.cfg.repair);
116+
if (info.stat != REPAIRABLE) {
117+
msg(sender, "user.repair.info_3", I18n._("user.repair.unrepairable." + info.stat.name()));
118+
return;
119+
}
120+
if (material.getType() != info.repairMaterial || material.getAmount() <= 0) {
121+
msg(sender, "user.repair.material_mismatch");
122+
return;
123+
}
124+
Player p = asPlayer(sender);
125+
126+
int expMax = (int)Math.floor(p.getTotalExperience()/(double)info.expConsumption);
127+
int materialMax = material.getAmount();
128+
int durMax = (int)Math.ceil(item.getDurability()/(double)info.durRecovered);
129+
int repairAmount = Math.min(Math.min(expMax, materialMax), durMax);
130+
131+
addPlayerExperience(p, -info.expConsumption*repairAmount);
132+
int dur = item.getDurability();
133+
dur -= info.durRecovered*repairAmount;
134+
if (dur < 0) dur = 0;
135+
item.setDurability((short) dur);
136+
p.getInventory().setItemInMainHand(item);
137+
int count = material.getAmount() - repairAmount;
138+
if (count <= 0) {
139+
p.getInventory().setItemInOffHand(new ItemStack(Material.AIR));
140+
} else {
141+
material.setAmount(count);
142+
p.getInventory().setItemInOffHand(material);
143+
}
144+
msg(p, "user.repair.repaired");
145+
}
146+
147+
public static void addPlayerExperience(Player p, int exp) {
148+
if (exp > 0) {
149+
p.giveExp(exp);
150+
} else if (exp < 0) {
151+
exp = -exp;
152+
int totalExp = p.getTotalExperience();
153+
if (totalExp < exp) throw new IllegalArgumentException("Negative Exp Left");
154+
totalExp -= exp;
155+
int currentLevelExp = (int)(p.getExpToLevel() * p.getExp());
156+
while(exp > 0) {
157+
if (currentLevelExp <= 0) {
158+
if (p.getLevel() <= 0) return;
159+
p.setLevel(p.getLevel()-1);
160+
currentLevelExp = p.getExpToLevel();
161+
}
162+
if (exp > currentLevelExp) {
163+
exp -= currentLevelExp;
164+
currentLevelExp = 0;
165+
} else {
166+
currentLevelExp -= exp;
167+
exp = 0;
168+
}
169+
}
170+
p.setExp(currentLevelExp/(float)p.getExpToLevel());
171+
p.setTotalExperience(totalExp);
172+
}
110173
}
111174
}

src/main/resources/lang/en_US.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,14 @@ user:
175175
info_7: "Durability gain: %d (%.01f%%) (per material)"
176176
info_8: "Repair limit: unlimited"
177177
info_9: "Repair limit: %d/%d"
178+
info_10: "Full repair material cost: %d units"
178179
unrepairable:
179180
UNREPAIRABLE: "Item unrepairable."
180181
UNREPAIRABLE_REPAIRED: "Already fully repaired."
181182
UNREPAIRABLE_UNBREAKABLE: "Item is unbreakable."
182183
UNREPAIRABLE_RLE: "Repair count limit exceeded."
183184
UNREPAIRABLE_LOWRECOVER: "Durability not recoverable."
185+
repaired: "You've repaired your item."
184186

185187
manual:
186188
no_description: "No description"

0 commit comments

Comments
 (0)