Skip to content

Commit 1db3733

Browse files
committed
ランクカラー実装
1 parent cbb1a83 commit 1db3733

File tree

3 files changed

+38
-14
lines changed

3 files changed

+38
-14
lines changed

src/main/java/com/github/elic0de/thejpspit/spigot/leveler/Level.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
package com.github.elic0de.thejpspit.spigot.leveler;
22

3+
import org.bukkit.ChatColor;
4+
35
public class Level {
46

57
private final int lev;
68
private final int neededXP;
79

8-
public Level(int lev, int neededXP) {
10+
private final ChatColor levelColor;
11+
12+
public Level(int lev, int neededXP, ChatColor color) {
913
this.lev = lev;
1014
this.neededXP = neededXP;
15+
this.levelColor = color;
1116
}
1217

1318
public int nextLevel() {
@@ -25,4 +30,8 @@ public int getLevel() {
2530
public int getNeededXP() {
2631
return neededXP;
2732
}
33+
34+
public ChatColor getLevelColor() {
35+
return levelColor;
36+
}
2837
}

src/main/java/com/github/elic0de/thejpspit/spigot/leveler/Levels.java

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,31 @@
44
import java.util.ArrayList;
55
import java.util.Arrays;
66
import java.util.List;
7+
import org.bukkit.ChatColor;
78

89
public class Levels {
910

1011
private final static List<Level> LEVELS = new ArrayList<>(10);
1112

1213
static {
1314
initialize(
14-
"1,15",
15-
"10,30",
16-
"20,50",
17-
"30,75",
18-
"40,125",
19-
"50,250",
20-
"60,600",
21-
"70,800",
22-
"80,900",
23-
"90,1000"
15+
"1,15," + ChatColor.GRAY.name(),
16+
"10,30," + ChatColor.BLUE.name(),
17+
"20,50," + ChatColor.DARK_AQUA.name(),
18+
"30,75," + ChatColor.DARK_GREEN.name(),
19+
"40,125," + ChatColor.GREEN.name(),
20+
"50,250," + ChatColor.YELLOW.name(),
21+
"60,600," + ChatColor.GOLD.name(),
22+
"70,800," + ChatColor.RED.name(),
23+
"80,900," + ChatColor.DARK_RED.name(),
24+
"90,1000," + ChatColor.AQUA.name()
2425
);
2526
}
2627

2728
private static void initialize(String... texts) {
2829
Arrays.stream(texts)
2930
.map(text -> text.split(","))
30-
.map(data -> new Level(Integer.parseInt(data[0]), Integer.parseInt(data[1])))
31+
.map(data -> new Level(Integer.parseInt(data[0]), Integer.parseInt(data[1]), ChatColor.valueOf(data[2])))
3132
.forEach(LEVELS::add);
3233

3334
// TODO
@@ -82,4 +83,16 @@ public static int getLevelNeededXP(PitPlayer player) {
8283
}
8384
return 0;
8485
}
86+
87+
public static ChatColor getPlayerLevelColor(PitPlayer player) {
88+
final List<Integer> requirements = LEVELS.stream().map(Level::getNeededXP)
89+
.toList();
90+
int maxLevel = requirements.size();
91+
for (int i = 0; i < maxLevel; i++) {
92+
if (player.getXp() < requirements.get(i)) {
93+
return LEVELS.get(i).getLevelColor();
94+
}
95+
}
96+
return LEVELS.get(maxLevel).getLevelColor();
97+
}
8598
}

src/main/java/com/github/elic0de/thejpspit/spigot/player/PitPlayer.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,10 @@ private void updateXpBar() {
161161
}
162162

163163
public void updateDisplayName() {
164-
player.setDisplayName("[" + Levels.getPlayerLevel(this) + "]" + " " + getName());
165-
player.setPlayerListName("[" + Levels.getPlayerLevel(this) + "]" + " " + getName());
164+
final int level = Levels.getPlayerLevel(this);
165+
final ChatColor color = Levels.getPlayerLevelColor(this);
166+
player.setDisplayName("[" + color + level + ChatColor.RESET + "]" + " " + getName());
167+
player.setPlayerListName("[" + color + Levels.getPlayerLevel(this) + ChatColor.RESET + "]" + " " + getName());
166168
}
167169

168170
public Player getPlayer() {

0 commit comments

Comments
 (0)