Version 2
This commit is contained in:
parent
fbe10c2572
commit
f0efb7826b
2 changed files with 138 additions and 5 deletions
2
pom.xml
2
pom.xml
|
|
@ -6,7 +6,7 @@
|
|||
|
||||
<groupId>xyz.tronax</groupId>
|
||||
<artifactId>ZalmikConnectVelocity</artifactId>
|
||||
<version>1.0</version>
|
||||
<version>2.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>ZalmikConnectVelocity</name>
|
||||
|
|
|
|||
|
|
@ -1,18 +1,151 @@
|
|||
package xyz.tronax.zalmikConnectVelocity;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import com.sun.net.httpserver.HttpHandler;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import com.velocitypowered.api.event.connection.PostLoginEvent;
|
||||
import com.velocitypowered.api.event.Subscribe;
|
||||
import com.velocitypowered.api.plugin.Plugin;
|
||||
import com.velocitypowered.api.proxy.Player;
|
||||
import com.velocitypowered.api.proxy.ProxyServer;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
@Plugin(id = "zalmikconnectvelocity", name = "ZalmikConnectVelocity", version = "1.0", description = "Chest and Hotbar Items!", url = "https://tronax.xyz", authors = {"Tronax_"})
|
||||
import java.io.*;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
@Plugin(id = "zalmikconnectvelocity", name = "ZalmikConnectVelocity", version = "2.0", description = "Connect MC Server with Discord!", url = "https://tronax.xyz", authors = {"Tronax_"})
|
||||
public class ZalmikConnectVelocity {
|
||||
|
||||
@Inject
|
||||
private ProxyServer server;
|
||||
private Logger logger;
|
||||
private final Map<UUID, String> verificationCodes = new HashMap<>();
|
||||
private final Map<UUID, Boolean> verifiedPlayers = new HashMap<>();
|
||||
private static final String DATA_FILE = "verified_players.dat";
|
||||
|
||||
@Inject
|
||||
public void DiscordLinkPlugin(ProxyServer server, Logger logger) {
|
||||
this.server = server;
|
||||
this.logger = logger;
|
||||
loadVerifiedPlayers();
|
||||
startHttpServer();
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void onProxyInitialization(ProxyInitializeEvent event) {
|
||||
public void onPostLogin(PostLoginEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
if (verifiedPlayers.getOrDefault(player.getUniqueId(), false)) {
|
||||
player.sendMessage(net.kyori.adventure.text.Component.text("§e§lZalmik §r§8| §3Du bist bereits mit Discord verbunden!"));
|
||||
} else {
|
||||
String code = generateVerificationCode();
|
||||
verificationCodes.put(player.getUniqueId(), code);
|
||||
player.sendMessage(net.kyori.adventure.text.Component.text("§e§lZalmik §r§8| §3Nutze diesen Code um deinen Discord Account zu verbinden: §c" + code));
|
||||
player.sendMessage(net.kyori.adventure.text.Component.text("§e§lZalmik §r§8| §3Nutze §c/verify §3im Discord um den Vorgang abzuschließen."));
|
||||
}
|
||||
}
|
||||
|
||||
private String generateVerificationCode() {
|
||||
return UUID.randomUUID().toString().substring(0, 6); // 6-character code
|
||||
}
|
||||
|
||||
private void startHttpServer() {
|
||||
try {
|
||||
HttpServer server = HttpServer.create(new InetSocketAddress(3636), 0);
|
||||
server.createContext("/verify", new VerificationHandler());
|
||||
server.setExecutor(null); // creates a default executor
|
||||
server.start();
|
||||
logger.info("HTTP server started on port 3636");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private class VerificationHandler implements HttpHandler {
|
||||
@Override
|
||||
public void handle(HttpExchange exchange) throws IOException {
|
||||
if ("POST".equals(exchange.getRequestMethod())) {
|
||||
String query = exchange.getRequestURI().getQuery();
|
||||
Map<String, String> params = queryToMap(query);
|
||||
|
||||
String discordId = params.get("discordId");
|
||||
String code = params.get("code");
|
||||
|
||||
UUID playerId = null;
|
||||
for (Map.Entry<UUID, String> entry : verificationCodes.entrySet()) {
|
||||
if (entry.getValue().equals(code)) {
|
||||
playerId = entry.getKey();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
String response;
|
||||
if (playerId != null && verificationCodes.get(playerId).equals(code)) {
|
||||
verificationCodes.remove(playerId);
|
||||
verifiedPlayers.put(playerId, true);
|
||||
saveVerifiedPlayers();
|
||||
exchange.sendResponseHeaders(200, 0);
|
||||
response = "{\"success\": true}";
|
||||
logger.info("Player with UUID " + playerId + " successfully linked their Discord account.");
|
||||
} else {
|
||||
exchange.sendResponseHeaders(400, 0);
|
||||
response = "{\"success\": false}";
|
||||
logger.info("Failed verification attempt for Discord ID " + discordId);
|
||||
}
|
||||
|
||||
OutputStream os = exchange.getResponseBody();
|
||||
os.write(response.getBytes());
|
||||
os.close();
|
||||
} else {
|
||||
exchange.sendResponseHeaders(405, 0); // Method Not Allowed
|
||||
exchange.close();
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, String> queryToMap(String query) {
|
||||
Map<String, String> result = new HashMap<>();
|
||||
if (query != null) {
|
||||
for (String param : query.split("&")) {
|
||||
String[] entry = param.split("=");
|
||||
if (entry.length > 1) {
|
||||
result.put(entry[0], entry[1]);
|
||||
} else {
|
||||
result.put(entry[0], "");
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
private void saveVerifiedPlayers() {
|
||||
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(DATA_FILE))) {
|
||||
oos.writeObject(verifiedPlayers);
|
||||
logger.info("Verified players data saved successfully.");
|
||||
} catch (IOException e) {
|
||||
logger.error("Failed to save verified players data.", e);
|
||||
}
|
||||
}
|
||||
|
||||
private void loadVerifiedPlayers() {
|
||||
Path path = Paths.get(DATA_FILE);
|
||||
if (Files.exists(path)) {
|
||||
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(DATA_FILE))) {
|
||||
Object obj = ois.readObject();
|
||||
if (obj instanceof HashMap) {
|
||||
verifiedPlayers.putAll((HashMap<UUID, Boolean>) obj);
|
||||
logger.info("Verified players data loaded successfully.");
|
||||
}
|
||||
} catch (IOException | ClassNotFoundException e) {
|
||||
logger.error("Failed to load verified players data.", e);
|
||||
}
|
||||
} else {
|
||||
logger.info("No verified players data file found, starting fresh.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue