Lock NCPAPIProvider against changing.

(Not really the best example application for ILockable.)
This commit is contained in:
asofold 2018-02-11 13:39:30 +01:00
parent 7b8390dd5a
commit 4705f75fa0
2 changed files with 63 additions and 23 deletions

View File

@ -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;
}
}

View File

@ -95,6 +95,8 @@ import fr.neatmonster.nocheatplus.components.registry.feature.IRegisterAsGeneric
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;
@ -135,6 +137,10 @@ import fr.neatmonster.nocheatplus.utilities.map.BlockProperties;
*/
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
@ -825,9 +831,7 @@ public class NoCheatPlus extends JavaPlugin implements NoCheatPlusAPI {
}
}
// API.
if (NCPAPIProvider.getNoCheatPlusAPI() == null) {
NCPAPIProvider.setNoCheatPlusAPI(this);
}
updateNoCheatPlusAPI();
// Initialize server version.
if (ServerVersion.getMinecraftVersion() == GenericVersion.UNKNOWN_VERSION) {
BukkitVersion.init();
@ -856,6 +860,14 @@ public class NoCheatPlus extends JavaPlugin implements NoCheatPlusAPI {
}
}
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()
*/