feat: code
This commit is contained in:
@@ -3,7 +3,6 @@ package online.mineroo.paper.listeners;
|
||||
|
||||
import com.destroystokyo.paper.event.player.PlayerConnectionCloseEvent;
|
||||
import io.papermc.paper.connection.PlayerConfigurationConnection;
|
||||
import io.papermc.paper.connection.PlayerGameConnection;
|
||||
import io.papermc.paper.dialog.Dialog;
|
||||
import io.papermc.paper.dialog.DialogResponseView;
|
||||
import io.papermc.paper.event.connection.configuration.AsyncPlayerConnectionConfigureEvent;
|
||||
@@ -20,10 +19,11 @@ import net.kyori.adventure.key.Key;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import online.mineroo.common.BindRequest;
|
||||
import online.mineroo.common.BindRequest.PlayerBindEnum;
|
||||
import online.mineroo.common.BindRequest.PlayerBindResponse;
|
||||
import online.mineroo.common.BindRequest.PlayerBindStatusEnum;
|
||||
import online.mineroo.paper.Config;
|
||||
import online.mineroo.paper.MinerooCore;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.jspecify.annotations.NullMarked;
|
||||
@@ -33,7 +33,8 @@ import org.slf4j.Logger;
|
||||
public class PlayerBindListener implements Listener {
|
||||
private final MinerooCore plugin;
|
||||
|
||||
private final Map<UUID, CompletableFuture<Boolean>> awaitingResponse = new ConcurrentHashMap<>();
|
||||
private final Map<UUID, CompletableFuture<PlayerBindResponse>> awaitingResponse =
|
||||
new ConcurrentHashMap<>();
|
||||
|
||||
public PlayerBindListener(MinerooCore plugin) {
|
||||
this.plugin = plugin;
|
||||
@@ -76,22 +77,31 @@ public class PlayerBindListener implements Listener {
|
||||
return;
|
||||
}
|
||||
|
||||
CompletableFuture<Boolean> dialogResponse = new CompletableFuture<>();
|
||||
dialogResponse.completeOnTimeout(false, 1, TimeUnit.MINUTES);
|
||||
CompletableFuture<PlayerBindResponse> dialogResponse = new CompletableFuture<>();
|
||||
dialogResponse.completeOnTimeout(null, 1, TimeUnit.MINUTES);
|
||||
awaitingResponse.put(uniqueId, dialogResponse);
|
||||
|
||||
Audience audience = connection.getAudience();
|
||||
audience.showDialog(dialog);
|
||||
|
||||
if (!dialogResponse.join()) {
|
||||
PlayerBindResponse bindResponse = dialogResponse.join();
|
||||
if (bindResponse.getStatus() != PlayerBindEnum.BOUND) {
|
||||
audience.closeDialog();
|
||||
connection.disconnect(Component.text("You hate Paper-chan :(", NamedTextColor.RED));
|
||||
|
||||
String statusStr = (status != null) ? status.toString() : "UNKNOWN_STATUS";
|
||||
String msg = bindResponse.getMessage();
|
||||
if (msg == null)
|
||||
msg = "Unkown";
|
||||
|
||||
String kickMessage = statusStr + " : " + msg;
|
||||
|
||||
connection.disconnect(Component.text(kickMessage, NamedTextColor.RED));
|
||||
}
|
||||
awaitingResponse.remove(uniqueId);
|
||||
|
||||
} else {
|
||||
event.getConnection().disconnect(
|
||||
Component.text("服务器内部错误,请稍后重试", NamedTextColor.RED)
|
||||
Component.text("Internal server error, please try again later.", NamedTextColor.RED)
|
||||
);
|
||||
return;
|
||||
}
|
||||
@@ -100,7 +110,7 @@ public class PlayerBindListener implements Listener {
|
||||
this.plugin.getLogger().severe("Failed to check bind status for " + uuid);
|
||||
e.printStackTrace();
|
||||
event.getConnection().disconnect(
|
||||
Component.text("服务器内部错误,请稍后重试", NamedTextColor.RED)
|
||||
Component.text("Internal server error, please try again later.", NamedTextColor.RED)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -109,8 +119,6 @@ public class PlayerBindListener implements Listener {
|
||||
void onHandleDialog(PlayerCustomClickEvent event) {
|
||||
Logger logger = plugin.getSLF4JLogger();
|
||||
|
||||
logger.info("12313213131231");
|
||||
|
||||
// Handle custom click only for configuration connection.
|
||||
if (!(event.getCommonConnection()
|
||||
instanceof PlayerConfigurationConnection configurationConnection)) {
|
||||
@@ -131,18 +139,33 @@ public class PlayerBindListener implements Listener {
|
||||
return;
|
||||
}
|
||||
|
||||
String user_email = view.getText("user_email");
|
||||
String userEmail = view.getText("user_email");
|
||||
|
||||
if (event.getCommonConnection() instanceof PlayerConfigurationConnection conn) {
|
||||
BindRequest bindRequest = plugin.getBindRequest();
|
||||
}
|
||||
String playerName = conn.getProfile().getName();
|
||||
|
||||
setConnectionJoinResult(uniqueId, true);
|
||||
bindRequest.PlayerBindRequest(userEmail, uniqueId.toString(), conn.getProfile().getName())
|
||||
.thenAccept(response -> { setConnectionJoinResult(uniqueId, response); })
|
||||
.exceptionally(ex -> {
|
||||
logger.error("Error occurred during bind request for " + playerName, ex);
|
||||
setConnectionJoinResult(
|
||||
uniqueId,
|
||||
BindRequest.createPlayerBindErrorResponse(
|
||||
"Network request failed, please contact the server administrator."
|
||||
)
|
||||
);
|
||||
return null;
|
||||
});
|
||||
;
|
||||
}
|
||||
|
||||
} else if (key.equals(Key.key("mineroo:bind_user/cancel"))) {
|
||||
// If it is the same as the agree one, set the result to true.
|
||||
|
||||
setConnectionJoinResult(uniqueId, false);
|
||||
setConnectionJoinResult(
|
||||
uniqueId, BindRequest.createPlayerBindErrorResponse("Canceled the bind requirement")
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -157,8 +180,8 @@ public class PlayerBindListener implements Listener {
|
||||
|
||||
*/
|
||||
|
||||
private void setConnectionJoinResult(UUID uniqueId, boolean value) {
|
||||
CompletableFuture<Boolean> future = awaitingResponse.get(uniqueId);
|
||||
private void setConnectionJoinResult(UUID uniqueId, PlayerBindResponse value) {
|
||||
CompletableFuture<PlayerBindResponse> future = awaitingResponse.get(uniqueId);
|
||||
|
||||
if (future != null) {
|
||||
future.complete(value);
|
||||
|
||||
Reference in New Issue
Block a user