Skip to content

Commit

Permalink
Lock NCPAPIProvider against changing.
Browse files Browse the repository at this point in the history
(Not really the best example application for ILockable.)
  • Loading branch information
asofold committed Feb 11, 2018
1 parent 7b8390d commit 4705f75
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 23 deletions.
Expand Up @@ -15,30 +15,58 @@
package fr.neatmonster.nocheatplus;

import fr.neatmonster.nocheatplus.components.NoCheatPlusAPI;
import fr.neatmonster.nocheatplus.components.registry.lockable.ILockable;

/**
* Static API provider utility.
* @author mc_dev
*
* @author asofold
*
*/
public class NCPAPIProvider {
private static NoCheatPlusAPI noCheatPlusAPI = null;

/**
* Get the registered API instance. This will work after the plugin has loaded (onLoad), asynchronous calls should be possible, however calls after plugin disable or before it is loaded should fail.
*/
public static NoCheatPlusAPI getNoCheatPlusAPI(){
return noCheatPlusAPI;
}

/**
* Setter for the NoCheatPlusAPI instance.
* <hr>
* For internal use only (onLoad).<br>
* Setting this to anything else than the NoCheatPlus plugin instance might lead to inconsistencies.
* @param noCheatPlusAPI
*/
protected static void setNoCheatPlusAPI(NoCheatPlusAPI noCheatPlusAPI){
NCPAPIProvider.noCheatPlusAPI = noCheatPlusAPI;
}
private static NoCheatPlusAPI noCheatPlusAPI = null;
/** Support locking against changing. */
private static ILockable lockableNoCheatPlusAPI = null;

/**
* Get the registered API instance. This will work after the plugin has
* loaded (onLoad), asynchronous calls should be possible, however calls
* after plugin disable or before it is loaded should fail.
*/
public static NoCheatPlusAPI getNoCheatPlusAPI(){
return noCheatPlusAPI;
}

/**
* Set the NoCheaPlusAPI without locking against changes. Further see:
* {@link #setNoCheatPlusAPI(NoCheatPlusAPI, ILockable)}
*
* @param noCheatPlusAPI
*/
static void setNoCheatPlusAPI(NoCheatPlusAPI noCheatPlusAPI) {
setNoCheatPlusAPI(noCheatPlusAPI, null);
}

/**
* Setter for the NoCheatPlusAPI instance.
* <hr>
* For internal use only (onLoad).<br>
* Setting this to anything else than the NoCheatPlus plugin instance might
* lead to inconsistencies.
*
* @param noCheatPlusAPI
* @param lockable
* The IILockable instance to use for locking against changes.
* @throws IllegalStateException
* If the API is locked against overriding by a previously set
* ILockable instance.
*/
static void setNoCheatPlusAPI(NoCheatPlusAPI noCheatPlusAPI, ILockable lockable){
if (lockableNoCheatPlusAPI != null) {
lockableNoCheatPlusAPI.throwIfLocked();
}
NCPAPIProvider.noCheatPlusAPI = noCheatPlusAPI;
lockableNoCheatPlusAPI = lockable;
}

}
Expand Up @@ -95,6 +95,8 @@
import fr.neatmonster.nocheatplus.components.registry.feature.JoinLeaveListener;
import fr.neatmonster.nocheatplus.components.registry.feature.NCPListener;
import fr.neatmonster.nocheatplus.components.registry.feature.TickListener;
import fr.neatmonster.nocheatplus.components.registry.lockable.BasicLockable;
import fr.neatmonster.nocheatplus.components.registry.lockable.ILockable;
import fr.neatmonster.nocheatplus.components.registry.order.SetupOrder;
import fr.neatmonster.nocheatplus.config.ConfPaths;
import fr.neatmonster.nocheatplus.config.ConfigFile;
Expand Down Expand Up @@ -135,6 +137,10 @@
*/
public class NoCheatPlus extends JavaPlugin implements NoCheatPlusAPI {

private static final Object lockableAPIsecret = new Object();
private static final ILockable lockableAPI = new BasicLockable(lockableAPIsecret,
true, true, true);

private static final String MSG_NOTIFY_OFF = ChatColor.RED + "NCP: " + ChatColor.WHITE + "Notifications are turned " + ChatColor.RED + "OFF" + ChatColor.WHITE + ".";

// Static API
Expand Down Expand Up @@ -825,9 +831,7 @@ private void setupBasics() {
}
}
// API.
if (NCPAPIProvider.getNoCheatPlusAPI() == null) {
NCPAPIProvider.setNoCheatPlusAPI(this);
}
updateNoCheatPlusAPI();
// Initialize server version.
if (ServerVersion.getMinecraftVersion() == GenericVersion.UNKNOWN_VERSION) {
BukkitVersion.init();
Expand Down Expand Up @@ -856,6 +860,14 @@ public StreamID getStreamId() {
}
}

private void updateNoCheatPlusAPI() {
if (NCPAPIProvider.getNoCheatPlusAPI() == null) {
lockableAPI.unlock(lockableAPIsecret);
NCPAPIProvider.setNoCheatPlusAPI(this, lockableAPI);
lockableAPI.lock(lockableAPIsecret);
}
}

/* (non-Javadoc)
* @see org.bukkit.plugin.java.JavaPlugin#onEnable()
*/
Expand Down

0 comments on commit 4705f75

Please sign in to comment.