Raxen/src/main/java/me/unurled/raxen/manager/entity/PlayerManager.java

275 lines
8.5 KiB
Java

package me.unurled.raxen.manager.entity;
import static me.unurled.raxen.components.entity.player.PlayerUtils.actionBarPlayer;
import static me.unurled.raxen.utils.Utils.colorTextComp;
import static me.unurled.raxen.utils.Utils.debug;
import java.util.HashMap;
import java.util.UUID;
import lombok.Getter;
import me.unurled.raxen.Raxen;
import me.unurled.raxen.components.entity.Attributes.Defense;
import me.unurled.raxen.components.entity.Attributes.Health;
import me.unurled.raxen.components.entity.Attributes.ItemDefense;
import me.unurled.raxen.components.entity.Attributes.ItemHealth;
import me.unurled.raxen.components.entity.Attributes.ItemLuck;
import me.unurled.raxen.components.entity.Attributes.ItemMana;
import me.unurled.raxen.components.entity.Attributes.ItemSpeed;
import me.unurled.raxen.components.entity.Attributes.ItemStrength;
import me.unurled.raxen.components.entity.Attributes.Luck;
import me.unurled.raxen.components.entity.Attributes.Mana;
import me.unurled.raxen.components.entity.Attributes.MaxHealth;
import me.unurled.raxen.components.entity.Attributes.MaxMana;
import me.unurled.raxen.components.entity.Attributes.MaxManaBuilder;
import me.unurled.raxen.components.entity.Attributes.Speed;
import me.unurled.raxen.components.entity.Attributes.Strength;
import me.unurled.raxen.components.entity.other.EntityNamespacedKey;
import me.unurled.raxen.components.entity.player.RaxenPlayer;
import me.unurled.raxen.components.entity.player.classes.Class;
import me.unurled.raxen.components.entity.player.classes.Classes;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitTask;
public class PlayerManager {
private final Raxen main;
private final HashMap<UUID, BukkitTask> actionBar = new HashMap<>();
// TODO: player classes
private final HashMap<String, Class> classes = new HashMap<>();
@Getter
private final HashMap<String, me.unurled.raxen.components.entity.Attributes.Attribute> attribute =
new HashMap<>();
private HashMap<UUID, RaxenPlayer> raxenPlayer = new HashMap<>();
private Classes classesRegister;
// Attributes
private Defense defense;
private Health health;
private ItemDefense itemDefense;
private ItemHealth itemHealth;
private ItemLuck itemLuck;
private ItemMana itemMana;
private ItemSpeed itemSpeed;
private ItemStrength itemStrength;
private Luck luck;
private Mana mana;
private MaxHealth maxHealth;
private MaxMana maxMana;
private Speed speed;
private Strength strength;
public PlayerManager(Raxen main) {
this.main = main;
registerAttributes();
}
public RaxenPlayer getRaxenPlayer(Player player) {
RaxenPlayer p = raxenPlayer.get(player.getUniqueId());
return p != null ? p : addRaxenPlayer(player);
}
public RaxenPlayer getRaxenPlayer(UUID uuid) {
RaxenPlayer p = raxenPlayer.get(uuid);
return p != null ? p : addRaxenPlayer(uuid);
}
public RaxenPlayer addRaxenPlayer(Player player) {
RaxenPlayer rp = new RaxenPlayer(main, player);
raxenPlayer.put(player.getUniqueId(), rp);
return rp;
}
public RaxenPlayer addRaxenPlayer(UUID uuid) {
RaxenPlayer rp = new RaxenPlayer(main, Bukkit.getPlayer(uuid));
raxenPlayer.put(uuid, rp);
return rp;
}
/**
* add a task to the player action bar.
*
* @param player
*/
public void registerActionBar(Player player) {
PlayerManager pm = main.getManager().getPlayerManager();
BukkitTask task = actionBarPlayer(main, pm, player);
if (actionBar.containsKey(player.getUniqueId())) {
actionBar.replace(player.getUniqueId(), task);
} else {
actionBar.put(player.getUniqueId(), task);
}
}
/**
* removes the task of action bar to player
*
* @param player
*/
public void unRegisterActionBar(Player player) {
if (actionBar.containsKey(player.getUniqueId())) {
BukkitTask task = actionBar.get(player.getUniqueId());
task.cancel();
actionBar.remove(player.getUniqueId());
} else {
debug(
colorTextComp(
"<red>Player " +
player.getName() +
" didn't have any Action Bar" +
player.getUniqueId().toString() +
"</red>"
)
);
}
}
/**
* Basically replace old task with old action bar with a new BukkitTask
*
* @param player
*/
public void updateActionBar(Player player) {
PlayerManager pm = main.getManager().getPlayerManager();
BukkitTask task = actionBarPlayer(main, pm, player);
if (actionBar.containsKey(player.getUniqueId())) {
actionBar.replace(player.getUniqueId(), task);
} else {
actionBar.put(player.getUniqueId(), task);
}
}
public void unRegisterRaxenPlayer(Player player) {
}
/**
* getClasses
*
* @return the classes HashMap
*/
public HashMap<String, Class> getClasses() {
return classes;
}
/**
* add @param to hashmap of classes
*
* @param clas a class instance
* @param id the id of the class
*/
public void addClasses(Class clas, String id) {
classes.put(id, clas);
}
/**
* register all Attributes in me.unurled.raxen.components.entity.Attributes;
*/
public void registerAttributes() {
EntityNamespacedKey key = new EntityNamespacedKey(main);
this.defense = new Defense(key.defense, "DEFENSE");
attribute.put("DEFENSE", defense);
this.health = new Health(key.health, "HEALTH");
attribute.put("HEALTH", health);
this.itemDefense = new ItemDefense(key.itemDefense, "ITEM_DEFENSE");
attribute.put("ITEM_DEFENSE", itemDefense);
this.itemHealth = new ItemHealth(key.itemHealth, "ITEM_HEALTH");
attribute.put("ITEM_HEALTH", itemHealth);
this.itemLuck = new ItemLuck(key.itemLuck, "ITEM_LUCK");
attribute.put("ITEM_LUCK", itemLuck);
this.itemMana = new ItemMana(key.itemMana, "ITEM_MANA");
attribute.put("ITEM_MANA", itemMana);
this.itemSpeed = new ItemSpeed(key.itemSpeed, "ITEM_SPEED");
attribute.put("ITEM_SPEED", itemSpeed);
this.itemStrength = new ItemStrength(key.itemStrength, "ITEM_STRENGTH");
attribute.put("ITEM_STRENGTH", itemStrength);
this.luck = new Luck(key.luck, "LUCK");
attribute.put("LUCK", luck);
this.mana = new Mana(key.mana, "MANA");
attribute.put("MANA", mana);
this.maxMana =
new MaxManaBuilder()
.setNamespacekey(key.maxMana)
.setName("MAX_MANA")
.createMaxMana();
attribute.put("MAX_MANA", maxMana);
this.maxHealth = new MaxHealth(key.maxHealth, "MAX_HEALTH");
attribute.put("MAX_HEALTH", maxHealth);
this.speed = new Speed(key.speed, "SPEED");
attribute.put("SPEED", speed);
this.strength = new Strength(key.strength, "STRENGTH");
attribute.put("STRENGTH", strength);
}
public Defense getDefense() {
return (Defense) attribute.get("DEFENSE");
}
public Health getHealth() {
return (Health) attribute.get("HEALTH");
}
public ItemDefense getItemDefense() {
return (ItemDefense) attribute.get("ITEM_DEFENSE");
}
public ItemHealth getItemHealth() {
return (ItemHealth) attribute.get("ITEM_HEALTH");
}
public ItemLuck getItemLuck() {
return (ItemLuck) attribute.get("ITEM_LUCK");
}
public ItemMana getItemMana() {
return (ItemMana) attribute.get("ITEM_MANA");
}
public ItemSpeed getItemSpeed() {
return (ItemSpeed) attribute.get("ITEM_SPEED");
}
public ItemStrength getItemStrength() {
return (ItemStrength) attribute.get("ITEM_STRENGTH");
}
public Luck getLuck() {
return (Luck) attribute.get("LUCK");
}
public Mana getMana() {
return (Mana) attribute.get("MANA");
}
public MaxMana getMaxMana() {
return (MaxMana) attribute.get("MAX_MANA");
}
public MaxHealth getMaxHealth() {
return (MaxHealth) attribute.get("MAX_HEALTH");
}
public Speed getSpeed() {
return (Speed) attribute.get("SPEED");
}
public Strength getStrength() {
return (Strength) attribute.get("STRENGTH");
}
}