0.6.1 redis mostly done

This commit is contained in:
unurled 2023-06-20 13:17:41 +02:00
parent ee3b04c18d
commit b9b0590417
Signed by: unurled
GPG key ID: FDBC9CBE1F82423F
16 changed files with 299 additions and 67 deletions

View file

@ -72,8 +72,8 @@ dependencies {
implementation 'redis.clients:jedis:4.3.1'
compileOnly 'io.papermc.paper:paper-api:1.19.4-R0.1-SNAPSHOT'
implementation 'de.tr7zw:item-nbt-api-plugin:2.11.2'
compileOnly 'io.papermc.paper:paper-api:1.20.1-R0.1-SNAPSHOT'
implementation 'de.tr7zw:item-nbt-api-plugin:2.11.3'
// compileOnly 'de.tr7zw:item-nbt-api:2.11.2'
compileOnly 'com.comphenix.protocol:ProtocolLib:5.0.0'
compileOnly 'com.github.MilkBowl:VaultAPI:1.7.1'
@ -81,12 +81,12 @@ dependencies {
compileOnly 'com.sk89q.worldedit:worldedit-core:7.2.14'
compileOnly 'com.sk89q.worldedit:worldedit-bukkit:7.2.14'
compileOnly 'me.clip:placeholderapi:2.11.3'
compileOnly('net.citizensnpcs:citizens-main:2.0.31-SNAPSHOT') {
compileOnly('net.citizensnpcs:citizens-main:2.0.32-SNAPSHOT') {
exclude group: '*', module: '*'
}
compileOnly 'com.onarandombox.multiversecore:Multiverse-Core:4.3.1'
implementation 'com.github.decentsoftware-eu:decentholograms:2.8.1'
paperweightDevelopmentBundle("io.papermc.paper:dev-bundle:1.19.4-R0.1-SNAPSHOT")
implementation 'com.github.decentsoftware-eu:decentholograms:2.8.2'
paperweightDevelopmentBundle("io.papermc.paper:dev-bundle:1.20-R0.1-SNAPSHOT")
}
group = 'me.unurled'
@ -116,12 +116,12 @@ tasks {
shadowJar {
dependencies {
include dependency('de.tr7zw:item-nbt-api-plugin:2.11.2')
include dependency('de.tr7zw:item-nbt-api:2.11.2')
include dependency('de.tr7zw:item-nbt-api-plugin:2.11.3')
include dependency('de.tr7zw:item-nbt-api:2.11.3')
include dependency('org.mongodb:mongodb-driver-sync:4.9.1')
include dependency('org.mongodb:bson:4.9.1')
include dependency('org.mongodb:mongodb-driver-core:4.9.1')
include dependency('com.github.decentsoftware-eu:decentholograms:2.8.1')
include dependency('com.github.decentsoftware-eu:decentholograms:2.8.2')
include dependency('org.apache.httpcomponents:httpclient:4.5.13')
include dependency('org.apache.httpcomponents:httpmime:4.5.13')
include dependency('redis.clients:jedis:4.3.1')

44
docs/data_save/Redis.md Normal file
View file

@ -0,0 +1,44 @@
# Redis storage data structure
## Player data
- key type : string (json)
- key name : uuid
| Field | Type | Description |
|-------------|---------|---------------------------------|
| uuid | string | player uuid |
| name | string | player name |
| ip | string | player ip |
| first_join | string | player first join time |
| last_join | string | player last join time |
| play_time | double | player play time |
| last_loc | string | player last location |
| inventory | string | player inventory hash |
| enderchest | string | player enderchests hash |
| health | double | player health |
| defense | double | player defense |
| strength | double | player strength |
| speed | double | player speed |
| luck | double | player luck |
| mana | double | player mana |
| exp | double | player exp until the next level |
| level | integer | player level |
| money | double | player money |
| kills | integer | player kills |
| deaths | integer | player deaths |
| collections | string | player collections hash |
## Config data
- key type : Hash
- key name : config_${server_name}
| Field | Type | Description |
|----------------|---------|--------------------------|
| server_name | string | server name |
| server_motd | string | server motd |
| server_address | string | server address |
| server_port | integer | server port |
| days | integer | days the server was open |
| online | integer | online players |

View file

@ -6,13 +6,25 @@ import static me.unurled.raxen.utils.Utils.log;
import me.unurled.raxen.Raxen;
import me.unurled.raxen.manager.entity.PlayerManager;
import org.bukkit.Material;
import org.bukkit.attribute.Attribute;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
public class PlayerUtils {
public static void updateItemStats(Raxen main, Player player) {
PlayerManager pm = main.getManager().getPlayerManager();
PlayerInventory i = player.getInventory();
ItemStack it;
if (i.getItemInMainHand().getType() != Material.AIR) {
it = i.getItemInMainHand();
}
}
public static void updateSkills(Raxen main, Player player) {
PlayerManager pm = main.getManager().getPlayerManager();
int maxHealth, health, itemHealth, defense, itemDefense, speed, itemSpeed, strength, itemStrength, maxMana, mana, itemMana, luck, itemLuck;

View file

@ -600,4 +600,8 @@ public class RaxenPlayer {
p.setExp((float) (((double) xp) / ((double) getXpForNextLevel())));
}
}
public double getMoney() {
return main.getManager().getLibsManager().getVault().getEcon().getBalance(player);
}
}

View file

@ -56,7 +56,27 @@ public class Attributes {
);
}
public static void setAttributes(
public static void setAttributes(Raxen main, Player player,
int maxHealth,
int health,
int itemHealth,
int defense,
int itemDefense,
int speed,
int itemSpeed,
int strength,
int itemStrength,
int maxMana,
int mana,
int itemMana,
int luck,
int itemLuck) {
new Attributes(main).setAttributes(player, maxHealth, health, itemHealth, defense,
itemDefense, speed, itemSpeed, strength, itemStrength, maxMana, mana, itemMana, luck,
itemLuck);
}
public void setAttributes(
Player player,
int maxHealth,
int health,

View file

@ -1,16 +1,17 @@
package me.unurled.raxen.components.items;
public class NBTNames {
public enum NBTNames {
public static String HEALTH = "HEALTH";
public static String SPEED = "SPEED";
public static String STRENGTH = "STRENGTH";
public static String DEFENSE = "DEFENSE";
public static String LUCK = "LUCK";
public static String MANA = "MANA";
public static String ID = "ID";
public static String CUSTOM_ABILITY = "CUSTOM_ABILITY";
public static String DROP_RATE = "DROP_RATE";
public static String ITEM_CLASS = "ITEM_CLASS";
public static String ITEM_TYPE = "ITEM_TYPE";
HEALTH,
SPEED,
DEFENSE,
STRENGTH,
LUCK,
MANA,
ID,
CUSTOM_ABILITY,
DROP_RATE,
ITEM_CLASS,
ITEM_TYPE;
}

View file

@ -45,10 +45,10 @@ public class CombatKnife extends Item {
NBT.modify(
item,
nbt -> {
nbt.setString(NBTNames.ID, ID);
nbt.setString(NBTNames.ITEM_TYPE, ITEM_TYPES.toString());
nbt.setString(NBTNames.ITEM_CLASS, ITEM_CLASS.toString());
nbt.setInteger(NBTNames.STRENGTH, 20);
nbt.setString(NBTNames.ID.name(), ID);
nbt.setString(NBTNames.ITEM_TYPE.name(), ITEM_TYPES.toString());
nbt.setString(NBTNames.ITEM_CLASS.name(), ITEM_CLASS.toString());
nbt.setInteger(NBTNames.STRENGTH.name(), 20);
}
);
return item;

View file

@ -40,11 +40,11 @@ public class Dagger extends Item {
itm.lore(lore);
item.setItemMeta(itm);
NBTItem nbti = new NBTItem(item);
nbti.setString(NBTNames.ID, ID);
nbti.setInteger(NBTNames.SPEED, 100);
nbti.setInteger(NBTNames.STRENGTH, 50);
nbti.setString(NBTNames.CUSTOM_ABILITY, "throwing_dager");
nbti.setDouble(NBTNames.DROP_RATE, 50.0);
nbti.setString(NBTNames.ID.name(), ID);
nbti.setInteger(NBTNames.SPEED.name(), 100);
nbti.setInteger(NBTNames.STRENGTH.name(), 50);
nbti.setString(NBTNames.CUSTOM_ABILITY.name(), "throwing_dager");
nbti.setDouble(NBTNames.DROP_RATE.name(), 50.0);
item = nbti.getItem();
return item;
}

View file

@ -28,8 +28,8 @@ public class CustomLoot {
public Double getDrop_rate() {
NBTItem nbti = new NBTItem(item);
if (nbti.hasTag(NBTNames.DROP_RATE)) {
return nbti.getDouble(NBTNames.DROP_RATE);
if (nbti.hasTag(NBTNames.DROP_RATE.name())) {
return nbti.getDouble(NBTNames.DROP_RATE.name());
}
return 0.0;
}

View file

@ -5,12 +5,16 @@ import static me.unurled.raxen.utils.Items.mapItemStackDeserialize;
import static me.unurled.raxen.utils.Items.setItemsToInventory;
import static me.unurled.raxen.utils.Utils.debug;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.Filters;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import me.unurled.raxen.Raxen;
import me.unurled.raxen.components.entity.Attributes.Attribute;
import me.unurled.raxen.components.entity.player.Inventories;
@ -23,6 +27,7 @@ import me.unurled.raxen.manager.entity.PlayerManager;
import me.unurled.raxen.manager.entity.StorageManager;
import me.unurled.raxen.utils.Items;
import me.unurled.raxen.utils.libs.MongoDB;
import me.unurled.raxen.utils.libs.Redis;
import net.kyori.adventure.text.Component;
import org.bson.Document;
import org.bukkit.Bukkit;
@ -39,6 +44,7 @@ import org.jetbrains.annotations.NotNull;
public class PlayerConfig {
private static Raxen main;
private Redis redis;
private MongoDB mongoDB;
private MongoCollection<Document> mongoCollection;
private static PlayerManager playerManager;
@ -50,6 +56,7 @@ public class PlayerConfig {
sto = main.getManager().getStorageManager();
this.mongoDB = main.getManager().getLibsManager().getMongoDB();
this.mongoCollection = mongoDB.getMongoCollection();
this.redis = main.getManager().getLibsManager().getRedis();
playerManager = main.getManager().getPlayerManager();
this.attributes = new Attributes(main);
}
@ -61,6 +68,63 @@ public class PlayerConfig {
mongoDB.close();
}
/**
* save player data to redis
*/
public void saveUsingRedis(Player p, Attributes attributes, String invStr) {
Map<String, String> hash = new HashMap<>();
hash.put("uuid", p.getUniqueId().toString());
hash.put("name", p.getName());
hash.put("last_join", String.valueOf(System.currentTimeMillis()));
// hash.put("play_time", String.valueOf(playerManager.getPlayTime(p)));
hash.put("location", p.getLocation().toString());
hash.put("inv", invStr);
if (playerManager.getRaxenPlayer(p).getStorage() != null) {
if (
playerManager.getRaxenPlayer(p).getStorage().getEc() !=
null
) {
EnderChest ec = playerManager
.getRaxenPlayer(p)
.getStorage()
.getEc();
List<String> list = new ArrayList<String>();
int reverse = 0;
for (ItemStack it : ec.getEc()) {
if (it != null && it.getType() != Material.AIR) {
String s = Items.itemStackSerialize(it, reverse);
list.add(s);
}
reverse += 1;
}
String ecstr = Items.listItemStackSerialize(list);
hash.put("enderchest", ecstr);
}
}
HashMap<String, Attribute> attribute = main
.getManager()
.getPlayerManager()
.getAttribute();
for (String s : attribute.keySet()) {
if (s.contains("ITEM") || s.contains("MAX")) {
continue;
}
hash.put(s, attribute.get(s).get(p).toString());
}
hash.put("level", String.valueOf(playerManager.getRaxenPlayer(p).getLevel()));
hash.put("exp", String.valueOf(playerManager.getRaxenPlayer(p).getXpForNextLevel()));
hash.put("kills", String.valueOf(playerManager.getRaxenPlayer(p).getMobKills()));
hash.put("deaths", String.valueOf(playerManager.getRaxenPlayer(p).getDeaths()));
hash.put("money", String.valueOf(playerManager.getRaxenPlayer(p).getMoney()));
// TODO: Save Collections
Gson gson = new GsonBuilder().create();
String json = gson.toJson(hash);
redis.getJedis().set(p.getUniqueId().toString(), json);
}
/**
* save players stuff to mongo db
*
@ -184,6 +248,81 @@ public class PlayerConfig {
}
}
/**
* load player stuff from redis
*
* @param p player
*/
public void loadUsingRedis(Player p) {
String json = redis.getJedis().get(p.getUniqueId().toString());
Gson gson = new Gson();
TypeToken<HashMap<String, String>> typeToken = new TypeToken<HashMap<String, String>>() {
};
HashMap<String, String> hashMap = gson.fromJson(json, typeToken.getType());
if (hashMap == null) {
playerManager.getRaxenPlayer(p).createNewAttribute();
playerManager.getRaxenPlayer(p).createStorage();
playerManager.getRaxenPlayer(p).createInventory();
PlayerUtils.updateSkills(main, p);
return;
}
int maxHealth = 0, health = 0, itemHealth = 0, defense = 0, itemDefense = 0, speed = 0, itemSpeed = 0, strength = 0, itemStrength = 0, maxMana = 0, mana = 0, itemMana = 0, luck = 0, itemLuck = 0;
try {
health = Integer.parseInt(hashMap.get("HEALTH"));
defense = Integer.parseInt(hashMap.get("DEFENSE"));
speed = Integer.parseInt(hashMap.get("SPEED"));
strength = Integer.parseInt(hashMap.get("STRENGTH"));
mana = Integer.parseInt(hashMap.get("MANA"));
luck = Integer.parseInt(hashMap.get("LUCK"));
} catch (NumberFormatException e) {
}
Attributes.setAttributes(main, p,
maxHealth,
health,
itemHealth,
defense,
itemDefense,
speed,
itemSpeed,
strength,
itemStrength,
maxMana,
mana,
itemMana,
luck,
itemLuck
);
Inventory ec = Bukkit.createInventory(
null,
54,
Component.text("Ender Chest")
);
if (hashMap.get("enderchest") != null) {
ec =
setItemsToInventory(
mapItemStackDeserialize(hashMap.get("enderchest")),
ec
);
}
playerManager.getRaxenPlayer(p).setStorage(ec);
PlayerInventory inv = p.getInventory();
inv =
setItemsToInventory(
listItemStackDeserialize(hashMap.get("inventory")),
inv
);
p.getInventory().setContents(inv.getContents());
p.updateInventory();
me.unurled.raxen.components.entity.player.storages.Inventory invv =
new me.unurled.raxen.components.entity.player.storages.Inventory(
inv
);
Inventories invvv = new Inventories(invv);
playerManager.getRaxenPlayer(p).setInventory(inv);
PlayerUtils.updateSkills(main, p);
}
/**
* load player stuff from mongo db
*

View file

@ -24,6 +24,7 @@ public class ArmorEvent implements Listener {
/**
* when player changes armor, calculates new stats for player and setting it
*
* @param e
*/
@EventHandler

View file

@ -1,5 +1,12 @@
package me.unurled.raxen.manager.entity;
import static me.unurled.raxen.components.items.NBTNames.CUSTOM_ABILITY;
import static me.unurled.raxen.components.items.NBTNames.DEFENSE;
import static me.unurled.raxen.components.items.NBTNames.DROP_RATE;
import static me.unurled.raxen.components.items.NBTNames.HEALTH;
import static me.unurled.raxen.components.items.NBTNames.ID;
import static me.unurled.raxen.components.items.NBTNames.SPEED;
import static me.unurled.raxen.components.items.NBTNames.STRENGTH;
import static me.unurled.raxen.utils.Utils.colorTextComp;
import static me.unurled.raxen.utils.Utils.debug;
import static me.unurled.raxen.utils.Utils.error;
@ -15,7 +22,6 @@ import java.util.Objects;
import lombok.Getter;
import me.unurled.raxen.Raxen;
import me.unurled.raxen.components.items.ItemBuilder;
import me.unurled.raxen.components.items.NBTNames;
import me.unurled.raxen.components.items.custom.Item;
import me.unurled.raxen.config.ItemConfig;
import net.kyori.adventure.text.Component;
@ -36,7 +42,6 @@ public class ItemManager {
@Getter
private HashMap<String, ItemStack> list = new HashMap<>();
private NBTNames nbt = new NBTNames();
private ItemConfig itemConfig;
private String[] items;
private Map<File, Map<String, ItemBuilder>> map;
@ -145,16 +150,16 @@ public class ItemManager {
);
it.setItemMeta(itm);
NBTItem nbti = new NBTItem(it);
nbti.setInteger(NBTNames.SPEED, file.getInt("speed"));
nbti.setInteger(NBTNames.HEALTH, file.getInt("health"));
nbti.setInteger(NBTNames.DEFENSE, file.getInt("defense"));
nbti.setInteger(NBTNames.STRENGTH, file.getInt("strength"));
nbti.setString(NBTNames.ID, file.getString("customId"));
nbti.setInteger(SPEED.name(), file.getInt("speed"));
nbti.setInteger(HEALTH.name(), file.getInt("health"));
nbti.setInteger(DEFENSE.name(), file.getInt("defense"));
nbti.setInteger(STRENGTH.name(), file.getInt("strength"));
nbti.setString(ID.name(), file.getString("customId"));
nbti.setString(
NBTNames.CUSTOM_ABILITY,
CUSTOM_ABILITY.name(),
file.getString("custom_ability")
);
nbti.setDouble(NBTNames.DROP_RATE, file.getDouble("drop_rate"));
nbti.setDouble(DROP_RATE.name(), file.getDouble("drop_rate"));
it = nbti.getItem();
//it = setLoreFromNBT(it); deprecated
list.put(file.getString("customId"), it);

View file

@ -6,6 +6,7 @@ import me.unurled.raxen.utils.libs.CitizensApi;
import me.unurled.raxen.utils.libs.MongoDB;
import me.unurled.raxen.utils.libs.Mysql;
import me.unurled.raxen.utils.libs.PlaceHolderAPI;
import me.unurled.raxen.utils.libs.Redis;
import me.unurled.raxen.utils.libs.Vault;
public class LibsManager {
@ -22,6 +23,9 @@ public class LibsManager {
@Getter
private static Mysql mysql;
@Getter
private Redis redis;
@Getter
private PlaceHolderAPI placeHolderAPI;
@ -35,5 +39,6 @@ public class LibsManager {
this.mongoDB = new MongoDB();
this.placeHolderAPI = new PlaceHolderAPI(main);
this.citizens = new CitizensApi(main);
this.redis = new Redis();
}
}

View file

@ -549,17 +549,17 @@ public class Items {
) {
switch (str) {
case "SPEED":
nbti.setInteger(NBTNames.SPEED, nb);
nbti.setInteger(NBTNames.SPEED.name(), nb);
case "STRENGTH":
nbti.setInteger(NBTNames.STRENGTH, nb);
nbti.setInteger(NBTNames.STRENGTH.name(), nb);
case "HEALTH":
nbti.setInteger(NBTNames.HEALTH, nb);
nbti.setInteger(NBTNames.HEALTH.name(), nb);
case "DEFENSE":
nbti.setInteger(NBTNames.DEFENSE, nb);
nbti.setInteger(NBTNames.DEFENSE.name(), nb);
case "LUCK":
nbti.setInteger(NBTNames.LUCK, nb);
nbti.setInteger(NBTNames.LUCK.name(), nb);
case "MANA":
nbti.setInteger(NBTNames.MANA, nb);
nbti.setInteger(NBTNames.MANA.name(), nb);
default:
error(
(Raxen) Bukkit.getPluginManager().getPlugin("Raxen"),
@ -583,99 +583,99 @@ public class Items {
lore.add(cp);
}
NBTItem nbti = new NBTItem(it);
if (nbti.hasTag(NBTNames.SPEED)) {
if (nbti.hasTag(NBTNames.SPEED.name())) {
if (lore.size() > 2) {
lore.add(
2,
colorComp(attributes("SPEED"))
.append(Component.text(nbti.getInteger(NBTNames.SPEED)))
.append(Component.text(nbti.getInteger(NBTNames.SPEED.name())))
);
} else {
lore.add(
colorComp(attributes("SPEED"))
.append(Component.text(nbti.getInteger(NBTNames.SPEED)))
.append(Component.text(nbti.getInteger(NBTNames.SPEED.name())))
);
}
}
if (nbti.hasTag(NBTNames.HEALTH)) {
if (nbti.hasTag(NBTNames.HEALTH.name())) {
if (lore.size() > 1) {
lore.add(
1,
colorComp(attributes("HEALTH"))
.append(
Component.text(nbti.getInteger(NBTNames.HEALTH))
Component.text(nbti.getInteger(NBTNames.HEALTH.name()))
)
);
} else {
lore.add(
colorComp(attributes("HEALTH"))
.append(
Component.text(nbti.getInteger(NBTNames.HEALTH))
Component.text(nbti.getInteger(NBTNames.HEALTH.name()))
)
);
}
}
if (nbti.hasTag(NBTNames.DEFENSE)) {
if (nbti.hasTag(NBTNames.DEFENSE.name())) {
if (lore.size() > 3) {
lore.add(
3,
colorComp(attributes("DEFENSE"))
.append(
Component.text(nbti.getInteger(NBTNames.DEFENSE))
Component.text(nbti.getInteger(NBTNames.DEFENSE.name()))
)
);
} else {
lore.add(
colorComp(attributes("DEFENSE"))
.append(
Component.text(nbti.getInteger(NBTNames.DEFENSE))
Component.text(nbti.getInteger(NBTNames.DEFENSE.name()))
)
);
}
}
if (nbti.hasTag(NBTNames.STRENGTH)) {
if (nbti.hasTag(NBTNames.STRENGTH.name())) {
if (lore.size() > 3) {
lore.add(
3,
colorComp(attributes("STRENGTH"))
.append(
Component.text(nbti.getInteger(NBTNames.STRENGTH))
Component.text(nbti.getInteger(NBTNames.STRENGTH.name()))
)
);
} else {
lore.add(
colorComp(attributes("STRENGTH"))
.append(
Component.text(nbti.getInteger(NBTNames.STRENGTH))
Component.text(nbti.getInteger(NBTNames.STRENGTH.name()))
)
);
}
}
if (nbti.hasTag(NBTNames.MANA)) {
if (nbti.hasTag(NBTNames.MANA.name())) {
if (lore.size() > 3) {
lore.add(
3,
colorComp(attributes("MANA"))
.append(Component.text(nbti.getInteger(NBTNames.MANA)))
.append(Component.text(nbti.getInteger(NBTNames.MANA.name())))
);
} else {
lore.add(
colorComp(attributes("MANA"))
.append(Component.text(nbti.getInteger(NBTNames.MANA)))
.append(Component.text(nbti.getInteger(NBTNames.MANA.name())))
);
}
}
if (nbti.hasTag(NBTNames.LUCK)) {
if (nbti.hasTag(NBTNames.LUCK.name())) {
if (lore.size() > 3) {
lore.add(
3,
colorComp(attributes("LUCK"))
.append(Component.text(nbti.getInteger(NBTNames.LUCK)))
.append(Component.text(nbti.getInteger(NBTNames.LUCK.name())))
);
} else {
lore.add(
colorComp(attributes("LUCK"))
.append(Component.text(nbti.getInteger(NBTNames.LUCK)))
.append(Component.text(nbti.getInteger(NBTNames.LUCK.name())))
);
}
}

View file

@ -1,9 +1,11 @@
package me.unurled.raxen.utils.libs;
import lombok.Getter;
import redis.clients.jedis.JedisPooled;
public class Redis {
@Getter
JedisPooled jedis;
public Redis() {

View file

@ -46,7 +46,6 @@ public class Vault {
return;
}
econ = rsp.getProvider();
return;
}
public String getBalanceString(Player player) {