Skip to content

Commit 2437424

Browse files
committed
added reset lore command
1 parent 63b0806 commit 2437424

File tree

1 file changed

+176
-4
lines changed

1 file changed

+176
-4
lines changed

src/main/java/lol/hyper/toolstats/commands/CommandToolStats.java

Lines changed: 176 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,36 +18,208 @@
1818
package lol.hyper.toolstats.commands;
1919

2020
import lol.hyper.toolstats.ToolStats;
21-
import org.bukkit.ChatColor;
21+
import lol.hyper.toolstats.UUIDDataType;
22+
import org.bukkit.*;
2223
import org.bukkit.command.Command;
2324
import org.bukkit.command.CommandExecutor;
2425
import org.bukkit.command.CommandSender;
26+
import org.bukkit.entity.Player;
27+
import org.bukkit.inventory.ItemStack;
28+
import org.bukkit.inventory.meta.ItemMeta;
29+
import org.bukkit.persistence.PersistentDataContainer;
30+
import org.bukkit.persistence.PersistentDataType;
2531
import org.jetbrains.annotations.NotNull;
2632

33+
import java.text.SimpleDateFormat;
34+
import java.util.*;
35+
2736
public class CommandToolStats implements CommandExecutor {
2837

2938
private final ToolStats toolStats;
3039

3140
public CommandToolStats(ToolStats toolStats) {
3241
this.toolStats = toolStats;
3342
}
43+
private final SimpleDateFormat format = new SimpleDateFormat("M/dd/yyyy", Locale.ENGLISH);
3444

3545
@Override
3646
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
3747
if (args.length == 0) {
3848
sender.sendMessage(ChatColor.GREEN + "ToolStats version " + toolStats.getDescription().getVersion() + ". Created by hyperdefined.");
39-
} else if (args.length == 1) {
40-
if (args[0].equalsIgnoreCase("reload")) {
49+
}
50+
switch (args[0]) {
51+
case "reload": {
4152
if (sender.isOp() || sender.hasPermission("toolstats.reload")) {
4253
toolStats.loadConfig();
4354
sender.sendMessage(ChatColor.GREEN + "Configuration reloaded!");
4455
} else {
4556
sender.sendMessage(ChatColor.RED + "You do not have permission for this command.");
4657
}
47-
} else {
58+
return true;
59+
}
60+
case "reset": {
61+
if (args.length == 2 && args[1].equalsIgnoreCase("confirm")) {
62+
Player player = (Player) sender;
63+
ItemStack heldItem = player.getInventory().getItemInMainHand();
64+
if (heldItem.getType() == Material.AIR) {
65+
sender.sendMessage(ChatColor.RED + "You must hold an item!");
66+
return true;
67+
}
68+
fixItemLore(heldItem, player);
69+
sender.sendMessage(ChatColor.GREEN + "The lore was reset!");
70+
return true;
71+
}
72+
sender.sendMessage(ChatColor.GREEN + "This will remove ALL current lore from the held item and replace it with the correct lore.");
73+
sender.sendMessage(ChatColor.GREEN + "The item owner will be who ever is currently running this command.");
74+
sender.sendMessage(ChatColor.GREEN + "Only use this if the tags on the tool are incorrect.");
75+
sender.sendMessage(ChatColor.GREEN + "Type /toolstats reset confirm to confirm this.");
76+
return true;
77+
}
78+
default: {
4879
sender.sendMessage(ChatColor.RED + "Invalid sub-command.");
4980
}
5081
}
5182
return true;
5283
}
84+
85+
private ItemStack fixItemLore(ItemStack original, Player player) {
86+
ItemStack finalItem = original.clone();
87+
ItemMeta finalMeta = finalItem.getItemMeta();
88+
if (finalMeta == null) {
89+
return null;
90+
}
91+
PersistentDataContainer container = finalMeta.getPersistentDataContainer();
92+
List<String> lore = new ArrayList<>();
93+
94+
String caughtByLore = toolStats.getLoreFromConfig("fished.caught-by", false);
95+
String lootedByLore = toolStats.getLoreFromConfig("looted.found-by", false);
96+
String tradedByLore = toolStats.getLoreFromConfig("traded.traded-by", false);
97+
98+
if (caughtByLore == null || lootedByLore == null || tradedByLore == null) {
99+
return null;
100+
}
101+
102+
String type = "DEFAULT";
103+
if (finalMeta.hasLore()) {
104+
if (finalMeta.getLore() != null) {
105+
for (String line : finalMeta.getLore()) {
106+
if (line.contains(caughtByLore)) {
107+
type = "CAUGHT";
108+
}
109+
if (line.contains(lootedByLore)) {
110+
type = "LOOTED";
111+
}
112+
if (line.contains(tradedByLore)) {
113+
type = "TRADED";
114+
}
115+
}
116+
}
117+
}
118+
if (toolStats.checkConfig(original, "created-by")) {
119+
if (container.has(toolStats.genericOwner, new UUIDDataType())) {
120+
container.set(toolStats.genericOwner, new UUIDDataType(), player.getUniqueId());
121+
switch (type) {
122+
case "DEFAULT": {
123+
lore.add(toolStats.getLoreFromConfig("created.created-by", true).replace("{player}", player.getName()));
124+
break;
125+
}
126+
case "CAUGHT": {
127+
lore.add(toolStats.getLoreFromConfig("fished.caught-by", true).replace("{player}", player.getName()));
128+
break;
129+
}
130+
case "LOOTED": {
131+
lore.add(toolStats.getLoreFromConfig("looted.looted-by", true).replace("{player}", player.getName()));
132+
break;
133+
}
134+
case "TRADED": {
135+
lore.add(toolStats.getLoreFromConfig("traded.traded-by", true).replace("{player}", player.getName()));
136+
break;
137+
}
138+
}
139+
}
140+
}
141+
if (toolStats.checkConfig(original, "created-date")) {
142+
if (container.has(toolStats.timeCreated, PersistentDataType.LONG)) {
143+
Long time = container.get(toolStats.timeCreated, PersistentDataType.LONG);
144+
if (time == null) {
145+
return null;
146+
}
147+
switch (type) {
148+
case "DEFAULT": {
149+
lore.add(toolStats.getLoreFromConfig("created.created-by", true).replace("{date}", format.format(new Date(time))));
150+
break;
151+
}
152+
case "CAUGHT": {
153+
lore.add(toolStats.getLoreFromConfig("fished.caught-on", true).replace("{date}", format.format(new Date(time))));
154+
break;
155+
}
156+
case "LOOTED": {
157+
lore.add(toolStats.getLoreFromConfig("looted.looted-on", true).replace("{date}", format.format(new Date(time))));
158+
break;
159+
}
160+
case "TRADED": {
161+
lore.add(toolStats.getLoreFromConfig("traded.traded-on", true).replace("{date}", format.format(new Date(time))));
162+
break;
163+
}
164+
}
165+
}
166+
}
167+
if (toolStats.checkConfig(original, "player-kills")) {
168+
if (container.has(toolStats.swordPlayerKills, PersistentDataType.INTEGER)) {
169+
Integer kills = container.get(toolStats.swordPlayerKills, PersistentDataType.INTEGER);
170+
if (kills == null) {
171+
return null;
172+
}
173+
lore.add(toolStats.getLoreFromConfig("kills.player", true).replace("{kills}", Integer.toString(kills)));
174+
}
175+
}
176+
if (toolStats.checkConfig(original, "mob-kills")) {
177+
if (container.has(toolStats.swordMobKills, PersistentDataType.INTEGER)) {
178+
Integer kills = container.get(toolStats.swordMobKills, PersistentDataType.INTEGER);
179+
if (kills == null) {
180+
return null;
181+
}
182+
lore.add(toolStats.getLoreFromConfig("kills.mob", true).replace("{kills}", Integer.toString(kills)));
183+
}
184+
}
185+
if (toolStats.checkConfig(original, "blocks-mined")) {
186+
if (container.has(toolStats.genericMined, PersistentDataType.INTEGER)) {
187+
Integer blocksMined = container.get(toolStats.genericMined, PersistentDataType.INTEGER);
188+
if (blocksMined == null) {
189+
return null;
190+
}
191+
lore.add(toolStats.getLoreFromConfig("blocks-mined", true).replace("{blocks}", Integer.toString(blocksMined)));
192+
}
193+
}
194+
if (toolStats.config.getBoolean("enabled.fish-caught")) {
195+
if (container.has(toolStats.fishingRodCaught, PersistentDataType.INTEGER)) {
196+
Integer fish = container.get(toolStats.fishingRodCaught, PersistentDataType.INTEGER);
197+
if (fish == null) {
198+
return null;
199+
}
200+
lore.add(toolStats.getLoreFromConfig("fished.fish-caught", true).replace("{fish}", Integer.toString(fish)));
201+
}
202+
}
203+
if (toolStats.config.getBoolean("enabled.sheep-sheared")) {
204+
if (container.has(toolStats.shearsSheared, PersistentDataType.INTEGER)) {
205+
Integer sheep = container.get(toolStats.shearsSheared, PersistentDataType.INTEGER);
206+
if (sheep == null) {
207+
return null;
208+
}
209+
lore.add(toolStats.getLoreFromConfig("sheep-sheared", true).replace("{sheep}", Integer.toString(sheep)));
210+
}
211+
}
212+
if (toolStats.config.getBoolean("enabled.armor-damage")) {
213+
if (container.has(toolStats.armorDamage, PersistentDataType.INTEGER)) {
214+
Integer damage = container.get(toolStats.armorDamage, PersistentDataType.INTEGER);
215+
if (damage == null) {
216+
return null;
217+
}
218+
lore.add(toolStats.getLoreFromConfig("damage-taken", true).replace("{damage}", Integer.toString(damage)));
219+
}
220+
}
221+
finalMeta.setLore(lore);
222+
finalItem.setItemMeta(finalMeta);
223+
return finalItem;
224+
}
53225
}

0 commit comments

Comments
 (0)