Skip to content

Commit

Permalink
添加 AutoWL 功能 #14
Browse files Browse the repository at this point in the history
  • Loading branch information
ApliNi committed Apr 7, 2024
1 parent 03c70e1 commit 0086a45
Show file tree
Hide file tree
Showing 13 changed files with 173 additions and 623 deletions.
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,7 @@ sqlite:
# 连接到其他插件
hook:
# 离线登录插件
AuthMe: true
AuthMe: true # 离线登录插件
# 白名单配置
Expand Down Expand Up @@ -270,6 +269,20 @@ whitelist:
preventNameDuplication: true
preventNameDuplicationMsg: '§6IpacEL §f> §b存在名称重复的玩家, 请联系管理员检查'
# 根据玩家登录来源自动添加到白名单, 这仅适用于新玩家
AutoWL:
enable: false
# 通过 Geyser 插件检查, 如果没有安装此插件, 则设置为 NOT
onGeyserPlayer: WHITE # 将所有新的基岩版玩家添加到白名单
# 通过 MojangApi **粗略**地检查是否为正版账户 https://api.mojang.com/users/profiles/minecraft/<playerName>
onPremiumPlayer: WHITE # 将所有新的正版账户添加到白名单
onPremiumPlayerErrMsg: '§6IpacEL §f> §b无法连接身份验证服务器, 请稍后重试'
# 不属于以上任何一类玩家
onOtherPlayer: NOT # 不处理其他玩家
# 以下根据玩家当前的类型对配置分类
Expand Down Expand Up @@ -464,6 +477,7 @@ command:
# 清除文件
# %worldPath% = 所有地图的根目录
# %worldName% = 所有地图的名称
# %playerUUID32% = 32位, 不带连字符的 UUID
clearFile:
# 清理所有地图下的存档
- '%worldPath%/playerdata/%playerUUID%.dat'
Expand Down
536 changes: 0 additions & 536 deletions README_MCBBS.txt

This file was deleted.

23 changes: 20 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>ApliNi</groupId>
<artifactId>IpacWhitelist</artifactId>
<version>4.0.1</version>
<version>4.1.0-rc1</version>
<packaging>jar</packaging>

<name>IpacWhitelist</name>
Expand Down Expand Up @@ -61,14 +61,20 @@
<id>sonatype</id>
<url>https://oss.sonatype.org/content/groups/public/</url>
</repository>
<!-- AuthMe -->
<!-- 以下是我们自己添加的存储库 -->
<repository>
<!-- AuthMe -->
<id>codemc-releases</id>
<url>https://repo.codemc.io/repository/maven-public/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<!-- Floodgate & Geyser -->
<id>opencollab-snapshot</id>
<url>https://repo.opencollab.dev/main/</url>
</repository>
</repositories>

<dependencies>
Expand All @@ -84,7 +90,7 @@
<version>24.1.0</version>
<scope>compile</scope>
</dependency>
<!-- AuthMe -->
<!-- 以下是我们自己添加的依赖 -->
<dependency>
<groupId>fr.xephi</groupId>
<artifactId>authme</artifactId>
Expand All @@ -98,5 +104,16 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.geysermc.geyser</groupId>
<artifactId>api</artifactId>
<version>2.2.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>5.0.0-alpha.12</version>
</dependency>
</dependencies>
</project>
5 changes: 3 additions & 2 deletions src/main/java/aplini/ipacwhitelist/IpacWhitelist.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package aplini.ipacwhitelist;

import aplini.ipacwhitelist.hook.hookAuthMe;
import aplini.ipacwhitelist.hook.authMe;
import aplini.ipacwhitelist.listener.commandHandler;
import aplini.ipacwhitelist.listener.onPlayerLogin;
import aplini.ipacwhitelist.utils.Metrics;
Expand Down Expand Up @@ -40,9 +40,10 @@ public void loadConfig(){

// 插件启动
public void onEnable(){

// 连接到插件
if(config.getBoolean("hook.AuthMe", true)){
getServer().getPluginManager().registerEvents(new hookAuthMe(), plugin);
getServer().getPluginManager().registerEvents(new authMe(), plugin);
}

// 注册指令
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/aplini/ipacwhitelist/enums/Key.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ public enum Key {
GET_VISIT_CONVERT,
GET_ALL,
GET_NAME_CONFLICT,
ERR,
TRUE,
FALSE,
}
5 changes: 5 additions & 0 deletions src/main/java/aplini/ipacwhitelist/enums/Type.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ public enum Type {
// SQL
// | ID | Type | Ban | UUID | Name | Time | Config |

// 权重
// 已添加完毕的记录始终大于未添加完毕的记录
// 黑名单等记录始终大于其他
// 标记为运行过程中的记录小于任何已被确定的记录

// 账户类型

// 未定义 or 不在白名单中
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
import static aplini.ipacwhitelist.func.eventFunc.runEventFunc;
import static aplini.ipacwhitelist.listener.onPlayerLogin.playerList;

public class hookAuthMe implements Listener {
public class authMe implements Listener {
private static AuthMeApi AuthmeAPI = null;
// 加载 API
public hookAuthMe(){
public authMe(){
AuthmeAPI = AuthMeApi.getInstance();
}

Expand Down
12 changes: 12 additions & 0 deletions src/main/java/aplini/ipacwhitelist/hook/geyser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package aplini.ipacwhitelist.hook;

import org.geysermc.geyser.api.GeyserApi;

import java.util.UUID;

public class geyser {

public static boolean isGeyserPlayer(UUID uuid){
return GeyserApi.api().connectionByUuid(uuid) != null;
}
}
92 changes: 48 additions & 44 deletions src/main/java/aplini/ipacwhitelist/listener/onPlayerLogin.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aplini.ipacwhitelist.listener;

import aplini.ipacwhitelist.IpacWhitelist;
import aplini.ipacwhitelist.enums.Key;
import aplini.ipacwhitelist.enums.Type;
import aplini.ipacwhitelist.enums.ph;
import aplini.ipacwhitelist.utils.PlayerData;
Expand All @@ -22,10 +23,14 @@

import static aplini.ipacwhitelist.IpacWhitelist.config;
import static aplini.ipacwhitelist.IpacWhitelist.plugin;
import static aplini.ipacwhitelist.enums.Type.getType;
import static aplini.ipacwhitelist.func.eventFunc.runEventFunc;
import static aplini.ipacwhitelist.hook.hookAuthMe.forceLoginPlayer;
import static aplini.ipacwhitelist.hook.hookAuthMe.registeredPlayerName;
import static aplini.ipacwhitelist.hook.authMe.forceLoginPlayer;
import static aplini.ipacwhitelist.hook.authMe.registeredPlayerName;
import static aplini.ipacwhitelist.hook.geyser.isGeyserPlayer;
import static aplini.ipacwhitelist.utils.netReq.isPremiumPlayer;
import static aplini.ipacwhitelist.utils.sql.getPlayerData;
import static aplini.ipacwhitelist.utils.util.SEL;
import static aplini.ipacwhitelist.utils.util.msg;
import static org.bukkit.event.player.PlayerLoginEvent.Result.KICK_OTHER;

Expand Down Expand Up @@ -124,7 +129,7 @@ public void onPlayerLoginEvent(PlayerLoginEvent event) {
pd = new PlayerData();
pd.setPlayerInfo(playerUUID, playerName);
}else{
// 更新玩家名称
// 我们始终需要更新玩家名称
pd.name = playerName;
}

Expand All @@ -137,7 +142,8 @@ public void onPlayerLoginEvent(PlayerLoginEvent event) {
// 如果 Type 和 Ban 均为 NOT 则将这条记录删除, 然后这不属于名称重复
// 处理名称重复, 但被标记为已删除的数据. 通过 /wl del 产生
if(li.type == Type.NOT && li.ban == Type.NOT){
// 可以选择删除或仅忽略
// 可以选择删除或仅忽略, 我们准备好了对于这些数据的"保护", 应避免任何可能有用的数据被删除
// 如果遇到出乎意料的错误, 可以尝试开启自动删除
// li.delete();
continue;
}
Expand All @@ -164,6 +170,43 @@ else if(!li.uuid.equals(playerUUID)){
}
}

// 根据玩家登录来源自动添加到白名单
if(config.getBoolean("whitelist.AutoWL.enable", false) && pd.type == Type.NOT){

Type onGeyserPlayer = (Type) SEL(getType(config.getString("whitelist.AutoWL.onGeyserPlayer", "NOT")), Type.NOT);
Type onPremiumPlayer = (Type) SEL(getType(config.getString("whitelist.AutoWL.onPremiumPlayer", "NOT")), Type.NOT);
Type onOtherPlayer = (Type) SEL(getType(config.getString("whitelist.AutoWL.onOtherPlayer", "NOT")), Type.NOT);

// 检查基岩版玩家
if(onGeyserPlayer != Type.NOT && isGeyserPlayer(player.getUniqueId())){
pd.type = onGeyserPlayer;
}
// 检查正版账户
else if(onPremiumPlayer != Type.NOT){
Key data = isPremiumPlayer(playerName, playerUUID);
if(data == Key.ERR){
event.disallow(KICK_OTHER, msg(config.getString("whitelist.AutoWL.onPremiumPlayerErrMsg", ""), playerUUID, playerName));
}else if(data == Key.TRUE){
pd.type = onPremiumPlayer;
}
}
// 其他的
else if(onOtherPlayer != Type.NOT){
pd.type = onOtherPlayer;
}

if(pd.type != Type.NOT) {
plugin.getLogger().info("[AutoWL] 玩家 " + playerName + " 已设置白名单 "+ pd.type.name);
}

// 处理 Ban 属性
if(pd.type == Type.BAN){
pd.type = Type.NOT;
pd.ban = Type.BAN;
}
}


// 被封禁的账户
if(pd.ban == Type.BAN){
event.disallow(KICK_OTHER, msg(config.getString("whitelist.BAN.kickMsg", ""), playerUUID, playerName));
Expand Down Expand Up @@ -274,45 +317,6 @@ public void onPlayerJoinEvent(PlayerJoinEvent event){
}
}

// @EventHandler(priority = EventPriority.MONITOR) // 玩家加入服务器, 最后执行
// public void onPlayerJoinEventMONITOR(PlayerJoinEvent event){
// CompletableFuture.runAsync(() -> {
// // 在这里实现参观账户自动登录
// Player player = event.getPlayer();
// if(visitPlayerList.contains(player.getUniqueId().toString())){
// // AuthMe 自动注册和登录
// if(config.getBoolean("whitelist.VISIT.AuthMePlugin.autoRegisterAndLogin", true)){
// // 登录账户
// forceLoginPlayer(player);
//
// final int loopCount = config.getInt("whitelist.VISIT.AuthMePlugin.doubleCheck", 4);
// if(loopCount <= 0){
// return;
// }
//
// // 每 20 刻度重复检查...
// new BukkitRunnable(){
// private int count = 0;
// @Override
// public void run() {
// try{
// if(count < loopCount){
// count++;
// plugin.getLogger().info("为参观账户自动注册/登录: " + player.getName());
// AuthMeAutoRegisteredAndLogin(player);
// }else{
// cancel();
// }
// }catch(Exception e){
// cancel();
// }
// }
// }.runTaskTimer(plugin, 10, 10);
// }
// }
// });
// }

@EventHandler(priority = EventPriority.LOWEST) // 玩家退出
public void onPlayerQuit(PlayerQuitEvent event){
CompletableFuture.runAsync(() -> {
Expand All @@ -331,7 +335,7 @@ public void onPlayerQuit(PlayerQuitEvent event){
// 玩家退出事件
runEventFunc("whitelist.WHITE.onPlayerQuitEvent", player, pd.uuid, pd.name);
}
// 玩家可能因为 del / ban 等操作被退出服务器
// 玩家可能因为 del / ban 等操作被退出服务器, 但不需要在这里进行处理
// default -> {}
}

Expand Down
45 changes: 45 additions & 0 deletions src/main/java/aplini/ipacwhitelist/utils/netReq.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package aplini.ipacwhitelist.utils;

import aplini.ipacwhitelist.enums.Key;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;

import static aplini.ipacwhitelist.IpacWhitelist.plugin;

public class netReq {

static public String fetch(String url){
OkHttpClient.Builder client = new OkHttpClient.Builder();
Request.Builder request = new Request.Builder()
.url(url);

try (Response response = client.build().newCall(request.build()).execute()) {
if (!response.isSuccessful()) {
return null;
}
return response.body().string();
} catch (IOException e) {
plugin.getLogger().warning("[HTTP Client] 无法访问 URL: "+ url);
plugin.getLogger().warning(e.getMessage());
}
return null;
}

static public Key isPremiumPlayer(String name, String uuid){
String data = fetch("https://api.mojang.com/users/profiles/minecraft/"+ name);
if(data == null){
return Key.ERR;
}

String uuidProbe = "\"id\" : \""+ uuid.replaceAll("-", "") +"\",";
String nameProbe = "\"name\" : \""+ name +"\"";

if(data.contains(uuidProbe) && data.contains(nameProbe)){
return Key.TRUE;
}
return Key.FALSE;
}
}
32 changes: 0 additions & 32 deletions src/main/java/aplini/ipacwhitelist/utils/sql.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,38 +45,6 @@ public static synchronized void reconnect() {

// 初始化数据库
public static void initialize(){
// try {
// Statement statement = conn.createStatement();
// statement.execute("PRAGMA auto_vacuum = FULL;");
// statement.execute("PRAGMA journal_mode = WAL;");
// statement.close();
//
// // 重新连接数据库
//// reconnect();
//
// // 加载数据表
// conn.prepareStatement("""
// CREATE TABLE IF NOT EXISTS "player" (
// "ID" INTEGER NOT NULL,
// "Type" INTEGER NOT NULL,
// "Ban" INTEGER NOT NULL,
// "UUID" TEXT NOT NULL,
// "Name" TEXT NOT NULL %s, -- 大小写敏感控制
// "Time" INTEGER NOT NULL,
// "Config" TEXT NOT NULL,
// PRIMARY KEY("ID" AUTOINCREMENT)
// );
// """.formatted(config.getBoolean("sqlite.nameCaseSensitive", false) ? "" : "COLLATE NOCASE")
// ).execute();
//
// statement = conn.createStatement();
// statement.execute("");
// statement.execute("");
// statement.execute("");
// statement.execute("");
// statement.execute("");
// statement.close();

try (Statement statement = conn.createStatement()) {

statement.executeUpdate("PRAGMA auto_vacuum = FULL;");
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/aplini/ipacwhitelist/utils/util.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,8 @@ public static String msg(String msg, String playerUUID, String playerName){
public static String msg(String msg, String var){
return msg(msg, var, var);
}

public static Object SEL(Object obj1, Object obj2){
return obj1 != null ? obj1 : obj2;
}
}
Loading

0 comments on commit 0086a45

Please sign in to comment.