Skip to content

Commit

Permalink
Remove dummy instance in CitizensAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Jan 11, 2016
1 parent 8b18b42 commit eee985d
Showing 1 changed file with 33 additions and 43 deletions.
76 changes: 33 additions & 43 deletions src/main/java/net/citizensnpcs/api/CitizensAPI.java
Expand Up @@ -2,6 +2,10 @@

import java.io.File;

import org.bukkit.Bukkit;
import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin;

import net.citizensnpcs.api.ai.speech.SpeechFactory;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.NPCDataStore;
Expand All @@ -10,45 +14,36 @@
import net.citizensnpcs.api.scripting.ScriptCompiler;
import net.citizensnpcs.api.trait.TraitFactory;

import org.bukkit.Bukkit;
import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin;

/**
* Contains methods used in order to utilize the Citizens API.
*/
public final class CitizensAPI {
private CitizensPlugin implementation;

private CitizensAPI() {
}

/**
* Creates a new <em>anonymous</em> {@link NPCRegistry} with its own set of
* {@link NPC}s. This is not stored by the Citizens plugin.
*
* Creates a new <em>anonymous</em> {@link NPCRegistry} with its own set of {@link NPC}s. This is not stored by the
* Citizens plugin.
*
* @since 2.0.8
* @param store
* The {@link NPCDataStore} to use with the registry
* @return A new anonymous NPCRegistry that is not accessible via
* {@link #getPluginNPCRegistry(String)}
* @return A new anonymous NPCRegistry that is not accessible via {@link #getPluginNPCRegistry(String)}
*/
public static NPCRegistry createAnonymousNPCRegistry(NPCDataStore store) {
return getImplementation().createAnonymousNPCRegistry(store);
}

/**
* Creates a new {@link NPCRegistry} with its own set of {@link NPC}s. This
* is stored in memory with the Citizens plugin, and can be accessed via
* {@link #getNamedNPCRegistry(String)}.
*
* Creates a new {@link NPCRegistry} with its own set of {@link NPC}s. This is stored in memory with the Citizens
* plugin, and can be accessed via {@link #getNamedNPCRegistry(String)}.
*
* @param name
* The plugin name
* @param store
* The {@link NPCDataStore} to use with the registry
* @since 2.0.8
* @return A new NPCRegistry, that can also be retrieved via
* {@link #getPluginNPCRegistry(String)}
* @return A new NPCRegistry, that can also be retrieved via {@link #getPluginNPCRegistry(String)}
*/
public static NPCRegistry createNamedNPCRegistry(String name, NPCDataStore store) {
return getImplementation().createNamedNPCRegistry(name, store);
Expand All @@ -66,25 +61,23 @@ public static NPCSelector getDefaultNPCSelector() {
}

private static CitizensPlugin getImplementation() {
if (instance.implementation == null)
if (instance == null)
throw new IllegalStateException("no implementation set");
return instance.implementation;
return instance;
}

private static ClassLoader getImplementationClassLoader() {
return getImplementation().getOwningClassLoader();
}

/**
* Retrieves the {@link NPCRegistry} previously created via
* {@link #createNamedNPCRegistry(String)} with the given name, or null if
* not found.
*
* Retrieves the {@link NPCRegistry} previously created via {@link #createNamedNPCRegistry(String)} with the given
* name, or null if not found.
*
* @param name
* The registry name
* @since 2.0.8
* @return A NPCRegistry previously created via
* {@link #createNamedNPCRegistry(String)}, or null if not found
* @return A NPCRegistry previously created via {@link #createNamedNPCRegistry(String)}, or null if not found
*/
public static NPCRegistry getNamedNPCRegistry(String name) {
return getImplementation().getNamedNPCRegistry(name);
Expand All @@ -96,7 +89,7 @@ public static Iterable<NPCRegistry> getNPCRegistries() {

/**
* Gets the current implementation's <em>default</em> {@link NPCRegistry}.
*
*
* @return The NPC registry
*/
public static NPCRegistry getNPCRegistry() {
Expand Down Expand Up @@ -129,7 +122,7 @@ public static File getScriptFolder() {

/**
* Gets the current implementation's {@link SpeechFactory}.
*
*
* @see CitizensPlugin
* @return Citizens speech factory
*/
Expand All @@ -139,7 +132,7 @@ public static SpeechFactory getSpeechFactory() {

/**
* Gets the current implementation's {@link TraitFactory}.
*
*
* @see CitizensPlugin
* @return Citizens trait factory
*/
Expand All @@ -151,13 +144,12 @@ public static TraitFactory getTraitFactory() {
* @return Whether a Citizens implementation is currently present
*/
public static boolean hasImplementation() {
return instance.implementation != null;
return instance != null;
}

/**
* A helper method for registering events using the current implementation's
* {@link Plugin}.
*
* A helper method for registering events using the current implementation's {@link Plugin}.
*
* @see #getPlugin()
* @param listener
* The listener to register events for
Expand All @@ -168,28 +160,27 @@ public static void registerEvents(Listener listener) {
}

/**
* Removes any previously created {@link NPCRegistry} stored under the given
* name.
*
* Removes any previously created {@link NPCRegistry} stored under the given name.
*
* @since 2.0.8
* @param name
* The name previously given to
* {@link #createNamedNPCRegistry(String)}
* The name previously given to {@link #createNamedNPCRegistry(String)}
*/
public static void removeNamedNPCRegistry(String name) {
getImplementation().removeNamedNPCRegistry(name);
}

/**
* Sets the current Citizens implementation.
*
*
* @param implementation
* The new implementation
*/
public static void setImplementation(CitizensPlugin implementation) {
if (implementation != null && hasImplementation())
if (implementation != null && hasImplementation()) {
getImplementation().onImplementationChanged();
instance.implementation = implementation;
}
instance = implementation;
}

/**
Expand All @@ -198,12 +189,11 @@ public static void setImplementation(CitizensPlugin implementation) {
public static void shutdown() {
if (scriptCompiler == null)
return;
instance.implementation = null;
instance = null;
scriptCompiler.interrupt();
scriptCompiler = null;
}

private static final CitizensAPI instance = new CitizensAPI();

private static CitizensPlugin instance = null;
private static ScriptCompiler scriptCompiler;
}

0 comments on commit eee985d

Please sign in to comment.