feat: code
This commit is contained in:
@@ -181,7 +181,7 @@ public class BindRequest {
|
|||||||
public class PlayerBindStatusResponse implements Serializable {
|
public class PlayerBindStatusResponse implements Serializable {
|
||||||
@SerializedName("status") private PlayerBindStatusEnum status;
|
@SerializedName("status") private PlayerBindStatusEnum status;
|
||||||
|
|
||||||
@SerializedName("message") private String message;
|
@SerializedName("msg") private String message;
|
||||||
|
|
||||||
@SerializedName("cached") private boolean cached;
|
@SerializedName("cached") private boolean cached;
|
||||||
|
|
||||||
@@ -311,10 +311,10 @@ public class BindRequest {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public class PlayerBindResponse implements Serializable {
|
public static class PlayerBindResponse implements Serializable {
|
||||||
@SerializedName("status") private PlayerBindEnum status;
|
@SerializedName("status") private PlayerBindEnum status;
|
||||||
@SerializedName("timestamp") private long timestamp;
|
@SerializedName("timestamp") private long timestamp;
|
||||||
@SerializedName("message") private String message;
|
@SerializedName("msg") private String message;
|
||||||
|
|
||||||
public PlayerBindEnum getStatus() {
|
public PlayerBindEnum getStatus() {
|
||||||
return status;
|
return status;
|
||||||
@@ -344,7 +344,7 @@ public class BindRequest {
|
|||||||
@SerializedName("error") ERROR
|
@SerializedName("error") ERROR
|
||||||
}
|
}
|
||||||
|
|
||||||
private PlayerBindResponse createPlayerBindErrorResponse(String errorMessage) {
|
public static PlayerBindResponse createPlayerBindErrorResponse(String errorMessage) {
|
||||||
PlayerBindResponse errorRes = new PlayerBindResponse();
|
PlayerBindResponse errorRes = new PlayerBindResponse();
|
||||||
errorRes.setStatus(PlayerBindEnum.ERROR);
|
errorRes.setStatus(PlayerBindEnum.ERROR);
|
||||||
errorRes.setMessage(errorMessage);
|
errorRes.setMessage(errorMessage);
|
||||||
@@ -356,13 +356,13 @@ public class BindRequest {
|
|||||||
) {
|
) {
|
||||||
String path = "/server/user-bind";
|
String path = "/server/user-bind";
|
||||||
|
|
||||||
Map<String, String> params = new HashMap<>();
|
JsonObject json = new JsonObject();
|
||||||
|
|
||||||
params.put("web_email", webEmail);
|
json.addProperty("web_email", webEmail);
|
||||||
params.put("mc_uuid", playerUuid);
|
json.addProperty("mc_uuid", playerUuid);
|
||||||
params.put("mc_username", playerUsername);
|
json.addProperty("mc_username", playerUsername);
|
||||||
|
|
||||||
return networkService.getData(path, params)
|
return networkService.postData(path, json)
|
||||||
.thenApply(response -> {
|
.thenApply(response -> {
|
||||||
Gson gson = new Gson();
|
Gson gson = new Gson();
|
||||||
String responseBody = response.getBody();
|
String responseBody = response.getBody();
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package online.mineroo.paper.listeners;
|
|||||||
|
|
||||||
import com.destroystokyo.paper.event.player.PlayerConnectionCloseEvent;
|
import com.destroystokyo.paper.event.player.PlayerConnectionCloseEvent;
|
||||||
import io.papermc.paper.connection.PlayerConfigurationConnection;
|
import io.papermc.paper.connection.PlayerConfigurationConnection;
|
||||||
import io.papermc.paper.connection.PlayerGameConnection;
|
|
||||||
import io.papermc.paper.dialog.Dialog;
|
import io.papermc.paper.dialog.Dialog;
|
||||||
import io.papermc.paper.dialog.DialogResponseView;
|
import io.papermc.paper.dialog.DialogResponseView;
|
||||||
import io.papermc.paper.event.connection.configuration.AsyncPlayerConnectionConfigureEvent;
|
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.Component;
|
||||||
import net.kyori.adventure.text.format.NamedTextColor;
|
import net.kyori.adventure.text.format.NamedTextColor;
|
||||||
import online.mineroo.common.BindRequest;
|
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.common.BindRequest.PlayerBindStatusEnum;
|
||||||
import online.mineroo.paper.Config;
|
import online.mineroo.paper.Config;
|
||||||
import online.mineroo.paper.MinerooCore;
|
import online.mineroo.paper.MinerooCore;
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.jspecify.annotations.NullMarked;
|
import org.jspecify.annotations.NullMarked;
|
||||||
@@ -33,7 +33,8 @@ import org.slf4j.Logger;
|
|||||||
public class PlayerBindListener implements Listener {
|
public class PlayerBindListener implements Listener {
|
||||||
private final MinerooCore plugin;
|
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) {
|
public PlayerBindListener(MinerooCore plugin) {
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
@@ -76,22 +77,31 @@ public class PlayerBindListener implements Listener {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
CompletableFuture<Boolean> dialogResponse = new CompletableFuture<>();
|
CompletableFuture<PlayerBindResponse> dialogResponse = new CompletableFuture<>();
|
||||||
dialogResponse.completeOnTimeout(false, 1, TimeUnit.MINUTES);
|
dialogResponse.completeOnTimeout(null, 1, TimeUnit.MINUTES);
|
||||||
awaitingResponse.put(uniqueId, dialogResponse);
|
awaitingResponse.put(uniqueId, dialogResponse);
|
||||||
|
|
||||||
Audience audience = connection.getAudience();
|
Audience audience = connection.getAudience();
|
||||||
audience.showDialog(dialog);
|
audience.showDialog(dialog);
|
||||||
|
|
||||||
if (!dialogResponse.join()) {
|
PlayerBindResponse bindResponse = dialogResponse.join();
|
||||||
|
if (bindResponse.getStatus() != PlayerBindEnum.BOUND) {
|
||||||
audience.closeDialog();
|
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);
|
awaitingResponse.remove(uniqueId);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
event.getConnection().disconnect(
|
event.getConnection().disconnect(
|
||||||
Component.text("服务器内部错误,请稍后重试", NamedTextColor.RED)
|
Component.text("Internal server error, please try again later.", NamedTextColor.RED)
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -100,7 +110,7 @@ public class PlayerBindListener implements Listener {
|
|||||||
this.plugin.getLogger().severe("Failed to check bind status for " + uuid);
|
this.plugin.getLogger().severe("Failed to check bind status for " + uuid);
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
event.getConnection().disconnect(
|
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) {
|
void onHandleDialog(PlayerCustomClickEvent event) {
|
||||||
Logger logger = plugin.getSLF4JLogger();
|
Logger logger = plugin.getSLF4JLogger();
|
||||||
|
|
||||||
logger.info("12313213131231");
|
|
||||||
|
|
||||||
// Handle custom click only for configuration connection.
|
// Handle custom click only for configuration connection.
|
||||||
if (!(event.getCommonConnection()
|
if (!(event.getCommonConnection()
|
||||||
instanceof PlayerConfigurationConnection configurationConnection)) {
|
instanceof PlayerConfigurationConnection configurationConnection)) {
|
||||||
@@ -131,18 +139,33 @@ public class PlayerBindListener implements Listener {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
String user_email = view.getText("user_email");
|
String userEmail = view.getText("user_email");
|
||||||
|
|
||||||
if (event.getCommonConnection() instanceof PlayerConfigurationConnection conn) {
|
if (event.getCommonConnection() instanceof PlayerConfigurationConnection conn) {
|
||||||
BindRequest bindRequest = plugin.getBindRequest();
|
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"))) {
|
} else if (key.equals(Key.key("mineroo:bind_user/cancel"))) {
|
||||||
// If it is the same as the agree one, set the result to true.
|
// 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) {
|
private void setConnectionJoinResult(UUID uniqueId, PlayerBindResponse value) {
|
||||||
CompletableFuture<Boolean> future = awaitingResponse.get(uniqueId);
|
CompletableFuture<PlayerBindResponse> future = awaitingResponse.get(uniqueId);
|
||||||
|
|
||||||
if (future != null) {
|
if (future != null) {
|
||||||
future.complete(value);
|
future.complete(value);
|
||||||
|
|||||||
Reference in New Issue
Block a user