1
0
Fork 0
forked from mc/VTools

master #1

Merged
NaAlOH4 merged 3 commits from mc/VTools:master into master 2023-10-28 15:23:28 +08:00
Showing only changes of commit d1f9dc803d - Show all commits

View file

@ -30,9 +30,7 @@ import java.io.File;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.ArrayList; import java.util.*;
import java.util.List;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -164,7 +162,12 @@ public class TGBridge {
out.add(String.format(fmt, server.getAllPlayers().size())); out.add(String.format(fmt, server.getAllPlayers().size()));
List<RegisteredServer> registeredServers = new ArrayList<>(server.getAllServers()); List<RegisteredServer> registeredServers = new ArrayList<>(server.getAllServers());
for (RegisteredServer registeredServer : registeredServers) { for (RegisteredServer registeredServer : registeredServers) {
List<Player> onServer = ImmutableList.copyOf(registeredServer.getPlayersConnected()); LinkedList<Player> onServer = new LinkedList<>();
for (Player player:registeredServer.getPlayersConnected()){
if(!lastDisconnect.equals(player.getUsername())){
onServer.add(player);
}
}
if (!onServer.isEmpty()) { if (!onServer.isEmpty()) {
out.add(String.format("[%s] (%d): %s", out.add(String.format("[%s] (%d): %s",
registeredServer.getServerInfo().getName(), registeredServer.getServerInfo().getName(),
@ -228,20 +231,30 @@ public class TGBridge {
public void onServerConnected(ServerConnectedEvent event) { public void onServerConnected(ServerConnectedEvent event) {
if (event.getPreviousServer().isEmpty()) { if (event.getPreviousServer().isEmpty()) {
if (!event.getPlayer().hasPermission("vtools.globalchat.bypassbridge.join")) { if (!event.getPlayer().hasPermission("vtools.globalchat.bypassbridge.join")) {
joinLeftAnnounce(String.format("%s joined the proxy", event.getPlayer().getUsername())); String username = event.getPlayer().getUsername();
if(lastDisconnect.equals(username)){
lastDisconnect="";
}
joinLeftAnnounce(String.format("%s joined the proxy", username));
} }
} }
updateRequests.add(new UpdateRequest()); updateRequests.add(new UpdateRequest());
} }
private String lastDisconnect = "";
@Subscribe @Subscribe
public void onDisconnect(DisconnectEvent event) { public void onDisconnect(DisconnectEvent event) {
if (!event.getPlayer().hasPermission("vtools.globalchat.bypassbridge.join")) { if (!event.getPlayer().hasPermission("vtools.globalchat.bypassbridge.join")) {
joinLeftAnnounce(String.format("%s left the proxy", event.getPlayer().getUsername())); String username = event.getPlayer().getUsername();
if(username!=null) {
lastDisconnect = username;
}
joinLeftAnnounce(String.format("%s left the proxy", username));
} }
updateRequests.add(new UpdateRequest(2)); updateRequests.add(new UpdateRequest());
} }
@Subscribe @Subscribe
public void onServerPostConnect(ServerPostConnectEvent event){ public void onServerPostConnect(ServerPostConnectEvent event){
updateRequests.add(new UpdateRequest()); updateRequests.add(new UpdateRequest());
@ -251,17 +264,7 @@ public class TGBridge {
private boolean PROXY_SHUT_DOWN = false; 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<>(); private LinkedBlockingQueue<UpdateRequest> updateRequests = new LinkedBlockingQueue<>();
@ -272,25 +275,22 @@ public class TGBridge {
setOnlineStatusNotAvailable(); setOnlineStatusNotAvailable();
return; return;
} }
int maxiterationNum = 0;
UpdateRequest oldestRequest = null; UpdateRequest oldestRequest = null;
try { try {
oldestRequest = updateRequests.take(); oldestRequest = updateRequests.take();
} catch (InterruptedException ignored) {} } catch (InterruptedException ignored) {}
if (oldestRequest == null) continue; if (oldestRequest == null) {
maxiterationNum = Math.max(maxiterationNum, oldestRequest.updateTimes - 1); plugin.logger.warn("updateRequests.take() return a null value, why?");
while (!updateRequests.isEmpty()){
try { try {
maxiterationNum = Math.max(maxiterationNum, updateRequests.take().updateTimes - 1); Thread.sleep(1000);
} catch (InterruptedException ignored) {} } catch (InterruptedException ignored) {}
continue;
} }
updateRequests.clear();
if (!updateOnlineStatus()) { if (!updateOnlineStatus()) {
updateRequests.add(oldestRequest); // 更新失败 回去吧您内 updateRequests.add(oldestRequest); // 更新失败 回去吧您内
} }
if(maxiterationNum>0){
updateRequests.add(new UpdateRequest(maxiterationNum));
}
} }
}).start(); }).start();
new Thread(()->{ new Thread(()->{