Skip to content

Commit 1d294c5

Browse files
committed
Refactor CoreProtect.java
1 parent 779b1ac commit 1d294c5

File tree

4 files changed

+371
-185
lines changed

4 files changed

+371
-185
lines changed
+12-185
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,18 @@
11
package net.coreprotect;
22

33
import java.io.File;
4-
import java.util.Iterator;
5-
import java.util.Map.Entry;
64

7-
import org.bstats.bukkit.MetricsLite;
8-
import org.bukkit.Bukkit;
9-
import org.bukkit.Location;
10-
import org.bukkit.block.data.BlockData;
11-
import org.bukkit.entity.Player;
12-
import org.bukkit.plugin.PluginDescriptionFile;
135
import org.bukkit.plugin.java.JavaPlugin;
146

15-
import net.coreprotect.command.CommandHandler;
16-
import net.coreprotect.command.TabHandler;
17-
import net.coreprotect.config.Config;
187
import net.coreprotect.config.ConfigHandler;
19-
import net.coreprotect.consumer.Consumer;
20-
import net.coreprotect.consumer.process.Process;
21-
import net.coreprotect.language.Language;
228
import net.coreprotect.language.Phrase;
23-
import net.coreprotect.listener.ListenerHandler;
24-
import net.coreprotect.listener.player.PlayerQuitListener;
25-
import net.coreprotect.paper.PaperAdapter;
26-
import net.coreprotect.thread.CacheHandler;
27-
import net.coreprotect.thread.NetworkHandler;
28-
import net.coreprotect.thread.Scheduler;
9+
import net.coreprotect.services.PluginInitializationService;
10+
import net.coreprotect.services.ShutdownService;
2911
import net.coreprotect.utility.Chat;
30-
import net.coreprotect.utility.ChatUtils;
31-
import net.coreprotect.utility.Color;
32-
import net.coreprotect.utility.Teleport;
33-
import net.coreprotect.utility.VersionUtils;
3412

13+
/**
14+
* Main class for the CoreProtect plugin
15+
*/
3516
public final class CoreProtect extends JavaPlugin {
3617

3718
private static CoreProtect instance;
@@ -45,7 +26,7 @@ public static CoreProtect getInstance() {
4526
return instance;
4627
}
4728

48-
private CoreProtectAPI api = new CoreProtectAPI();
29+
private final CoreProtectAPI api = new CoreProtectAPI();
4930

5031
/**
5132
* Get the CoreProtect API
@@ -58,176 +39,22 @@ public CoreProtectAPI getAPI() {
5839

5940
@Override
6041
public void onEnable() {
42+
// Set plugin instance and data folder path
6143
instance = this;
6244
ConfigHandler.path = this.getDataFolder().getPath() + File.separator;
63-
Language.loadPhrases();
6445

65-
boolean start = performVersionChecks();
66-
if (start) {
67-
try {
68-
Consumer.initialize(); // Prepare consumer (keep this here)
69-
new ListenerHandler(this);
70-
getCommand("coreprotect").setExecutor(CommandHandler.getInstance());
71-
getCommand("coreprotect").setTabCompleter(new TabHandler());
72-
getCommand("core").setExecutor(CommandHandler.getInstance());
73-
getCommand("core").setTabCompleter(new TabHandler());
74-
getCommand("co").setExecutor(CommandHandler.getInstance());
75-
getCommand("co").setTabCompleter(new TabHandler());
46+
// Initialize plugin using the initialization service
47+
boolean initialized = PluginInitializationService.initializePlugin(this);
7648

77-
boolean exists = (new File(ConfigHandler.path)).exists();
78-
if (!exists) {
79-
new File(ConfigHandler.path).mkdir();
80-
}
81-
start = ConfigHandler.performInitialization(true); // Perform any necessary initialization
82-
}
83-
catch (Exception e) {
84-
e.printStackTrace();
85-
start = false;
86-
}
87-
}
88-
89-
if (start) {
90-
PluginDescriptionFile pluginDescription = this.getDescription();
91-
ChatUtils.sendConsoleComponentStartup(Bukkit.getServer().getConsoleSender(), Phrase.build(Phrase.ENABLE_SUCCESS, ConfigHandler.EDITION_NAME));
92-
if (Config.getGlobal().MYSQL) {
93-
Chat.console(Phrase.build(Phrase.USING_MYSQL));
94-
}
95-
else {
96-
Chat.console(Phrase.build(Phrase.USING_SQLITE));
97-
}
98-
99-
Chat.console("--------------------");
100-
Chat.console(Phrase.build(Phrase.ENJOY_COREPROTECT, pluginDescription.getName()));
101-
Chat.console(Phrase.build(Phrase.LINK_DISCORD, "www.coreprotect.net/discord/"));
102-
Chat.console("--------------------");
103-
104-
Scheduler.scheduleSyncDelayedTask(this, () -> {
105-
try {
106-
Thread networkHandler = new Thread(new NetworkHandler(true, true));
107-
networkHandler.start();
108-
}
109-
catch (Exception e) {
110-
e.printStackTrace();
111-
}
112-
}, 0);
113-
114-
Thread cacheCleanUpThread = new Thread(new CacheHandler());
115-
cacheCleanUpThread.start();
116-
117-
Consumer.startConsumer();
118-
119-
// Enabling bStats
120-
try {
121-
new MetricsLite(this, 2876);
122-
}
123-
catch (Exception e) {
124-
// Failed to connect to bStats server or something else went wrong.
125-
}
126-
}
127-
else {
49+
// Disable plugin if initialization failed
50+
if (!initialized) {
12851
Chat.console(Phrase.build(Phrase.ENABLE_FAILED, ConfigHandler.EDITION_NAME));
12952
getServer().getPluginManager().disablePlugin(this);
13053
}
13154
}
13255

13356
@Override
13457
public void onDisable() {
135-
safeShutdown(this);
58+
ShutdownService.safeShutdown(this);
13659
}
137-
138-
private static boolean performVersionChecks() {
139-
try {
140-
String[] bukkitVersion = Bukkit.getServer().getBukkitVersion().split("[-.]");
141-
if (VersionUtils.newVersion(bukkitVersion[0] + "." + bukkitVersion[1], ConfigHandler.MINECRAFT_VERSION)) {
142-
Chat.console(Phrase.build(Phrase.VERSION_REQUIRED, "Minecraft", ConfigHandler.MINECRAFT_VERSION));
143-
return false;
144-
}
145-
if (VersionUtils.newVersion(ConfigHandler.LATEST_VERSION, bukkitVersion[0] + "." + bukkitVersion[1]) && VersionUtils.isBranch("master")) {
146-
Chat.console(Phrase.build(Phrase.VERSION_INCOMPATIBLE, "Minecraft", bukkitVersion[0] + "." + bukkitVersion[1]));
147-
return false;
148-
}
149-
String[] javaVersion = (System.getProperty("java.version").replaceAll("[^0-9.]", "") + ".0").split("\\.");
150-
if (VersionUtils.newVersion(javaVersion[0] + "." + javaVersion[1], ConfigHandler.JAVA_VERSION)) {
151-
Chat.console(Phrase.build(Phrase.VERSION_REQUIRED, "Java", ConfigHandler.JAVA_VERSION));
152-
return false;
153-
}
154-
155-
if (ConfigHandler.EDITION_BRANCH.length() == 0) {
156-
Chat.sendConsoleMessage(Color.RED + "[CoreProtect] " + Phrase.build(Phrase.INVALID_BRANCH_1));
157-
Chat.sendConsoleMessage(Color.GREY + "[CoreProtect] " + Phrase.build(Phrase.INVALID_BRANCH_2));
158-
Chat.sendConsoleMessage(Color.GREY + "[CoreProtect] " + Phrase.build(Phrase.INVALID_BRANCH_3));
159-
return false;
160-
}
161-
162-
ConfigHandler.SERVER_VERSION = Integer.parseInt(bukkitVersion[1]);
163-
}
164-
catch (Exception e) {
165-
e.printStackTrace();
166-
return false;
167-
}
168-
169-
return true;
170-
}
171-
172-
private static void safeShutdown(CoreProtect plugin) {
173-
try {
174-
/* if server is stopping, log disconnections of online players */
175-
if (ConfigHandler.serverRunning && PaperAdapter.ADAPTER.isStopping(plugin.getServer())) {
176-
for (Player player : plugin.getServer().getOnlinePlayers()) {
177-
PlayerQuitListener.queuePlayerQuit(player);
178-
}
179-
}
180-
181-
if (!ConfigHandler.isFolia) {
182-
Iterator<Entry<Location, BlockData>> iterator = Teleport.revertBlocks.entrySet().iterator();
183-
while (iterator.hasNext()) {
184-
Entry<Location, BlockData> entry = iterator.next();
185-
entry.getKey().getBlock().setBlockData(entry.getValue());
186-
iterator.remove();
187-
}
188-
}
189-
190-
ConfigHandler.serverRunning = false;
191-
long shutdownTime = System.currentTimeMillis();
192-
long alertTime = shutdownTime + (10 * 1000);
193-
if (ConfigHandler.converterRunning) {
194-
Chat.console(Phrase.build(Phrase.FINISHING_CONVERSION));
195-
}
196-
else {
197-
Chat.console(Phrase.build(Phrase.FINISHING_LOGGING));
198-
}
199-
200-
if (ConfigHandler.migrationRunning) {
201-
ConfigHandler.purgeRunning = false;
202-
}
203-
while ((Consumer.isRunning() || ConfigHandler.converterRunning) && !ConfigHandler.purgeRunning) {
204-
long time = System.currentTimeMillis();
205-
if (time >= alertTime) {
206-
if (!ConfigHandler.converterRunning) {
207-
int consumerId = (Consumer.currentConsumer == 1) ? 1 : 0;
208-
int consumerCount = Consumer.getConsumerSize(consumerId) + Process.getCurrentConsumerSize();
209-
Chat.console(Phrase.build(Phrase.LOGGING_ITEMS, String.format("%,d", consumerCount)));
210-
}
211-
alertTime = alertTime + (30 * 1000);
212-
}
213-
else if (!ConfigHandler.databaseReachable && (time - shutdownTime) >= (5 * 60 * 1000)) {
214-
Chat.console(Phrase.build(Phrase.DATABASE_UNREACHABLE));
215-
break;
216-
}
217-
else if ((time - shutdownTime) >= (15 * 60 * 1000)) {
218-
Chat.console(Phrase.build(Phrase.LOGGING_TIME_LIMIT));
219-
break;
220-
}
221-
222-
Thread.sleep(100);
223-
}
224-
225-
ConfigHandler.performDisable();
226-
Chat.console(Phrase.build(Phrase.DISABLE_SUCCESS, "CoreProtect v" + plugin.getDescription().getVersion()));
227-
}
228-
catch (Exception e) {
229-
e.printStackTrace();
230-
}
231-
}
232-
23360
}

0 commit comments

Comments
 (0)