Skip to content

Commit

Permalink
Redis (#61)
Browse files Browse the repository at this point in the history
* Start work on Redis syncing.

Signed-off-by: creatorfromhell <[email protected]>

* Add texture config to currency denomination files.

Signed-off-by: creatorfromhell <[email protected]>

* Finish implementing Redis Support.

Signed-off-by: creatorfromhell <[email protected]>

---------

Signed-off-by: creatorfromhell <[email protected]>
  • Loading branch information
creatorfromhell authored Sep 2, 2023
1 parent a5d3e20 commit 994263b
Show file tree
Hide file tree
Showing 12 changed files with 238 additions and 7 deletions.
8 changes: 8 additions & 0 deletions Core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@
<version>1.8.4</version>
<scope>compile</scope>
</dependency>
<!--Redis-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>4.4.3</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
</dependencies>

<parent>
Expand Down
2 changes: 0 additions & 2 deletions Core/resources/currency/USD.yml
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,6 @@ Formatting:
#The separator to use for numeric separation.
Major_Separator: ','



#All configurations relating to currency notes.
Note:

Expand Down
3 changes: 3 additions & 0 deletions Core/resources/currency/USD/one.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ Options:
#The material used for this item.
Material: 'PAPER'

#The base64 texture to use if the material is PLAYER_HEAD
Texture: "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvZDA0NzE5YjNiOTdkMTk1YTIwNTcxOGI2ZWUyMWY1Yzk1Y2FmYTE2N2U3YWJjYTg4YTIxMDNkNTJiMzdkNzIyIn19fQ=="

#The damage value used for this item. Defaults to 0.
#Optional.
#Damage: 0
Expand Down
3 changes: 3 additions & 0 deletions Core/resources/currency/USD/penny.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ Options:
#The material used for this item.
Material: 'PAPER'

#The base64 texture to use if the material is PLAYER_HEAD
Texture: "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvZDA0NzE5YjNiOTdkMTk1YTIwNTcxOGI2ZWUyMWY1Yzk1Y2FmYTE2N2U3YWJjYTg4YTIxMDNkNTJiMzdkNzIyIn19fQ=="

#The damage value used for this item. Defaults to 0.
#Optional.
#Damage: 0
Expand Down
43 changes: 43 additions & 0 deletions Core/resources/data.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,49 @@ Data:
#DO NOT MODIFY
Version: "0.1.2.0"

#Configurations related to syncing data across servers.
Sync:

#The data syncing method to utilize. Options: Bungee, Redis
Type: Bungee

#Configurations relating to redis.
Redis:

#The redis host
Host: "localhost"

#The redis port
Port: 6379

#Your redis user's name
User: "user"

#Your redis user's password
Password: "password"

#The redis DB index
Index: 1

#The amount of time before timeout.
Timeout: 2000

#Whether to use SSL.
SSL: false

#Settings related to the redis connection pool
Pool:

#The max size of our pool.
MaxSize: 10

#The max idle amount.
MaxIdle: 10

#The min idle amount
MinIdle: 1


#Configurations relating to purging old data.
Purge:

Expand Down
1 change: 0 additions & 1 deletion Core/src/net/tnemc/core/account/PlayerAccount.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import net.tnemc.core.compatibility.Location;
import net.tnemc.core.compatibility.PlayerProvider;

import java.util.Date;
import java.util.Optional;
import java.util.UUID;

Expand Down
4 changes: 2 additions & 2 deletions Core/src/net/tnemc/core/channel/handlers/BalanceHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ public BalanceHandler() {
}

public static void send(final String account, String region, UUID currency, Identifier handler, BigDecimal amount) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
final ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF(TNECore.instance().getServerID().toString());
out.writeUTF(account);
out.writeUTF(region);
out.writeUTF(currency.toString());
out.writeUTF(handler.asID());
out.writeUTF(amount.toPlainString());

TNECore.server().proxy().send("tne:balance", out.toByteArray());
TNECore.storage().sendMessage("tne:balance", out.toByteArray());
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion Core/src/net/tnemc/core/channel/handlers/SyncHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static void send(String account) {
out.writeUTF(TNECore.instance().getServerID().toString());
out.writeUTF(account);

TNECore.server().proxy().send("tne:sync", out.toByteArray());
TNECore.storage().sendMessage("tne:sync", out.toByteArray());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import net.tnemc.core.api.callback.currency.DenominationLoadCallback;
import net.tnemc.core.compatibility.helper.CraftingRecipe;
import net.tnemc.core.compatibility.log.DebugLevel;
import net.tnemc.core.config.MainConfig;
import net.tnemc.core.currency.Currency;
import net.tnemc.core.currency.CurrencyLoader;
import net.tnemc.core.currency.CurrencyRegion;
Expand Down
115 changes: 115 additions & 0 deletions Core/src/net/tnemc/core/io/redis/TNEJedisManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package net.tnemc.core.io.redis;
/*
* The New Economy
* Copyright (C) 2022 - 2023 Daniel "creatorfromhell" Vidmar
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import net.tnemc.core.TNECore;
import net.tnemc.core.compatibility.log.DebugLevel;
import net.tnemc.core.config.DataConfig;
import net.tnemc.core.utils.Identifier;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.UUID;

/**
* TNEJedisManager
*
* @author creatorfromhell
* @since 0.1.2.0
*/
public class TNEJedisManager {

protected final JedisPool pool;
protected final TNESubscriber subscriber = new TNESubscriber();
protected final byte[] channel = "tne:balance".getBytes(StandardCharsets.UTF_8);
final Thread redisThread;

private static TNEJedisManager instance;

public TNEJedisManager() {
final JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(DataConfig.yaml().getInt("Data.Sync.Redis.Pool.MaxSize", 10));
config.setMaxIdle(DataConfig.yaml().getInt("Data.Sync.Redis.Pool.MaxIdle", 10));
config.setMinIdle(DataConfig.yaml().getInt("Data.Sync.Redis.Pool.MinIdle", 1));
this.pool = new JedisPool(config, DataConfig.yaml().getString("Data.Sync.Redis.Host"), DataConfig.yaml().getInt("Data.Sync.Redis.Port"),
DataConfig.yaml().getInt("Data.Sync.Redis.Timeout"), DataConfig.yaml().getString("Data.Sync.Redis.User"),
DataConfig.yaml().getString("Data.Sync.Redis.Password"), DataConfig.yaml().getInt("Data.Sync.Redis.Index"),
DataConfig.yaml().getBoolean("Data.Sync.Redis.SSL"));

if(connectionTest()) {

redisThread = new Thread(() -> {
try(Jedis jedis = pool.getResource()) {
jedis.subscribe(subscriber, channel);
}
}, "TNE Redis Thread");

redisThread.start();

TNECore.log().error("Redis Subscriber Thread Started", DebugLevel.OFF);
} else {
TNECore.log().error("Redis Connection Test Failed!", DebugLevel.OFF);
redisThread = null;
}

instance = this;
}

public boolean connectionTest() {
try(Jedis jedis = pool.getResource()) {
jedis.ping();
return true;
} catch(Exception ignore) {
return false;
}
}

public void publish(final byte[] data) {
try(Jedis jedis = pool.getResource()) {
jedis.publish(channel, data);
}
}

public void publish(final String channel, final byte[] data) {
try(Jedis jedis = pool.getResource()) {
jedis.publish(channel.getBytes(StandardCharsets.UTF_8), data);
}
}

public static TNEJedisManager instance() {
return instance;
}

public static void send(final String account, final String region, final UUID currency, final Identifier handler, final BigDecimal amount) {

final ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF(TNECore.instance().getServerID().toString());
out.writeUTF(account);
out.writeUTF(region);
out.writeUTF(currency.toString());
out.writeUTF(handler.asID());
out.writeUTF(amount.toPlainString());

instance.publish(out.toByteArray());
}
}
36 changes: 36 additions & 0 deletions Core/src/net/tnemc/core/io/redis/TNESubscriber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package net.tnemc.core.io.redis;
/*
* The New Economy
* Copyright (C) 2022 - 2023 Daniel "creatorfromhell" Vidmar
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import net.tnemc.core.TNECore;
import redis.clients.jedis.BinaryJedisPubSub;

/**
* TNESubscriber
*
* @author creatorfromhell
* @since 0.1.2.0
*/
public class TNESubscriber extends BinaryJedisPubSub {

@Override
public void onMessage(byte[] channel, byte[] message) {
super.onMessage(channel, message);
TNECore.instance().getChannelMessageManager().handle("tne:balance", message);
}
}
27 changes: 27 additions & 0 deletions Core/src/net/tnemc/core/io/storage/StorageManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import net.tnemc.core.compatibility.scheduler.ChoreExecution;
import net.tnemc.core.compatibility.scheduler.ChoreTime;
import net.tnemc.core.config.DataConfig;
import net.tnemc.core.io.redis.TNEJedisManager;
import net.tnemc.core.io.storage.connect.SQLConnector;
import net.tnemc.core.io.storage.connect.YAMLConnector;
import net.tnemc.core.io.storage.dialect.MariaDialect;
Expand Down Expand Up @@ -54,11 +55,25 @@ public class StorageManager {
private static StorageManager instance;
private StorageEngine engine;
private final StorageConnector<?> connector;
private final TNEJedisManager jedisManager;

final String sync;

public StorageManager() {
instance = this;

if(DataConfig.yaml().contains("Data.Sync")) {

sync = DataConfig.yaml().getString("Data.Sync.Type", "Bungee");
switch(sync.toLowerCase()) {
case "jedis" -> this.jedisManager = new TNEJedisManager();
default -> this.jedisManager = null;
}
} else {
sync = "Bungee";
this.jedisManager = null;
}

final String engine = DataConfig.yaml().getString("Data.Database.Type");

switch(engine.toLowerCase()) {
Expand Down Expand Up @@ -105,6 +120,18 @@ public StorageManager() {
initialize();
}

public void sendMessage(final String channel, final byte[] data) {
switch(sync.toLowerCase()) {
case "redis":
if(jedisManager != null) {
jedisManager.publish(channel, data);
}
break;
default:
TNECore.server().proxy().send(channel, data);
}
}

public boolean meetsRequirement() {
if(this.connector instanceof SQLConnector) {
return ((SQLConnector)this.connector).checkVersion();
Expand Down

0 comments on commit 994263b

Please sign in to comment.