-
Notifications
You must be signed in to change notification settings - Fork 5
Standalone usage
Jiří Apjár edited this page Aug 4, 2022
·
2 revisions
You can use the ForestRedisAPI as a standalone library. Then you need to initialize RedisManager yourself and provide it with required data.
If you want to make your plugin independent on any other plugins, this can be useful. However ForestRedisAPI bootstrap plugin (both bungee/spigot edition) is very light-weight and doesn't contain any other functionalities except reload and version commands.
The main benefit of not using standalone version is that the API can be used by many plugins without any additional overhead.
import cz.foresttech.forestredis.shared.IForestRedisPlugin;
import cz.foresttech.forestredis.shared.RedisManager;
import org.bukkit.plugin.java.JavaPlugin;
/**
* Use this ONLY if ForestRedisAPI plugin is not present and
* for some reason you don't want to install it.
*/
public class MyExamplePlugin extends JavaPlugin implements IForestRedisPlugin {
@Override
public void onEnable() {
// ...
load();
// ...
}
@Override
public void onDisable() {
//...
if (RedisManager.getAPI() == null) {
return;
}
// Close the RedisManager
RedisManager.getAPI().close();
//...
}
@Override
public void load() {
// Construct RedisConfiguration object
RedisConfiguration redisConfiguration = new RedisConfiguration(
"localhost", //hostname
6379, //port
null, //username (null if not any)
null, //password (null if not any)
false //ssl
);
// Initialize RedisManager instance (singleton)
// Since init, use RedisManager#getAPI() to obtain the instance
RedisManager.init(this, serverIdentifier, redisConfiguration);
// Now setup the connection
RedisManager.getAPI().setup(/*channels*/);
// Now you can use #getAPI() call to get singleton instance
RedisManager.getAPI().subscribe("MyChannel1");
}
@Override
public void runAsync(Runnable task) {
// Required so RedisManager can run tasks async
Bukkit.getScheduler().runTaskAsynchronously(instance, task);
}
@Override
@SuppressWarnings("Called asynchronously!")
public void onMessageReceived(String channel, MessageTransferObject messageTransferObject) {
// Async call - what shall be done when the message arrives
// You can completely remove lines below, but then built-in events won't work
Bukkit.getPluginManager().callEvent(new AsyncRedisMessageReceivedEvent(channel, messageTransferObject));
Bukkit.getScheduler().runTask(this, () -> Bukkit.getPluginManager().callEvent(new RedisMessageReceivedEvent(channel, messageTransferObject)));
}
@Override
public Logger logger() {
return this.getLogger();
}
}