Sacred realms
This commit is contained in:
commit
aa1c13c74c
28 changed files with 1651 additions and 0 deletions
32
src/main/java/me/unurled/sacredrealms/sr/SR.java
Normal file
32
src/main/java/me/unurled/sacredrealms/sr/SR.java
Normal file
|
@ -0,0 +1,32 @@
|
|||
package me.unurled.sacredrealms.sr;
|
||||
|
||||
import me.unurled.sacredrealms.sr.managers.Managers;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public final class SR extends JavaPlugin {
|
||||
|
||||
private static SR instance;
|
||||
|
||||
private Managers managers;
|
||||
|
||||
public static SR getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
instance = this;
|
||||
|
||||
managers = new Managers();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
managers.unload();
|
||||
managers = null;
|
||||
}
|
||||
|
||||
public Managers getManagers() {
|
||||
return managers;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,110 @@
|
|||
package me.unurled.sacredrealms.sr.components.attributes;
|
||||
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
|
||||
public enum Attribute {
|
||||
HEALTH(
|
||||
"HEALTH",
|
||||
"Health",
|
||||
100,
|
||||
100000,
|
||||
0,
|
||||
PersistentDataType.DOUBLE,
|
||||
new NamespacedKey("sr", "HEALTH")),
|
||||
STRENGTH(
|
||||
"STRENGTH",
|
||||
"Strength",
|
||||
10,
|
||||
10000,
|
||||
0,
|
||||
PersistentDataType.DOUBLE,
|
||||
new NamespacedKey("sr", "STRENGTH")),
|
||||
DEFENSE(
|
||||
"DEFENSE",
|
||||
"Defense",
|
||||
10,
|
||||
10000,
|
||||
0,
|
||||
PersistentDataType.DOUBLE,
|
||||
new NamespacedKey("sr", "DEFENSE")),
|
||||
AGILITY(
|
||||
"AGILITY",
|
||||
"Agility",
|
||||
100,
|
||||
10000,
|
||||
100,
|
||||
PersistentDataType.DOUBLE,
|
||||
new NamespacedKey("sr", "AGILITY")),
|
||||
LUCK(
|
||||
"LUCK",
|
||||
"Luck",
|
||||
100,
|
||||
99999,
|
||||
-99999,
|
||||
PersistentDataType.INTEGER,
|
||||
new NamespacedKey("sr", "LUCK")),
|
||||
MANA("MANA", "Mana", 100, 10000, 0, PersistentDataType.DOUBLE, new NamespacedKey("sr", "MANA")),
|
||||
CHARISMA(
|
||||
"CHARISMA",
|
||||
"Charisma",
|
||||
0,
|
||||
10000,
|
||||
-10000,
|
||||
PersistentDataType.INTEGER,
|
||||
new NamespacedKey("sr", "CHARISMA")),
|
||||
;
|
||||
|
||||
private final String name;
|
||||
private final String ID;
|
||||
private final int defaultValue;
|
||||
private final int maxValue;
|
||||
private final int minValue;
|
||||
private final PersistentDataType type;
|
||||
private final NamespacedKey key;
|
||||
|
||||
Attribute(
|
||||
String ID,
|
||||
String name,
|
||||
int defaultValue,
|
||||
int maxValue,
|
||||
int minValue,
|
||||
PersistentDataType type,
|
||||
NamespacedKey key) {
|
||||
this.ID = ID;
|
||||
this.name = name;
|
||||
this.defaultValue = defaultValue;
|
||||
this.maxValue = maxValue;
|
||||
this.minValue = minValue;
|
||||
this.type = type;
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getID() {
|
||||
return ID;
|
||||
}
|
||||
|
||||
public int getDefaultValue() {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public int getMaxValue() {
|
||||
return maxValue;
|
||||
}
|
||||
|
||||
public int getMinValue() {
|
||||
return minValue;
|
||||
}
|
||||
|
||||
public PersistentDataType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public NamespacedKey getKey() {
|
||||
return key;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,84 @@
|
|||
package me.unurled.sacredrealms.sr.components.combat;
|
||||
|
||||
import static me.unurled.sacredrealms.sr.utils.Component.comp;
|
||||
|
||||
import me.unurled.sacredrealms.sr.components.attributes.Attribute;
|
||||
import me.unurled.sacredrealms.sr.managers.Manager;
|
||||
import me.unurled.sacredrealms.sr.utils.Items;
|
||||
import org.bukkit.Sound;
|
||||
import org.bukkit.entity.LivingEntity;
|
||||
import org.bukkit.entity.Mob;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.entity.EntityDamageByBlockEvent;
|
||||
import org.bukkit.event.entity.EntityDamageByEntityEvent;
|
||||
import org.bukkit.event.entity.EntityDamageEvent;
|
||||
import org.bukkit.inventory.EntityEquipment;
|
||||
|
||||
public class CombatManager extends Manager {
|
||||
|
||||
@EventHandler
|
||||
public void onDamage(EntityDamageByBlockEvent e) {
|
||||
// TODO: Implement
|
||||
// blast enchant :shrug:
|
||||
}
|
||||
|
||||
// TODO: Finish this
|
||||
@EventHandler
|
||||
public void onDamage(EntityDamageByEntityEvent e) {
|
||||
e.setDamage(0.0);
|
||||
|
||||
if (!(e.getDamager() instanceof LivingEntity) || !(e.getEntity() instanceof LivingEntity)) {
|
||||
return;
|
||||
}
|
||||
|
||||
LivingEntity damager = (LivingEntity) e.getDamager();
|
||||
LivingEntity entity = (LivingEntity) e.getEntity();
|
||||
|
||||
if (damager instanceof Player d) {
|
||||
if (entity instanceof Player) {
|
||||
// no pvp
|
||||
e.setCancelled(true);
|
||||
d.sendMessage(comp("<red>You can't attack players!"));
|
||||
d.playSound(d, Sound.BLOCK_ANVIL_HIT, 1.0f, 1.0f);
|
||||
return;
|
||||
}
|
||||
if (entity instanceof Mob) {
|
||||
Double dStrength = Items.getTotalAttribute(d, Attribute.STRENGTH);
|
||||
EntityEquipment equipment = ((Mob) entity).getEquipment();
|
||||
Double eDefense = Items.getTotalAttribute(equipment, Attribute.DEFENSE);
|
||||
|
||||
Double dLuck = Items.getTotalAttribute(d, Attribute.LUCK);
|
||||
Double eLuck = Items.getTotalAttribute(equipment, Attribute.LUCK);
|
||||
|
||||
Double luck = dLuck - eLuck;
|
||||
if (luck < -1000) {
|
||||
// 100% chance of miss
|
||||
luck = -10000d;
|
||||
d.sendMessage(comp("<red>You missed!"));
|
||||
// TODO: play miss sound
|
||||
// d.playSound();
|
||||
e.setCancelled(true);
|
||||
return;
|
||||
} else if (luck > 1000) {
|
||||
// 100% chance of critical hit
|
||||
luck = 10000d;
|
||||
}
|
||||
|
||||
if (luck < 0) {
|
||||
// chances of miss
|
||||
|
||||
} else if (luck > 0) {
|
||||
// chance of critical hit
|
||||
} else {
|
||||
}
|
||||
|
||||
}
|
||||
} else if (entity instanceof Player) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onDamage(EntityDamageEvent e) {}
|
||||
}
|
|
@ -0,0 +1,149 @@
|
|||
package me.unurled.sacredrealms.sr.components.item;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import me.unurled.sacredrealms.sr.components.attributes.Attribute;
|
||||
import me.unurled.sacredrealms.sr.components.item.abilities.Ability;
|
||||
import me.unurled.sacredrealms.sr.components.item.enchantments.Enchantment;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
/* <b><color of the rarity>rarity
|
||||
* <dark_gray>Description: blablabla
|
||||
* <dark_gray>blabbla
|
||||
* <dark_gray>blabla
|
||||
* <gray>Attributes:
|
||||
* <gray>➤ Health: <green>+100
|
||||
* <gray>➤ Strength: <red>+10
|
||||
* <gray>➤ Mana: <dark_aqua>+10
|
||||
* <gray>Abilities:
|
||||
* <gray>➤ Ability 1,<yellow> On Rightclick unleash a powerful attack
|
||||
* <dark_gray>cooldown: 10s,
|
||||
* <dark_gray>damage: 100,
|
||||
* <dark_gray>mana cost: <dark_aqua>50
|
||||
* <gray>➤ Ability 2,<yellow> On Rightclick unleash a powerful attack
|
||||
* <dark_gray>cooldown: 10s,
|
||||
* <dark_gray>damage: 100,
|
||||
* <dark_gray>mana cost: <dark_aqua>50
|
||||
* <dark_purple>Enchantments:
|
||||
* <blue>➤ Enchantment <if max level gold>1
|
||||
* <blue>➤ Enchantment <if max level gold>2
|
||||
* */
|
||||
public class Item {
|
||||
|
||||
private final String ID;
|
||||
|
||||
private String name;
|
||||
|
||||
private Material material;
|
||||
|
||||
private String description;
|
||||
|
||||
private Rarity rarity;
|
||||
|
||||
private HashMap<Attribute, Double> attributes;
|
||||
|
||||
private HashMap<Enchantment, Integer> enchantments;
|
||||
|
||||
private List<Ability> abilities;
|
||||
|
||||
public Item(String ID, String name, Material material) {
|
||||
this.ID = ID;
|
||||
this.name = name;
|
||||
this.material = material;
|
||||
}
|
||||
|
||||
public String getID() {
|
||||
return ID;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Material getMaterial() {
|
||||
return material;
|
||||
}
|
||||
|
||||
public void setMaterial(Material material) {
|
||||
this.material = material;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public Rarity getRarity() {
|
||||
return rarity;
|
||||
}
|
||||
|
||||
public void setRarity(Rarity rarity) {
|
||||
this.rarity = rarity;
|
||||
}
|
||||
|
||||
public HashMap<Attribute, Double> getAttributes() {
|
||||
return attributes;
|
||||
}
|
||||
|
||||
public Double getAttribute(Attribute attribute) {
|
||||
return attributes.get(attribute);
|
||||
}
|
||||
|
||||
public void setAttributes(HashMap<Attribute, Double> attributes) {
|
||||
this.attributes = attributes;
|
||||
}
|
||||
|
||||
public void addAttribute(Attribute attribute, double value) {
|
||||
attributes.put(attribute, value);
|
||||
}
|
||||
|
||||
public void removeAttribute(Attribute attribute) {
|
||||
attributes.remove(attribute);
|
||||
}
|
||||
|
||||
public HashMap<Enchantment, Integer> getEnchantments() {
|
||||
return enchantments;
|
||||
}
|
||||
|
||||
public void setEnchantments(HashMap<Enchantment, Integer> enchantments) {
|
||||
this.enchantments = enchantments;
|
||||
}
|
||||
|
||||
public void addEnchantment(Enchantment enchantment, int level) {
|
||||
enchantments.put(enchantment, level);
|
||||
}
|
||||
|
||||
public void removeEnchantment(Enchantment enchantment) {
|
||||
enchantments.remove(enchantment);
|
||||
}
|
||||
|
||||
public List<Ability> getAbilities() {
|
||||
return abilities;
|
||||
}
|
||||
|
||||
public void setAbilities(List<Ability> abilities) {
|
||||
this.abilities = abilities;
|
||||
}
|
||||
|
||||
public void addAbility(Ability ability) {
|
||||
abilities.add(ability);
|
||||
}
|
||||
|
||||
public void removeAbility(Ability ability) {
|
||||
abilities.remove(ability);
|
||||
}
|
||||
|
||||
public ItemStack toItemStack() {
|
||||
ItemStack item = new ItemStack(material);
|
||||
// set item meta
|
||||
return item;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package me.unurled.sacredrealms.sr.components.item;
|
||||
|
||||
import java.util.HashMap;
|
||||
import me.unurled.sacredrealms.sr.managers.Manager;
|
||||
import org.bukkit.NamespacedKey;
|
||||
|
||||
public class ItemManager extends Manager {
|
||||
public static final NamespacedKey ID = new NamespacedKey("sr", "ID");
|
||||
|
||||
public HashMap<String, Item> items;
|
||||
|
||||
public ItemManager() {
|
||||
super();
|
||||
items = new HashMap<>();
|
||||
}
|
||||
|
||||
public void addItem(Item item) {
|
||||
items.put(item.getID(), item);
|
||||
}
|
||||
|
||||
public Item getItem(String id) {
|
||||
return items.get(id);
|
||||
}
|
||||
|
||||
public void removeItem(String id) {
|
||||
items.remove(id);
|
||||
}
|
||||
|
||||
public boolean isItem(String id) {
|
||||
return items.containsKey(id);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package me.unurled.sacredrealms.sr.components.item;
|
||||
|
||||
import org.bukkit.NamespacedKey;
|
||||
|
||||
public enum Rarity {
|
||||
|
||||
COMMON("<white>Common", "COMMON", 1),
|
||||
UNCOMMON("<green>UnCommon", "UnCOMMON", 2),
|
||||
RARE("<dark_aqua>Rare", "RARE", 3),
|
||||
EPIC("<dark_purple>Epic", "EPIC", 4),
|
||||
LEGENDARY("<gold>Legendary", "LEGENDARY", 5),
|
||||
MYTHIC("<light_purple>Mythic", "MYTHIC", 6),
|
||||
SPECIAL("<red>Special", "SPECIAL", 7),
|
||||
UNIQUE("<yellow>Unique", "UNIQUE", 8),
|
||||
ADMIN("<color:#800000><obf>aa</obf><dark_red>Admin</dark_red><obf>aa</obf></color>", "ADMIN",
|
||||
100);
|
||||
|
||||
|
||||
private final String name;
|
||||
private final String ID;
|
||||
private final Integer weight;
|
||||
|
||||
Rarity(String name, String ID, Integer weight) {
|
||||
this.name = name;
|
||||
this.ID = ID;
|
||||
this.weight = weight;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
package me.unurled.sacredrealms.sr.components.item.abilities;
|
||||
|
||||
public class Ability {}
|
|
@ -0,0 +1,3 @@
|
|||
package me.unurled.sacredrealms.sr.components.item.enchantments;
|
||||
|
||||
public class Enchantment {}
|
|
@ -0,0 +1,94 @@
|
|||
package me.unurled.sacredrealms.sr.components.player;
|
||||
|
||||
import static me.unurled.sacredrealms.sr.utils.Logger.error;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import java.util.HashMap;
|
||||
import java.util.UUID;
|
||||
import me.unurled.sacredrealms.sr.data.DataHandler;
|
||||
import me.unurled.sacredrealms.sr.data.DataManager;
|
||||
import me.unurled.sacredrealms.sr.managers.Manager;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class PlayerManager extends Manager {
|
||||
|
||||
private final HashMap<UUID, SRPlayer> players;
|
||||
|
||||
public PlayerManager() {
|
||||
super();
|
||||
players = new HashMap<>();
|
||||
}
|
||||
|
||||
/** Save the data */
|
||||
@Override
|
||||
public void saveData() {
|
||||
DataManager dm = (DataManager) DataManager.getInstance(DataManager.class);
|
||||
if (dm == null) {
|
||||
error("DataManager is null, Can't save player data.");
|
||||
return;
|
||||
}
|
||||
DataHandler dh = dm.getDataHandler();
|
||||
Gson gson = new Gson();
|
||||
dh.set("sr.players", gson.toJson(players));
|
||||
}
|
||||
|
||||
/** Load the data */
|
||||
@Override
|
||||
public void loadData() {}
|
||||
|
||||
@Nullable
|
||||
public SRPlayer getPlayer(UUID uuid) {
|
||||
return players.get(uuid);
|
||||
}
|
||||
|
||||
public void addPlayer(@NotNull SRPlayer player) {
|
||||
players.put(player.getUuid(), player);
|
||||
}
|
||||
|
||||
public void removePlayer(UUID uuid) {
|
||||
players.remove(uuid);
|
||||
}
|
||||
|
||||
public boolean isSRPlayer(UUID uuid) {
|
||||
return players.containsKey(uuid);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerJoin(PlayerJoinEvent e) {
|
||||
SRPlayer player = new SRPlayer(e.getPlayer());
|
||||
// load data from db // TODO: test this after redis cache
|
||||
/*DataManager dm = (DataManager) DataManager.getInstance(DataManager.class);
|
||||
if (dm == null) {
|
||||
error("DataManager is null, Can't load player " + e.getPlayer().getName() + "'s data" + ".");
|
||||
return;
|
||||
}
|
||||
DataHandler dh = dm.getDataHandler();
|
||||
Gson gson = new Gson();
|
||||
Object json = dh.getGson("sr.players." + e.getPlayer().getUniqueId());*/
|
||||
addPlayer(player);
|
||||
}
|
||||
|
||||
// TODO: test if it works, i have a doubt about the jsonAppend (key + path seems weird)
|
||||
@EventHandler
|
||||
public void onPlayerQuit(PlayerQuitEvent e) {
|
||||
SRPlayer player = getPlayer(e.getPlayer().getUniqueId());
|
||||
if (player == null) {
|
||||
return;
|
||||
}
|
||||
// save for player
|
||||
DataManager dm = (DataManager) DataManager.getInstance(DataManager.class);
|
||||
if (dm == null) {
|
||||
error("DataManager is null, Can't save player " + e.getPlayer().getName() + "'s data" + ".");
|
||||
return;
|
||||
}
|
||||
DataHandler dh = dm.getDataHandler();
|
||||
Gson gson = new Gson();
|
||||
dh.set("sr.players." + e.getPlayer().getUniqueId(), gson.toJson(player));
|
||||
|
||||
removePlayer(e.getPlayer().getUniqueId());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,155 @@
|
|||
package me.unurled.sacredrealms.sr.components.player;
|
||||
|
||||
import static me.unurled.sacredrealms.sr.utils.Logger.warn;
|
||||
|
||||
import java.util.UUID;
|
||||
import me.unurled.sacredrealms.sr.components.attributes.Attribute;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.persistence.PersistentDataContainer;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class SRPlayer {
|
||||
|
||||
private final UUID uuid;
|
||||
|
||||
public SRPlayer(@NotNull Player player) {
|
||||
this.uuid = player.getUniqueId();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public UUID getUuid() {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Object getAttribute(@NotNull Attribute attribute) {
|
||||
Player p = Bukkit.getPlayer(uuid);
|
||||
if (p == null) {
|
||||
return null;
|
||||
}
|
||||
PersistentDataContainer pdc = p.getPersistentDataContainer();
|
||||
return pdc.get(new NamespacedKey("sr", attribute.getID()), attribute.getType());
|
||||
}
|
||||
|
||||
public void setAttribute(@NotNull Attribute attribute, @NotNull Object value) {
|
||||
Player p = Bukkit.getPlayer(uuid);
|
||||
if (p == null) {
|
||||
return;
|
||||
}
|
||||
PersistentDataContainer pdc = p.getPersistentDataContainer();
|
||||
if (!attribute.getType().getComplexType().isInstance(value)) {
|
||||
return;
|
||||
}
|
||||
if (attribute.getType().equals(PersistentDataType.DOUBLE)) {
|
||||
double d = (double) value;
|
||||
if (d < attribute.getMinValue() || d > attribute.getMaxValue()) {
|
||||
warn(
|
||||
"(Player "
|
||||
+ p.getName()
|
||||
+ ") Value "
|
||||
+ d
|
||||
+ " for attribute "
|
||||
+ attribute.getID()
|
||||
+ " is out of bounds. Min: "
|
||||
+ attribute.getMinValue()
|
||||
+ " Max: "
|
||||
+ attribute.getMaxValue()
|
||||
+ " Value: "
|
||||
+ d);
|
||||
return;
|
||||
}
|
||||
} else if (attribute.getType().equals(PersistentDataType.INTEGER)) {
|
||||
int i = (int) value;
|
||||
if (i < attribute.getMinValue() || i > attribute.getMaxValue()) {
|
||||
warn(
|
||||
"(Player "
|
||||
+ p.getName()
|
||||
+ ") Value "
|
||||
+ i
|
||||
+ " for attribute "
|
||||
+ attribute.getID()
|
||||
+ " is out of bounds. Min: "
|
||||
+ attribute.getMinValue()
|
||||
+ " Max: "
|
||||
+ attribute.getMaxValue()
|
||||
+ " Value: "
|
||||
+ i);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
pdc.set(new NamespacedKey("sr", attribute.getID()), attribute.getType(), value);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Object getHandItemAttribute(@NotNull Attribute attribute) {
|
||||
Player p = Bukkit.getPlayer(uuid);
|
||||
if (p == null) {
|
||||
return null;
|
||||
}
|
||||
return p.getInventory()
|
||||
.getItemInMainHand()
|
||||
.getItemMeta()
|
||||
.getPersistentDataContainer()
|
||||
.get(new NamespacedKey("sr", attribute.getID()), attribute.getType());
|
||||
}
|
||||
|
||||
public void setHandItemAttribute(@NotNull Attribute attribute, @NotNull Object value) {
|
||||
Player p = Bukkit.getPlayer(uuid);
|
||||
if (p == null) {
|
||||
return;
|
||||
}
|
||||
if (!attribute.getType().getComplexType().isInstance(value)) {
|
||||
return;
|
||||
}
|
||||
if (attribute.getType().equals(PersistentDataType.DOUBLE)) {
|
||||
double d = (double) value;
|
||||
if (d < attribute.getMinValue() || d > attribute.getMaxValue()) {
|
||||
warn(
|
||||
"(Player "
|
||||
+ p.getName()
|
||||
+ ") Value "
|
||||
+ d
|
||||
+ " for attribute "
|
||||
+ attribute.getID()
|
||||
+ " is out of bounds. Min: "
|
||||
+ attribute.getMinValue()
|
||||
+ " Max: "
|
||||
+ attribute.getMaxValue()
|
||||
+ " Value: "
|
||||
+ d);
|
||||
return;
|
||||
}
|
||||
} else if (attribute.getType().equals(PersistentDataType.INTEGER)) {
|
||||
int i = (int) value;
|
||||
if (i < attribute.getMinValue() || i > attribute.getMaxValue()) {
|
||||
warn(
|
||||
"(Player "
|
||||
+ p.getName()
|
||||
+ ") Value "
|
||||
+ i
|
||||
+ " for attribute "
|
||||
+ attribute.getID()
|
||||
+ " is out of bounds. Min: "
|
||||
+ attribute.getMinValue()
|
||||
+ " Max: "
|
||||
+ attribute.getMaxValue()
|
||||
+ " Value: "
|
||||
+ i);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
ItemStack i = p.getInventory().getItemInMainHand();
|
||||
ItemMeta im = i.getItemMeta();
|
||||
im.getPersistentDataContainer()
|
||||
.set(new NamespacedKey("sr", attribute.getID()), attribute.getType(), value);
|
||||
im.lore();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
package me.unurled.sacredrealms.sr.data;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public interface DataHandler {
|
||||
|
||||
/**
|
||||
* Get a value from the data source
|
||||
*
|
||||
* @param key The key to get the value from
|
||||
* @return The value of the key
|
||||
*/
|
||||
String get(@NotNull String key);
|
||||
|
||||
/**
|
||||
* Get a value from the data source
|
||||
*
|
||||
* @param key The key to get the value from
|
||||
* @return The value of the key
|
||||
*/
|
||||
Object getGson(@NotNull String key);
|
||||
|
||||
/**
|
||||
* Set a value in the data source
|
||||
*
|
||||
* @param key The key to set the value to
|
||||
* @param value The value to set
|
||||
*/
|
||||
void set(@NotNull String key, @Nullable String value);
|
||||
|
||||
/**
|
||||
* Set a value in the data source
|
||||
*
|
||||
* @param key The key to set the value to
|
||||
* @param value The value to set
|
||||
*/
|
||||
void set(@NotNull String key, Gson 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
|
||||
*/
|
||||
void jsonAppend(@NotNull String key, @NotNull String path, @NotNull Object... json);
|
||||
|
||||
/**
|
||||
* Remove a value from the data source
|
||||
*
|
||||
* @param key The key to remove the value from
|
||||
*/
|
||||
void remove(@NotNull String key);
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
package me.unurled.sacredrealms.sr.data;
|
||||
|
||||
import static me.unurled.sacredrealms.sr.utils.Logger.error;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import me.unurled.sacredrealms.sr.SR;
|
||||
import me.unurled.sacredrealms.sr.data.DataHandler;
|
||||
import me.unurled.sacredrealms.sr.data.Redis;
|
||||
import me.unurled.sacredrealms.sr.managers.Manager;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
|
||||
/** The data and config manager */
|
||||
public class DataManager extends Manager {
|
||||
|
||||
private FileConfiguration config;
|
||||
|
||||
private DataHandler dh;
|
||||
|
||||
/** Load the manager */
|
||||
@Override
|
||||
public void load() {
|
||||
super.load();
|
||||
dh = new Redis();
|
||||
}
|
||||
|
||||
/** Save the data */
|
||||
@Override
|
||||
public void saveData() {
|
||||
try {
|
||||
if (config != null) config.save(new File(SR.getInstance().getDataFolder(), "config.yml"));
|
||||
} catch (IOException e) {
|
||||
error("Failed to save config.yml");
|
||||
}
|
||||
}
|
||||
|
||||
/** Load the data */
|
||||
@Override
|
||||
public void loadData() {
|
||||
SR.getInstance().saveConfig();
|
||||
SR.getInstance().reloadConfig();
|
||||
config = SR.getInstance().getConfig();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the config
|
||||
*
|
||||
* @return The config
|
||||
*/
|
||||
public FileConfiguration getConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the data handler
|
||||
*
|
||||
* @return The data handler
|
||||
*/
|
||||
public DataHandler getDataHandler() {
|
||||
return dh;
|
||||
}
|
||||
}
|
97
src/main/java/me/unurled/sacredrealms/sr/data/Redis.java
Normal file
97
src/main/java/me/unurled/sacredrealms/sr/data/Redis.java
Normal 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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
package me.unurled.sacredrealms.sr.managers;
|
||||
|
||||
import me.unurled.sacredrealms.sr.SR;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
public class Manager implements Listener {
|
||||
|
||||
/** Constructor for the Manager class */
|
||||
public Manager() {
|
||||
SR.getInstance().getManagers().addManager(this);
|
||||
Bukkit.getScheduler()
|
||||
.runTaskAsynchronously(
|
||||
SR.getInstance(),
|
||||
() -> {
|
||||
load();
|
||||
Bukkit.getPluginManager().registerEvents(this, SR.getInstance());
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an instance of a manager
|
||||
*
|
||||
* @param clazz The class of the manager
|
||||
* @return The instance of the manager
|
||||
* @param <T> The type of the manager
|
||||
*/
|
||||
public static <T extends Manager> Manager getInstance(Class<? extends Manager> clazz) {
|
||||
return clazz.cast(SR.getInstance().getManagers().getManager(clazz));
|
||||
}
|
||||
|
||||
/** Load the manager */
|
||||
public void load() {
|
||||
loadData();
|
||||
}
|
||||
|
||||
/** Unload the manager */
|
||||
public void unload() {
|
||||
saveData();
|
||||
}
|
||||
|
||||
/** Save the data */
|
||||
public void saveData() {}
|
||||
|
||||
/** Load the data */
|
||||
public void loadData() {}
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
package me.unurled.sacredrealms.sr.managers;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import me.unurled.sacredrealms.sr.SR;
|
||||
import me.unurled.sacredrealms.sr.components.combat.CombatManager;
|
||||
import me.unurled.sacredrealms.sr.components.item.ItemManager;
|
||||
import me.unurled.sacredrealms.sr.components.player.PlayerManager;
|
||||
import me.unurled.sacredrealms.sr.data.DataManager;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class Managers {
|
||||
|
||||
private final List<Manager> managers;
|
||||
|
||||
public Managers() {
|
||||
managers = new ArrayList<>();
|
||||
// register managers here (like a new instance of them)
|
||||
|
||||
Bukkit.getScheduler()
|
||||
.runTaskLater(
|
||||
SR.getInstance(),
|
||||
() -> {
|
||||
new DataManager();
|
||||
new PlayerManager();
|
||||
new ItemManager();
|
||||
new CombatManager();
|
||||
},
|
||||
10L);
|
||||
}
|
||||
|
||||
public void addManager(Manager manager) {
|
||||
managers.add(manager);
|
||||
}
|
||||
|
||||
public void unload() {
|
||||
for (Manager manager : managers) {
|
||||
manager.unload();
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Manager getManager(Class<? extends Manager> clazz) {
|
||||
for (Manager manager : managers) {
|
||||
if (manager.getClass().equals(clazz)) {
|
||||
return manager;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package me.unurled.sacredrealms.sr.utils;
|
||||
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
|
||||
public class Component {
|
||||
|
||||
public static net.kyori.adventure.text.Component comp(String msg) {
|
||||
MiniMessage mm = MiniMessage.miniMessage();
|
||||
return mm.deserialize(msg);
|
||||
}
|
||||
}
|
56
src/main/java/me/unurled/sacredrealms/sr/utils/Items.java
Normal file
56
src/main/java/me/unurled/sacredrealms/sr/utils/Items.java
Normal file
|
@ -0,0 +1,56 @@
|
|||
package me.unurled.sacredrealms.sr.utils;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import me.unurled.sacredrealms.sr.components.attributes.Attribute;
|
||||
import me.unurled.sacredrealms.sr.components.item.Item;
|
||||
import me.unurled.sacredrealms.sr.components.item.ItemManager;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.EntityEquipment;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.persistence.PersistentDataContainer;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class Items {
|
||||
public static @NotNull Double getTotalAttribute(Player player, Attribute attribute) {
|
||||
return getTotalAttribute(player.getEquipment(), attribute);
|
||||
}
|
||||
|
||||
public static @NotNull Double getTotalAttribute(EntityEquipment inv, Attribute attribute) {
|
||||
ItemManager im = (ItemManager) ItemManager.getInstance(ItemManager.class);
|
||||
if (im == null) return 0.0;
|
||||
|
||||
AtomicReference<Double> total = new AtomicReference<>(0.0);
|
||||
|
||||
Arrays.stream(inv.getArmorContents()).toList().forEach(item ->
|
||||
total.updateAndGet(
|
||||
v -> (v + getAttribute(item,
|
||||
attribute))));
|
||||
|
||||
total.updateAndGet(
|
||||
v -> (v + getAttribute(inv.getItemInMainHand(),
|
||||
attribute)));
|
||||
total.updateAndGet(
|
||||
v -> (v + getAttribute(inv.getItemInOffHand(),
|
||||
attribute)));
|
||||
|
||||
return total.get();
|
||||
}
|
||||
|
||||
public static Double getAttribute(ItemStack item, Attribute attribute) {
|
||||
ItemManager im = (ItemManager) ItemManager.getInstance(ItemManager.class);
|
||||
if (im == null) return 0.0;
|
||||
if (item == null) return 0.0;
|
||||
if (item.getType().equals(Material.AIR)) return 0.0;
|
||||
PersistentDataContainer p = item.getItemMeta().getPersistentDataContainer();
|
||||
if (p.has(ItemManager.ID, PersistentDataType.STRING)) {
|
||||
Item i = im.getItem(p.get(ItemManager.ID, PersistentDataType.STRING));
|
||||
if (i != null) {
|
||||
return i.getAttribute(attribute);
|
||||
}
|
||||
}
|
||||
return 0.0;
|
||||
}
|
||||
}
|
31
src/main/java/me/unurled/sacredrealms/sr/utils/Logger.java
Normal file
31
src/main/java/me/unurled/sacredrealms/sr/utils/Logger.java
Normal file
|
@ -0,0 +1,31 @@
|
|||
package me.unurled.sacredrealms.sr.utils;
|
||||
|
||||
import me.unurled.sacredrealms.sr.SR;
|
||||
import net.kyori.adventure.text.Component;
|
||||
|
||||
public class Logger {
|
||||
|
||||
public static void log(String message) {
|
||||
SR.getInstance().getLogger().info(message);
|
||||
}
|
||||
|
||||
public static void warn(String message) {
|
||||
SR.getInstance().getLogger().warning(message);
|
||||
}
|
||||
|
||||
public static void error(String message) {
|
||||
SR.getInstance().getLogger().severe(message);
|
||||
}
|
||||
|
||||
public static void log(Component message) {
|
||||
SR.getInstance().getComponentLogger().info(message);
|
||||
}
|
||||
|
||||
public static void warn(Component message) {
|
||||
SR.getInstance().getComponentLogger().warn(message);
|
||||
}
|
||||
|
||||
public static void error(Component message) {
|
||||
SR.getInstance().getComponentLogger().error(message);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue