Velocity queueing solution
at master 179 lines 8.2 kB view raw
1/* 2 * ProxyQueues, a Velocity queueing solution 3 * 4 * Copyright (c) 2022 James Lyne 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a copy 7 * of this software and associated documentation files (the "Software"), to deal 8 * in the Software without restriction, including without limitation the rights 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 * copies of the Software, and to permit persons to whom the Software is 11 * furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in all 14 * copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 */ 23 24package uk.co.notnull.proxyqueues.queues; 25 26import com.velocitypowered.api.plugin.PluginContainer; 27import com.velocitypowered.api.proxy.InboundConnection; 28import net.kyori.adventure.title.Title; 29import org.jetbrains.annotations.Nullable; 30import uk.co.notnull.platformdetection.PlatformDetectionVelocity; 31import uk.co.notnull.proxyqueues.Messages; 32import uk.co.notnull.proxyqueues.ProxyQueuesImpl; 33import uk.co.notnull.proxyqueues.api.MessageType; 34import uk.co.notnull.proxyqueues.api.queues.QueuePlayer; 35import uk.co.notnull.proxyqueues.configuration.sections.ConfigOptions; 36 37import java.util.Collections; 38import java.util.Map; 39import java.util.Optional; 40import java.util.stream.Collectors; 41 42public class ProxyQueueNotifier { 43 44 private final ProxyQueuesImpl plugin; 45 private final ProxyQueueImpl queue; 46 private final String notifyMethod; 47 private final boolean platformDetectionEnabled; 48 private PlatformDetectionVelocity platformDetection; 49 50 public ProxyQueueNotifier(ProxyQueuesImpl plugin, ProxyQueueImpl queue) { 51 this.plugin = plugin; 52 this.queue = queue; 53 54 notifyMethod = plugin.getSettingsHandler().getSettingsManager().getProperty(ConfigOptions.INFORM_METHOD); 55 56 Optional<PluginContainer> platformDetection = plugin.getProxyServer().getPluginManager() 57 .getPlugin("platform-detection"); 58 platformDetectionEnabled = platformDetection.isPresent(); 59 60 if(platformDetectionEnabled) { 61 this.platformDetection = (PlatformDetectionVelocity) platformDetection.orElseThrow().getInstance() 62 .orElseThrow(); 63 } 64 } 65 66 /** 67 * Notify the player that they are in the queue 68 * @param player the player to check 69 */ 70 public void notifyPlayer(QueuePlayer player) { 71 StringBuilder key = switch (player.getQueueType()) { 72 case STAFF -> new StringBuilder("notify.staff"); 73 case PRIORITY -> new StringBuilder("notify.priority"); 74 default -> new StringBuilder("notify.normal"); 75 }; 76 77 switch (notifyMethod.toLowerCase()) { 78 case "bossbar" -> updateBossBar(player); 79 case "actionbar" -> { 80 key.append(".actionbar").append(queue.isPaused() ? ".paused" : ".active"); 81 player.getPlayer().sendActionBar(Messages.getComponent(key.toString(), Map.of( 82 "server", queue.getServer().getServerInfo().getName(), 83 "pos", String.valueOf(player.getPosition()), 84 "size", String.valueOf(queue.getQueueSize(player.getQueueType())) 85 ), Collections.emptyMap())); 86 } 87 case "text" -> { 88 key.append(".chat").append(queue.isPaused() ? ".paused" : ".active"); 89 player.getPlayer().sendMessage(Messages.getComponent(key.toString(), Map.of( 90 "server", queue.getServer().getServerInfo().getName(), 91 "pos", String.valueOf(player.getPosition()), 92 "size", String.valueOf(queue.getQueueSize(player.getQueueType())) 93 ), Collections.emptyMap())); 94 } 95 case "title" -> { 96 key.append(".title").append(queue.isPaused() ? ".paused" : ".active"); 97 player.getPlayer().showTitle( 98 Title.title(Messages.getComponent(key + ".title"), 99 Messages.getComponent(key + ".subtitle", Map.of( 100 "server", queue.getServer().getServerInfo().getName(), 101 "pos", String.valueOf(player.getPosition()), 102 "size", String.valueOf(queue.getQueueSize(player.getQueueType())) 103 ), Collections.emptyMap()))); 104 } 105 } 106 } 107 108 private void updateBossBar(QueuePlayer player) { 109 int position = player.getPosition(); 110 String key = switch (player.getQueueType()) { 111 case STAFF -> queue.isPaused() ? "notify.staff.bossbar.paused" : "notify.staff.bossbar.active"; 112 case PRIORITY -> queue.isPaused() ? "notify.priority.bossbar.paused" : "notify.priority.bossbar.active"; 113 default -> queue.isPaused() ? "notify.normal.bossbar.paused" : "notify.normal.bossbar.active"; 114 }; 115 116 //if(platformDetectionEnabled && platformDetection.getPlatform(player.getPlayer()).isBedrock()) { 117 player.hideBossBar(); 118 //} 119 120 player.showBossBar(); 121 player.getBossBar().name( 122 Messages.getComponent(key, Map.of( 123 "server", queue.getServer().getServerInfo().getName(), 124 "pos", String.valueOf(position), 125 "size", String.valueOf(queue.getQueueSize(player.getQueueType()))), 126 Collections.emptyMap())).color(player.getBossBarColor()); 127 } 128 129 public String getNotifyMethod() { 130 return notifyMethod; 131 } 132 133 public void notifyPause() { 134 notifyPause(null); 135 } 136 137 public void notifyPause(@Nullable QueuePlayer player) { 138 Map<PluginContainer, String> pauses = queue.getPauses(); 139 String reasons = pauses.values().stream() 140 .map(r -> Messages.get("notify.pause-reason", Collections.singletonMap("reason", r))) 141 .collect(Collectors.joining("\n")); 142 143 String message = Messages.getPrefixed("notify.paused", MessageType.WARNING, 144 Map.of( 145 "server", queue.getServer().getServerInfo().getName(), 146 "reasons", reasons)); 147 148 if(player != null) { 149 plugin.sendMessage(player.getPlayer(), message); 150 } else { 151 notifyAll(message); 152 } 153 } 154 155 public void notifyResume() { 156 notifyResume(null); 157 } 158 159 public void notifyResume(@Nullable QueuePlayer player) { 160 String message = Messages.getPrefixed( 161 "notify.unpaused", MessageType.INFO, 162 Collections.singletonMap("server", queue.getServer().getServerInfo().getName())); 163 164 if(player != null) { 165 plugin.sendMessage(player.getPlayer(), message); 166 } else { 167 notifyAll(message); 168 } 169 } 170 171 private void notifyAll(String message) { 172 queue.getQueue().stream().map(QueuePlayerImpl::getPlayer).filter(InboundConnection::isActive) 173 .forEach(p -> plugin.sendMessage(p, message)); 174 queue.getPriorityQueue().stream().map(QueuePlayerImpl::getPlayer).filter(InboundConnection::isActive) 175 .forEach(p -> plugin.sendMessage(p, message)); 176 queue.getStaffQueue().stream().map(QueuePlayerImpl::getPlayer).filter(InboundConnection::isActive) 177 .forEach(p -> plugin.sendMessage(p, message)); 178 } 179}