Skip to content

Commit

Permalink
Kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
shulng committed Aug 29, 2024
1 parent 80f5a77 commit 4d49353
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 192 deletions.
20 changes: 9 additions & 11 deletions src/main/java/cc/baka9/catseedlogin/bungee/Commands.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package cc.baka9.catseedlogin.bungee;
package cc.baka9.catseedlogin.bungee

import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.CommandSender
import net.md_5.bungee.api.plugin.Command

public class Commands extends net.md_5.bungee.api.plugin.Command {

public Commands(String name, String permission, String... aliases) {
super(name, permission, aliases);
Config.load(); // 加载配置
class Commands(name: String?, permission: String?, vararg aliases: String?) : Command(name, permission, *aliases) {
init {
Config.load() // 加载配置
}

@Override
public void execute(CommandSender commandSender, String[] args) {
if (args.length > 0 && args[0].equalsIgnoreCase("reload")) {
Config.load();
override fun execute(commandSender: CommandSender, args: Array<String>) {
if (args.isNotEmpty() && args[0].equals("reload", ignoreCase = true)) {
Config.load()
}
}
}
110 changes: 61 additions & 49 deletions src/main/java/cc/baka9/catseedlogin/bungee/Communication.kt
Original file line number Diff line number Diff line change
@@ -1,67 +1,79 @@
package cc.baka9.catseedlogin.bungee;
package cc.baka9.catseedlogin.bungee

import cc.baka9.catseedlogin.util.CommunicationAuth;
import net.md_5.bungee.api.ProxyServer;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.Socket;
import cc.baka9.catseedlogin.util.CommunicationAuth.encryption
import net.md_5.bungee.api.ProxyServer
import java.io.BufferedWriter
import java.io.IOException
import java.io.OutputStreamWriter
import java.net.Socket

/**
* bc 与 bukkit 的通讯交流
*/
public class Communication {
private static final String HOST = Config.Host;
private static final int PORT = Config.Port;
object Communication {
private val HOST: String? = Config.Host
private val PORT = Config.Port

public static int sendConnectRequest(String playerName) {
try (Socket socket = getSocket(); BufferedWriter bufferedWriter = getSocketBufferedWriter(socket)) {
bufferedWriter.write("Connect");
bufferedWriter.newLine();
bufferedWriter.write(playerName);
bufferedWriter.newLine();
bufferedWriter.flush();
return socket.getInputStream().read();
} catch (IOException e) {
handleIOException(e);
@JvmStatic
fun sendConnectRequest(playerName: String?): Int {
try {
socket.use { socket ->
getSocketBufferedWriter(socket).use { bufferedWriter ->
bufferedWriter.write("Connect")
bufferedWriter.newLine()
playerName?.let { bufferedWriter.write(it) }
bufferedWriter.newLine()
bufferedWriter.flush()
return socket.getInputStream().read()
}
}
} catch (e: IOException) {
handleIOException(e)
}
return 0;
return 0
}

public static void sendKeepLoggedInRequest(String playerName) {
try (Socket socket = getSocket(); BufferedWriter bufferedWriter = getSocketBufferedWriter(socket)) {
bufferedWriter.write("KeepLoggedIn");
bufferedWriter.newLine();
bufferedWriter.write(playerName);
bufferedWriter.newLine();
long currentTime = System.currentTimeMillis();
String time = String.valueOf(currentTime);
bufferedWriter.write(time);
bufferedWriter.newLine();
String sign = CommunicationAuth.encryption(playerName, time, Config.AuthKey);
bufferedWriter.write(sign);
bufferedWriter.newLine();
bufferedWriter.flush();
} catch (IOException e) {
handleIOException(e);
@JvmStatic
fun sendKeepLoggedInRequest(playerName: String?) {
try {
socket.use { socket ->
getSocketBufferedWriter(socket).use { bufferedWriter ->
bufferedWriter.write("KeepLoggedIn")
bufferedWriter.newLine()
playerName?.let { bufferedWriter.write(it) }
bufferedWriter.newLine()
val currentTime = System.currentTimeMillis()
val time = currentTime.toString()
bufferedWriter.write(time)
bufferedWriter.newLine()
val sign = encryption(playerName, time, Config.AuthKey)
bufferedWriter.write(sign)
bufferedWriter.newLine()
bufferedWriter.flush()
}
}
} catch (e: IOException) {
handleIOException(e)
}
}

private static Socket getSocket() throws IOException {
try {
return new Socket(HOST, PORT);
} catch (IOException e) {
ProxyServer.getInstance().getLogger().warning("§c请检查装载登录插件的子服是否在 bungeecord.yml 中开启了bungeecord功能,以及Host和Port是否与bc端的配置相同");
throw new IOException(e);
@get:Throws(IOException::class)
private val socket: Socket
get() {
try {
return Socket(HOST, PORT)
} catch (e: IOException) {
ProxyServer.getInstance().logger.warning("§c请检查装载登录插件的子服是否在 bungeecord.yml 中开启了bungeecord功能,以及Host和Port是否与bc端的配置相同")
throw IOException(e)
}
}
}

private static BufferedWriter getSocketBufferedWriter(Socket socket) throws IOException {
return new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
@Throws(IOException::class)
private fun getSocketBufferedWriter(socket: Socket): BufferedWriter {
return BufferedWriter(OutputStreamWriter(socket.getOutputStream()))
}

private static void handleIOException(IOException e) {
ProxyServer.getInstance().getLogger().severe("发生 I/O 异常: " + e.getMessage());
private fun handleIOException(e: IOException) {
ProxyServer.getInstance().logger.severe("发生 I/O 异常: " + e.message)
}
}
86 changes: 42 additions & 44 deletions src/main/java/cc/baka9/catseedlogin/bungee/Config.kt
Original file line number Diff line number Diff line change
@@ -1,57 +1,55 @@
package cc.baka9.catseedlogin.bungee;

import net.md_5.bungee.config.Configuration;
import net.md_5.bungee.config.ConfigurationProvider;
import net.md_5.bungee.config.YamlConfiguration;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.logging.Logger;

public class Config {

public static boolean Enable;
public static String Host;
public static int Port;
public static String LoginServerName;
public static String AuthKey;
private static Logger logger = PluginMain.instance.getLogger();

public static void load() {
File dataFolder = PluginMain.instance.getDataFolder();
package cc.baka9.catseedlogin.bungee

import net.md_5.bungee.config.ConfigurationProvider
import net.md_5.bungee.config.YamlConfiguration
import java.io.File
import java.io.IOException
import java.nio.file.Files
import java.util.logging.Logger

object Config {
var Enable: Boolean = false
var Host: String? = null
var Port: Int = 0
var LoginServerName: String? = null
var AuthKey: String? = null
private val logger: Logger = PluginMain.instance!!.logger

fun load() {
val dataFolder = PluginMain.instance!!.dataFolder
if (!dataFolder.exists()) {
dataFolder.mkdir();
dataFolder.mkdir()
}

String fileName = "bungeecord.yml";
File configFile = new File(dataFolder, fileName);
val fileName = "bungeecord.yml"
val configFile = File(dataFolder, fileName)
if (!configFile.exists()) {
try (InputStream in = PluginMain.instance.getResourceAsStream("bungee-resources/bungeecord.yml")) {
Files.copy(in, configFile.toPath());
} catch (IOException e) {
e.printStackTrace();
try {
PluginMain.instance!!.getResourceAsStream("bungee-resources/bungeecord.yml").use { `in` ->
Files.copy(`in`, configFile.toPath())
}
} catch (e: IOException) {
e.printStackTrace()
}
}

ConfigurationProvider configurationProvider = ConfigurationProvider.getProvider(YamlConfiguration.class);
val configurationProvider = ConfigurationProvider.getProvider(YamlConfiguration::class.java)
try {
Configuration config = configurationProvider.load(configFile);
Enable = config.getBoolean("Enable");
Host = config.getString("Host");
Port = config.getInt("Port");
LoginServerName = config.getString("LoginServerName");
AuthKey = config.getString("AuthKey");
val config = configurationProvider.load(configFile)
Enable = config.getBoolean("Enable")
Host = config.getString("Host")
Port = config.getInt("Port")
LoginServerName = config.getString("LoginServerName")
AuthKey = config.getString("AuthKey")

// 合并日志输出
StringBuilder logBuilder = new StringBuilder();
logBuilder.append("Host: ").append(Host).append("\n");
logBuilder.append("Port: ").append(Port).append("\n");
logBuilder.append("LoginServerName: ").append(LoginServerName);
logger.info(logBuilder.toString());
} catch (IOException e) {
e.printStackTrace();
val logBuilder = StringBuilder()
logBuilder.append("Host: ").append(Host).append("\n")
logBuilder.append("Port: ").append(Port).append("\n")
logBuilder.append("LoginServerName: ").append(LoginServerName)
logger.info(logBuilder.toString())
} catch (e: IOException) {
e.printStackTrace()
}
}
}
Loading

0 comments on commit 4d49353

Please sign in to comment.