From aebff1a12ad6f39e2489075ff868d4f159d888fc Mon Sep 17 00:00:00 2001 From: Sodium-Aluminate Date: Sun, 28 May 2023 17:42:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=88=E5=B9=B6=E4=B8=80=E5=88=86=E9=92=9F?= =?UTF-8?q?=E5=86=85=E7=9A=84=E8=BF=9B=E5=85=A5/=E7=A6=BB=E5=BC=80?= =?UTF-8?q?=E6=B6=88=E6=81=AF=EF=BC=8C=E5=B0=9D=E8=AF=95=E4=BF=AE=E7=90=86?= =?UTF-8?q?=E7=A6=BB=E5=BC=80=E6=97=B6=E7=BD=AE=E9=A1=B6=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E6=B7=B7=E4=B9=B1=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../de/strifel/VTools/listeners/TGBridge.java | 139 +++++++++++++++++- 1 file changed, 133 insertions(+), 6 deletions(-) diff --git a/src/main/java/de/strifel/VTools/listeners/TGBridge.java b/src/main/java/de/strifel/VTools/listeners/TGBridge.java index 666d39f..e5fada4 100644 --- a/src/main/java/de/strifel/VTools/listeners/TGBridge.java +++ b/src/main/java/de/strifel/VTools/listeners/TGBridge.java @@ -14,6 +14,7 @@ import com.pengrad.telegrambot.response.SendResponse; import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.event.connection.DisconnectEvent; import com.velocitypowered.api.event.player.ServerConnectedEvent; +import com.velocitypowered.api.event.player.ServerPostConnectEvent; import com.velocitypowered.api.event.proxy.ProxyShutdownEvent; import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.ProxyServer; @@ -227,24 +228,40 @@ public class TGBridge { public void onServerConnected(ServerConnectedEvent event) { if (event.getPreviousServer().isEmpty()) { if (!event.getPlayer().hasPermission("vtools.globalchat.bypassbridge.join")) { - outbound(String.format("%s joined the proxy", event.getPlayer().getUsername())); - updateRequests.add(new UpdateRequest()); + joinLeftAnnounce(String.format("%s joined the proxy", event.getPlayer().getUsername())); } } + updateRequests.add(new UpdateRequest()); } @Subscribe public void onDisconnect(DisconnectEvent event) { if (!event.getPlayer().hasPermission("vtools.globalchat.bypassbridge.join")) { - outbound(String.format("%s left the proxy", event.getPlayer().getUsername())); - updateRequests.add(new UpdateRequest()); + joinLeftAnnounce(String.format("%s left the proxy", event.getPlayer().getUsername())); } + updateRequests.add(new UpdateRequest(2)); } + @Subscribe + public void onServerPostConnect(ServerPostConnectEvent event){ + updateRequests.add(new UpdateRequest()); + } + + private boolean PROXY_SHUT_DOWN = false; - private static class UpdateRequest {} + private static class UpdateRequest { + int updateTimes; + + UpdateRequest() { + this(1); + } + + UpdateRequest(int updateTimes) { + this.updateTimes = updateTimes; + } + } private LinkedBlockingQueue updateRequests = new LinkedBlockingQueue<>(); @@ -253,16 +270,51 @@ public class TGBridge { while (true) { if (PROXY_SHUT_DOWN) { setOnlineStatusNotAvailable(); + return; } + int maxiterationNum = 0; UpdateRequest oldestRequest = null; try { oldestRequest = updateRequests.take(); } catch (InterruptedException ignored) {} if (oldestRequest == null) continue; - updateRequests.clear(); + maxiterationNum = Math.max(maxiterationNum, oldestRequest.updateTimes - 1); + while (!updateRequests.isEmpty()){ + try { + maxiterationNum = Math.max(maxiterationNum, updateRequests.take().updateTimes - 1); + } catch (InterruptedException ignored) {} + } + if (!updateOnlineStatus()) { updateRequests.add(oldestRequest); // 更新失败 回去吧您内 } + if(maxiterationNum>0){ + updateRequests.add(new UpdateRequest(maxiterationNum)); + } + } + }).start(); + new Thread(()->{ + while (true) { + if (PROXY_SHUT_DOWN) { + return; + } + String oldestMessage = null; + try { + oldestMessage = announceQueue.take(); + } catch (InterruptedException ignored) {} + + if(!currentAnnounce.isValid()){ + currentAnnounce = new JoinLeftAnnounceMessage(oldestMessage); + continue; + } + ArrayList messages = new ArrayList<>(announceQueue.size() + 1); + messages.add(oldestMessage); + while (!announceQueue.isEmpty()) { + try { + messages.add(announceQueue.take()); + } catch (InterruptedException ignored) {} + } + currentAnnounce.addLines(messages); } }).start(); } @@ -285,4 +337,79 @@ public class TGBridge { } catch (RuntimeException e) {return false;} return response != null && response.isOk(); } + + private class JoinLeftAnnounceMessage { + private int messageId; + private long time; + + private StringBuilder text; + + boolean isValid() { + if (messageId < 1) { + return false; + } + long dt = System.currentTimeMillis() - time; + return dt <= 60_000 && dt >= 0; + } + + protected JoinLeftAnnounceMessage(String firstMessage) { + text = new StringBuilder(firstMessage); + time = System.currentTimeMillis(); + sendAnnounceMessage(); + } + + private void sendAnnounceMessage() { + if (bot == null) { + messageId = -1; + return; + } + SendResponse response; + try { + response = bot.execute(new SendMessage(CHAT_ID, text.toString())); + + } catch (RuntimeException e) { + messageId = -1; + return; + } + if(response.isOk() == false){ + messageId = -1; + return; + } + messageId = response.message().messageId(); + } + + protected JoinLeftAnnounceMessage(){ + messageId = 0; + time = 0; + text = new StringBuilder(); + } //dummy + private void addLines(List messages) { + for (String message : messages) { + text.append('\n').append(message); + } + updateAnnounceMessage(); + } + + private void updateAnnounceMessage() { + if(!isValid()){ + plugin.logger.error("message should only push to a valid object"); + return; + } + if (bot == null) { + messageId = -1; + return; + } + try { + bot.execute(new EditMessageText(CHAT_ID, messageId, text.toString())); + } catch (RuntimeException ignored) {} + } + } + + private JoinLeftAnnounceMessage currentAnnounce = new JoinLeftAnnounceMessage(); + private LinkedBlockingQueue announceQueue = new LinkedBlockingQueue<>(); + + private void joinLeftAnnounce(String message){ + announceQueue.add(message); + } + }