feat: Properly cleanup AbstractMVPlugin

This commit is contained in:
Ben Woo 2023-02-16 10:46:01 +08:00
parent 3fc3663d5d
commit 79ae8754dc
4 changed files with 76 additions and 84 deletions

View File

@ -249,19 +249,10 @@ public class MultiverseCore extends JavaPlugin implements MVCore {
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public MultiverseCore getCore() { public MVCore getCore() {
return this; return this;
} }
/**
* {@inheritDoc}
*/
@Override
public void setCore(MultiverseCore core) {
// This method is required by the interface (so core is effectively a plugin of itself) and therefore
// this is never used.
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */

View File

@ -1,95 +1,105 @@
package com.onarandombox.MultiverseCore.api; package com.onarandombox.MultiverseCore.api;
import java.io.File;
import java.io.IOException;
import java.util.List; import java.util.List;
import com.onarandombox.MultiverseCore.MultiverseCore; import com.dumptruckman.minecraft.util.Logging;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
/** /**
* Make things easier for MV-Plugins! * Make things easier for Multiverse Plugins.
*/ */
public abstract class AbstractMVPlugin extends JavaPlugin implements MVPlugin { public abstract class AbstractMVPlugin extends JavaPlugin implements MVPlugin {
private MultiverseCore core; private MVCore core;
/** /**
* {@inheritDoc} * {@inheritDoc}
* *
* Note: You can't override this, use {@link #onPluginEnable()} instead! * Note: You should not override this, use {@link #onMVPluginEnable()} instead!
* @see #onPluginEnable() * @see #onMVPluginEnable()
*/ */
@Override @Override
public final void onEnable() { public final void onEnable() {
MultiverseCore theCore = (MultiverseCore) this.getServer().getPluginManager().getPlugin("Multiverse-Core"); MVCore mvCore = (MVCore) this.getServer().getPluginManager().getPlugin("Multiverse-Core");
if (theCore == null) { if (mvCore == null) {
this.getLogger().severe("Core not found! The plugin dev needs to add a dependency!"); Logging.severe("Core not found! You must have Multiverse-Core installed to use this plugin!");
this.getLogger().severe("Disabling!"); Logging.severe("Grab a copy at: ");
Logging.severe("https://dev.bukkit.org/projects/multiverse-core");
Logging.severe("Disabling!");
this.getServer().getPluginManager().disablePlugin(this); this.getServer().getPluginManager().disablePlugin(this);
return; return;
} }
if (theCore.getProtocolVersion() < this.getProtocolVersion()) { if (mvCore.getProtocolVersion() < this.getProtocolVersion()) {
this.getLogger().severe("You need a newer version of Multiverse-Core!"); Logging.severe("Your Multiverse-Core is OUT OF DATE");
this.getLogger().severe("Disabling!"); Logging.severe("This version of " + this.getDescription().getName() + " requires Protocol Level: " + this.getProtocolVersion());
Logging.severe("Your of Core Protocol Level is: " + this.core.getProtocolVersion());
Logging.severe("Grab an updated copy at: ");
Logging.severe("https://dev.bukkit.org/projects/multiverse-core");
Logging.severe("Disabling!");
this.getServer().getPluginManager().disablePlugin(this); this.getServer().getPluginManager().disablePlugin(this);
return; return;
} }
this.setCore(theCore); this.core = mvCore;
this.core.incrementPluginCount();
this.getServer().getLogger().info(String.format("%s - Version %s enabled - By %s", this.onMVPluginEnable();
this.getDescription().getName(), this.getDescription().getVersion(), getAuthors())); Logging.config("Version %s (API v%s) Enabled - By %s", this.getDescription().getVersion(), getProtocolVersion(), getAuthors());
getDataFolder().mkdirs();
File debugLogFile = new File(getDataFolder(), "debug.log");
try {
debugLogFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
this.onPluginEnable();
} }
/** /**
* Parse the Authors Array into a readable String with ',' and 'and'. * {@inheritDoc}
* *
* @return The readable authors-{@link String} * Note: You should not override this, use {@link #onMVPluginDisable()} instead!
* @see #onMVPluginDisable()
*/ */
protected String getAuthors() { @Override
String authors = ""; public void onDisable() {
List<String> auths = this.getDescription().getAuthors(); this.core.decrementPluginCount();
if (auths.size() == 0) { this.onMVPluginDisable();
return "";
}
if (auths.size() == 1) {
return auths.get(0);
}
for (int i = 0; i < auths.size(); i++) {
if (i == this.getDescription().getAuthors().size() - 1) {
authors += " and " + this.getDescription().getAuthors().get(i);
} else {
authors += ", " + this.getDescription().getAuthors().get(i);
}
}
return authors.substring(2);
} }
/** /**
* Called when the plugin is enabled. * Called when the plugin is enabled.
* @see #onEnable() * @see #onEnable()
*/ */
protected abstract void onPluginEnable(); protected abstract void onMVPluginEnable();
/**
* Called when the plugin is disabled.
* @see #onDisable()
*/
protected abstract void onMVPluginDisable();
/**
* {@inheritDoc}
*/
@Override @Override
public final MultiverseCore getCore() { public String getAuthors() {
if (this.core == null) List<String> authorsList = this.getDescription().getAuthors();
throw new IllegalStateException("Core is null!"); if (authorsList.size() == 0) {
return "";
}
StringBuilder authors = new StringBuilder();
authors.append(authorsList.get(0));
for (int i = 1; i < authorsList.size(); i++) {
if (i == authorsList.size() - 1) {
authors.append(" and ").append(authorsList.get(i));
} else {
authors.append(", ").append(authorsList.get(i));
}
}
return authors.toString();
}
/**
* {@inheritDoc}
*/
@Override
public final MVCore getCore() {
if (this.core == null) {
throw new IllegalStateException("MVCore is null!");
}
return this.core; return this.core;
} }
@Override
public final void setCore(MultiverseCore core) {
this.core = core;
}
} }

View File

@ -113,13 +113,6 @@ public interface MVCore extends MVPlugin {
*/ */
int getPluginCount(); int getPluginCount();
/**
* Parse the Authors Array into a readable String with ',' and 'and'.
*
* @return The readable authors-{@link String}
*/
String getAuthors();
/** /**
* Gets the {@link BlockSafety} this {@link MVCore} is using. * Gets the {@link BlockSafety} this {@link MVCore} is using.
* @return The {@link BlockSafety} this {@link MVCore} is using. * @return The {@link BlockSafety} this {@link MVCore} is using.

View File

@ -7,8 +7,6 @@
package com.onarandombox.MultiverseCore.api; package com.onarandombox.MultiverseCore.api;
import com.onarandombox.MultiverseCore.MultiverseCore;
/** /**
* This interface is implemented by every official Multiverse-plugin. * This interface is implemented by every official Multiverse-plugin.
*/ */
@ -18,14 +16,7 @@ public interface MVPlugin {
* *
* @return A valid {@link com.onarandombox.MultiverseCore}. * @return A valid {@link com.onarandombox.MultiverseCore}.
*/ */
MultiverseCore getCore(); MVCore getCore();
/**
* Sets the reference to MultiverseCore.
*
* @param core A valid {@link com.onarandombox.MultiverseCore}.
*/
void setCore(MultiverseCore core);
/** /**
* Allows Multiverse or a plugin to query another Multiverse plugin to see what version its protocol is. This * Allows Multiverse or a plugin to query another Multiverse plugin to see what version its protocol is. This
@ -35,4 +26,11 @@ public interface MVPlugin {
* @return The Integer protocol version. * @return The Integer protocol version.
*/ */
int getProtocolVersion(); int getProtocolVersion();
/**
* Parse the Authors Array into a readable String with ',' and 'and'.
*
* @return The readable authors-{@link String}
*/
String getAuthors();
} }