Skip to content

Commit 00590da

Browse files
committed
Initial commit
0 parents  commit 00590da

22 files changed

+1522
-0
lines changed

ForestRedis.iml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module version="4">
3+
<component name="FacetManager">
4+
<facet type="minecraft" name="Minecraft">
5+
<configuration>
6+
<autoDetectTypes>
7+
<platformType>SPIGOT</platformType>
8+
<platformType>BUNGEECORD</platformType>
9+
</autoDetectTypes>
10+
</configuration>
11+
</facet>
12+
</component>
13+
</module>

LICENSE.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
MIT License
2+
3+
Copyright (c) [2022] [Jiří Apjár]
4+
Copyright (c) [2022] [Filip Zeman]
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.

README.md

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
# ForestRedisAPI
2+
![badge](https://img.shields.io/github/v/release/ForestTechMC/ForestRedisAPI)
3+
[![badge](https://jitpack.io/v/ForestTechMC/ForestRedisAPI.svg)](https://jitpack.io/#ForestTechMC/ForestRedisAPI)
4+
![badge](https://img.shields.io/github/downloads/ForestTechMC/ForestRedisAPI/total)
5+
![badge](https://img.shields.io/github/last-commit/ForestTechMC/ForestRedisAPI)
6+
![badge](https://img.shields.io/badge/platform-spigot%20%7C%20bungeecord-lightgrey)
7+
![badge](https://img.shields.io/discord/896466173166747650?label=discord)
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+
[![badge](https://jitpack.io/v/ForestTechMC/ForestRedisAPI.svg)](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.

pom.xml

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.example</groupId>
8+
<artifactId>ForestRedisAPI</artifactId>
9+
<version>1.0.0</version>
10+
11+
<properties>
12+
<maven.compiler.source>11</maven.compiler.source>
13+
<maven.compiler.target>11</maven.compiler.target>
14+
</properties>
15+
16+
<build>
17+
<resources>
18+
<resource>
19+
<directory>src/main/resources</directory>
20+
<filtering>true</filtering>
21+
<includes>
22+
<include>plugin.yml</include>
23+
</includes>
24+
</resource>
25+
<resource>
26+
<directory>src/main/resources</directory>
27+
<filtering>false</filtering>
28+
<excludes>
29+
<exclude>plugin.yml</exclude>
30+
</excludes>
31+
</resource>
32+
</resources>
33+
<plugins>
34+
<plugin>
35+
<groupId>org.apache.maven.plugins</groupId>
36+
<artifactId>maven-compiler-plugin</artifactId>
37+
<configuration>
38+
<source>${maven.compiler.source}</source>
39+
<target>${maven.compiler.target}</target>
40+
</configuration>
41+
</plugin>
42+
<plugin>
43+
<groupId>org.apache.maven.plugins</groupId>
44+
<artifactId>maven-shade-plugin</artifactId>
45+
<version>3.2.2</version>
46+
<configuration>
47+
<createDependencyReducedPom>false</createDependencyReducedPom>
48+
<shadedArtifactAttached>false</shadedArtifactAttached>
49+
<relocations>
50+
<relocation>
51+
<pattern>com.google.code.gson</pattern>
52+
<shadedPattern>shaded.com.google.code.gson</shadedPattern>
53+
</relocation>
54+
</relocations>
55+
</configuration>
56+
<executions>
57+
<execution>
58+
<phase>package</phase>
59+
<goals>
60+
<goal>shade</goal>
61+
</goals>
62+
<configuration>
63+
<minimizeJar>true</minimizeJar>
64+
</configuration>
65+
</execution>
66+
</executions>
67+
</plugin>
68+
</plugins>
69+
</build>
70+
71+
<repositories>
72+
<repository>
73+
<id>jitpack.io</id>
74+
<url>https://jitpack.io</url>
75+
</repository>
76+
<repository>
77+
<id>spigot-repo</id>
78+
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
79+
</repository>
80+
<repository>
81+
<id>maven-central</id>
82+
<url>https://oss.sonatype.org/content/groups/public</url>
83+
</repository>
84+
<repository>
85+
<id>bungeecord-repo</id>
86+
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
87+
</repository>
88+
</repositories>
89+
90+
91+
<dependencies>
92+
<dependency>
93+
<groupId>com.github.ForestTechMC</groupId>
94+
<artifactId>ForestRedisAPI</artifactId>
95+
<version>v${project.version}</version>
96+
</dependency>
97+
<dependency>
98+
<groupId>org.spigotmc</groupId>
99+
<artifactId>spigot-api</artifactId>
100+
<version>1.17.1-R0.1-SNAPSHOT</version>
101+
<scope>provided</scope>
102+
</dependency>
103+
<dependency>
104+
<groupId>net.md-5</groupId>
105+
<artifactId>bungeecord-api</artifactId>
106+
<version>1.17-R0.1-SNAPSHOT</version>
107+
<type>jar</type>
108+
<scope>provided</scope>
109+
</dependency>
110+
<dependency>
111+
<groupId>redis.clients</groupId>
112+
<artifactId>jedis</artifactId>
113+
<version>4.2.3</version>
114+
</dependency>
115+
<dependency>
116+
<groupId>com.google.code.gson</groupId>
117+
<artifactId>gson</artifactId>
118+
<version>2.9.0</version>
119+
</dependency>
120+
</dependencies>
121+
</project>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package cz.foresttech.forestredis.bungee;
2+
3+
import cz.foresttech.forestredis.bungee.commands.BungeeForestRedisCommand;
4+
import cz.foresttech.forestredis.bungee.config.BungeeConfigAdapter;
5+
import cz.foresttech.forestredis.bungee.events.RedisMessageReceivedEvent;
6+
import cz.foresttech.forestredis.shared.*;
7+
import cz.foresttech.forestredis.shared.config.IConfigurationAdapter;
8+
import cz.foresttech.forestredis.shared.models.MessageTransferObject;
9+
import net.md_5.bungee.api.ProxyServer;
10+
import net.md_5.bungee.api.plugin.Plugin;
11+
12+
import java.util.logging.Logger;
13+
14+
public class ForestRedisBungee extends Plugin implements IForestRedisPlugin {
15+
16+
private static ForestRedisBungee instance;
17+
18+
@Override
19+
public void onEnable() {
20+
instance = this;
21+
load();
22+
ProxyServer.getInstance().getPluginManager().registerCommand(this, new BungeeForestRedisCommand());
23+
}
24+
25+
@Override
26+
public void onDisable() {
27+
// Close the RedisManager
28+
if (RedisManager.getAPI() == null) {
29+
return;
30+
}
31+
RedisManager.getAPI().close();
32+
}
33+
34+
@Override
35+
public void runAsync(Runnable task) {
36+
ProxyServer.getInstance().getScheduler().runAsync(instance, task);
37+
}
38+
39+
@Override
40+
public void callEvent(String channel, MessageTransferObject messageTransferObject) {
41+
ProxyServer.getInstance().getPluginManager().callEvent(new RedisMessageReceivedEvent(channel, messageTransferObject));
42+
}
43+
44+
@Override
45+
public Logger logger() {
46+
return this.getLogger();
47+
}
48+
49+
@Override
50+
public IConfigurationAdapter getConfigAdapter() {
51+
BungeeConfigAdapter bungeeConfigAdapter = new BungeeConfigAdapter();
52+
bungeeConfigAdapter.setup("config");
53+
return bungeeConfigAdapter;
54+
}
55+
56+
public static ForestRedisBungee getInstance() {
57+
return instance;
58+
}
59+
}

0 commit comments

Comments
 (0)