Skip to content
This repository was archived by the owner on May 14, 2025. It is now read-only.

Commit 6edb95b

Browse files
fixed the playtime bug and did some code cleanup
1 parent 695c319 commit 6edb95b

File tree

3 files changed

+69
-24
lines changed

3 files changed

+69
-24
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>org.zeroBzeroT</groupId>
88
<artifactId>0b0t_PlaytimeNameColor</artifactId>
9-
<version>6.10</version>
9+
<version>6.11</version>
1010
<packaging>jar</packaging>
1111

1212
<name>PlaytimeNameColor</name>

src/main/java/org/zerobzerot/playtimenamecolor/PlaytimeNameColor.java

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.bukkit.event.player.PlayerJoinEvent;
1515
import org.bukkit.plugin.java.JavaPlugin;
1616

17+
import java.util.AbstractMap;
1718
import java.util.ArrayList;
1819
import java.util.List;
1920
import java.util.regex.Matcher;
@@ -22,16 +23,17 @@
2223
import java.util.stream.Stream;
2324

2425
public final class PlaytimeNameColor extends JavaPlugin implements Listener {
26+
public static final List<String> defaultColors = Stream.of(ChatColor.GRAY, ChatColor.WHITE, ChatColor.GREEN, ChatColor.BLUE, ChatColor.DARK_PURPLE, ChatColor.GOLD, ChatColor.RED, ChatColor.YELLOW, ChatColor.AQUA, ChatColor.DARK_RED).map(ChatColor::toString).collect(Collectors.toList());
2527
static final String pluginPrefix = ChatColor.WHITE + "<" + ChatColor.DARK_GREEN + "NC" + ChatColor.WHITE + "> " + ChatColor.RESET;
2628
static final ArrayList<String> muted = new ArrayList<>();
27-
final List<String> defaultColors = Stream.of(ChatColor.GRAY, ChatColor.WHITE, ChatColor.GREEN, ChatColor.BLUE, ChatColor.DARK_PURPLE, ChatColor.GOLD, ChatColor.RED, ChatColor.YELLOW, ChatColor.AQUA).map(ChatColor::toString).collect(Collectors.toList());
2829
// sanitize color statements [also at the start of the name string from the old config]
2930
// always uses first match group
3031
final Pattern regexPattern = Pattern.compile("^(([Â]?§[0-9abcdefklmno])+).*$");
3132
List<String> colors;
3233
int maxPlaytime;
3334
int maxJoinDate;
3435
boolean boldEnabled;
36+
int boldIndex;
3537
boolean configModified = false;
3638

3739
public static ChatColor getChatColor(String color) {
@@ -51,9 +53,10 @@ public static ChatColor getChatColor(String color) {
5153
@Override
5254
public void onEnable() {
5355
getConfig().addDefault("colors", defaultColors);
54-
getConfig().addDefault("play-time-h", 384);
55-
getConfig().addDefault("join-date-d", 365);
56+
getConfig().addDefault("play-time-h", 384 * 2);
57+
getConfig().addDefault("join-date-d", 365 * 2);
5658
getConfig().addDefault("bold-enabled", true);
59+
getConfig().addDefault("bold-index", 8);
5760

5861
getConfig().options().copyDefaults(true);
5962
saveConfig();
@@ -62,6 +65,7 @@ public void onEnable() {
6265
maxPlaytime = getConfig().getInt("play-time-h");
6366
maxJoinDate = getConfig().getInt("join-date-d");
6467
boldEnabled = getConfig().getBoolean("bold-enabled");
68+
boldIndex = getConfig().getInt("bold-index");
6569

6670
getServer().getPluginManager().registerEvents(this, this);
6771

@@ -80,7 +84,7 @@ public void onPlayerJoin(PlayerJoinEvent event) {
8084
Bukkit.getScheduler().runTaskAsynchronously(this, () -> {
8185
if (getConfig().getString(event.getPlayer().getUniqueId().toString()) != null) {
8286
// If [UUID] entry exists then use this
83-
String sanitizedColor = sanitizeAndSaveColoredName(event.getPlayer(), getConfig().getString(player.getUniqueId().toString()), false);
87+
String sanitizedColor = sanitizeColoredName(getConfig().getString(player.getUniqueId().toString()));
8488

8589
// set the name
8690
setColoredName(event.getPlayer(), sanitizedColor);
@@ -92,15 +96,17 @@ public void onPlayerJoin(PlayerJoinEvent event) {
9296
getConfig().set(event.getPlayer().getName(), null);
9397

9498
// add [UUID] entry and set color
95-
String sanitizedColor = sanitizeAndSaveColoredName(event.getPlayer(), coloredName, true);
99+
String sanitizedColor = sanitizeColoredName(coloredName);
100+
saveColoredName(event.getPlayer(), sanitizedColor);
96101

97102
// set the name
98103
setColoredName(event.getPlayer(), sanitizedColor);
99104

100105
configModified = true;
101106
} else {
102107
// set default color (first one)
103-
String sanitizedColor = sanitizeAndSaveColoredName(event.getPlayer(), colors.get(0), true);
108+
String sanitizedColor = sanitizeColoredName(colors.get(0));
109+
saveColoredName(event.getPlayer(), sanitizedColor);
104110

105111
// set the new name
106112
setColoredName(event.getPlayer(), sanitizedColor);
@@ -126,15 +132,29 @@ public boolean onCommand(CommandSender sender, Command cmd, String commandLabel,
126132
return true;
127133
}
128134

129-
if (setColorFromCommand(sender, args[1], target)) {
135+
AbstractMap.SimpleEntry<Boolean, String> colorResult = getColorFromCommand(sender, args[1], target);
136+
137+
if (colorResult.getKey()) {
138+
String sanitizedColor = colorResult.getValue();
139+
140+
saveColoredName(target, sanitizedColor);
141+
setColoredName(target, sanitizedColor);
142+
130143
sender.sendMessage(pluginPrefix + "The name color of " + target.getDisplayName() + " has been changed.");
131144
target.sendMessage(pluginPrefix + "Your name color has been changed: " + target.getDisplayName() + ".");
132145
} else {
133146
sender.sendMessage(pluginPrefix + "Incorrect color specification or insufficient playing time or joining date. Type " + ChatColor.GOLD + "/nc" + ChatColor.RESET + " for help.");
134147
}
135148
} else if (args.length == 1 && sender instanceof Player) {
136149
// Player Command
137-
if (setColorFromCommand(sender, args[0], (Player) sender)) {
150+
AbstractMap.SimpleEntry<Boolean, String> colorResult = getColorFromCommand(sender, args[0], (Player) sender);
151+
152+
if (colorResult.getKey()) {
153+
String sanitizedColor = colorResult.getValue();
154+
155+
saveColoredName((Player) sender, sanitizedColor);
156+
setColoredName((Player) sender, sanitizedColor);
157+
138158
sender.sendMessage(pluginPrefix + "Your name color has been changed: " + ((Player) sender).getDisplayName() + ".");
139159
} else {
140160
sender.sendMessage(pluginPrefix + "Incorrect color specification or insufficient playing time or joining date. Type " + ChatColor.GOLD + "/nc" + ChatColor.RESET + " for help.");
@@ -211,7 +231,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String commandLabel,
211231
return false;
212232
}
213233

214-
private int getMaximumColorIndex(Player player) {
234+
public int getMaximumColorIndex(Player player) {
215235
double playTime = getPlayTimeInHours(player);
216236
double joinDate = getJoinDateInDays(player);
217237

@@ -231,7 +251,7 @@ private double getPlayTimeInHours(Player player) {
231251
return playTimeS / (60d * 60d);
232252
}
233253

234-
private String sanitizeAndSaveColoredName(Player player, String colorString, boolean save) {
254+
private String sanitizeColoredName(String colorString) {
235255
// use sanitize regex
236256
Matcher matcher = regexPattern.matcher(colorString);
237257

@@ -244,20 +264,20 @@ private String sanitizeAndSaveColoredName(Player player, String colorString, boo
244264
colorString = colors.get(0);
245265
}
246266

247-
if (save) {
248-
getConfig().set(player.getUniqueId().toString(), colorString);
249-
}
267+
return colorString;
268+
}
250269

251-
configModified = true;
270+
private void saveColoredName(Player player, String colorString) {
271+
getConfig().set(player.getUniqueId().toString(), colorString);
252272

253-
return colorString;
273+
configModified = true;
254274
}
255275

256276
private void setColoredName(Player player, String colorString) {
257277
player.setDisplayName(colorString + player.getName() + ChatColor.RESET);
258278
}
259279

260-
private boolean setColorFromCommand(CommandSender sender, String colorString, Player target) {
280+
private AbstractMap.SimpleEntry<Boolean, String> getColorFromCommand(CommandSender sender, String colorString, Player target) {
261281
ChatColor color;
262282
boolean bold = false;
263283

@@ -271,25 +291,23 @@ private boolean setColorFromCommand(CommandSender sender, String colorString, Pl
271291
}
272292

273293
if (color == null)
274-
return false;
294+
return new AbstractMap.SimpleEntry<>(false, "");
275295

276296
if (!(sender instanceof ConsoleCommandSender) && !sender.isOp()) {
277297
int colorIndex = getChatColorIndex(color);
278298

279299
if (colorIndex < 0 || colorIndex > getMaximumColorIndex(target)) {
280-
return false;
300+
return new AbstractMap.SimpleEntry<>(false, "");
281301
}
282302

283-
if (bold && getMaximumColorIndex(target) < colors.size() - 1) {
303+
if (bold && getMaximumColorIndex(target) < boldIndex) {
284304
bold = false;
285305
}
286306
}
287307

288-
String sanitizedColor = sanitizeAndSaveColoredName(target, color.toString() + (bold ? ChatColor.BOLD : ""), true);
289-
290-
setColoredName(target, sanitizedColor);
308+
String sanitizedColor = sanitizeColoredName(color.toString() + (bold ? ChatColor.BOLD : ""));
291309

292-
return true;
310+
return new AbstractMap.SimpleEntry<>(false, sanitizedColor);
293311
}
294312

295313
private int getChatColorIndex(ChatColor color) {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.zerobzerot.playtimenamecolor;
2+
3+
import junit.framework.TestCase;
4+
5+
public class PlaytimeNameColorTest extends TestCase {
6+
7+
public void testGetMaximumColorIndex() {
8+
9+
double maxPlaytime = 384 * 2;
10+
double maxJoinDate = 365 * 2;
11+
12+
double joinDates[] = {0.001, 2.9, 3, 4.9, 5, 7.9, 8, 15.9, 16, 45.9, 46, 90.9, 91, 182.9, 183, 364.9, 365, 729, 730.1};
13+
double playTimes[] = {0.00001, 2.9, 3, 5.9, 6, 11.9, 12, 23.9, 24, 47.9, 48, 95.9, 96, 191.9, 192, 383.9, 384, 767, 768.1};
14+
15+
for (int i = 0; i < playTimes.length; i++) {
16+
double playTime = playTimes[i];
17+
double joinDate = joinDates[i];
18+
19+
int indexPlayTime = (int) Math.round(PlaytimeNameColor.defaultColors.size() - 1 - Math.log(Math.ceil(maxPlaytime / playTime)) / Math.log(2));
20+
int indexJoinDate = (int) Math.round(PlaytimeNameColor.defaultColors.size() - 1 - Math.log(Math.ceil(maxJoinDate / joinDate)) / Math.log(2));
21+
22+
System.out.println(playTime + " " + joinDate + " " + indexPlayTime + " " + indexJoinDate + " " + Math.max(0, Math.min(PlaytimeNameColor.defaultColors.size() - 1, Math.min(indexPlayTime, indexJoinDate))));
23+
}
24+
25+
// Not a real test since there is no check if the results are correct
26+
}
27+
}

0 commit comments

Comments
 (0)