Skip to content

Commit 5df1f43

Browse files
authored
Merge pull request #4 from ChafficPlugins/patch
Patch 1.2.1
2 parents 322a205 + 88a2127 commit 5df1f43

File tree

7 files changed

+113
-34
lines changed

7 files changed

+113
-34
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>de.chafficplugins</groupId>
88
<artifactId>MiningLevels</artifactId>
9-
<version>1.2.0</version>
9+
<version>1.2.1</version>
1010
<packaging>jar</packaging>
1111

1212
<name>MiningLevels</name>
@@ -88,7 +88,7 @@
8888
<dependency>
8989
<groupId>com.github.Chafficui</groupId>
9090
<artifactId>CrucialAPI</artifactId>
91-
<version>2.1.3</version>
91+
<version>2.1.4</version>
9292
<scope>provided</scope>
9393
</dependency>
9494
<dependency>

src/main/java/de/chafficplugins/mininglevels/api/MiningPlayer.java

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import io.github.chafficui.CrucialAPI.io.Json;
99
import org.bukkit.Bukkit;
1010
import org.bukkit.ChatColor;
11+
import org.bukkit.OfflinePlayer;
1112
import org.bukkit.boss.BarColor;
1213
import org.bukkit.entity.Player;
1314
import org.bukkit.inventory.ItemStack;
@@ -121,6 +122,13 @@ public Player getPlayer() {
121122
return Bukkit.getPlayer(uuid);
122123
}
123124

125+
/**
126+
* @return The bukkit player of the MiningPlayer.
127+
*/
128+
public OfflinePlayer getOfflinePlayer() {
129+
return Bukkit.getOfflinePlayer(uuid);
130+
}
131+
124132
/**
125133
* Calculates if the player needs to level up or not.
126134
* Sends a status message to the player.
@@ -210,25 +218,6 @@ public static void init() throws IOException {
210218
}
211219
}
212220

213-
/**
214-
* Reloads the MiningPlayers from the file FileManager.PLAYERS and adds all new players to the static ArrayList miningPlayers.
215-
* Also saves this new list to the file FileManager.PLAYERS.
216-
* @throws IOException If the file FileManager.PLAYERS is not found.
217-
*/
218-
public static void reload() throws IOException {
219-
ArrayList<MiningPlayer> mPs = Json.fromJson(FileManager.PLAYERS, new TypeToken<ArrayList<MiningPlayer>>() {
220-
}.getType());
221-
if(mPs == null) mPs = new ArrayList<>();
222-
for (MiningPlayer miningPlayer : miningPlayers) {
223-
if(!mPs.contains(miningPlayer)) {
224-
mPs.add(miningPlayer);
225-
}
226-
}
227-
228-
FileManager.saveFile(FileManager.PLAYERS, mPs);
229-
init();
230-
}
231-
232221
/**
233222
* A method to save all the MiningPlayers in the static ArrayList miningPlayers to the file FileManager.PLAYERS.
234223
* @throws IOException If the file FileManager.PLAYERS is not found.
@@ -285,6 +274,18 @@ public void showLevelProgress() {
285274
}
286275
}
287276

277+
public static List<MiningPlayer> getSortedPlayers() {
278+
List<MiningPlayer> miningPlayers = MiningPlayer.miningPlayers;
279+
miningPlayers.sort((o1, o2) -> {
280+
if(o1.getLevel().getOrdinal() == o2.getLevel().getOrdinal()) {
281+
return o2.getXp() - o1.getXp();
282+
} else {
283+
return o1.getLevel().getOrdinal() - o2.getLevel().getOrdinal();
284+
}
285+
});
286+
return miningPlayers;
287+
}
288+
288289
public void showMessage(String key, String... values) {
289290
showMessage(key, ChatColor.RESET, values);
290291
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package de.chafficplugins.mininglevels.gui.leaderboard;
2+
3+
import de.chafficplugins.mininglevels.api.MiningPlayer;
4+
import io.github.chafficui.CrucialAPI.Utils.customItems.Stack;
5+
import io.github.chafficui.CrucialAPI.Utils.player.inventory.InventoryItem;
6+
import io.github.chafficui.CrucialAPI.Utils.player.inventory.Page;
7+
import org.bukkit.Material;
8+
import org.bukkit.inventory.ItemStack;
9+
10+
import java.util.List;
11+
12+
public class LeaderboardList extends Page {
13+
14+
public LeaderboardList() {
15+
super(6, "Leaderboard", Material.WHITE_STAINED_GLASS_PANE);
16+
}
17+
18+
@Override
19+
public void populate() {
20+
List<MiningPlayer> miningPlayers = MiningPlayer.getSortedPlayers();
21+
22+
for (int i = 0; i < 53; i++) {
23+
if (i < miningPlayers.size()) {
24+
MiningPlayer miningPlayer = miningPlayers.get(i);
25+
if (miningPlayer == null) continue;
26+
ItemStack stack = Stack.getStack(miningPlayer.getUUID(), i + 1 + ". " + miningPlayer.getOfflinePlayer().getName(), List.of(
27+
"§7Level: §e" + miningPlayer.getLevel().getName(),
28+
"§7XP: §e" + miningPlayer.getXp() + " / " + miningPlayer.getLevel().getNextLevelXP()
29+
));
30+
31+
addItem(new InventoryItem(i, stack));
32+
}
33+
}
34+
}
35+
}

src/main/java/de/chafficplugins/mininglevels/listeners/MiningLevelsCommandListener.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
7070
plugin.reloadConfig();
7171
MiningLevel.reload();
7272
MiningBlock.reload();
73-
MiningPlayer.reload();
7473
try {
7574
MiningLevels.lvlUpSound = Sound.valueOf(plugin.getConfigString(LVL_UP_SOUND).toUpperCase(Locale.ROOT));
7675
} catch (NullPointerException | IllegalArgumentException e) {

src/main/java/de/chafficplugins/mininglevels/listeners/commands/LevelingCommands.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
import de.chafficplugins.mininglevels.api.MiningLevel;
44
import de.chafficplugins.mininglevels.api.MiningPlayer;
5+
import de.chafficplugins.mininglevels.gui.leaderboard.LeaderboardList;
56
import org.bukkit.Bukkit;
67
import org.bukkit.ChatColor;
78
import org.bukkit.command.CommandSender;
89
import org.bukkit.entity.Player;
910

1011
import java.util.List;
1112

13+
import static de.chafficplugins.mininglevels.api.MiningPlayer.getSortedPlayers;
1214
import static de.chafficplugins.mininglevels.utils.ConfigStrings.*;
1315
import static de.chafficplugins.mininglevels.utils.SenderUtils.sendMessage;
1416

@@ -73,21 +75,23 @@ public static void level(CommandSender sender, String[] args) {
7375
}
7476

7577
public static void leaderboard(CommandSender sender) {
76-
sendMessage(sender, LEADERBOARD_HEADER);
7778
//sort miningPlayers by level and xp
78-
List<MiningPlayer> miningPlayers = MiningPlayer.miningPlayers;
79-
miningPlayers.sort((o1, o2) -> {
80-
if(o1.getLevel().getOrdinal() == o2.getLevel().getOrdinal()) {
81-
return o2.getXp() - o1.getXp();
82-
} else {
83-
return o1.getLevel().getOrdinal() - o2.getLevel().getOrdinal();
84-
}
85-
});
79+
List<MiningPlayer> miningPlayers = getSortedPlayers();
80+
81+
if(sender instanceof Player) {
82+
Player player = (Player) sender;
83+
84+
new LeaderboardList().open(player);
85+
86+
return;
87+
}
88+
sendMessage(sender, LEADERBOARD_HEADER);
8689

8790
for(int i = 0; i < 5; i++) {
8891
if(i < miningPlayers.size()) {
8992
MiningPlayer miningPlayer = miningPlayers.get(i);
90-
sender.sendMessage(PREFIX + ChatColor.YELLOW + (i + 1) + ChatColor.RESET + ". " + ChatColor.GREEN + miningPlayer.getPlayer().getDisplayName() + ChatColor.RESET + " | " + miningPlayer.getLevel().getName() + " (" + miningPlayer.getXp() + "xp)");
93+
if(miningPlayer == null) break;
94+
sender.sendMessage(PREFIX + ChatColor.YELLOW + (i + 1) + ChatColor.RESET + ". " + ChatColor.GREEN + miningPlayer.getOfflinePlayer().getName() + ChatColor.RESET + " | " + miningPlayer.getLevel().getName() + " (" + miningPlayer.getXp() + "xp)");
9195
}
9296
}
9397
}

src/main/java/de/chafficplugins/mininglevels/placeholders/LevelPlaceholders.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22

33
import de.chafficplugins.mininglevels.MiningLevels;
44
import de.chafficplugins.mininglevels.api.MiningPlayer;
5+
import de.chafficplugins.mininglevels.listeners.commands.LevelingCommands;
56
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
67
import org.bukkit.OfflinePlayer;
78
import org.jetbrains.annotations.NotNull;
89

10+
import java.util.List;
11+
12+
import static de.chafficplugins.mininglevels.api.MiningPlayer.getSortedPlayers;
13+
914
public class LevelPlaceholders extends PlaceholderExpansion {
1015
private static final MiningLevels plugin = MiningLevels.getPlugin(MiningLevels.class);
1116

@@ -56,7 +61,42 @@ public String onRequest(OfflinePlayer player, @NotNull String identifier) {
5661
return String.valueOf(miningPlayer.getLevel().getHasteLevel());
5762
}
5863
default -> {
59-
return null;
64+
if(identifier.startsWith("rank")) {
65+
List<MiningPlayer> miningPlayers = getSortedPlayers();
66+
if(identifier.equals("rank")) {
67+
return String.valueOf(miningPlayers.indexOf(miningPlayer) + 1);
68+
} else if(identifier.split("_").length == 3) {
69+
try {
70+
int index = Integer.parseInt(identifier.split("_")[1]);
71+
if(index > 0 && index <= miningPlayers.size()) {
72+
MiningPlayer rankedPlayer = miningPlayers.get(index - 1);
73+
if(rankedPlayer == null) return "error";
74+
switch (identifier.split("_")[2]) {
75+
case "level" -> {
76+
return rankedPlayer.getLevel().getName();
77+
}
78+
case "xp" -> {
79+
return String.valueOf(rankedPlayer.getXp());
80+
}
81+
case "name" -> {
82+
return rankedPlayer.getOfflinePlayer().getName();
83+
}
84+
default -> {
85+
return "error";
86+
}
87+
}
88+
} else {
89+
return "error";
90+
}
91+
} catch (NumberFormatException e) {
92+
return "error";
93+
}
94+
} else {
95+
return null;
96+
}
97+
} else {
98+
return null;
99+
}
60100
}
61101
}
62102
}

src/main/java/de/chafficplugins/mininglevels/utils/ConfigStrings.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
public class ConfigStrings {
44
public final static int SPIGOT_ID = 100886;
55
public final static int BSTATS_ID = 14709;
6-
public final static String CRUCIAL_API_VERSION = "2.1.3";
6+
public final static String CRUCIAL_API_VERSION = "2.1.4";
77
public final static String LOCALIZED_IDENTIFIER = "mininglevels";
88

99
public static String PREFIX = "§8[§6ML§8] §r";

0 commit comments

Comments
 (0)