1
0
Fork 0
forked from mc/VTools

合并一分钟内的进入/离开消息,尝试修理离开时置顶消息混乱的问题

This commit is contained in:
Sodium-Aluminate 2023-05-28 17:42:51 +08:00
parent a57cd87fff
commit aebff1a12a

View file

@ -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<UpdateRequest> 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<String> 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<String> 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<String> announceQueue = new LinkedBlockingQueue<>();
private void joinLeftAnnounce(String message){
announceQueue.add(message);
}
}