Sacred realms

This commit is contained in:
unurled 2024-02-26 21:04:42 +01:00
commit aa1c13c74c
Signed by: unurled
GPG key ID: FDBC9CBE1F82423F
28 changed files with 1651 additions and 0 deletions

View file

@ -0,0 +1,97 @@
package me.unurled.sacredrealms.sr.data;
import static me.unurled.sacredrealms.sr.utils.Logger.error;
import com.google.gson.Gson;
import me.unurled.sacredrealms.sr.SR;
import org.jetbrains.annotations.NotNull;
import redis.clients.jedis.JedisPooled;
import redis.clients.jedis.json.Path2;
public class Redis implements DataHandler {
JedisPooled client;
public Redis() {
DataManager dm = (DataManager) DataManager.getInstance(DataManager.class);
if (dm != null) {
String host = dm.getConfig().getString("redis.host", "127.0.0.1");
int port = dm.getConfig().getInt("redis.port", 6379);
try {
client = new JedisPooled(host, port);
client.get("test");
} catch (Exception e) {
error("Failed to connect to Redis, shutting down server.");
SR.getInstance().getServer().shutdown();
}
} else {
error("Failed to get DataManager instance. Can't connect to Redis, shutting down server.");
SR.getInstance().getServer().shutdown();
}
}
/**
* Get a value from the data source
*
* @param key The key to get the value from
* @return The value of the key
*/
@Override
public String get(@NotNull String key) {
return client.get(key);
}
/**
* Get a value from the data source
*
* @param key The key to get the value from
* @return The value of the key
*/
@Override
public Object getGson(@NotNull String key) {
return client.jsonGet(key);
}
/**
* Set a value in the data source
*
* @param key The key to set the value to
* @param value The value to set
*/
@Override
public void set(@NotNull String key, String value) {
client.set(key, value);
}
/**
* Set a value in the data source
*
* @param key The key to set the value to
* @param value The value to set
*/
public void set(@NotNull String key, Gson value) {
client.jsonSet(key, value);
}
/**
* Append a value to the data source
*
* @param key The key to append the value to
* @param path The path to append the value to
* @param json The json to append
*/
@Override
public void jsonAppend(@NotNull String key, @NotNull String path, @NotNull Object... json) {
client.jsonArrAppend(key, Path2.of(path), json);
}
/**
* Remove a value from the data source
*
* @param key The key to remove the value from
*/
@Override
public void remove(@NotNull String key) {
client.del(key);
}
}