Skip to content

Commit

Permalink
Initialize extensions earlier during plugin load
Browse files Browse the repository at this point in the history
Moves extension initialization to the plugin's onLoad() method. This enables extensions to process things earlier in the loading process using the new extension onLoad() method.

Moves version assignment earlier so that it can be used for upgrade tasks. This is much faster than loadSelfVersion(), saving tens of milliseconds on the load time.
  • Loading branch information
PseudoKnight committed Mar 25, 2024
1 parent ed6f397 commit 5979012
Showing 1 changed file with 27 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ public class CommandHelperPlugin extends JavaPlugin {
private static int hostnameThreadPoolID = 0;
private boolean firstLoad = true;
private long interpreterUnlockedUntil = 0;
private Thread loadingThread;

/**
* Listener for the plugin system.
Expand All @@ -128,6 +127,8 @@ public void onLoad() {
AppsApiUtil.ConfigureDefaults();
Implementation.setServerType(Implementation.Type.BUKKIT);

myServer = BukkitMCServer.Get();
version = new SimpleVersion(getDescription().getVersion());
self = this;

CommandHelperFileLocations.setDefault(new CommandHelperFileLocations());
Expand All @@ -142,7 +143,7 @@ public void onLoad() {
@Override
public boolean doRun() {
try {
version = "versionUpgrade-" + Static.loadSelfVersion();
version = "versionUpgrade-" + CommandHelperPlugin.version;
return !hasBreadcrumb(version);
} catch (Exception ex) {
getLogger().log(Level.SEVERE, null, ex);
Expand Down Expand Up @@ -269,12 +270,12 @@ public void run() {

try {
Prefs.init(CommandHelperFileLocations.getDefault().getPreferencesFile());
Prefs.SetColors();
} catch (IOException ex) {
getLogger().log(Level.SEVERE, null, ex);
return;
}

Prefs.SetColors();
Installer.Install(CommandHelperFileLocations.getDefault().getConfigDirectory());

ClassDiscoveryCache cdc = new ClassDiscoveryCache(CommandHelperFileLocations.getDefault().getCacheDirectory());
Expand All @@ -289,7 +290,7 @@ public void run() {
Telemetry.GetDefault().log(DefaultTelemetry.StartupModeMetric.class,
MapBuilder.start("mode", Implementation.GetServerType().getBranding()), null);

loadingThread = new Thread("extensionloader") {
Thread loadingThread = new Thread("extensionloader") {
@Override
public void run() {
ExtensionManager.AddDiscoveryLocation(CommandHelperFileLocations.getDefault().getExtensionsDirectory());
Expand All @@ -302,8 +303,6 @@ public void run() {
};
loadingThread.start();

myServer = BukkitMCServer.Get();

// Build dynamic enums
BukkitMCEntityType.build();
BukkitMCBiomeType.build();
Expand All @@ -317,13 +316,21 @@ public void run() {
BukkitMCTrimMaterial.build();
BukkitMCTrimPattern.build();
}
}

/**
* Called on plugin enable.
*/
@Override
public void onEnable() {
// Suppress warnings from the PluginClassLoader when extensions load classes from another plugin.
// This should be done before ExtensionManager.Initialize().
try {
Class cls = Class.forName("org.bukkit.plugin.java.PluginClassLoader");
Set<String> ignored = (Set<String>) ReflectionUtils.get(cls, getClassLoader(), "seenIllegalAccess");
for(Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
if(plugin != self) {
ignored.add(plugin.getName());
}
}
} catch (ReflectionUtils.ReflectionException | ClassNotFoundException ex) {
// The server may still log class load warnings for unlisted dependencies.
}

if(loadingThread.isAlive()) {
getLogger().log(Level.INFO, "Waiting for extension caching to complete...");
try {
Expand All @@ -332,25 +339,15 @@ public void onEnable() {
getLogger().log(Level.SEVERE, null, ex);
}
}
ExtensionManager.Initialize(ClassDiscovery.getDefaultInstance());
getLogger().log(Level.INFO, "Extensions initialized.");
}

if(firstLoad) {
// Suppress warnings from the PluginClassLoader when extensions load classes from another plugin.
// This should be done before ExtensionManager.Initialize().
try {
Class cls = Class.forName("org.bukkit.plugin.java.PluginClassLoader");
Set<String> ignored = (Set<String>) ReflectionUtils.get(cls, getClassLoader(), "seenIllegalAccess");
for(Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
if(plugin != self) {
ignored.add(plugin.getName());
}
}
} catch (ReflectionUtils.ReflectionException | ClassNotFoundException ex) {
// The server may still log class load warnings for unlisted dependencies.
}

ExtensionManager.Initialize(ClassDiscovery.getDefaultInstance());
getLogger().log(Level.INFO, "Extensions initialized.");
}
/**
* Called on plugin enable.
*/
@Override
public void onEnable() {

//Metrics
Metrics m = new Metrics(this, 2987);
Expand All @@ -362,8 +359,6 @@ public void onEnable() {
+ " Consider turning this off if you can.");
}

version = new SimpleVersion(getDescription().getVersion());

if(Prefs.ShowSplashScreen()) {
StreamUtils.GetSystemOut().println(TermColors.reset());
StreamUtils.GetSystemOut().println("\n\n" + Static.Logo());
Expand Down

0 comments on commit 5979012

Please sign in to comment.