Skip to content

Commit 502f907

Browse files
committed
feat: 日志管理器和服务器管理器 perf:配置管理器
1 parent c1286b6 commit 502f907

File tree

9 files changed

+162
-95
lines changed

9 files changed

+162
-95
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.gradle
22
.idea
33
gradle
4+
build
45

5-
6+
debug.bat

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ dependencies {
2525
implementation ('cc.carm.lib:easysql-hikaricp:0.4.7')
2626
}
2727

28-
def targetJavaVersion = 17
28+
def targetJavaVersion = 19
2929
java {
3030
def javaVersion = JavaVersion.toVersion(targetJavaVersion)
3131
sourceCompatibility = javaVersion

src/main/java/top/redstarmc/plugin/redstarlib/RedStarLib.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,31 @@
22

33
import org.bukkit.plugin.java.JavaPlugin;
44
import top.redstarmc.plugin.redstarlib.impl.ImplConfigManager;
5+
import top.redstarmc.plugin.redstarlib.impl.ImplLoggerManager;
56
import top.redstarmc.plugin.redstarlib.impl.ImplServerManager;
67
import top.redstarmc.plugin.redstarlib.manager.ConfigurationManager;
8+
import top.redstarmc.plugin.redstarlib.manager.LoggerManager;
79
import top.redstarmc.plugin.redstarlib.manager.ServerManager;
810

911
public final class RedStarLib extends JavaPlugin implements RedStarLibInterface{
1012

1113
private static RedStarLib instance;
1214

15+
public String INFO_PREFIX = "[RedStarLib]";
16+
1317
private ConfigurationManager configManager;
1418

19+
private LoggerManager loggerManager;
20+
1521
private ServerManager serverManager;
1622

1723
@Override
1824
public void onEnable() {
1925
instance = this;
2026
loadManager();
2127

28+
loggerManager.info("a");
29+
2230
}
2331

2432
@Override
@@ -31,7 +39,9 @@ public void loadManager() {
3139
configManager = new ImplConfigManager();
3240
configManager.init();
3341

34-
serverManager = new ImplServerManager();
42+
loggerManager = new ImplLoggerManager(INFO_PREFIX);
43+
44+
serverManager = new ImplServerManager(INFO_PREFIX);
3545
}
3646

3747
public static RedStarLib getInstance() {
@@ -42,6 +52,10 @@ public ConfigurationManager getConfigManager() {
4252
return configManager;
4353
}
4454

55+
public LoggerManager getLoggerManager() {
56+
return loggerManager;
57+
}
58+
4559
public ServerManager getServerManager() {
4660
return serverManager;
4761
}

src/main/java/top/redstarmc/plugin/redstarlib/impl/ImplConfigManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class ImplConfigManager extends ConfigurationManager {
2020

2121
private static YamlConfiguration config;
2222

23-
private static final String versioning = "${version}";
23+
private static final String versioning = "0.0.0";
2424

2525
@Override
2626
public void init() {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package top.redstarmc.plugin.redstarlib.impl;
2+
3+
import top.redstarmc.plugin.redstarlib.manager.LoggerManager;
4+
5+
public class ImplLoggerManager extends LoggerManager {
6+
7+
8+
public ImplLoggerManager(String INFO_PREFIX) {
9+
super(INFO_PREFIX);
10+
}
11+
}
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
package top.redstarmc.plugin.redstarlib.impl;
22

3-
import org.bukkit.Bukkit;
43
import top.redstarmc.plugin.redstarlib.manager.ServerManager;
54

65
public class ImplServerManager extends ServerManager {
76

8-
@Override
9-
public void broadcast(String... messages) {
10-
for (String message : messages) {
11-
Bukkit.broadcastMessage(message);
12-
}
7+
public ImplServerManager(String INFO_PREFIX) {
8+
super(INFO_PREFIX);
139
}
1410

11+
1512
}

src/main/java/top/redstarmc/plugin/redstarlib/manager/ConfigurationManager.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
*/
1313
public abstract class ConfigurationManager {
1414

15-
15+
/**
16+
* <h2>初始化方法</h2>
17+
*/
1618
public abstract void init();
1719

1820
/**
@@ -48,6 +50,11 @@ public void saveMapConfig(Map<String, Object> configMap, YamlConfiguration confi
4850
save(config, configFile);
4951
}
5052

53+
/**
54+
* <h2>从内存中保存 {@link YamlConfiguration} 格式文件</h2>
55+
* @param config {@link YamlConfiguration} 内存中配置文件
56+
* @param configFile {@link File} IO配置文件
57+
*/
5158
public void save(YamlConfiguration config, File configFile){
5259
try {
5360
config.save(configFile);
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package top.redstarmc.plugin.redstarlib.manager;
2+
3+
import org.bukkit.Bukkit;
4+
import top.redstarmc.plugin.redstarlib.utils.toStrings;
5+
6+
public abstract class LoggerManager {
7+
8+
public String INFO_PREFIX;
9+
10+
public LoggerManager(String INFO_PREFIX) {
11+
this.INFO_PREFIX = INFO_PREFIX;
12+
}
13+
14+
public boolean debugMode;
15+
16+
/**
17+
* <h2>发送插件普通信息</h2>
18+
* @param messages 字符串
19+
*/
20+
public final void info(String... messages) {
21+
if (messages == null) return;
22+
for (String message : messages) {
23+
if (message == null) continue;
24+
Bukkit.getConsoleSender().sendMessage(INFO_PREFIX + "§a[INFO] §r" + message + "§r");
25+
}
26+
}
27+
28+
/**
29+
* <h2>发送插件格式化信息</h2>
30+
* @param messages 字符串
31+
* @param objects 传入的格式化内容
32+
*/
33+
public final void info(String messages,Object... objects) {
34+
if (messages == null) return;
35+
Bukkit.getConsoleSender().sendMessage(INFO_PREFIX + "§a[INFO] §r" + toStrings.format(messages,objects) + "§r");
36+
}
37+
38+
/**
39+
* <h2>发送插件警告信息</h2>
40+
* @param messages 字符串
41+
*/
42+
public final void warn(String... messages) {
43+
if (messages == null) return;
44+
for (String message : messages) {
45+
if (message == null) continue;
46+
Bukkit.getConsoleSender().sendMessage(INFO_PREFIX + "§e[WARN] §r" + message + "§r");
47+
}
48+
}
49+
50+
/**
51+
* <h2>发送插件错误信息</h2>
52+
* @param messages 字符串
53+
*/
54+
public final void error(String... messages) {
55+
if (messages == null) return;
56+
for (String message : messages) {
57+
if (message == null) continue;
58+
Bukkit.getConsoleSender().sendMessage(INFO_PREFIX + "§c[ERROR] §r" + message + "§r");
59+
}
60+
}
61+
62+
/**
63+
* <h2>发送插件debug信息</h2>
64+
* @param messages 字符串
65+
*/
66+
public final void debug(String... messages) {
67+
if (messages == null) return;
68+
if (isDebugMode()) {
69+
for (String message : messages) {
70+
if (message == null) continue;
71+
Bukkit.getConsoleSender().sendMessage(INFO_PREFIX + "§6[DEBUG] §r" + message + "§r");
72+
}
73+
}
74+
}
75+
76+
/**
77+
* <h2>发送插件debug堆栈</h2>
78+
* @param e 堆栈
79+
*/
80+
public final void debug(Throwable e) {
81+
if (e == null) return;
82+
if (isDebugMode())
83+
e.printStackTrace();
84+
}
85+
86+
/**
87+
* <h2>同时发送插件debug信息和堆栈</h2>
88+
* @param e 堆栈
89+
* @param msg 字符串
90+
*/
91+
public final void debug(String msg, Throwable e) {
92+
if (msg == null || e == null) return;
93+
if (isDebugMode()) {
94+
debug(msg);
95+
debug(e);
96+
}
97+
}
98+
99+
100+
101+
102+
public String getINFO_PREFIX() {
103+
return INFO_PREFIX;
104+
}
105+
106+
public boolean isDebugMode() {
107+
return debugMode;
108+
}
109+
110+
}

src/main/java/top/redstarmc/plugin/redstarlib/manager/ServerManager.java

Lines changed: 11 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,40 @@
11
package top.redstarmc.plugin.redstarlib.manager;
22

33
import org.bukkit.Bukkit;
4-
import top.redstarmc.plugin.redstarlib.utils.toStrings;
54

65
public abstract class ServerManager {
76

8-
public static String INFO_PREFIX;
7+
public String INFO_PREFIX;
98

10-
/**
11-
* <h2>向服务器发送插件消息</h2>
12-
* <p>抽象方法,需要子类实现具体操作</p>
13-
* @param messages 信息字符串
14-
*/
15-
public abstract void broadcast(String... messages);
9+
public ServerManager(String INFO_PREFIX) {
10+
this.INFO_PREFIX = INFO_PREFIX;
11+
}
1612

1713
/**
18-
* <h2>发送带前缀的插件消息</h2>
14+
* <h2>向服务器发送聊天栏消息</h2>
1915
* @param messages 信息字符串
2016
*/
21-
public void broadcastPrefix(String... messages) {
22-
if (messages == null) return;
17+
public void broadcast(String... messages) {
2318
for (String message : messages) {
24-
if (message == null) continue;
25-
broadcast(INFO_PREFIX + message + "§r");
19+
Bukkit.broadcastMessage(message);
2620
}
2721
}
2822

2923
/**
30-
* <h2>发送插件普通信息</h2>
31-
* @param messages 字符串
24+
* <h2>发送带前缀的聊天栏消息</h2>
25+
* @param messages 信息字符串
3226
*/
33-
public final void info(String... messages) {
27+
public void broadcastPrefix(String... messages) {
3428
if (messages == null) return;
3529
for (String message : messages) {
3630
if (message == null) continue;
37-
Bukkit.getConsoleSender().sendMessage(INFO_PREFIX + "§a[INFO] §r" + message + "§r");
31+
broadcast(INFO_PREFIX + message + "§r");
3832
}
3933
}
4034

41-
/**
42-
* <h2>发送插件格式化信息</h2>
43-
* @param messages 字符串
44-
* @param objects 传入的格式化内容
45-
*/
46-
public final void info(String messages,Object... objects) {
47-
if (messages == null) return;
48-
Bukkit.getConsoleSender().sendMessage(INFO_PREFIX + "§a[INFO] §r" + toStrings.format(messages,objects) + "§r");
49-
}
5035

51-
/**
52-
* <h2>发送插件警告信息</h2>
53-
* @param messages 字符串
54-
*/
55-
public final void warn(String... messages) {
56-
if (messages == null) return;
57-
for (String message : messages) {
58-
if (message == null) continue;
59-
Bukkit.getConsoleSender().sendMessage(INFO_PREFIX + "§e[WARN] §r" + message + "§r");
60-
}
61-
}
6236

63-
/**
64-
* <h2>发送插件错误信息</h2>
65-
* @param messages 字符串
66-
*/
67-
public final void error(String... messages) {
68-
if (messages == null) return;
69-
for (String message : messages) {
70-
if (message == null) continue;
71-
Bukkit.getConsoleSender().sendMessage(INFO_PREFIX + "§c[ERROR] §r" + message + "§r");
72-
}
73-
}
7437

75-
// /**
76-
// * <h2>发送插件debug信息</h2>
77-
// * @param messages 字符串
78-
// */
79-
// public final void debug(String... messages) {
80-
// if (messages == null) return;
81-
// if (ConfigManager.getConfigManager().isDebugMode()) {
82-
// for (String message : messages) {
83-
// if (message == null) continue;
84-
// Bukkit.getConsoleSender().sendMessage(INFO_PREFIX + "§6[DEBUG] §r" + message + "§r");
85-
// }
86-
// }
87-
// }
88-
//
89-
// /**
90-
// * <h2>发送插件debug堆栈</h2>
91-
// * @param e 堆栈
92-
// */
93-
// public final void debug(Throwable e) {
94-
// if (e == null) return;
95-
// if (ConfigurationManager())
96-
// e.printStackTrace();
97-
// }
98-
//
99-
// /**
100-
// * <h2>同时发送插件debug信息和堆栈</h2>
101-
// * @param e 堆栈
102-
// * @param msg 字符串
103-
// */
104-
// public final void debug(String msg, Throwable e) {
105-
// if (msg == null || e == null) return;
106-
// if (ConfigManager.getConfigManager().isDebugMode()) {
107-
// debug(msg);
108-
// debug(e);
109-
// }
110-
// }
11138

11239

11340

0 commit comments

Comments
 (0)