|
| 1 | +# ForestRedisAPI |
| 2 | + |
| 3 | +[](https://jitpack.io/#ForestTechMC/ForestRedisAPI) |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +Simple Spigot&Bungee Redis API based on Jedis library. ForestRedisAPI allows developers to comfortably maintain |
| 10 | +communication between servers using simple API calls and Events. **Supports both BungeeCord and Spigot servers.** |
| 11 | + |
| 12 | +## Getting started |
| 13 | + |
| 14 | +Make sure the server has ForestRedisAPI plugin installed. Otherwise, look at **[Standalone Usage](https://github.com/ForestTechMC/ForestRedisAPI#standalone-usage-without-forestredisapi-installed)**. |
| 15 | + |
| 16 | +### Add ForestRedisAPI to your project |
| 17 | + |
| 18 | +[](https://jitpack.io/#ForestTechMC/ForestRedisAPI) |
| 19 | + |
| 20 | +First, you need to setup the dependency on the ForestRedisAPI. Replace **VERSION** with the version of the release. |
| 21 | + |
| 22 | +#### Maven |
| 23 | + |
| 24 | +```xml |
| 25 | +<repositories> |
| 26 | + <repository> |
| 27 | + <id>jitpack.io</id> |
| 28 | + <url>https://jitpack.io</url> |
| 29 | + </repository> |
| 30 | +</repositories> |
| 31 | + |
| 32 | +<dependencies> |
| 33 | + <dependency> |
| 34 | + <groupId>com.github.ForestTechMC</groupId> |
| 35 | + <artifactId>ForestRedisAPI</artifactId> |
| 36 | + <version>VERSION</version> |
| 37 | + </dependency> |
| 38 | +</dependencies> |
| 39 | +``` |
| 40 | + |
| 41 | +#### Gradle |
| 42 | + |
| 43 | +```gradle |
| 44 | +allprojects { |
| 45 | + repositories { |
| 46 | + ... |
| 47 | + maven { url 'https://jitpack.io' } |
| 48 | + } |
| 49 | +} |
| 50 | +
|
| 51 | +dependencies { |
| 52 | + implementation 'com.github.ForestTechMC:ForestRedisAPI:VERSION' |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +### Plugin configuration |
| 57 | + |
| 58 | +You need to (soft)depend on ForestRedisAPI in order to work properly. Choose depend(s) for mandatory usage of the |
| 59 | +ForestRedisAPI or softdepend(s) for optional usage. |
| 60 | + |
| 61 | +for **plugin.yml**: `depend: [ForestRedisAPI]` or `softdepend: [ForestRedisAPI]` |
| 62 | + |
| 63 | +for **bungee.yml**: `depends: [ForestRedisAPI]` or `softDepends: [ForestRedisAPI]` |
| 64 | + |
| 65 | +### Standalone usage (without ForestRedisAPI installed) |
| 66 | + |
| 67 | +You can use the ForestRedisAPI as standalone library. Then you need to initialize RedisManager and provide him with |
| 68 | +required data. |
| 69 | + |
| 70 | +```java |
| 71 | +/** |
| 72 | + * Use this ONLY if ForestRedisAPI plugin is not present and |
| 73 | + * for some reason you don't want to install it. |
| 74 | + */ |
| 75 | +public void setupRedis() { |
| 76 | + // Construct RedisConfiguration object |
| 77 | + RedisConfiguration redisConfiguration = new RedisConfiguration( |
| 78 | + "localhost", //hostname |
| 79 | + 6379, //port |
| 80 | + null, //username (null if not any) |
| 81 | + null, //password (null if not any) |
| 82 | + false //ssl |
| 83 | + ); |
| 84 | + |
| 85 | + // Initialize RedisManager instance (singleton) |
| 86 | + new RedisManager(this, serverIdentifier, redisConfiguration).setup(); |
| 87 | + |
| 88 | + // Now you can use #getAPI() call to get singleton instance |
| 89 | + RedisManager.getAPI().subscribe("MyChannel1"); |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +## Subscribing to channel |
| 94 | + |
| 95 | +To receive data from Redis server, you need to subscribe to selected channels. You can do it simply just by calling: |
| 96 | + |
| 97 | +```java |
| 98 | +// You can check if the channel is subscribed or not |
| 99 | +if(RedisManager.getAPI().isSubscribed("MyChannel")){ |
| 100 | + this.log().warning("Channel 'MyChannel' is already subscribed!"); |
| 101 | + return; |
| 102 | +} |
| 103 | + |
| 104 | +// You can subscribe as many channels as you want. |
| 105 | +// Already subscribed channels will be skipped. |
| 106 | +RedisManager.getAPI().subscribe("MyChannel1","MyChannel2","MyChannel3"); |
| 107 | +``` |
| 108 | + |
| 109 | +## Publishing messages / objects |
| 110 | + |
| 111 | +You can easily publish messages and objects to Redis server. It is not required to subscribe channel you want to send |
| 112 | +data in. |
| 113 | + |
| 114 | +```java |
| 115 | +// For simple messages in String format use #publishMessage method. |
| 116 | +RedisManager.getAPI().publishMessage("MyChannel1","Hello, how are you?"); |
| 117 | + |
| 118 | +// You can also publish any object. They'll be serialized using JSON. |
| 119 | +RedisManager.getAPI().publishObject("MyChannel1",new MyObject()); |
| 120 | +``` |
| 121 | + |
| 122 | +## Events & Incoming messages |
| 123 | + |
| 124 | +Using ForestRedisAPI you can retrieve data from Redis using bukkit's (bungee's) Listeners. **But make sure the correct |
| 125 | +Event is chosen as the names are same for Bungee and Spigot!** |
| 126 | + |
| 127 | +```java |
| 128 | +// Use bungee event import for BungeeCord!!! |
| 129 | + |
| 130 | +import cz.foresttech.forestredis.spigot.events.RedisMessageReceivedEvent; |
| 131 | + |
| 132 | +public class MyListener implements Listener { |
| 133 | + |
| 134 | + @EventHandler |
| 135 | + public void onRedisMessageReceived(RedisMessageReceivedEvent event) { |
| 136 | + |
| 137 | + // Whether the message was sent by this server or not. |
| 138 | + // Uses the serverIdentifier from ForestRedisAPI config.yml |
| 139 | + boolean isSelfMessage = event.isSelfSender(); |
| 140 | + |
| 141 | + // Name of the channel. Must be subscribed first. |
| 142 | + String channel = event.getChannel(); |
| 143 | + |
| 144 | + // Identifier of the sender server. |
| 145 | + String senderServerId = event.getSenderIdentifier(); |
| 146 | + |
| 147 | + // Text of the message received. |
| 148 | + String messageText = event.getMessage(); |
| 149 | + |
| 150 | + // Parses any object from JSON. Can be used instead of #getMessage() |
| 151 | + // Returns 'null' if it couldn't be parsed. |
| 152 | + MyObject myObject = event.getMessageObject(MyObject.class); |
| 153 | + |
| 154 | + } |
| 155 | + |
| 156 | +} |
| 157 | +``` |
| 158 | + |
| 159 | +## License |
| 160 | +ForestRedisAPI is licensed under the permissive MIT license. Please see [`LICENSE.txt`](https://github.com/ForestTechMC/ForestRedisAPI/blob/master/LICENSE.txt) for more information. |
0 commit comments