Skip to content

Commit 0cf85ed

Browse files
committed
support for custom mob names
closes #71
1 parent 7fe0234 commit 0cf85ed

File tree

6 files changed

+110
-19
lines changed

6 files changed

+110
-19
lines changed

src/main/java/lol/hyper/toolstats/ToolStats.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public final class ToolStats extends JavaPlugin {
113113
*/
114114
public final NamespacedKey originType = new NamespacedKey(this, "origin");
115115

116-
public final int CONFIG_VERSION = 7;
116+
public final int CONFIG_VERSION = 8;
117117
public final Logger logger = this.getLogger();
118118
public final File configFile = new File(this.getDataFolder(), "config.yml");
119119

src/main/java/lol/hyper/toolstats/events/EntityDeath.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
import org.bukkit.persistence.PersistentDataContainer;
3030
import org.bukkit.persistence.PersistentDataType;
3131

32-
import java.util.*;
32+
import java.util.List;
33+
import java.util.UUID;
3334

3435
public class EntityDeath implements Listener {
3536

@@ -59,7 +60,7 @@ public void onDeath(EntityDeathEvent event) {
5960

6061
}
6162
if (toolStats.itemChecker.isValidItem(droppedItem.getType())) {
62-
ItemStack newItem = addLore(droppedItem, livingEntity.getName());
63+
ItemStack newItem = addLore(droppedItem, livingEntity);
6364
if (newItem != null) {
6465
event.getDrops().set(i, newItem);
6566
}
@@ -73,9 +74,9 @@ public void onDeath(EntityDeathEvent event) {
7374
* Adds "drop by" tag to item.
7475
*
7576
* @param oldItem The item to add lore to.
76-
* @param mob The mob or player name.
77+
* @param entity The mob dying.
7778
*/
78-
private ItemStack addLore(ItemStack oldItem, String mob) {
79+
private ItemStack addLore(ItemStack oldItem, LivingEntity entity) {
7980
ItemStack newItem = oldItem.clone();
8081
ItemMeta meta = newItem.getItemMeta();
8182
if (meta == null) {
@@ -86,7 +87,13 @@ private ItemStack addLore(ItemStack oldItem, String mob) {
8687
container.set(toolStats.originType, PersistentDataType.INTEGER, 1);
8788

8889
if (toolStats.config.getBoolean("enabled.dropped-by")) {
89-
String newLine = toolStats.configTools.formatLore("dropped-by", "{name}", mob);
90+
String mobName = toolStats.config.getString("messages.mob." + entity.getType());
91+
toolStats.logger.info("messages.mob." + entity.getType());
92+
toolStats.logger.info(mobName);
93+
if (mobName == null) {
94+
mobName = entity.getName();
95+
}
96+
String newLine = toolStats.configTools.formatLore("dropped-by", "{name}", mobName);
9097
List<String> newLore = toolStats.itemLore.addItemLore(meta, newLine);
9198
meta.setLore(newLore);
9299
}

src/main/java/lol/hyper/toolstats/tools/config/ConfigUpdater.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import lol.hyper.toolstats.ToolStats;
3838
import lol.hyper.toolstats.tools.config.versions.Version6;
3939
import lol.hyper.toolstats.tools.config.versions.Version7;
40+
import lol.hyper.toolstats.tools.config.versions.Version8;
4041

4142
public class ConfigUpdater {
4243

@@ -49,17 +50,22 @@ public ConfigUpdater(ToolStats toolStats) {
4950
public void updateConfig() {
5051
int version = toolStats.config.getInt("config-version");
5152

52-
// this will be a switch in the future
53-
// Upgrade 5 to 6
54-
if (version == 5) {
55-
Version6 version6 = new Version6(toolStats);
56-
version6.update();
57-
}
58-
59-
// Upgrade 6 to 7
60-
if (version == 6) {
61-
Version7 version7 = new Version7(toolStats);
62-
version7.update();
53+
switch(version) {
54+
case 5: {
55+
// Version 5 to 6
56+
Version6 version6 = new Version6(toolStats);
57+
version6.update();
58+
}
59+
case 6: {
60+
// Version 6 to 7
61+
Version7 version7 = new Version7(toolStats);
62+
version7.update();
63+
}
64+
case 7: {
65+
// Version 7 to 8
66+
Version8 version8 = new Version8(toolStats);
67+
version8.update();
68+
}
6369
}
6470
}
6571
}

src/main/java/lol/hyper/toolstats/tools/config/versions/Version7.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public void update() {
4343
try {
4444
toolStats.config.save("plugins" + File.separator + "ToolStats" + File.separator + "config-6.yml");
4545
} catch (IOException exception) {
46-
toolStats.logger.severe("Unable to save config-5.yml!");
46+
toolStats.logger.severe("Unable to save config-6.yml!");
4747
throw new RuntimeException(exception);
4848
}
4949

@@ -65,6 +65,6 @@ public void update() {
6565
throw new RuntimeException(exception);
6666
}
6767
toolStats.loadConfig();
68-
toolStats.logger.info("Config has been updated to version 7. A copy of version 5 has been saved as config-6.yml");
68+
toolStats.logger.info("Config has been updated to version 7. A copy of version 6 has been saved as config-6.yml");
6969
}
7070
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* This file is part of ToolStats.
3+
*
4+
* ToolStats is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* ToolStats is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with ToolStats. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package lol.hyper.toolstats.tools.config.versions;
19+
20+
import lol.hyper.toolstats.ToolStats;
21+
import org.bukkit.configuration.ConfigurationSection;
22+
23+
import java.io.File;
24+
import java.io.IOException;
25+
import java.util.ArrayList;
26+
import java.util.List;
27+
28+
public class Version8 {
29+
30+
private final ToolStats toolStats;
31+
32+
/**
33+
* Used for updating from version 7 to 8.
34+
*
35+
* @param toolStats ToolStats instance.
36+
*/
37+
public Version8(ToolStats toolStats) {
38+
this.toolStats = toolStats;
39+
}
40+
41+
/**
42+
* Perform the config update.
43+
*/
44+
public void update() {
45+
// save the old config first
46+
try {
47+
toolStats.config.save("plugins" + File.separator + "ToolStats" + File.separator + "config-7.yml");
48+
} catch (IOException exception) {
49+
toolStats.logger.severe("Unable to save config-7.yml!");
50+
throw new RuntimeException(exception);
51+
}
52+
53+
// we make this super verbose so that admins can see what's being added
54+
toolStats.logger.info("Updating config.yml to version 8.");
55+
toolStats.config.set("config-version", 8);
56+
57+
// Add example to setting mob names
58+
toolStats.logger.info("Adding example for messages.mob.ZOMBIE");
59+
toolStats.config.set("messages.mob.ZOMBIE", "Zombie");
60+
61+
List<String> mobComments = new ArrayList<>();
62+
mobComments.add("Set display name for mobs. See: https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/entity/EntityType.html");
63+
toolStats.config.setComments("messages.mob", mobComments);
64+
65+
// save the config and reload it
66+
try {
67+
toolStats.config.save("plugins" + File.separator + "ToolStats" + File.separator + "config.yml");
68+
} catch (IOException exception) {
69+
toolStats.logger.severe("Unable to save config.yml!");
70+
throw new RuntimeException(exception);
71+
}
72+
toolStats.loadConfig();
73+
toolStats.logger.info("Config has been updated to version 8. A copy of version 7 has been saved as config-7.yml");
74+
}
75+
}

src/main/resources/config.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ messages:
117117
shift-click-warning:
118118
crafting: "&cCrafting items via shift clicking does not fully apply tags to each item. This is a limitation with the Bukkit API."
119119
trading: "&cTrading items via shift clicking does not fully apply tags to each item. This is a limitation with the Bukkit API."
120+
# Set display name for mobs. See: https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/entity/EntityType.html
121+
mobs:
122+
ZOMBIE: "Zombie"
120123

121124
# Change the default formatting for dates.
122125
# See: https://www.digitalocean.com/community/tutorials/java-simpledateformat-java-date-format

0 commit comments

Comments
 (0)