Skip to content

Commit

Permalink
数据库操作失败时禁用插件
Browse files Browse the repository at this point in the history
  • Loading branch information
klxf committed Apr 5, 2022
1 parent f2b82b0 commit c720978
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ A Minecraft bukkit plugin

## 未来的计划
- [ ] 重载命令
- [ ] 数据库不可用时采用文件储存数据
- [ ] 数据库不可用时采用文件储存数据
-
7 changes: 5 additions & 2 deletions src/fang/uniqueuuid/DBDataManager.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fang.uniqueuuid;

import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.Bukkit;

import javax.annotation.Nonnull;
import java.sql.*;
Expand Down Expand Up @@ -29,7 +30,8 @@ public void loadAll(){

if(host == null || port == null || username == null || db_name == null || password == null){
UniqueUUID.dbError = true;
UniqueUUID.instance.getLogger().warning("数据库配置不完全,将关闭插件。");
UniqueUUID.instance.getLogger().warning("数据库配置不完全,关闭插件。");
Bukkit.getPluginManager().disablePlugin(UniqueUUID.instance);
return;
}

Expand All @@ -49,8 +51,9 @@ public void loadAll(){

private void putError(Exception e) {
UniqueUUID.dbError = true;
UniqueUUID.instance.getLogger().warning("SQL语句裂开了!");
UniqueUUID.instance.getLogger().warning("数据库操作失败,请检查配置文件!");
e.printStackTrace();
Bukkit.getPluginManager().disablePlugin(UniqueUUID.instance);
}

@Nonnull
Expand Down
6 changes: 3 additions & 3 deletions src/fang/uniqueuuid/UniqueUUID.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package fang.uniqueuuid;

import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.logging.Level;
Expand All @@ -17,13 +16,14 @@ public void onLoad() {

@Override
public void onEnable() {
getLogger().log(Level.INFO, "启动插件辣!");
getLogger().log(Level.INFO, "启动插件...");
Bukkit.getPluginManager().registerEvents(new EventListener(), this);
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
getLogger().log(Level.WARNING, "数据库驱动加载失败,将关闭插件。");
e.printStackTrace();
Bukkit.getPluginManager().disablePlugin(this);
dbError = true;
}
saveDefaultConfig();
Expand All @@ -34,7 +34,7 @@ public void onEnable() {

@Override
public void onDisable() {
getLogger().log(Level.INFO, "关闭插件辣!");
getLogger().log(Level.INFO, "关闭插件...");
new DBDataManager().saveAll();
}
}
2 changes: 1 addition & 1 deletion src/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: UniqueUUID
main: fang.uniqueuuid.UniqueUUID
version: 1.0
version: 1.0.1
api-version: 1.18

0 comments on commit c720978

Please # to comment.