Sacred realms
This commit is contained in:
commit
aa1c13c74c
28 changed files with 1651 additions and 0 deletions
|
@ -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();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue