Skip to content

Commit

Permalink
Changed the plugin's instance inside the API to be set using a method…
Browse files Browse the repository at this point in the history
… and not reflection
  • Loading branch information
OmerBenGera committed Feb 4, 2022
1 parent a9c766c commit 0c90b0f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
Expand Up @@ -27,6 +27,20 @@ public final class SuperiorSkyblockAPI {
* Player Methods
*/

/**
* Set the plugin's instance for the API.
* Do not use this method on your own, as it may cause an undefined behavior when using the API.
*
* @param plugin The instance of the plugin to set to the API.
*/
public static void setPluginInstance(SuperiorSkyblock plugin) {
if (SuperiorSkyblockAPI.plugin != null) {
throw new UnsupportedOperationException("You cannot initialize the plugin instance after it was initialized.");
}

SuperiorSkyblockAPI.plugin = plugin;
}

/**
* Get the superior player object from a player instance.
*/
Expand Down
@@ -1,6 +1,5 @@
package com.bgsoftware.superiorskyblock;

import com.bgsoftware.common.reflection.ReflectField;
import com.bgsoftware.common.updater.Updater;
import com.bgsoftware.superiorskyblock.api.SuperiorSkyblock;
import com.bgsoftware.superiorskyblock.api.SuperiorSkyblockAPI;
Expand Down Expand Up @@ -96,7 +95,6 @@

public final class SuperiorSkyblockPlugin extends JavaPlugin implements SuperiorSkyblock {

private static final ReflectField<SuperiorSkyblock> PLUGIN = new ReflectField<>(SuperiorSkyblockAPI.class, SuperiorSkyblock.class, "plugin");
private static final Pattern LISTENER_REGISTER_FAILURE =
Pattern.compile("Plugin SuperiorSkyblock2 v(.*) has failed to register events for (.*) because (.*) does not exist\\.");

Expand Down Expand Up @@ -169,13 +167,23 @@ public void onLoad() {

initCustomFilter();

PLUGIN.set(null, this);
try {
SuperiorSkyblockAPI.setPluginInstance(this);
} catch (UnsupportedOperationException error) {
log("&cThe API instance was already initialized. " +
"This can be caused by a reload or another plugin initializing it.");
shouldEnable = false;
}

if (!loadNMSAdapter()) {
shouldEnable = false;
}

Runtime.getRuntime().addShutdownHook(new ShutdownTask(this));

IslandPrivileges.registerPrivileges();
SortingTypes.registerSortingTypes();
IslandFlags.registerFlags();
}

@Override
Expand Down Expand Up @@ -233,9 +241,6 @@ public void onEnable() {

Executor.init(this);

IslandPrivileges.registerPrivileges();
SortingTypes.registerSortingTypes();
IslandFlags.registerFlags();
loadUpgradeCostLoaders();

EnchantsUtils.registerGlowEnchantment();
Expand Down

0 comments on commit 0c90b0f

Please sign in to comment.