Velocity queueing solution
1/*
2 * ProxyQueues, a Velocity queueing solution
3 * Copyright (c) 2021 James Lyne
4 *
5 * Some portions of this file were taken from https://github.com/darbyjack/DeluxeQueues
6 * These portions are Copyright (c) 2019 Glare
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in all
16 * copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26package uk.co.notnull.proxyqueues.queues;
27import com.velocitypowered.api.proxy.Player;
28import net.kyori.adventure.bossbar.BossBar;
29import uk.co.notnull.proxyqueues.Messages;
30import uk.co.notnull.proxyqueues.api.QueueType;
31import uk.co.notnull.proxyqueues.api.queues.ProxyQueue;
32import uk.co.notnull.proxyqueues.api.queues.QueuePlayer;
33
34import java.time.Instant;
35import java.time.temporal.ChronoUnit;
36
37public final class QueuePlayerImpl implements QueuePlayer {
38
39 private final BossBar bossBar;
40 private boolean connecting;
41 private Player player;
42 private int position = -1;
43 private final ProxyQueue queue;
44 private final QueueType queueType;
45
46 private Instant lastConnectionAttempt;
47 private Instant lastSeen;
48 private final Instant queued;
49
50 public QueuePlayerImpl(Player player, ProxyQueue queue, QueueType queueType) {
51 this.player = player;
52 this.connecting = false;
53 this.queue = queue;
54 this.queueType = queueType;
55 this.lastConnectionAttempt = Instant.EPOCH;
56 this.lastSeen = null;
57 this.queued = Instant.now();
58
59 this.bossBar = BossBar.bossBar(Messages.getComponent("notify.joining"), 0, getBossBarColor(),
60 BossBar.Overlay.PROGRESS);
61 this.player.showBossBar(bossBar);
62 }
63
64 public void showBossBar() {
65 getPlayer().showBossBar(getBossBar());
66 }
67
68 public void hideBossBar() {
69 getPlayer().hideBossBar(getBossBar());
70 }
71
72 public Player getPlayer() {
73 return player;
74 }
75
76 public void setPlayer(Player player) {
77 this.player.hideBossBar(bossBar);
78 this.player = player;
79 this.player.showBossBar(bossBar);
80 }
81
82 public BossBar getBossBar() {
83 return bossBar;
84 }
85
86 public boolean isConnecting() {
87 return connecting;
88 }
89
90 public void setConnecting(boolean connecting) {
91 this.connecting = connecting;
92
93 if(connecting) {
94 this.lastConnectionAttempt = Instant.now();
95 }
96 }
97
98 public Instant getLastConnectionAttempt() {
99 return lastConnectionAttempt;
100 }
101
102 public int getPosition() {
103 return position;
104 }
105
106 public void setPosition(int position) {
107 this.position = position;
108 }
109
110 public QueueType getQueueType() {
111 return queueType;
112 }
113
114 public Instant getLastSeen() {
115 return lastSeen;
116 }
117
118 public void setLastSeen(Instant lastSeen) {
119 this.lastSeen = lastSeen;
120 }
121
122 public long getQueuedTime() {
123 return queued.until(Instant.now(), ChronoUnit.SECONDS);
124 }
125
126 public BossBar.Color getBossBarColor() {
127 if(queue.isPaused()) {
128 return BossBar.Color.WHITE;
129 }
130
131 return switch (queueType) {
132 case PRIORITY -> BossBar.Color.GREEN;
133 case STAFF -> BossBar.Color.BLUE;
134 default -> BossBar.Color.PURPLE;
135 };
136 }
137
138 @Override
139 public String toString() {
140 return "QueuePlayer{" +
141 "player=" + player +
142 ", queueType=" + queueType +
143 ", position=" + position +
144 ", connecting=" + connecting +
145 ", lastSeen=" + lastSeen +
146 '}';
147 }
148}