From c3fd05053cba1cb08efad6c803bfd13e87f91abf Mon Sep 17 00:00:00 2001 From: bierdosenhalter <529651+bierdosenhalter@users.noreply.github.com> Date: Tue, 19 Nov 2024 09:08:28 +0100 Subject: [PATCH] semaphore replaced by reentrant lock, removed lock being removed twice and lock aquiring before try blocks --- gradle.properties | 2 +- .../org/zeroBzeroT/anarchyqueue/Queue.java | 95 +++++++++---------- 2 files changed, 47 insertions(+), 50 deletions(-) diff --git a/gradle.properties b/gradle.properties index dc21adf..eff5db4 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,3 @@ -plugin_version=3.0.6-INDEV +plugin_version=3.0.7-INDEV velocity_api_version=3.3.0-SNAPSHOT minecraft_version=1.20.6 diff --git a/src/main/java/org/zeroBzeroT/anarchyqueue/Queue.java b/src/main/java/org/zeroBzeroT/anarchyqueue/Queue.java index 89e209a..55c2153 100644 --- a/src/main/java/org/zeroBzeroT/anarchyqueue/Queue.java +++ b/src/main/java/org/zeroBzeroT/anarchyqueue/Queue.java @@ -14,7 +14,7 @@ import java.time.Instant; import java.util.*; import java.util.concurrent.ExecutionException; -import java.util.concurrent.Semaphore; +import java.util.concurrent.locks.ReentrantLock; import static org.zeroBzeroT.anarchyqueue.Components.mm; @@ -28,7 +28,7 @@ public class Queue { private final ProxyServer proxyServer; - private final Semaphore mutex = new Semaphore(1); + private final ReentrantLock lock = new ReentrantLock(); private final Deque playerQueue; @@ -79,14 +79,14 @@ public void onServerConnected(ServerConnectedEvent event) { return; // Add Player to queue + lock.lock(); try { - mutex.acquire(); playerQueue.add(event.getPlayer()); log.info(mm("" + event.getPlayer().getUsername() + " was added to the queue. Queue count is " + playerQueue.size() + ".")); - } catch (InterruptedException e1) { - e1.printStackTrace(); + } catch (Exception e) { + log.error(e.getMessage()); } finally { - mutex.release(); + lock.unlock(); } } @@ -97,16 +97,16 @@ public void onServerConnected(ServerConnectedEvent event) { public void onKickedFromServer(KickedFromServerEvent event) { if (event.getServer().getServerInfo().getName().equals(Config.target)) { // kick from target server + lock.lock(); try { - mutex.acquire(); Player player = event.getPlayer(); Component reason = event.getServerKickReason().isPresent() ? event.getServerKickReason().get() : mm("Kicked without a reason."); KickOrRequeue(player, reason); - } catch (InterruptedException e1) { - e1.printStackTrace(); + } catch (Exception e) { + log.error(e.getMessage()); } finally { - mutex.release(); + lock.unlock(); } } } @@ -168,9 +168,8 @@ public void flushQueue() { // TODO: full notification return; + lock.lock(); try { - mutex.acquire(); - Player currPlayer = null; // try to find the first player that got not kicked recently @@ -183,46 +182,44 @@ public void flushQueue() { } // no player to connect to the main server - if (currPlayer == null) { - mutex.release(); - return; - } - - // connect next player - UUID uuid = currPlayer.getUniqueId(); - Player finalCurrPlayer = currPlayer; - - serverQueue.getPlayersConnected().stream() - .filter(p -> p.getUniqueId().equals(uuid)) - .findAny().ifPresentOrElse(p -> { - p.sendMessage(mm(Config.messageConnecting)); - try { - var conReq = p.createConnectionRequest(serverMain).connect().get(); - - if (conReq.isSuccessful()) { - playerQueue.removeFirst(); - // Clear the title - if (Config.sendTitle) { - sendTitle(finalCurrPlayer, mm(" "), mm(" "), 0, 0, 0); + if (currPlayer != null) { + + // connect next player + UUID uuid = currPlayer.getUniqueId(); + Player finalCurrPlayer = currPlayer; + + serverQueue.getPlayersConnected().stream() + .filter(p -> p.getUniqueId().equals(uuid)) + .findAny().ifPresentOrElse(p -> { + p.sendMessage(mm(Config.messageConnecting)); + try { + var conReq = p.createConnectionRequest(serverMain).connect().get(); + + if (conReq.isSuccessful()) { + playerQueue.removeFirst(); + // Clear the title + if (Config.sendTitle) { + sendTitle(finalCurrPlayer, mm(" "), mm(" "), 0, 0, 0); + } + log.info(mm("" + p.getUsername() + " connected to server " + serverMain.getServerInfo().getName() + ". Queue count is " + serverQueue.getPlayersConnected().size() + ". Main count is " + (serverMain.getPlayersConnected().size()) + " of " + Config.maxPlayers + ".")); + } else { + var reason = conReq.getReasonComponent().isPresent() ? conReq.getReasonComponent().get() : mm("Connection to the main server failed."); + KickOrRequeue(finalCurrPlayer, reason); } - log.info(mm("" + p.getUsername() + " connected to server " + serverMain.getServerInfo().getName() + ". Queue count is " + serverQueue.getPlayersConnected().size() + ". Main count is " + (serverMain.getPlayersConnected().size()) + " of " + Config.maxPlayers + ".")); - } else { - var reason = conReq.getReasonComponent().isPresent() ? conReq.getReasonComponent().get() : mm("Connection to the main server failed."); - KickOrRequeue(finalCurrPlayer, reason); + } catch (InterruptedException | ExecutionException e) { + log.error(mm("" + p.getUsername() + "s connection to server " + Config.target + " failed with an exception: " + e.getMessage())); + // server down? + kickedPlayers.put(finalCurrPlayer, Instant.now().getEpochSecond()); } - } catch (InterruptedException | ExecutionException e) { - log.error(mm("" + p.getUsername() + "s connection to server " + Config.target + " failed with an exception: " + e.getMessage())); - // server down? - kickedPlayers.put(finalCurrPlayer, Instant.now().getEpochSecond()); - } - }, - // player is in the queue, but not connected to the queue server - playerQueue::removeFirst - ); - } catch (InterruptedException e) { - e.printStackTrace(); + }, + // player is in the queue, but not connected to the queue server + playerQueue::removeFirst + ); + } + } catch (Exception e) { + log.error(e.getMessage()); } finally { - mutex.release(); + lock.unlock(); } }