add inventory, attributes, potionsEffect to SRPlayer
This commit is contained in:
parent
3c47676b39
commit
31ba32933e
1 changed files with 97 additions and 83 deletions
|
@ -2,24 +2,40 @@ package me.unurled.sacredrealms.sr.components.player;
|
||||||
|
|
||||||
import static me.unurled.sacredrealms.sr.utils.Logger.warn;
|
import static me.unurled.sacredrealms.sr.utils.Logger.warn;
|
||||||
|
|
||||||
|
import com.google.gson.annotations.Expose;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import me.unurled.sacredrealms.sr.components.attributes.Attribute;
|
import me.unurled.sacredrealms.sr.components.attributes.Attribute;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.NamespacedKey;
|
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.inventory.Inventory;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.inventory.meta.ItemMeta;
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
import org.bukkit.persistence.PersistentDataContainer;
|
|
||||||
import org.bukkit.persistence.PersistentDataType;
|
import org.bukkit.persistence.PersistentDataType;
|
||||||
|
import org.bukkit.potion.PotionEffect;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
public class SRPlayer {
|
public class SRPlayer {
|
||||||
|
|
||||||
|
@Expose
|
||||||
private final UUID uuid;
|
private final UUID uuid;
|
||||||
|
|
||||||
|
@Expose
|
||||||
|
private Inventory inventory;
|
||||||
|
|
||||||
|
@Expose
|
||||||
|
private final HashMap<Attribute, Double> attributes;
|
||||||
|
|
||||||
|
@Expose
|
||||||
|
private final List<PotionEffect> potionEffects;
|
||||||
|
|
||||||
public SRPlayer(@NotNull Player player) {
|
public SRPlayer(@NotNull Player player) {
|
||||||
this.uuid = player.getUniqueId();
|
this.uuid = player.getUniqueId();
|
||||||
|
this.inventory = player.getInventory();
|
||||||
|
this.attributes = new HashMap<>();
|
||||||
|
this.potionEffects = (List<PotionEffect>) player.getActivePotionEffects();
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
|
@ -27,64 +43,35 @@ public class SRPlayer {
|
||||||
return uuid;
|
return uuid;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
public void setInventory(Inventory inventory) {
|
||||||
public Object getAttribute(@NotNull Attribute attribute) {
|
this.inventory = inventory;
|
||||||
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) {
|
@NotNull
|
||||||
Player p = Bukkit.getPlayer(uuid);
|
public Inventory getInventory() {
|
||||||
if (p == null) {
|
return inventory;
|
||||||
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;
|
@NotNull
|
||||||
}
|
public HashMap<Attribute, Double> getAttributes() {
|
||||||
}
|
return attributes;
|
||||||
pdc.set(new NamespacedKey("sr", attribute.getID()), attribute.getType(), value);
|
}
|
||||||
|
|
||||||
|
public double getAttribute(@NotNull Attribute attribute) {
|
||||||
|
return getAttribute(attribute, attribute.getDefaultValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public Double getAttribute(@NotNull Attribute attribute, @NotNull Double defaultValue) {
|
||||||
|
return attributes.getOrDefault(attribute, defaultValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAttribute(@NotNull Attribute attribute, @NotNull Double value) {
|
||||||
|
attributes.put(attribute, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeAttribute(@NotNull Attribute attribute) {
|
||||||
|
attributes.remove(attribute);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
|
@ -93,20 +80,37 @@ public class SRPlayer {
|
||||||
if (p == null) {
|
if (p == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return p.getInventory()
|
Object o = p.getInventory()
|
||||||
.getItemInMainHand()
|
.getItemInMainHand()
|
||||||
.getItemMeta()
|
.getItemMeta()
|
||||||
.getPersistentDataContainer()
|
.getPersistentDataContainer()
|
||||||
.get(new NamespacedKey("sr", attribute.getID()), attribute.getType());
|
.get(attribute.getKey(), attribute.getType());
|
||||||
|
if (o == null) {
|
||||||
|
setHandItemAttribute(attribute, attribute.getDefaultValue());
|
||||||
|
return attribute.getDefaultValue();
|
||||||
|
}
|
||||||
|
return o;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setHandItemAttribute(@NotNull Attribute attribute, @NotNull Object value) {
|
public void setHandItemAttribute(@NotNull Attribute attribute, @NotNull Double value) {
|
||||||
Player p = Bukkit.getPlayer(uuid);
|
Player p = Bukkit.getPlayer(uuid);
|
||||||
if (p == null) {
|
if (p == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!attribute.getType().getComplexType().isInstance(value)) {
|
if (checkIfValueIsOutOfBoundsForAttribute(attribute, value, p))
|
||||||
return;
|
return;
|
||||||
|
ItemStack i = p.getInventory().getItemInMainHand();
|
||||||
|
ItemMeta im = i.getItemMeta();
|
||||||
|
im.getPersistentDataContainer()
|
||||||
|
.set(attribute.getKey(), PersistentDataType.DOUBLE, value);
|
||||||
|
im.lore();
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean checkIfValueIsOutOfBoundsForAttribute(@NotNull Attribute attribute,
|
||||||
|
@NotNull Double value,
|
||||||
|
Player p) {
|
||||||
|
if (!attribute.getType().getComplexType().isInstance(value)) {
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
if (attribute.getType().equals(PersistentDataType.DOUBLE)) {
|
if (attribute.getType().equals(PersistentDataType.DOUBLE)) {
|
||||||
double d = (double) value;
|
double d = (double) value;
|
||||||
|
@ -124,32 +128,42 @@ public class SRPlayer {
|
||||||
+ attribute.getMaxValue()
|
+ attribute.getMaxValue()
|
||||||
+ " Value: "
|
+ " Value: "
|
||||||
+ d);
|
+ d);
|
||||||
return;
|
return true;
|
||||||
}
|
}
|
||||||
} else if (attribute.getType().equals(PersistentDataType.INTEGER)) {
|
}
|
||||||
int i = (int) value;
|
return false;
|
||||||
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;
|
public void removeHandItemAttribute(@NotNull Attribute attribute) {
|
||||||
}
|
Player p = Bukkit.getPlayer(uuid);
|
||||||
|
if (p == null) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
ItemStack i = p.getInventory().getItemInMainHand();
|
ItemStack i = p.getInventory().getItemInMainHand();
|
||||||
ItemMeta im = i.getItemMeta();
|
ItemMeta im = i.getItemMeta();
|
||||||
im.getPersistentDataContainer()
|
im.getPersistentDataContainer().remove(attribute.getKey());
|
||||||
.set(new NamespacedKey("sr", attribute.getID()), attribute.getType(), value);
|
i.setItemMeta(im);
|
||||||
im.lore();
|
}
|
||||||
|
|
||||||
|
public void addPotionEffect(@NotNull PotionEffect effect) {
|
||||||
|
potionEffects.add(effect);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removePotionEffect(@NotNull PotionEffect effect) {
|
||||||
|
potionEffects.remove(effect);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void clearPotionEffects() {
|
||||||
|
potionEffects.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPotionEffects(@NotNull List<PotionEffect> effects) {
|
||||||
|
potionEffects.clear();
|
||||||
|
potionEffects.addAll(effects);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public List<PotionEffect> getPotionEffects() {
|
||||||
|
return potionEffects;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue