Skip to content

Commit f1d44f3

Browse files
committed
fix: ranking command
1 parent de93fa0 commit f1d44f3

File tree

1 file changed

+70
-10
lines changed

1 file changed

+70
-10
lines changed

src/main/java/net/azisaba/simplepoint/RankingShortcutCommand.java

Lines changed: 70 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
package net.azisaba.simplepoint;
22

3+
import org.bukkit.Bukkit;
4+
import org.bukkit.OfflinePlayer;
5+
import org.bukkit.Sound;
36
import org.bukkit.command.Command;
47
import org.bukkit.command.CommandExecutor;
58
import org.bukkit.command.CommandSender;
69
import org.bukkit.command.TabCompleter;
10+
import org.bukkit.configuration.file.FileConfiguration;
711
import org.bukkit.entity.Player;
812
import org.bukkit.util.StringUtil;
913

10-
import java.util.ArrayList;
11-
import java.util.List;
14+
import java.util.*;
1215

1316
public class RankingShortcutCommand implements CommandExecutor, TabCompleter {
1417
private final SimplePointPlugin plugin;
@@ -19,27 +22,84 @@ public RankingShortcutCommand(SimplePointPlugin plugin) {
1922

2023
@Override
2124
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
25+
if (!(sender instanceof Player)) {
26+
sender.sendMessage("§cこのコマンドはプレイヤーのみ実行可能です。");
27+
return true;
28+
}
29+
Player player = (Player) sender;
30+
2231
if (args.length < 1) {
23-
sender.sendMessage("§c使用法: /ranking <ポイント名>");
32+
player.sendMessage("§c使用法: /ranking <ポイント名>");
33+
return true;
34+
}
35+
36+
String pointId = args[0];
37+
FileConfiguration config = plugin.getPointManager().getPointConfig(pointId);
38+
String displayName = plugin.getPointManager().getDisplayName(pointId);
39+
40+
// コンフィグが存在するか、ランキングが有効かチェック
41+
if (config == null || !config.getBoolean("_settings.ranking_enabled", true)) {
42+
player.sendMessage("§c" + displayName + " §cのランキングは非公開、または存在しません。");
2443
return true;
2544
}
2645

27-
// SPPCommand の showRanking と同じロジックを実行する。
28-
// ※ SPTCommand 側の showPersonalRanking を使いたい場合はそちらに合わせます。
29-
// ここでは全員に放送される形式ではなく、実行者にだけ見せる「個人用ランキング表示」にします。
30-
if (!(sender instanceof Player)) return true;
46+
// --- ランキング集計ロジック ---
47+
Map<UUID, Integer> allScores = new HashMap<>();
48+
for (String key : config.getKeys(false)) {
49+
if (key.startsWith("_")) continue;
50+
try {
51+
UUID uuid = UUID.fromString(key);
52+
int total = config.getInt(key + ".total", 0);
53+
allScores.put(uuid, total);
54+
} catch (IllegalArgumentException ignored) {}
55+
}
56+
57+
// ソート (降順)
58+
List<Map.Entry<UUID, Integer>> sortedList = new ArrayList<>(allScores.entrySet());
59+
sortedList.sort((a, b) -> b.getValue().compareTo(a.getValue()));
60+
61+
// 自分の順位特定
62+
int myRank = -1;
63+
int myScore = 0;
64+
for (int i = 0; i < sortedList.size(); i++) {
65+
if (sortedList.get(i).getKey().equals(player.getUniqueId())) {
66+
myRank = i + 1;
67+
myScore = sortedList.get(i).getValue();
68+
break;
69+
}
70+
}
71+
72+
// 表示
73+
player.sendMessage("§8§m----------§r " + displayName + " §6§lRANKING §8§m----------");
3174

32-
// 既存の SPTCommand 内にある showPersonalRanking と同じロジックをここに書くか、
33-
// メソッドを共通化して呼び出します。
75+
for (int i = 0; i < Math.min(sortedList.size(), 7); i++) {
76+
UUID uuid = sortedList.get(i).getKey();
77+
OfflinePlayer op = Bukkit.getOfflinePlayer(uuid);
78+
String name = op.getName();
79+
int score = sortedList.get(i).getValue();
80+
81+
String color = (i == 0) ? "§e" : (i == 1) ? "§f" : (i == 2) ? "§6" : "§7";
82+
player.sendMessage(color + (i + 1) + ". §r" + (name != null ? name : "Unknown") + " §7: §b" + score + " pt");
83+
}
84+
85+
player.sendMessage("§8§m--------------------------------------");
86+
if (myRank != -1) {
87+
player.sendMessage("§fあなたの順位: §a" + myRank + "位 §7(累計: §e" + myScore + " pt§7)");
88+
} else {
89+
player.sendMessage("§fあなたの順位: §7圏外");
90+
}
91+
player.sendMessage("§8§m--------------------------------------");
92+
player.playSound(player.getLocation(), Sound.BLOCK_NOTE_BLOCK_CHIME, 1.0f, 1.0f);
3493

35-
return true; // ここには SPTCommand の showPersonalRanking の内容を移植してください
94+
return true;
3695
}
3796

3897
@Override
3998
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
4099
if (args.length == 1) {
41100
List<String> completions = new ArrayList<>();
42101
StringUtil.copyPartialMatches(args[0], plugin.getPointManager().getPointNames(), completions);
102+
Collections.sort(completions);
43103
return completions;
44104
}
45105
return new ArrayList<>();

0 commit comments

Comments
 (0)