Skip to content

Commit e9141bd

Browse files
committed
(api) add api to create login push message for other plugins
1 parent bc6767c commit e9141bd

File tree

4 files changed

+77
-39
lines changed

4 files changed

+77
-39
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>cat.nyaa</groupId>
88
<artifactId>ukit</artifactId>
9-
<version>1.6</version>
9+
<version>1.7</version>
1010

1111
<properties>
1212
<maven.compiler.source>21</maven.compiler.source>

src/main/java/cat/nyaa/ukit/SpigotLoader.java

+8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cat.nyaa.ukit;
22

33
import cat.nyaa.ecore.EconomyCore;
4+
import cat.nyaa.ukit.api.UKitAPI;
45
import cat.nyaa.ukit.chat.ChatFunction;
56
import cat.nyaa.ukit.elytra.ElytraFunction;
67
import cat.nyaa.ukit.item.ItemFunction;
@@ -57,6 +58,7 @@ public class SpigotLoader extends JavaPlugin implements TabExecutor {
5758
private ElytraFunction elytraFunction;
5859
private MailFunction mailFunction;
5960
private LoginPushFunction loginPushFunction;
61+
private static UKitAPI uKitAPI;
6062

6163
@Override
6264
public void onEnable() {
@@ -139,6 +141,8 @@ public boolean reload() {
139141
getServer().getPluginManager().registerEvents(lockFunction, this);
140142
getServer().getPluginManager().registerEvents(loginPushFunction, this);
141143

144+
new UKitAPI(this,loginPushFunction.getLoginPushRecorder());
145+
142146
return true;
143147
}
144148

@@ -305,6 +309,10 @@ private boolean setupChat() {
305309
private void IGNORE_RESULT(Object o) {
306310
}
307311

312+
public static UKitAPI getUKitAPI(){
313+
return uKitAPI;
314+
}
315+
308316
enum SubCommands {
309317
RELOAD,
310318
SHOW,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package cat.nyaa.ukit.api;
2+
3+
import cat.nyaa.ukit.SpigotLoader;
4+
import cat.nyaa.ukit.loginpush.LoginPushRecorder;
5+
import net.kyori.adventure.text.Component;
6+
7+
import java.sql.SQLException;
8+
import java.util.UUID;
9+
10+
public class UKitAPI {
11+
private final SpigotLoader pluginInstance;
12+
private final LoginPushRecorder loginPushRecorder;
13+
private static UKitAPI instance;
14+
15+
public UKitAPI(SpigotLoader pluginInstance, LoginPushRecorder loginPushRecorder) {
16+
this.pluginInstance = pluginInstance;
17+
this.loginPushRecorder = loginPushRecorder;
18+
instance = this;
19+
}
20+
21+
public static UKitAPI getAPIInstance() {
22+
return instance;
23+
}
24+
25+
public void createLoginPush(UUID playerUniqueID, Component message, Component senderName) throws SQLException {
26+
loginPushRecorder.createLoginPush(playerUniqueID, message, senderName);
27+
}
28+
}

src/main/java/cat/nyaa/ukit/loginpush/LoginPushFunction.java

+40-38
Original file line numberDiff line numberDiff line change
@@ -42,51 +42,53 @@ public boolean invokeCommand(CommandSender commandSender, Command command, Strin
4242
return true;
4343
}
4444
var dateFormat = new SimpleDateFormat(pluginInstance.language.loginPushLang.push_timestamp_format.produce());
45-
46-
try {
47-
var loginPushes = loginPushRecorder.getLoginPush(senderPlayer.getUniqueId());
48-
if (loginPushes.isEmpty()) {
49-
senderPlayer.sendMessage(pluginInstance.language.loginPushLang.no_new_push.produce());
50-
return true;
45+
Bukkit.getAsyncScheduler().runNow(pluginInstance, (task) -> {
46+
try {
47+
var loginPushes = loginPushRecorder.getLoginPush(senderPlayer.getUniqueId());
48+
if (loginPushes.isEmpty()) {
49+
senderPlayer.sendMessage(pluginInstance.language.loginPushLang.no_new_push.produce());
50+
return;
51+
}
52+
senderPlayer.sendMessage(pluginInstance.language.loginPushLang.login_push_title.produce());
53+
loginPushes.forEach(loginPush -> {
54+
var line1 = pluginInstance.language.loginPushLang.push_message_line1.produceAsComponent(
55+
Pair.of("message", loginPush.content()),
56+
Pair.of("time", dateFormat.format(loginPush.time())),
57+
Pair.of("sender", loginPush.sender())
58+
);
59+
var line2 = pluginInstance.language.loginPushLang.push_message_line2.produceAsComponent(
60+
Pair.of("message", loginPush.content()),
61+
Pair.of("time", dateFormat.format(loginPush.time())),
62+
Pair.of("sender", loginPush.sender())
63+
);
64+
senderPlayer.sendMessage(
65+
Component.text().append(line1).appendNewline().append(line2));
66+
});
67+
loginPushRecorder.deleteLoginPush(loginPushes.stream().map(LoginPush::id).toList());
68+
senderPlayer.sendMessage(pluginInstance.language.loginPushLang.login_push_end.produce());
69+
} catch (SQLException e) {
70+
e.printStackTrace();
71+
commandSender.sendMessage(pluginInstance.language.commonLang.sqlErrorOccurred.produce());
5172
}
52-
53-
senderPlayer.sendMessage(pluginInstance.language.loginPushLang.login_push_title.produce());
54-
loginPushes.forEach(loginPush -> {
55-
var line1 = pluginInstance.language.loginPushLang.push_message_line1.produceAsComponent(
56-
Pair.of("message", loginPush.content()),
57-
Pair.of("time", dateFormat.format(loginPush.time())),
58-
Pair.of("sender", loginPush.sender())
59-
);
60-
var line2 = pluginInstance.language.loginPushLang.push_message_line2.produceAsComponent(
61-
Pair.of("message", loginPush.content()),
62-
Pair.of("time", dateFormat.format(loginPush.time())),
63-
Pair.of("sender", loginPush.sender())
64-
);
65-
senderPlayer.sendMessage(
66-
Component.text().append(line1).appendNewline().append(line2));
67-
});
68-
loginPushRecorder.deleteLoginPush(loginPushes.stream().map(LoginPush::id).toList());
69-
senderPlayer.sendMessage(pluginInstance.language.loginPushLang.login_push_end.produce());
70-
} catch (SQLException e) {
71-
e.printStackTrace();
72-
commandSender.sendMessage(pluginInstance.language.commonLang.sqlErrorOccurred.produce());
73-
}
73+
});
7474
return true;
7575
}
7676

7777
@EventHandler
7878
public void onPlayerJoin(PlayerJoinEvent event) {
79-
try {
80-
int unreadPush = loginPushRecorder.countUnreadPush(event.getPlayer().getUniqueId());
81-
if (unreadPush > 0) {
82-
Bukkit.getGlobalRegionScheduler().runDelayed(pluginInstance, (ignored) -> event.getPlayer().sendMessage(pluginInstance.language.loginPushLang.login_push_notice.produce(
83-
Pair.of("number", unreadPush)
84-
)), 20 * 3);
79+
Bukkit.getAsyncScheduler().runNow(pluginInstance,(task)->{
80+
try {
81+
int unreadPush = loginPushRecorder.countUnreadPush(event.getPlayer().getUniqueId());
82+
if (unreadPush > 0) {
83+
Bukkit.getGlobalRegionScheduler().runDelayed(pluginInstance, (ignored) -> event.getPlayer().sendMessage(pluginInstance.language.loginPushLang.login_push_notice.produce(
84+
Pair.of("number", unreadPush)
85+
)), 20 * 3);
86+
}
87+
} catch (SQLException e) {
88+
e.printStackTrace();
89+
event.getPlayer().sendMessage(pluginInstance.language.commonLang.sqlErrorOccurred.produce());
8590
}
86-
} catch (SQLException e) {
87-
e.printStackTrace();
88-
event.getPlayer().sendMessage(pluginInstance.language.commonLang.sqlErrorOccurred.produce());
89-
}
91+
});
9092
}
9193

9294
@Override

0 commit comments

Comments
 (0)