Skip to content

Commit 13fb59f

Browse files
committed
2.4.13
1 parent 86b77eb commit 13fb59f

File tree

8 files changed

+209
-132
lines changed

8 files changed

+209
-132
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ plugins {
55
}
66

77
group = 'com.github.getplusm'
8-
version = '2.4.12'
8+
version = '2.4.13'
99
description = 'PLAZMER-ENGINE'
1010

1111
allprojects {

engine/src/main/java/t/me/p1azmer/engine/NexPlugin.java

Lines changed: 62 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,22 @@
3434
import java.lang.reflect.Field;
3535
import java.util.function.Consumer;
3636
import java.util.logging.Level;
37+
import java.util.logging.Logger;
3738

3839
@Getter
3940
@FieldDefaults(level = AccessLevel.PROTECTED)
4041
public abstract class NexPlugin<P extends NexPlugin<P>> extends JavaPlugin {
4142
public static boolean isPaper = false;
4243
public static boolean isFolia = false;
4344

45+
@Getter
46+
static Logger globalLogger;
4447
ConfigManager<P> configManager;
4548
LangManager<P> langManager;
4649
CommandManager<P> commandManager;
4750
ActionManager<P> actionManager;
48-
@Getter @Nullable ServerImplementation foliaScheduler;
51+
@Getter
52+
@Nullable ServerImplementation foliaScheduler;
4953

5054
final boolean isEngine = this instanceof NexEngine;
5155

@@ -60,40 +64,47 @@ public File getFile() {
6064

6165
@Override
6266
public void onEnable() {
63-
long loadTook = System.currentTimeMillis();
64-
Version.printCaution(getSelf());
67+
globalLogger = getLogger();
68+
try {
69+
long loadTook = System.currentTimeMillis();
70+
Version.printCaution(getSelf());
6571

66-
if (Version.getCurrent().isDropped()) {
67-
getPluginManager().disablePlugin(this);
68-
return;
69-
}
70-
71-
if (isEngine()) {
72-
if (ServerVersions.isPaper()) {
73-
isPaper = true;
72+
if (Version.getCurrent().isDropped()) {
73+
getPluginManager().disablePlugin(this);
74+
return;
7475
}
75-
if (ServerVersions.isFolia()) {
76-
isFolia = true;
77-
foliaScheduler = new FoliaCompatibility(this).getServerImplementation();
78-
error("""
79-
==================================
80-
Attention! When using Folia, many functions of the plugin may not work.
81-
You can report all errors here:
82-
https://github.com/getplusm/Engine/issues
83-
==================================
84-
""");
76+
77+
if (isEngine()) {
78+
if (ServerVersions.isPaper()) {
79+
isPaper = true;
80+
}
81+
if (ServerVersions.isFolia()) {
82+
isFolia = true;
83+
foliaScheduler = new FoliaCompatibility(this).getServerImplementation();
84+
error("""
85+
==================================
86+
Attention! When using Folia, many functions of the plugin may not work.
87+
You can report all errors here:
88+
https://github.com/getplusm/Engine/issues
89+
==================================
90+
""");
91+
}
92+
} else {
93+
EngineUtils.ENGINE.addChildren(this);
8594
}
86-
} else {
87-
EngineUtils.ENGINE.addChildren(this);
88-
}
8995

90-
loadManagers();
91-
info("Plugin loaded in " + (System.currentTimeMillis() - loadTook) + " ms!");
96+
loadManagers();
97+
setupDatabase();
98+
info("Plugin loaded in " + (System.currentTimeMillis() - loadTook) + " ms!");
99+
} catch (RuntimeException exception) {
100+
globalLogger.log(Level.SEVERE, "Got exception while loading plugin!", exception);
101+
}
92102
}
93103

94104
@Override
95105
public void onDisable() {
96106
unloadManagers();
107+
databaseShutdown();
97108
}
98109

99110
public abstract void enable();
@@ -206,6 +217,10 @@ protected void loadManagers() {
206217
commandManager.setup();
207218
actionManager.setup();
208219

220+
enable();
221+
}
222+
223+
protected void setupDatabase() {
209224
UserDataHolder<?, ?> dataHolder = null;
210225
if (this instanceof UserDataHolder) {
211226
dataHolder = (UserDataHolder<?, ?>) this;
@@ -215,37 +230,40 @@ protected void loadManagers() {
215230
return;
216231
}
217232
}
218-
219-
enable();
220233
if (dataHolder != null) {
221234
dataHolder.getUserManager().loadOnlineUsers();
222235
}
223236
}
224237

225238
private void unloadManagers() {
226-
disable();
227-
if (commandManager != null) {
228-
commandManager.shutdown();
229-
}
239+
try {
240+
disable();
241+
if (commandManager != null) {
242+
commandManager.shutdown();
243+
}
230244

231-
if (actionManager != null) {
232-
actionManager.shutdown();
233-
}
245+
if (actionManager != null) {
246+
actionManager.shutdown();
247+
}
234248

235-
// Unregister ALL plugin listeners.
236-
unregisterListeners();
249+
// Unregister ALL plugin listeners.
250+
unregisterListeners();
237251

238-
// Save user data and disconnect from the database.
252+
getConfigManager().shutdown();
253+
getLangManager().shutdown();
254+
255+
if (isFolia && foliaScheduler != null) foliaScheduler.cancelTasks();
256+
if (isPaper) getServer().getScheduler().cancelTasks(this);
257+
} catch (RuntimeException exception) {
258+
globalLogger.log(Level.SEVERE, "Got exception while disable plugin", exception);
259+
}
260+
}
261+
262+
private void databaseShutdown() {
239263
if (this instanceof UserDataHolder<?, ?> dataHolder) {
240264
dataHolder.getUserManager().shutdown();
241265
dataHolder.getData().shutdown();
242266
}
243-
244-
getConfigManager().shutdown();
245-
getLangManager().shutdown();
246-
247-
if (isFolia && foliaScheduler != null) foliaScheduler.cancelTasks();
248-
if (isPaper) getServer().getScheduler().cancelTasks(this);
249267
}
250268

251269
@NotNull

engine/src/main/java/t/me/p1azmer/engine/Version.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true)
2222
public enum Version {
2323
V1_17_R1("1.17.1", Status.OUTDATED),
24+
V1_18_R1("1.18.1", Status.OUTDATED),
2425
V1_18_R2("1.18.2", Status.OUTDATED),
2526
V1_19_R1("1.19.2", Status.OUTDATED),
2627
V1_19_R2("1.19.3", Status.OUTDATED),

engine/src/main/java/t/me/p1azmer/engine/api/config/JOption.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.bukkit.inventory.ItemStack;
99
import org.jetbrains.annotations.NotNull;
1010
import org.jetbrains.annotations.Nullable;
11+
import t.me.p1azmer.engine.NexPlugin;
1112
import t.me.p1azmer.engine.utils.StringUtil;
1213
import t.me.p1azmer.engine.utils.TriFunction;
1314
import t.me.p1azmer.engine.utils.wrapper.UniFormatter;
@@ -16,6 +17,7 @@
1617

1718
import java.util.*;
1819
import java.util.function.*;
20+
import java.util.logging.Level;
1921
import java.util.stream.Collectors;
2022

2123
@Setter
@@ -325,16 +327,21 @@ public JOption<T> onRead(@NotNull UnaryOperator<T> onRead) {
325327

326328
@NotNull
327329
public T read(@NotNull JYML config) {
328-
if (!config.contains(this.getPath())) {
329-
this.write(config);
330-
}
331-
if (this.getDescription().length > 0 && !this.getDescription()[0].isEmpty()) {
332-
config.setComments(this.getPath(), this.getDescription());
333-
}
330+
try {
331+
if (!config.contains(this.getPath())) {
332+
this.write(config);
333+
}
334+
if (this.getDescription().length > 0 && !this.getDescription()[0].isEmpty()) {
335+
config.setComments(this.getPath(), this.getDescription());
336+
}
334337

335-
UnaryOperator<T> operator = this.onRead == null ? value -> value : this.onRead;
338+
UnaryOperator<T> operator = this.onRead == null ? value -> value : this.onRead;
336339

337-
return (this.value = operator.apply(this.reader.read(config, this.getPath(), this.getDefaultValue())));
340+
return (this.value = operator.apply(this.reader.read(config, this.getPath(), this.getDefaultValue())));
341+
} catch (RuntimeException exception) {
342+
NexPlugin.getGlobalLogger().log(Level.SEVERE, "Got exception while reading " + this.getPath(), exception);
343+
throw exception;
344+
}
338345
}
339346

340347
public void write(@NotNull JYML config) {

engine/src/main/java/t/me/p1azmer/engine/api/config/JYML.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@
2626
import java.io.File;
2727
import java.io.IOException;
2828
import java.io.InputStream;
29+
import java.lang.reflect.Field;
2930
import java.nio.charset.StandardCharsets;
3031
import java.util.*;
3132
import java.util.function.UnaryOperator;
33+
import java.util.logging.Level;
3234
import java.util.stream.IntStream;
3335

3436
@FieldDefaults(level = AccessLevel.PRIVATE)
@@ -104,11 +106,23 @@ public void initializeOptions(@NotNull Class<?> clazz) {
104106
initializeOptions(clazz, this);
105107
}
106108

107-
public static void initializeOptions(@NotNull Class<?> clazz, @NotNull JYML config) {
108-
for (JOption<?> value : Reflex.getFields(clazz, JOption.class)) {
109-
value.read(config);
109+
public static void initializeOptions(@NotNull Class<?> clazz, @NotNull JYML cfg) {
110+
try {
111+
for (Field field : Reflex.getFields(clazz)) {
112+
if (!JOption.class.isAssignableFrom(field.getType())) continue;
113+
if (!field.trySetAccessible()) continue;
114+
115+
try {
116+
JOption<?> option = (JOption<?>) field.get(clazz);
117+
option.read(cfg);
118+
} catch (IllegalAccessException e) {
119+
e.printStackTrace();
120+
}
121+
}
122+
cfg.saveChanges();
123+
} catch (RuntimeException exception) {
124+
NexPlugin.getGlobalLogger().log(Level.SEVERE, "Failed to initialize options for " + clazz.getSimpleName(), exception);
110125
}
111-
config.save();
112126
}
113127

114128
@NotNull

engine/src/main/java/t/me/p1azmer/engine/api/data/AbstractDataHandler.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ protected void onLoad() {
6666
if (this.getConfig().syncInterval > 0) {
6767
if (this.getDataType() != StorageType.SQLITE) {
6868
this.synchronizationTask = new DataSynchronizationTask<>(this);
69-
this.synchronizationTask.start();
7069
this.plugin.info("Enabled data synchronization with " + config.syncInterval + " seconds interval.");
7170
}
7271
}
@@ -80,7 +79,7 @@ protected void onLoad() {
8079
@Override
8180
protected void onShutdown() {
8281
if (this.synchronizationTask != null) {
83-
this.synchronizationTask.stop();
82+
this.synchronizationTask.shutdown();
8483
this.synchronizationTask = null;
8584
}
8685
if (this.saveTask != null) {
Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,40 @@
11
package t.me.p1azmer.engine.api.data.task;
22

3+
import lombok.AccessLevel;
4+
import lombok.experimental.FieldDefaults;
35
import org.jetbrains.annotations.NotNull;
46
import t.me.p1azmer.engine.NexPlugin;
57
import t.me.p1azmer.engine.api.data.AbstractDataHandler;
6-
import t.me.p1azmer.engine.api.server.AbstractTask;
78

8-
public class DataSynchronizationTask<P extends NexPlugin<P>> extends AbstractTask<P> {
9+
import java.util.concurrent.Executors;
10+
import java.util.concurrent.ScheduledExecutorService;
11+
import java.util.concurrent.TimeUnit;
12+
import java.util.logging.Level;
913

10-
private final AbstractDataHandler<P> dataHandler;
14+
@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true)
15+
public class DataSynchronizationTask<P extends NexPlugin<P>> {
16+
17+
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
1118

1219
public DataSynchronizationTask(@NotNull AbstractDataHandler<P> dataHandler) {
13-
super(dataHandler.plugin(), dataHandler.getConfig().syncInterval, true);
14-
this.dataHandler = dataHandler;
20+
scheduler.scheduleWithFixedDelay(() -> {
21+
try {
22+
dataHandler.onSynchronize();
23+
} catch (RuntimeException exception) {
24+
dataHandler.plugin().getLogger().log(Level.SEVERE, "Error while synchronizing data", exception);
25+
}
26+
}, 0, dataHandler.getConfig().syncInterval, TimeUnit.SECONDS);
1527
}
1628

17-
@Override
18-
public void action() {
19-
this.dataHandler.onSynchronize();
29+
public void shutdown() {
30+
scheduler.shutdown();
31+
try {
32+
if (!scheduler.awaitTermination(5, TimeUnit.SECONDS)) {
33+
scheduler.shutdownNow();
34+
}
35+
} catch (InterruptedException e) {
36+
scheduler.shutdownNow();
37+
Thread.currentThread().interrupt();
38+
}
2039
}
21-
}
40+
}

0 commit comments

Comments
 (0)