Skip to content

Commit

Permalink
基本实现参观账户功能. 移除 MySQL 支持. 性能优化和错误修复.
Browse files Browse the repository at this point in the history
  • Loading branch information
ApliNi committed Aug 13, 2023
1 parent 8bcce35 commit 0d39a32
Show file tree
Hide file tree
Showing 11 changed files with 665 additions and 322 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ Ipacamod 服务器的新白名单插件
- [x] 白名单功能
- [x] 使用数据库
- [x] 根据最后上线时间取消白名单
- [ ] 完全删除功能
- [ ] 同时删除各处的用户数据


**指令列表**
- `/wl` - 主命令
- `/wl reload` - 重载配置
- `/wl add <name> [uuid]` - 添加到白名单, uuid为空时自动获取
- `/wl del <name|uuid>` - 取消一个玩家的白名单, 不删除数据库中的记录
- `/wl add <playerName> [playerUUID]` - 添加到白名单, uuid为空时自动获取
- `/wl del <playerName | playerUUID>` - 取消一个玩家的白名单, 不删除数据库中的记录
- `/wl reconnect_database` - 重新连接数据库


**特性**
Expand Down
24 changes: 23 additions & 1 deletion 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>1.1.2</version>
<version>1.1.3</version>
<packaging>jar</packaging>

<name>IpacWhitelist</name>
Expand Down Expand Up @@ -61,6 +61,14 @@
<id>sonatype</id>
<url>https://oss.sonatype.org/content/groups/public/</url>
</repository>
<!-- AuthMe -->
<repository>
<id>codemc-releases</id>
<url>https://repo.codemc.io/repository/maven-public/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>

<dependencies>
Expand All @@ -76,5 +84,19 @@
<version>24.0.1</version>
<scope>compile</scope>
</dependency>
<!-- AuthMe -->
<dependency>
<groupId>fr.xephi</groupId>
<artifactId>authme</artifactId>
<version>5.6.0-beta2</version>
<scope>provided</scope>
<optional>true</optional>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
222 changes: 138 additions & 84 deletions src/main/java/aplini/ipacwhitelist/IpacWhitelist.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package aplini.ipacwhitelist;

import aplini.ipacwhitelist.util.SQL;
import aplini.ipacwhitelist.visit.Visit;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
Expand All @@ -17,46 +19,43 @@
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import static aplini.ipacwhitelist.util.SQL.getVisitPlayerUUIDFromName;
import static org.bukkit.Bukkit.getLogger;

public class IpacWhitelist extends JavaPlugin implements Listener {
private static IpacWhitelist plugin;
boolean allowJoin = false;


@Override // 插件加载
public void onLoad() {
plugin = this;
plugin.saveDefaultConfig();
plugin.reloadConfig();
plugin.getConfig();
}

// 连接数据库
SQL.connection();
// 初始化数据库
SQL.initialize();
}

// 插件启动
public void onEnable() {
// SQL.openConnection();

Bukkit.getPluginManager().registerEvents(this, this);

if(!getConfig().getString("sql.jdbc_driver", "").isEmpty()){
try {
Class.forName(getConfig().getString("sql.jdbc_driver"));
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}

SQL.initialize();

// 注册监听器
getServer().getPluginManager().registerEvents(this, this);
Objects.requireNonNull(plugin.getCommand("wl")).setExecutor(this);
}

// 参观账户
if(plugin.getConfig().getBoolean("visit.enable", false)){
// 注册监听器
getServer().getPluginManager().registerEvents(new Visit(this), this);
}
}

// 插件禁用
public void onDisable() {
// SQL.closeConnection();
// 关闭数据库连接
SQL.closeConnection();
}


Expand All @@ -71,10 +70,10 @@ public void onServerLoad(ServerLoadEvent event) {
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
// 允许加入
allowJoin = true;

getLogger().info("[IpacWhitelist] 启动等待结束");
// 允许加入
allowJoin = true;
});
executor.shutdown();
}
Expand All @@ -83,113 +82,167 @@ public void onServerLoad(ServerLoadEvent event) {
@EventHandler // 玩家加入
public void onPlayerLogin(PlayerLoginEvent event) {

// 服务器启动等待
if(!allowJoin){
event.setKickMessage(plugin.getConfig().getString("message.late-join-time", ""));
event.setResult(PlayerLoginEvent.Result.KICK_OTHER);
return;
}

// 白名单逻辑
// 0 = 不在, 1 = 存在, 2 = 出错
switch(SQL.isWhitelisted(event.getPlayer())){
case 0 -> {
getLogger().warning("[IpacWhitelist] "+ event.getPlayer().getName() +" 不在白名单中");
event.setKickMessage(plugin.getConfig().getString("message.not", "").replace("%player%", event.getPlayer().getName()));
event.setResult(PlayerLoginEvent.Result.KICK_WHITELIST);
return;

case NOT -> { // 不在白名单中
// 非白名单玩家加入服务器 -> 交给参观账户
if(plugin.getConfig().getBoolean("visit.enable", false)){
Visit.onNewVisitPlayerLoginEvent(event);
}else{
getLogger().warning("[IpacWhitelist] %s 不在白名单中".formatted(event.getPlayer().getName()));
event.setKickMessage(plugin.getConfig().getString("message.not", "").replace("%player%", event.getPlayer().getName()));
event.setResult(PlayerLoginEvent.Result.KICK_WHITELIST);
}
}

case VISIT_DEL_DATA -> { // 已删除数据的参观账户
Visit.onNewVisitPlayerLoginEvent(event);
}

case VISIT -> { // 参观账户
Visit.onVisitPlayerLoginEvent(event);
}
case 2 -> {
getLogger().warning("[IpacWhitelist] "+ event.getPlayer().getName() +" 触发内部错误");

case ERROR -> { // 内部错误
getLogger().warning("[IpacWhitelist] %s 触发内部错误".formatted(event.getPlayer().getName()));
event.setKickMessage(plugin.getConfig().getString("message.err-sql-player-join", ""));
event.setResult(PlayerLoginEvent.Result.KICK_WHITELIST);
return;
event.setResult(PlayerLoginEvent.Result.KICK_OTHER);
}
}

// 服务器启动等待
if(!allowJoin){
getLogger().info("[IpacWhitelist] "+ event.getPlayer().getName() +" 因启动等待被拒绝加入");
event.setKickMessage(plugin.getConfig().getString("message.late-join-time", ""));
event.setResult(PlayerLoginEvent.Result.KICK_WHITELIST);
return;
}
// 允许玩家登录
event.setResult(PlayerLoginEvent.Result.ALLOWED);
}


public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
switch (args[0]) {
switch(args[0]){
case "reload" -> {
plugin.reloadConfig();
sender.sendMessage(plugin.getConfig().getString("message.reload", ""));
}

// 重新连接数据库
case "reconnect_database" -> {
allowJoin = false;
SQL.closeConnection();
SQL.connection();
SQL.initialize();
allowJoin = true;
sender.sendMessage(plugin.getConfig().getString("message.reconnect-database", ""));
}

// 添加一个账户
case "add" -> {
if (args.length == 2) {
if(args.length == 2){
// 检查参数
if (args[1].length() > 16) {
if(args[1].length() > 16){
sender.sendMessage(plugin.getConfig().getString("message.err-name-length", ""));
return true;
}

// 0=错误, 1=已添加, 重置白名单, 2=未添加, 创建记录
switch (SQL.addPlayer(args[1])) {
case 0 ->
sender.sendMessage(plugin.getConfig().getString("message.err-sql", ""));
case 1 ->
// VISIT=是参观账户, 重置数据. DEFAULT=是普通账户, 重置数据. NOT=是新账户, 创建数据
switch(SQL.addPlayer(args[1])){
case VISIT -> {
// 运行 wl-add
String UUID = getVisitPlayerUUIDFromName(args[1]);
for(String li : plugin.getConfig().getStringList("visit.wl-add.command")){
Bukkit.getScheduler().callSyncMethod(plugin, () -> Bukkit.dispatchCommand(Bukkit.getConsoleSender(),
li
.replace("%playerName%", args[1])
.replace("%playerUUID%", UUID)));
}
// DEFAULT ->
sender.sendMessage(plugin.getConfig().getString("message.add-ok-reset", "")
.replace("%player%", args[1]));
}
case DEFAULT ->
sender.sendMessage(plugin.getConfig().getString("message.add-ok-reset", "")
.replace("%player%", args[1]));
case 2 ->
case NOT ->
sender.sendMessage(plugin.getConfig().getString("message.add-ok", "")
.replace("%player%", args[1]));
default ->
sender.sendMessage(plugin.getConfig().getString("message.err-sql", ""));
}
return true;
} else if (args.length >= 3) {
}else if(args.length >= 3){
// 检查参数
if (args[1].length() > 16) {
if(args[1].length() > 16){
sender.sendMessage(plugin.getConfig().getString("message.err-name-length", ""));
return true;
}
if (args[2].length() != 36) {
if(args[2].length() != 36){
sender.sendMessage(plugin.getConfig().getString("message.err-uuid-length", ""));
return true;
}

// 0=错误, 1=已添加, 重置白名单, 2=未添加, 创建记录
switch (SQL.addPlayer(args[1], args[2])) {
case 0 ->
sender.sendMessage(plugin.getConfig().getString("message.err-sql", ""));
case 1 ->
// VISIT=是参观账户, 重置数据. DEFAULT=是普通账户, 重置数据. NOT=是新账户, 创建数据
switch(SQL.addPlayer(args[1])){
case VISIT -> {
// 运行 wl-add
for(String li : plugin.getConfig().getStringList("visit.wl-add.command")){
Bukkit.getScheduler().callSyncMethod(plugin, () -> Bukkit.dispatchCommand(Bukkit.getConsoleSender(),
li
.replace("%playerName%", args[1])
.replace("%playerUUID%", args[2])));
}
// DEFAULT ->
sender.sendMessage(plugin.getConfig().getString("message.add-ok-reset", "")
.replace("%player%", args[1]));
}
case DEFAULT ->
sender.sendMessage(plugin.getConfig().getString("message.add-ok-reset", "")
.replace("%player%", args[1]));
case 2 ->
case NOT ->
sender.sendMessage(plugin.getConfig().getString("message.add-ok", "")
.replace("%player%", args[1]));
default ->
sender.sendMessage(plugin.getConfig().getString("message.err-sql", ""));
}
return true;
}else{
sender.sendMessage("/wl add <Name> [UUID]");
}
sender.sendMessage("/wl add <Name> [UUID]");
return true;
}

// 删除一个账户
case "del" -> {
if (args.length == 2) {
if (args[1].length() == 36) {
// uuid
boolean s = SQL.delPlayerUUID(args[1]);
if (s) {
sender.sendMessage(plugin.getConfig().getString("message.add-ok", "")
.replace("%player%", args[1]));
} else {
sender.sendMessage(plugin.getConfig().getString("message.err-sql", ""));
}
return true;
} else if (args[1].length() <= 16) {
// name
boolean s = SQL.delPlayerName(args[1]);
if (s) {
sender.sendMessage(plugin.getConfig().getString("message.del-ok", "")
.replace("%player%", args[1]));
} else {
sender.sendMessage(plugin.getConfig().getString("message.err-sql", ""));
}
return true;
} else {
sender.sendMessage(plugin.getConfig().getString("message.err-name-length", ""));
return true;
}
if(args.length != 2){
sender.sendMessage("/wl del <Name|UUID>");
return true;
}

boolean b;

// uuid
if(args[1].length() == 36){
b = SQL.delPlayerUUID(args[1]);
}

// name
else if(args[1].length() <= 16){
b = SQL.delPlayerName(args[1]);
}

else{
sender.sendMessage(plugin.getConfig().getString("message.err-name-length", ""));
return true;
}

if(b){
sender.sendMessage(plugin.getConfig().getString("message.del-ok", "").replace("%player%", args[1]));
}else{
sender.sendMessage(plugin.getConfig().getString("message.err-sql", ""));
}
sender.sendMessage("/wl del <Name|UUID>");
return true;
}
}

Expand All @@ -203,6 +256,7 @@ public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Comman
list.add("reload");
list.add("add");
list.add("del");
list.add("reconnect_database");
return list;
}
return null;
Expand Down
Loading

0 comments on commit 0d39a32

Please sign in to comment.