Skip to content

Commit 6e7e22f

Browse files
committed
Fix SQL database closure when plugin disables
1 parent 0862a27 commit 6e7e22f

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

src/main/java/xyz/srnyx/annoyingapi/AnnoyingPlugin.java

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -175,26 +175,45 @@ public final void onEnable() {
175175
*/
176176
@Override
177177
public final void onDisable() {
178-
if (dataManager != null && dataManager.storageConfig.cache.saveOn.contains(StorageConfig.SaveOn.DISABLE)) dataManager.dialect.saveCache();
178+
if (dataManager != null) {
179+
// Save cache
180+
if (dataManager.storageConfig.cache.saveOn.contains(StorageConfig.SaveOn.DISABLE)) dataManager.dialect.saveCache();
181+
// Close connection (if SQL)
182+
if (dataManager.dialect instanceof SQLDialect) try {
183+
((SQLDialect) dataManager.dialect).connection.close();
184+
} catch (final SQLException e) {
185+
log(Level.SEVERE, "Failed to close the database connection", e);
186+
}
187+
}
188+
189+
// Run custom onDisable
179190
disable();
180191
}
181192

182193
/**
183194
* Called when the plugin is loaded
195+
*
196+
* @see #onLoad()
184197
*/
185198
public void load() {
186199
// This method is meant to be overridden
187200
}
188201

189202
/**
190-
* Called after dependency checks, start-up messages, and command/listener registration.
203+
* Called after dependency checks, start-up messages, and command/listener registration
204+
*
205+
* @see #onEnable()
206+
* @see #enablePlugin()
191207
*/
192208
public void enable() {
193209
// This method is meant to be overridden
194210
}
195211

196212
/**
197213
* Called when the plugin is disabled
214+
*
215+
* @see #onDisable()
216+
* @see #disablePlugin()
198217
*/
199218
public void disable() {
200219
// This method is meant to be overridden
@@ -211,7 +230,7 @@ public void reload() {
211230

212231
/**
213232
* Plugin enabling stuff
214-
* <b><p>Do not try to override this method! Override {@link #enable()} instead</b>
233+
* <b><p>Do not override this method! Override {@link #enable()} instead</b>
215234
*
216235
* @see #enable()
217236
*/
@@ -291,10 +310,20 @@ private void enablePlugin() {
291310
enable();
292311
}
293312

313+
/**
314+
* Runs {@link PluginManager#disablePlugin(Plugin)} with {@code this} as the plugin
315+
* <br><b>Do not override this method! Override {@link #disable()} instead</b>
316+
*
317+
* @see #disable()
318+
*/
319+
public void disablePlugin() {
320+
Bukkit.getPluginManager().disablePlugin(this);
321+
}
322+
294323
/**
295324
* Reloads the plugin ({@link #messages}, etc...). This will not trigger {@link #onLoad()} or {@link #onEnable()}
296325
* <p>This is not run automatically (such as {@link #onEnable()} and {@link #onDisable()}), it is to be used manually by the plugin itself (ex: in a {@code /reload} command)
297-
* <p><b>Do not try to override this method! Override {@link #reload()} instead</b>
326+
* <p><b>Do not override this method! Override {@link #reload()} instead</b>
298327
*
299328
* @see #reload()
300329
*/
@@ -315,17 +344,6 @@ public void loadMessages() {
315344
if (section != null) section.getKeys(false).forEach(key -> globalPlaceholders.put(key, section.getString(key)));
316345
}
317346

318-
/**
319-
* Disables the plugin. Unregisters commands/listeners, cancels tasks, and then runs {@link PluginManager#disablePlugin(Plugin)}
320-
* <p><i>This is not meant to be overriden, only override if you know what you're doing!</i>
321-
*/
322-
public void disablePlugin() {
323-
new HashSet<>(registeredClasses).forEach(Registrable::unregister);
324-
Bukkit.getScheduler().cancelTasks(this);
325-
if (dataManager != null) dataManager.dialect.saveCache();
326-
Bukkit.getPluginManager().disablePlugin(this);
327-
}
328-
329347
/**
330348
* Gets a string from {@link #messages} with the specified key
331349
*

src/main/java/xyz/srnyx/annoyingapi/data/storage/StorageConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,13 +221,13 @@ public enum SaveOn {
221221
/**
222222
* Saves the cache on plugin reload
223223
*
224-
* @see AnnoyingPlugin#reloadPlugin()
224+
* @see AnnoyingPlugin#reload()
225225
*/
226226
RELOAD,
227227
/**
228228
* Saves the cache on plugin disable
229229
*
230-
* @see AnnoyingPlugin#disablePlugin()
230+
* @see AnnoyingPlugin#disable()
231231
*/
232232
DISABLE,
233233
/**

0 commit comments

Comments
 (0)