|
| 1 | +package com.sqcred.sboards; |
| 2 | + |
| 3 | +import net.kyori.adventure.text.Component; |
| 4 | + |
| 5 | +import net.minestom.server.entity.Player; |
| 6 | +import net.minestom.server.scoreboard.Sidebar; |
| 7 | + |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Map; |
| 11 | +import java.util.function.Function; |
| 12 | + |
| 13 | +/* |
| 14 | + MIT License |
| 15 | +
|
| 16 | + Copyright (c) 2022 sqcred |
| 17 | +
|
| 18 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 19 | + of this software and associated documentation files (the "Software"), to deal |
| 20 | + in the Software without restriction, including without limitation the rights |
| 21 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 22 | + copies of the Software, and to permit persons to whom the Software is |
| 23 | + furnished to do so, subject to the following conditions: |
| 24 | +
|
| 25 | + The above copyright notice and this permission notice shall be included in all |
| 26 | + copies or substantial portions of the Software. |
| 27 | +
|
| 28 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 29 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 30 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 31 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 32 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 33 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 34 | + SOFTWARE. |
| 35 | + */ |
| 36 | + |
| 37 | +public class SBoard { |
| 38 | + |
| 39 | + private static final int MAX_LINES = 15; |
| 40 | + |
| 41 | + private Map<Player, Sidebar> boards = new HashMap<>(); |
| 42 | + |
| 43 | + private final Function<Player, Component> title; |
| 44 | + private final Function<Player, List<Component>> lines; |
| 45 | + |
| 46 | + public SBoard(Function<Player, Component> title, Function<Player, List<Component>> lines) { |
| 47 | + this.title = title; |
| 48 | + this.lines = lines; |
| 49 | + } |
| 50 | + |
| 51 | + public void addPlayer(Player player){ |
| 52 | + if(boards.containsKey(player)) return; |
| 53 | + Sidebar sidebar = new Sidebar(title.apply(player)); |
| 54 | + List<Component> lineList = lines.apply(player); |
| 55 | + int lineNameInt = 0; |
| 56 | + int lineInt = lineList.size(); |
| 57 | + for (Component component : lineList) { |
| 58 | + if(lineInt == 0 || lineList.size() > MAX_LINES) { |
| 59 | + throw new SBoardMaxLinesException(); |
| 60 | + } |
| 61 | + Sidebar.ScoreboardLine scoreboardLine = new Sidebar.ScoreboardLine("line" + lineNameInt, component, lineInt); |
| 62 | + lineNameInt++; |
| 63 | + lineInt--; |
| 64 | + sidebar.createLine(scoreboardLine); |
| 65 | + } |
| 66 | + sidebar.addViewer(player); |
| 67 | + boards.put(player, sidebar); |
| 68 | + } |
| 69 | + |
| 70 | + public void removePlayer(Player player){ |
| 71 | + if(boards.containsKey(player)){ |
| 72 | + Sidebar sidebar = boards.get(player); |
| 73 | + sidebar.removeViewer(player); |
| 74 | + boards.remove(player); |
| 75 | + return; |
| 76 | + } |
| 77 | + throw new SBoardNotFoundException(player); |
| 78 | + } |
| 79 | + |
| 80 | + public void removeAll(){ |
| 81 | + for (Player player : boards.keySet()){ |
| 82 | + removePlayer(player); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + public void update(Player player){ |
| 87 | + if(boards.containsKey(player)){ |
| 88 | + Sidebar sidebar = boards.get(player); |
| 89 | + sidebar.setTitle(title.apply(player)); |
| 90 | + List<Component> linesList = lines.apply(player); |
| 91 | + for(Sidebar.ScoreboardLine line : sidebar.getLines()){ |
| 92 | + int number = Integer.parseInt(line.getId().split("line")[1]); |
| 93 | + sidebar.updateLineContent(line.getId(), linesList.get(number)); |
| 94 | + } |
| 95 | + return; |
| 96 | + } |
| 97 | + throw new SBoardNotFoundException(player); |
| 98 | + } |
| 99 | + |
| 100 | + public void updateAll(){ |
| 101 | + for(Player player : boards.keySet()){ |
| 102 | + update(player); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + public static class SBoardNotFoundException extends RuntimeException { |
| 107 | + |
| 108 | + public SBoardNotFoundException(Player player) { |
| 109 | + super("SBoard for player: " + player.getUsername() + " not found!"); |
| 110 | + } |
| 111 | + |
| 112 | + } |
| 113 | + |
| 114 | + public static class SBoardMaxLinesException extends RuntimeException { |
| 115 | + |
| 116 | + public SBoardMaxLinesException() { |
| 117 | + super("15 lines is the max."); |
| 118 | + } |
| 119 | + |
| 120 | + } |
| 121 | + |
| 122 | +} |
0 commit comments