|
| 1 | +package me.iwareq.scoreboard; |
| 2 | + |
| 3 | +import cn.nukkit.Player; |
| 4 | +import lombok.Getter; |
| 5 | +import lombok.ToString; |
| 6 | +import me.iwareq.scoreboard.line.ScoreboardLine; |
| 7 | +import me.iwareq.scoreboard.manager.ScoreboardManager; |
| 8 | +import me.iwareq.scoreboard.packet.RemoveObjectivePacket; |
| 9 | +import me.iwareq.scoreboard.packet.SetDisplayObjectivePacket; |
| 10 | +import me.iwareq.scoreboard.packet.SetScorePacket; |
| 11 | +import me.iwareq.scoreboard.packet.data.DisplaySlot; |
| 12 | +import me.iwareq.scoreboard.packet.data.ScorerInfo; |
| 13 | +import me.iwareq.scoreboard.packet.data.SortOrder; |
| 14 | + |
| 15 | +import java.util.HashMap; |
| 16 | +import java.util.HashSet; |
| 17 | +import java.util.Map; |
| 18 | +import java.util.Set; |
| 19 | +import java.util.function.BiConsumer; |
| 20 | + |
| 21 | +@ToString |
| 22 | +public class Scoreboard { |
| 23 | + |
| 24 | + @Getter |
| 25 | + private final Set<Player> viewers = new HashSet<>(); |
| 26 | + |
| 27 | + private final String displayName; |
| 28 | + |
| 29 | + private final Map<Integer, ScoreboardLine> lines = new HashMap<>(); |
| 30 | + |
| 31 | + private final BiConsumer<Scoreboard, Player> callback; |
| 32 | + |
| 33 | + private final ScoreboardManager manager; |
| 34 | + @Getter |
| 35 | + private final int updateTime; |
| 36 | + |
| 37 | + private int lastIndex; |
| 38 | + |
| 39 | + public Scoreboard(String displayName, BiConsumer<Scoreboard, Player> callback, int updateTime) { |
| 40 | + this.displayName = displayName; |
| 41 | + this.callback = callback; |
| 42 | + this.updateTime = updateTime; |
| 43 | + this.manager = ScoreboardAPI.getInstance().getScoreboardManager(); |
| 44 | + } |
| 45 | + |
| 46 | + public void setLine(int index, String text) { |
| 47 | + this.checkLineIndex(index); |
| 48 | + |
| 49 | + this.lastIndex = index; |
| 50 | + |
| 51 | + ScoreboardLine line = new ScoreboardLine(this, text); |
| 52 | + this.lines.put(index, line); |
| 53 | + } |
| 54 | + |
| 55 | + public void addLine(String text) { |
| 56 | + this.lastIndex++; |
| 57 | + |
| 58 | + this.setLine(this.lastIndex, text); |
| 59 | + } |
| 60 | + |
| 61 | + private void checkLineIndex(int index) { |
| 62 | + if (index < 1 || index > 15) { |
| 63 | + throw new IllegalArgumentException("The line index value should be from 1 to 15"); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + public void refresh() { |
| 68 | + this.viewers.removeIf(p -> { |
| 69 | + boolean remove = !p.isConnected() || !p.isOnline(); |
| 70 | + if (remove) { |
| 71 | + this.manager.removeScoreboard(p); |
| 72 | + } |
| 73 | + |
| 74 | + return remove; |
| 75 | + }); |
| 76 | + |
| 77 | + this.viewers.forEach(player -> { |
| 78 | + this.hide(player, false); |
| 79 | + this.show(player, false); |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + public void show(Player player) { |
| 84 | + this.show(player, true); |
| 85 | + } |
| 86 | + |
| 87 | + private void show(Player player, boolean add) { |
| 88 | + if (!add || this.viewers.add(player)) { |
| 89 | + this.callback.accept(this, player); |
| 90 | + |
| 91 | + SetDisplayObjectivePacket objectivePacket = new SetDisplayObjectivePacket(); |
| 92 | + objectivePacket.setDisplaySlot(DisplaySlot.SIDEBAR); |
| 93 | + objectivePacket.setObjectiveId("objective"); |
| 94 | + objectivePacket.setDisplayName(this.displayName); |
| 95 | + objectivePacket.setCriteria("dummy"); |
| 96 | + objectivePacket.setSortOrder(SortOrder.ASCENDING); |
| 97 | + |
| 98 | + player.dataPacket(objectivePacket); |
| 99 | + |
| 100 | + SetScorePacket scorePacket = new SetScorePacket(SetScorePacket.Action.SET); |
| 101 | + this.lines.forEach((index, line) -> |
| 102 | + scorePacket.getInfos().add(new ScorerInfo(index, "objective", index, line.getText()))); |
| 103 | + |
| 104 | + player.dataPacket(scorePacket); |
| 105 | + |
| 106 | + if (add) { |
| 107 | + this.manager.addScoreboard(player, this); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + public void hide(Player player) { |
| 113 | + this.hide(player, true); |
| 114 | + } |
| 115 | + |
| 116 | + private void hide(Player player, boolean remove) { |
| 117 | + if (!remove || this.viewers.remove(player)) { |
| 118 | + RemoveObjectivePacket packet = new RemoveObjectivePacket(); |
| 119 | + packet.setObjectiveId("objective"); |
| 120 | + |
| 121 | + player.dataPacket(packet); |
| 122 | + |
| 123 | + if (remove) { |
| 124 | + this.manager.removeScoreboard(player); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments