Расположение предметов в инвентаре

Akultist

Активный участник
Пользователь
Баллы
46
Имя в Minecraft
Name
Здравствуйте, у меня есть список с играми. (Мапа). В этих играх есть методы получения количества людей в игре. Мне нужно расположить предметы в инвентаре что-бы количество предмета была от большого к меньшему по количеству игроков в игре. То есть: https://prnt.sc/jtji50
И вот ещё кодом:

Код:
Inventory inv = Bukkit.createInventory(this, 54, ""); //Инвентарь
Map<String, Game> games = GameManager.getAllGame(); //Мапа с играми (String - индентификатор игры, Game - сама игра)
for (Game game: games.values()) {
    game.getInGame().size(); //Метод получения игроков (количества)
}
Сорри, но по другому не могу объяснить, попытайтесь понять 😁
Желательно все кодом.
 
PHP:
Map<Game, Integer> gamesMap = games.entrySet().stream().collect(Collectors.toMap(entry -> entry.getValue(), entry -> entry.getValue().getInGame().size()));
TreeSet<Game> gamesSet = new TreeSet<>(gamesMap.entrySet().stream().sorted(Map.Entry.<Game, Integer>comparingByValue()).map(entry -> entry.getKey()).collect(Collectors.toSet()));
gamesSet - твой отсортированный по кол-ву игроков сет с играми.
 
PHP:
Map<Game, Integer> gamesMap = games.entrySet().stream().collect(Collectors.toMap(entry -> entry.getValue(), entry -> entry.getValue().getInGame().size()));
TreeSet<Game> gamesSet = new TreeSet<>(gamesMap.entrySet().stream().sorted(Map.Entry.<Game, Integer>comparingByValue()).map(entry -> entry.getKey()).collect(Collectors.toSet()));
gamesSet - твой отсортированный по кол-ву игроков сет с играми.
Код:
[12:10:54 WARN]: [RFLGmq] Task #440 for RFLGmq v2.9 generated an exception
java.lang.ClassCastException: null
P.S - юзаю это в bukkitrunnable
 
Весь код покажи, я вроде ничего нигде не кастил.
Код:
public static void reload() {
        new BukkitRunnable() {

            @Override
            public void run() {
                Map<String, Game> games = GameManager.getAllGame();
                Map<Game, Integer> gamesMap = games.entrySet().stream().collect(Collectors.toMap(entry -> entry.getValue(), entry -> entry.getValue().getInGame().size()));
                TreeSet<Game> gamesSet = new TreeSet<>(gamesMap.entrySet().stream().sorted(Map.Entry.<Game, Integer>comparingByValue()).map(entry -> entry.getKey()).collect(Collectors.toSet()));
            }

        }.runTaskTimer(RFL.getPlugin(RFL.class), 0, 20);
    }
 
PHP:
Map<Game, Integer> gamesMap = games.entrySet().stream().collect(Collectors.toMap(entry -> entry.getValue(), entry -> entry.getValue().getInGame().size()));
SortedSet<Game> gamesSet = new TreeSet<>();
gamesMap.entrySet().stream().sorted(Map.Entry.<Game, Integer>comparingByValue()).forEach(entry -> gamesSet.add(entry.getKey()));
Попробуй так
 
PHP:
Map<Game, Integer> gamesMap = games.entrySet().stream().collect(Collectors.toMap(entry -> entry.getValue(), entry -> entry.getValue().getInGame().size()));
SortedSet<Game> gamesSet = new TreeSet<>();
gamesMap.entrySet().stream().sorted(Map.Entry.<Game, Integer>comparingByValue()).forEach(entry -> gamesSet.add(entry.getKey()));
Попробуй так
Такая же ошибка
 
Походу у тебя что-то null. Возможно games.
Нет, он не нулл, если я сделаю цикл который будет выводит количество всех игроков в играх которые в списке games все будет работать. При добавлении этого кода выходит ошибка.
 
Не знаю в чём была причина, но я изменил SortedSet на LinkedList и всё заработало
 
Не знаю в чём была причина, но я изменил SortedSet на LinkedList и всё заработало
Скорее из-за сортировки, для сортировки в TreeSet нужно писать свой Comparable/Comparator, возможно, я ошибаюсь
 
Назад
Верх