Added API to the plugin

This commit is contained in:
OmerBenGera 2020-09-18 16:54:09 +03:00
parent ce6511332b
commit c4973486c9
2 changed files with 75 additions and 6 deletions

View File

@ -1,7 +1,44 @@
package com.bgsoftware.wildloaders.api;
import com.bgsoftware.wildloaders.api.loaders.ChunkLoader;
import org.bukkit.Chunk;
import org.bukkit.Location;
import java.util.Optional;
public final class WildLoadersAPI {
private static WildLoaders instance;
/**
* Get an active chunk loader from a chunk.
* @param chunk The chunk to check.
*/
public static Optional<ChunkLoader> getChunkLoader(Chunk chunk){
return instance.getLoaders().getChunkLoader(chunk);
}
/**
* Get a chunk loader by it's location.
* @param location The location of the chunk loader.
*/
public static Optional<ChunkLoader> getChunkLoader(Location location){
return instance.getLoaders().getChunkLoader(location);
}
/**
* Remove a chunk loader from the database.
* It's recommended to use ChunkLoader#remove instead!
*/
public static void removeChunkLoader(ChunkLoader chunkLoader){
instance.getLoaders().removeChunkLoader(chunkLoader);
}
/**
* Get the wildloaders object.
*/
public static WildLoaders getWildLoaders() {
return instance;
}
}

View File

@ -1,6 +1,7 @@
package com.bgsoftware.wildloaders;
import com.bgsoftware.wildloaders.api.WildLoaders;
import com.bgsoftware.wildloaders.api.WildLoadersAPI;
import com.bgsoftware.wildloaders.command.CommandsHandler;
import com.bgsoftware.wildloaders.handlers.DataHandler;
import com.bgsoftware.wildloaders.handlers.LoadersHandler;
@ -11,8 +12,11 @@ import com.bgsoftware.wildloaders.listeners.ChunksListener;
import com.bgsoftware.wildloaders.metrics.Metrics;
import com.bgsoftware.wildloaders.nms.NMSAdapter;
import com.bgsoftware.wildloaders.utils.database.Database;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import java.lang.reflect.Field;
public final class WildLoadersPlugin extends JavaPlugin implements WildLoaders {
private static WildLoadersPlugin plugin;
@ -24,14 +28,28 @@ public final class WildLoadersPlugin extends JavaPlugin implements WildLoaders {
private NMSAdapter nmsAdapter;
private boolean shouldEnable = true;
@Override
public void onEnable() {
public void onLoad() {
plugin = this;
new Metrics(this);
log("******** ENABLE START ********");
loadNMSAdapter();
loadAPI();
if(!shouldEnable)
log("&cThere was an error while loading the plugin.");
}
@Override
public void onEnable() {
if(!shouldEnable) {
Bukkit.getPluginManager().disablePlugin(this);
return;
}
log("******** ENABLE START ********");
dataHandler = new DataHandler(this);
loadersHandler = new LoadersHandler(this);
@ -59,9 +77,11 @@ public final class WildLoadersPlugin extends JavaPlugin implements WildLoaders {
@Override
public void onDisable() {
Database.stop();
loadersHandler.removeChunkLoaders();
npcHandler.killAllNPCs();
if(shouldEnable) {
Database.stop();
loadersHandler.removeChunkLoaders();
npcHandler.killAllNPCs();
}
}
private void loadNMSAdapter(){
@ -74,6 +94,18 @@ public final class WildLoadersPlugin extends JavaPlugin implements WildLoaders {
}
}
private void loadAPI(){
try{
Field instance = WildLoadersAPI.class.getDeclaredField("instance");
instance.setAccessible(true);
instance.set(null, this);
}catch(Exception ex){
log("Failed to set-up API - disabling plugin...");
ex.printStackTrace();
shouldEnable = false;
}
}
public SettingsHandler getSettings() {
return settingsHandler;
}