Skip to content

Commit 54b26f5

Browse files
committed
feat: join message sync
- Sync join message on startup with database - Code clean up in onEnable - /sjm sync create/remove/set command
1 parent 5fa505e commit 54b26f5

File tree

7 files changed

+401
-138
lines changed

7 files changed

+401
-138
lines changed

src/main/java/cc/happyareabean/sjm/SimpleJoinMessage.java

Lines changed: 212 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,27 @@
33
import cc.happyareabean.sjm.commands.SJMCommand;
44
import cc.happyareabean.sjm.config.SJMConfig;
55
import cc.happyareabean.sjm.config.SJMMisc;
6+
import cc.happyareabean.sjm.config.SJMSync;
67
import cc.happyareabean.sjm.database.DatabaseManager;
78
import cc.happyareabean.sjm.listener.PlayerJoinListener;
89
import cc.happyareabean.sjm.listener.UpdateNotifyListener;
910
import cc.happyareabean.sjm.utils.AdventureWebEditorAPI;
1011
import cc.happyareabean.sjm.utils.Constants;
12+
import cc.happyareabean.sjm.utils.Util;
1113
import com.alessiodp.libby.BukkitLibraryManager;
1214
import lombok.Getter;
15+
import lombok.extern.slf4j.Slf4j;
1316
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
1417
import net.kyori.adventure.text.minimessage.MiniMessage;
1518
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
1619
import org.bstats.bukkit.Metrics;
20+
import org.bukkit.Bukkit;
1721
import org.bukkit.plugin.java.JavaPlugin;
1822
import org.bukkit.util.FileUtil;
1923
import org.inventivetalent.update.spiget.SpigetUpdate;
2024
import org.inventivetalent.update.spiget.UpdateCallback;
2125
import org.inventivetalent.update.spiget.comparator.VersionComparator;
26+
import org.slf4j.Logger;
2227
import revxrsal.commands.bukkit.BukkitCommandHandler;
2328

2429
import java.io.File;
@@ -29,136 +34,213 @@
2934
import java.time.format.DateTimeFormatter;
3035
import java.util.Arrays;
3136

37+
@Slf4j(topic = "SimpleJoinMessage")
3238
public class SimpleJoinMessage extends JavaPlugin {
3339

34-
public static String NEXT_VERSION = "";
35-
public static String DOWNLOAD_URL = "https://www.spigotmc.org/resources/103413/";
36-
37-
@Getter private static SimpleJoinMessage instance;
38-
@Getter public static final MiniMessage MINIMESSAGE = MiniMessage.miniMessage();
39-
@Getter public static final LegacyComponentSerializer LEGACY_SERIALIZER = LegacyComponentSerializer.legacy('&');
40-
@Getter private static BukkitAudiences adventure;
41-
@Getter private AdventureWebEditorAPI adventureWebEditorAPI;
42-
@Getter private BukkitCommandHandler commandHandler;
43-
@Getter private DatabaseManager database;
44-
@Getter private SJMConfig SJMConfig;
45-
@Getter private SJMMisc miscConfig;
46-
47-
@Override
48-
public void onEnable() {
49-
instance = this;
50-
adventure = BukkitAudiences.create(this);
51-
52-
getLogger().info("Loading libraries...");
53-
BukkitLibraryManager libraryManager = new BukkitLibraryManager(this);
54-
55-
libraryManager.addMavenCentral();
56-
libraryManager.addJitPack();
57-
58-
Arrays.stream(Dependency.values()).forEachOrdered(d -> {
59-
libraryManager.loadLibrary(d.getLibrary());
60-
});
61-
62-
getLogger().info("Loading settings...");
63-
SJMConfig = new SJMConfig(new File(getDataFolder(), "settings.yml").toPath());
64-
miscConfig = new SJMMisc(new File(getDataFolder(), "misc.yml").toPath());
65-
66-
getLogger().info("Loading database...");
67-
database = new DatabaseManager();
68-
69-
getLogger().info("Registering listener...");
70-
getServer().getPluginManager().registerEvents(new PlayerJoinListener(), this);
71-
getServer().getPluginManager().registerEvents(new UpdateNotifyListener(), this);
72-
73-
getLogger().info("Registering commands...");
74-
commandHandler = BukkitCommandHandler.create(this);
75-
commandHandler.enableAdventure(adventure);
76-
commandHandler.setMessagePrefix(LEGACY_SERIALIZER.serialize(Constants.PREFIX));
77-
commandHandler.setHelpWriter((command, actor) -> {
78-
StringBuilder sb = new StringBuilder();
79-
sb.append("&8• &e/");
80-
sb.append(command.getPath().toRealString());
81-
if (!command.getUsage().isEmpty()) {
82-
sb.append(" ");
83-
sb.append(command.getUsage());
84-
}
85-
sb.append(" &7- &f");
86-
sb.append(command.getDescription());
87-
return sb.toString();
88-
});
89-
commandHandler.register(new SJMCommand());
90-
91-
adventureWebEditorAPI = new AdventureWebEditorAPI(URI.create(this.SJMConfig.getAdventureWebURL()));
92-
93-
getLogger().info("Check supported plugins...");
94-
checkSupportedPlugin();
95-
96-
new Metrics(this, 15462);
97-
98-
getLogger().info("SimpleJoinMessage version " + getDescription().getVersion() + " has been successfully enabled!");
99-
100-
if (miscConfig.isUpdateChecker()) checkUpdate();
101-
}
102-
103-
public void checkUpdate() {
104-
String version = getDescription().getVersion();
105-
if (version.contains("-SNAPSHOT")) {
106-
Arrays.asList(
107-
"******************************************",
108-
"You are currently using development build of SimpleJoinMessage!",
109-
"Please report issues here: https://go.happyareabean.cc/sjm/issues",
110-
"******************************************"
111-
).forEach(s -> getLogger().warning(s));
112-
return;
113-
}
114-
115-
SpigetUpdate updater = new SpigetUpdate(this, 103413);
116-
updater.setVersionComparator(VersionComparator.SEM_VER);
117-
updater.checkForUpdate(new UpdateCallback() {
118-
@Override
119-
public void updateAvailable(String newVersion, String downloadUrl, boolean hasDirectDownload) {
120-
NEXT_VERSION = newVersion;
121-
122-
Arrays.asList(
123-
"******************************************",
124-
"",
125-
"There is a new version of SimpleJoinMessage available!",
126-
"",
127-
"Your Version: " + version,
128-
"New Version: " + NEXT_VERSION,
129-
"",
130-
"Download at " + downloadUrl,
131-
"",
132-
"******************************************"
133-
).forEach(s -> getLogger().warning(s));
134-
}
135-
136-
@Override
137-
public void upToDate() {
138-
getLogger().info(String.format("SimpleJoinMessage is up to date! (%s)", version));
139-
}
140-
});
141-
}
142-
143-
public void checkSupportedPlugin() {
144-
if (!isPAPISupported()) {
145-
getLogger().warning("PlaceholderAPI is not enabled, you will not be able to use any placeholder from PlaceholderAPI.");
146-
}
147-
}
148-
149-
public boolean isPAPISupported() {
150-
return getServer().getPluginManager().isPluginEnabled("PlaceholderAPI");
151-
}
152-
153-
public String regenerateSettings() {
154-
Path path = SJMConfig.getPath();
155-
String fileName = path.getFileName().toString();
156-
String timestamp = ZonedDateTime.now(ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd.HH-mm-ss"));
157-
String backupFileName = fileName + ".backup." + timestamp;
158-
FileUtil.copy(path.toFile(), path.getParent().resolve(backupFileName).toFile());
159-
if (path.toFile().delete()) {
160-
SJMConfig = new SJMConfig(path);
161-
}
162-
return backupFileName;
163-
}
40+
public static String NEXT_VERSION = "";
41+
public static String DOWNLOAD_URL = "https://www.spigotmc.org/resources/103413/";
42+
43+
@Getter
44+
private static SimpleJoinMessage instance;
45+
@Getter
46+
public static final MiniMessage MINIMESSAGE = MiniMessage.miniMessage();
47+
@Getter
48+
public static final LegacyComponentSerializer LEGACY_SERIALIZER = LegacyComponentSerializer.legacy('&');
49+
@Getter
50+
private static BukkitAudiences adventure;
51+
@Getter
52+
private AdventureWebEditorAPI adventureWebEditorAPI;
53+
@Getter
54+
private BukkitCommandHandler commandHandler;
55+
@Getter
56+
private DatabaseManager database;
57+
@Getter
58+
private SJMConfig SJMConfig;
59+
@Getter
60+
private SJMMisc miscConfig;
61+
@Getter
62+
private SJMSync syncConfig;
63+
64+
@Override
65+
public void onEnable() {
66+
instance = this;
67+
adventure = BukkitAudiences.create(this);
68+
69+
loadLibraries();
70+
loadSettings();
71+
loadDatabase();
72+
loadSync();
73+
loadMisc();
74+
75+
registerListeners();
76+
registerCommands();
77+
78+
checkUpdate();
79+
80+
getLogger().info("SimpleJoinMessage version " + getDescription().getVersion() + " has been successfully enabled!");
81+
}
82+
83+
private void loadLibraries() {
84+
getLogger().info("Loading libraries...");
85+
86+
BukkitLibraryManager libraryManager = new BukkitLibraryManager(this);
87+
88+
libraryManager.addMavenCentral();
89+
libraryManager.addJitPack();
90+
91+
Arrays.stream(Dependency.values()).forEachOrdered(d -> {
92+
libraryManager.loadLibrary(d.getLibrary());
93+
});
94+
}
95+
96+
private void loadSettings() {
97+
getLogger().info("Loading settings...");
98+
SJMConfig = new SJMConfig(new File(getDataFolder(), "settings.yml").toPath());
99+
miscConfig = new SJMMisc(new File(getDataFolder(), "misc.yml").toPath());
100+
syncConfig = new SJMSync(new File(getDataFolder(), "sync.yml").toPath());
101+
}
102+
103+
private void loadDatabase() {
104+
getLogger().info("Loading database...");
105+
database = new DatabaseManager();
106+
}
107+
108+
private void loadSync() {
109+
if (!syncConfig.isEnabled()) return;
110+
111+
getLogger().info("Loading sync...");
112+
String collection = syncConfig.getCollectionName();
113+
114+
if (collection.isEmpty()) {
115+
logger().warn("Sync is enabled, but the collection name is empty.");
116+
return;
117+
}
118+
119+
database.getContent(collection).ifPresentOrElse(data -> {
120+
getLogger().info("Updating join message from collection [%s]...".formatted(collection));
121+
122+
String content = new String(Util.BASE64_DECODER.decode(data.getContent()));
123+
SJMConfig.setJoinMessage(Arrays.stream(content.split("\n")).toList());
124+
125+
if (syncConfig.isSaveJoinMessage()) {
126+
getLogger().info("Saving config...");
127+
SJMConfig.save();
128+
getLogger().info("Config has been successfully saved.");
129+
}
130+
131+
getLogger().info("Sync completed!");
132+
}, () -> {
133+
134+
if (syncConfig.isDisablePluginIfFailed()) {
135+
Bukkit.getPluginManager().disablePlugin(this);
136+
return;
137+
}
138+
139+
getLogger().warning("The collection [%s] does not exist, skipping sync process.".formatted(collection));
140+
});
141+
}
142+
143+
private void loadMisc() {
144+
getLogger().info("Loading misc...");
145+
adventureWebEditorAPI = new AdventureWebEditorAPI(URI.create(this.SJMConfig.getAdventureWebURL()));
146+
new Metrics(this, 15462);
147+
148+
getLogger().info("Checking supported plugins...");
149+
checkSupportedPlugin();
150+
}
151+
152+
private void registerListeners() {
153+
getLogger().info("Registering listener...");
154+
getServer().getPluginManager().registerEvents(new PlayerJoinListener(), this);
155+
getServer().getPluginManager().registerEvents(new UpdateNotifyListener(), this);
156+
}
157+
158+
private void registerCommands() {
159+
getLogger().info("Registering commands...");
160+
commandHandler = BukkitCommandHandler.create(this);
161+
commandHandler.enableAdventure(adventure);
162+
commandHandler.setMessagePrefix(LEGACY_SERIALIZER.serialize(Constants.PREFIX));
163+
commandHandler.setHelpWriter((command, actor) -> {
164+
StringBuilder sb = new StringBuilder();
165+
sb.append("&8• &e/");
166+
sb.append(command.getPath().toRealString());
167+
if (!command.getUsage().isEmpty()) {
168+
sb.append(" ");
169+
sb.append(command.getUsage());
170+
}
171+
sb.append(" &7- &f");
172+
sb.append(command.getDescription());
173+
return sb.toString();
174+
});
175+
commandHandler.register(new SJMCommand());
176+
}
177+
178+
public void checkUpdate() {
179+
if (!miscConfig.isUpdateChecker()) return;
180+
181+
String version = getDescription().getVersion();
182+
if (version.contains("-SNAPSHOT")) {
183+
Arrays.asList(
184+
"******************************************",
185+
"You are currently using development build of SimpleJoinMessage!",
186+
"Please report issues here: https://go.happyareabean.cc/sjm/issues",
187+
"******************************************"
188+
).forEach(s -> getLogger().warning(s));
189+
return;
190+
}
191+
192+
SpigetUpdate updater = new SpigetUpdate(this, 103413);
193+
updater.setVersionComparator(VersionComparator.SEM_VER);
194+
updater.checkForUpdate(new UpdateCallback() {
195+
@Override
196+
public void updateAvailable(String newVersion, String downloadUrl, boolean hasDirectDownload) {
197+
NEXT_VERSION = newVersion;
198+
199+
Arrays.asList(
200+
"******************************************",
201+
"",
202+
"There is a new version of SimpleJoinMessage available!",
203+
"",
204+
"Your Version: " + version,
205+
"New Version: " + NEXT_VERSION,
206+
"",
207+
"Download at " + downloadUrl,
208+
"",
209+
"******************************************"
210+
).forEach(s -> getLogger().warning(s));
211+
}
212+
213+
@Override
214+
public void upToDate() {
215+
getLogger().info(String.format("SimpleJoinMessage is up to date! (%s)", version));
216+
}
217+
});
218+
}
219+
220+
public void checkSupportedPlugin() {
221+
if (!isPAPISupported()) {
222+
getLogger().warning("PlaceholderAPI is not enabled, you will not be able to use any placeholder from PlaceholderAPI.");
223+
}
224+
}
225+
226+
public boolean isPAPISupported() {
227+
return getServer().getPluginManager().isPluginEnabled("PlaceholderAPI");
228+
}
229+
230+
public String regenerateSettings() {
231+
Path path = SJMConfig.getPath();
232+
String fileName = path.getFileName().toString();
233+
String timestamp = ZonedDateTime.now(ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd.HH-mm-ss"));
234+
String backupFileName = fileName + ".backup." + timestamp;
235+
FileUtil.copy(path.toFile(), path.getParent().resolve(backupFileName).toFile());
236+
if (path.toFile().delete()) {
237+
SJMConfig = new SJMConfig(path);
238+
}
239+
return backupFileName;
240+
}
241+
242+
public static Logger logger() {
243+
return log;
244+
}
245+
164246
}

0 commit comments

Comments
 (0)