Skip to content

Commit

Permalink
调整 MiraiMC 插件接口
Browse files Browse the repository at this point in the history
  • Loading branch information
DreamVoid committed Aug 13, 2024
1 parent 7c21b92 commit 5690b8c
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* MiraiMC 生命周期
*/
public class LifeCycle {
public final class LifeCycle {
public static LifeCycle INSTANCE;
private static Platform platform;
private Logger logger;
Expand Down Expand Up @@ -72,7 +72,7 @@ public void preLoad() throws IOException {
if (PluginConfig.General.MiraiCoreVersion.equalsIgnoreCase("latest")) {
MiraiLoader.loadMiraiCore();
} else if (PluginConfig.General.MiraiCoreVersion.equalsIgnoreCase("stable")) {
MiraiLoader.loadMiraiCore(MiraiLoader.getStableVersion(getPlatform().getPluginVersion()));
MiraiLoader.loadMiraiCore(MiraiLoader.getStableVersion(platform.getPluginVersion()));
} else {
MiraiLoader.loadMiraiCore(PluginConfig.General.MiraiCoreVersion);
}
Expand Down
181 changes: 136 additions & 45 deletions MiraiMC-Base/src/main/java/me/dreamvoid/miraimc/api/MiraiMC.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package me.dreamvoid.miraimc.api;

import me.dreamvoid.miraimc.LifeCycle;
import me.dreamvoid.miraimc.Platform;
import me.dreamvoid.miraimc.internal.config.PluginConfig;
import me.dreamvoid.miraimc.internal.database.DatabaseHandler;

Expand All @@ -13,93 +15,182 @@
*/
@SuppressWarnings("unused")
public class MiraiMC {
private static final String prefix = PluginConfig.Database.Settings.Prefix;
/**
* MiraiMC 绑定管理
*/
public static class Bind {
private static final String prefix = PluginConfig.Database.Settings.Prefix;

static {
try {
DatabaseHandler.executeUpdate("CREATE TABLE IF NOT EXISTS " + prefix + "bind (uuid TINYTEXT NOT NULL, qqid long NOT NULL)");
} catch (SQLException e) {
throw new RuntimeException("处理数据时出现异常,请检查MiraiMC数据库配置是否正确", e);
}
}

/**
* 添加一个MC-QQ绑定
* 如果数据库已经有相关数据,将会直接替换
*
* @param uuid 玩家UUID
* @param account 玩家QQ号
*/
public static void addBind(UUID uuid, long account) {
try (ResultSet resultSetUUID = DatabaseHandler.executeQuery("SELECT * FROM " + prefix + "bind WHERE uuid=? LIMIT 1", uuid);
ResultSet resultSetAccount = DatabaseHandler.executeQuery("SELECT * FROM " + prefix + "bind WHERE qqid=? LIMIT 1", account)) {

if (!resultSetUUID.isBeforeFirst() && resultSetAccount.isBeforeFirst()) {
DatabaseHandler.executeUpdate("UPDATE " + prefix + "bind SET uuid=? WHERE qqid=?", uuid, account);
} else if (resultSetUUID.isBeforeFirst() && !resultSetAccount.isBeforeFirst()) {
DatabaseHandler.executeUpdate("UPDATE " + prefix + "bind SET qqid=? WHERE uuid=?", account, uuid);
} else if (!resultSetUUID.isBeforeFirst() && !resultSetAccount.isBeforeFirst()) {
DatabaseHandler.executeUpdate("INSERT INTO " + prefix + "bind VALUES(?,?)", uuid, account);
}
} catch (SQLException e) {
throw new RuntimeException("处理数据时出现异常,请检查MiraiMC数据库配置是否正确", e);
}
}

/**
* 移除一个Minecraft账号绑定的QQ
*
* @param uuid 玩家UUID
*/
public static void removeBind(UUID uuid) {
try (ResultSet resultSet = DatabaseHandler.executeQuery("SELECT * FROM " + prefix + "bind WHERE uuid=? LIMIT 1", uuid)) {
if (resultSet.next()) {
DatabaseHandler.executeUpdate("DELETE FROM " + prefix + "bind WHERE uuid=?", uuid);
}
} catch (SQLException e) {
throw new RuntimeException("处理数据时出现异常,请检查MiraiMC数据库配置是否正确", e);
}
}

/**
* 移除一个QQ账号绑定的玩家
*
* @param account 玩家QQ号
*/
public static void removeBind(long account) {
try (ResultSet resultSet = DatabaseHandler.executeQuery("SELECT * FROM " + prefix + "bind WHERE qqid=? LIMIT 1", account)) {
if (resultSet.next()) {
DatabaseHandler.executeUpdate("DELETE FROM " + prefix + "bind WHERE qqid=?", account);
}
} catch (SQLException e) {
throw new RuntimeException("处理数据时出现异常,请检查MiraiMC数据库配置是否正确", e);
}
}

/**
* 获取Minecraft账号绑定的QQ号
* 如果不存在,返回0
*
* @param uuid 玩家UUID
* @return QQ号
*/
public static long getBind(UUID uuid) {
try (ResultSet resultSet = DatabaseHandler.executeQuery("SELECT * FROM " + prefix + "bind WHERE uuid=? LIMIT 1", uuid)) {
return resultSet.next() ? resultSet.getLong("qqid") : 0L;
} catch (SQLException e) {
throw new RuntimeException("处理数据时出现异常,请检查MiraiMC数据库配置是否正确", e);
}
}

static {
try {
DatabaseHandler.executeUpdate("CREATE TABLE IF NOT EXISTS " + prefix + "bind (uuid TINYTEXT NOT NULL, qqid long NOT NULL)");
} catch (SQLException e) {
throw new RuntimeException("处理数据时出现异常,请检查MiraiMC数据库配置是否正确", e);
/**
* 获取QQ号绑定的Minecraft账号
* 此方法返回数据库记录的UUID
* 如果不存在,返回null
*
* @param account 玩家QQ号
* @return UUID
*/
@Nullable
public static UUID getBind(long account) {
try (ResultSet resultSet = DatabaseHandler.executeQuery("SELECT * FROM " + prefix + "bind WHERE qqid=? LIMIT 1", account)) {
return resultSet.next() ? UUID.fromString(resultSet.getString("uuid")) : null;
} catch (SQLException e) {
throw new RuntimeException("处理数据时出现异常,请检查MiraiMC数据库配置是否正确", e);
}
}
}

/**
* 添加一个MC-QQ绑定
* 如果数据库已经有相关数据,将会直接替换
* @param uuid 玩家UUID
*
* @param uuid 玩家UUID
* @param account 玩家QQ号
* @deprecated
*/
@Deprecated
public static void addBind(UUID uuid, long account) {
try(ResultSet resultSetUUID = DatabaseHandler.executeQuery("SELECT * FROM " + prefix + "bind WHERE uuid=? LIMIT 1", uuid);
ResultSet resultSetAccount = DatabaseHandler.executeQuery("SELECT * FROM " + prefix + "bind WHERE qqid=? LIMIT 1", account)) {

if (!resultSetUUID.isBeforeFirst() && resultSetAccount.isBeforeFirst()) {
DatabaseHandler.executeUpdate("UPDATE " + prefix + "bind SET uuid=? WHERE qqid=?", uuid, account);
} else if (resultSetUUID.isBeforeFirst() && !resultSetAccount.isBeforeFirst()) {
DatabaseHandler.executeUpdate("UPDATE " + prefix + "bind SET qqid=? WHERE uuid=?", account, uuid);
} else if (!resultSetUUID.isBeforeFirst() && !resultSetAccount.isBeforeFirst()) {
DatabaseHandler.executeUpdate("INSERT INTO " + prefix + "bind VALUES(?,?)", uuid, account);
}
} catch (SQLException e){
throw new RuntimeException("处理数据时出现异常,请检查MiraiMC数据库配置是否正确", e);
}
getPlatform().getPluginLogger().warning("正在调用一个弃用的 MiraiMC 方法,请通知开发者尽快更新插件以避免未来出现问题!");
Bind.addBind(uuid,account);
}

/**
* 移除一个Minecraft账号绑定的QQ
*
* @param uuid 玩家UUID
* @deprecated
*/
@Deprecated
public static void removeBind(UUID uuid) {
try (ResultSet resultSet = DatabaseHandler.executeQuery("SELECT * FROM " + prefix + "bind WHERE uuid=? LIMIT 1", uuid)){
if (resultSet.next()) {
DatabaseHandler.executeUpdate("DELETE FROM " + prefix + "bind WHERE uuid=?", uuid);
}
} catch (SQLException e) {
throw new RuntimeException("处理数据时出现异常,请检查MiraiMC数据库配置是否正确", e);
}
getPlatform().getPluginLogger().warning("正在调用一个弃用的 MiraiMC 方法,请通知开发者尽快更新插件以避免未来出现问题!");
Bind.removeBind(uuid);
}

/**
* 移除一个QQ账号绑定的玩家
*
* @param account 玩家QQ号
* @deprecated
*/
@Deprecated
public static void removeBind(long account) {
try (ResultSet resultSet = DatabaseHandler.executeQuery("SELECT * FROM " + prefix + "bind WHERE qqid=? LIMIT 1", account)){
if (resultSet.next()) {
DatabaseHandler.executeUpdate("DELETE FROM " + prefix + "bind WHERE qqid=?", account);
}
} catch (SQLException e) {
throw new RuntimeException("处理数据时出现异常,请检查MiraiMC数据库配置是否正确", e);
}
getPlatform().getPluginLogger().warning("正在调用一个弃用的 MiraiMC 方法,请通知开发者尽快更新插件以避免未来出现问题!");
Bind.removeBind(account);
}

/**
* 获取Minecraft账号绑定的QQ号
* 如果不存在,返回0
*
* @param uuid 玩家UUID
* @return QQ号
* @deprecated
*/
public static long getBind(UUID uuid){
try (ResultSet resultSet = DatabaseHandler.executeQuery("SELECT * FROM " + prefix + "bind WHERE uuid=? LIMIT 1", uuid)){
return resultSet.next() ? resultSet.getLong("qqid") : 0L;
} catch (SQLException e) {
throw new RuntimeException("处理数据时出现异常,请检查MiraiMC数据库配置是否正确", e);
}
@Deprecated
public static long getBind(UUID uuid) {
getPlatform().getPluginLogger().warning("正在调用一个弃用的 MiraiMC 方法,请通知开发者尽快更新插件以避免未来出现问题!");
return Bind.getBind(uuid);
}

/**
* 获取QQ号绑定的Minecraft账号
* 此方法返回数据库记录的UUID
* 如果不存在,返回null
*
* @param account 玩家QQ号
* @return UUID
* @deprecated
*/
@Deprecated
@Nullable
public static UUID getBind(long account) {
try (ResultSet resultSet = DatabaseHandler.executeQuery("SELECT * FROM " + prefix + "bind WHERE qqid=? LIMIT 1", account)){
return resultSet.next() ? UUID.fromString(resultSet.getString("uuid")) : null;
} catch (SQLException e) {
throw new RuntimeException("处理数据时出现异常,请检查MiraiMC数据库配置是否正确", e);
}
getPlatform().getPluginLogger().warning("正在调用一个弃用的 MiraiMC 方法,请通知开发者尽快更新插件以避免未来出现问题!");
return Bind.getBind(account);
}

/**
* 获取 MiraiMC 用于适配不同平台的桥接接口<br>
* 开发者可利用此接口为 MiraiMC 强关联的功能兼容不同的平台<br>
* 在 Paper 和 Folia 中非常有用
*
* @return MiraiMC 桥接接口
*/
public static Platform getPlatform() {
return LifeCycle.getPlatform();
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package me.dreamvoid.miraimc.commands;

import me.dreamvoid.miraimc.IMiraiAutoLogin;
import me.dreamvoid.miraimc.LifeCycle;
import me.dreamvoid.miraimc.api.MiraiBot;
import me.dreamvoid.miraimc.api.MiraiMC;
import me.dreamvoid.miraimc.internal.config.PluginConfig;
import net.mamoe.mirai.Bot;
import net.mamoe.mirai.utils.BotConfiguration;
Expand All @@ -15,20 +15,20 @@
import java.util.NoSuchElementException;

public class MiraiCommand implements ICommandExecutor {
private final IMiraiAutoLogin MiraiAutoLogin = LifeCycle.getPlatform().getAutoLogin();
private final IMiraiAutoLogin MiraiAutoLogin = MiraiMC.getPlatform().getAutoLogin();

@Override
public boolean onCommand(ICommandSender sender, String[] args) {
if (args.length == 0) {
sender.sendMessage("This server is running "+ LifeCycle.getPlatform().getPluginName() +" version "+ LifeCycle.getPlatform().getPluginVersion()+" by "+ LifeCycle.getPlatform().getAuthors().toString().replace("[","").replace("]",""));
sender.sendMessage("This server is running "+ MiraiMC.getPlatform().getPluginName() +" version "+ MiraiMC.getPlatform().getPluginVersion()+" by "+ MiraiMC.getPlatform().getAuthors().toString().replace("[","").replace("]",""));
return false;
}

switch (args[0].toLowerCase()){
case "login": {
if(sender.hasPermission("miraimc.command.mirai.login")){
if(args.length >= 3) {
LifeCycle.getPlatform().runTaskAsync(() -> {
MiraiMC.getPlatform().runTaskAsync(() -> {
BotConfiguration.MiraiProtocol Protocol;
if (args.length == 3) {
Protocol = BotConfiguration.MiraiProtocol.ANDROID_PHONE;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package me.dreamvoid.miraimc.commands;

import me.dreamvoid.miraimc.LifeCycle;
import me.dreamvoid.miraimc.api.MiraiMC;
import me.dreamvoid.miraimc.internal.Utils;
import me.dreamvoid.miraimc.internal.config.PluginConfig;
Expand All @@ -13,7 +12,7 @@ public class MiraiMcCommand implements ICommandExecutor {
@Override
public boolean onCommand(ICommandSender sender, String[] args) {
if (args.length == 0) {
sender.sendMessage("This server is running "+ LifeCycle.getPlatform().getPluginName() +" version "+ LifeCycle.getPlatform().getPluginVersion()+" by "+ LifeCycle.getPlatform().getAuthors().toString().replace("[","").replace("]",""));
sender.sendMessage("This server is running "+ MiraiMC.getPlatform().getPluginName() +" version "+ MiraiMC.getPlatform().getPluginVersion()+" by "+ MiraiMC.getPlatform().getAuthors().toString().replace("[","").replace("]",""));
return false;
}

Expand All @@ -36,10 +35,10 @@ public boolean onCommand(ICommandSender sender, String[] args) {
switch (args[1].toLowerCase()) {
case "add": {
if (args.length >= 4) {
LifeCycle.getPlatform().runTaskAsync(() -> {
UUID uuid = LifeCycle.getPlatform().getPlayerUUID(args[2]);
MiraiMC.getPlatform().runTaskAsync(() -> {
UUID uuid = MiraiMC.getPlatform().getPlayerUUID(args[2]);
long qqid = Long.parseLong(args[3]);
MiraiMC.addBind(uuid, qqid);
MiraiMC.Bind.addBind(uuid, qqid);
sender.sendMessage("&a已添加绑定!");
});
} else
Expand All @@ -48,9 +47,9 @@ public boolean onCommand(ICommandSender sender, String[] args) {
}
case "removeplayer": {
if (args.length >= 3) {
LifeCycle.getPlatform().runTaskAsync(() -> {
UUID uuid = LifeCycle.getPlatform().getPlayerUUID(args[2]);
MiraiMC.removeBind(uuid);
MiraiMC.getPlatform().runTaskAsync(() -> {
UUID uuid = MiraiMC.getPlatform().getPlayerUUID(args[2]);
MiraiMC.Bind.removeBind(uuid);
sender.sendMessage("&a已移除相应绑定!");
});
} else
Expand All @@ -59,9 +58,9 @@ public boolean onCommand(ICommandSender sender, String[] args) {
}
case "removeqq": {
if (args.length >= 3) {
LifeCycle.getPlatform().runTaskAsync(() -> {
MiraiMC.getPlatform().runTaskAsync(() -> {
long qqid = Long.parseLong(args[2]);
MiraiMC.removeBind(qqid);
MiraiMC.Bind.removeBind(qqid);
sender.sendMessage("&a已移除相应绑定!");
});
} else
Expand All @@ -70,9 +69,9 @@ public boolean onCommand(ICommandSender sender, String[] args) {
}
case "getplayer": {
if (args.length >= 3) {
LifeCycle.getPlatform().runTaskAsync(() -> {
UUID uuid = LifeCycle.getPlatform().getPlayerUUID(args[2]);
long qqId = MiraiMC.getBind(uuid);
MiraiMC.getPlatform().runTaskAsync(() -> {
UUID uuid = MiraiMC.getPlatform().getPlayerUUID(args[2]);
long qqId = MiraiMC.Bind.getBind(uuid);
if (qqId != 0) {
sender.sendMessage("&a绑定的QQ号:" + qqId);
} else
Expand All @@ -84,11 +83,11 @@ public boolean onCommand(ICommandSender sender, String[] args) {
}
case "getqq": {
if (args.length >= 3) {
LifeCycle.getPlatform().runTaskAsync(() -> {
MiraiMC.getPlatform().runTaskAsync(() -> {
long qqid = Long.parseLong(args[2]);
UUID uuid = MiraiMC.getBind(qqid);
UUID uuid = MiraiMC.Bind.getBind(qqid);
if (uuid != null) {
sender.sendMessage("&a绑定的玩家名:" + LifeCycle.getPlatform().getPlayerName(uuid));
sender.sendMessage("&a绑定的玩家名:" + MiraiMC.getPlatform().getPlayerName(uuid));
} else
sender.sendMessage("&c未找到符合条件的记录!");
});
Expand Down
Loading

0 comments on commit 5690b8c

Please sign in to comment.