add inventory, attributes, potionsEffect to SRPlayer

This commit is contained in:
unurled 2024-02-27 20:54:48 +01:00
parent 3c47676b39
commit 31ba32933e
Signed by: unurled
GPG key ID: FDBC9CBE1F82423F

View file

@ -2,24 +2,40 @@ package me.unurled.sacredrealms.sr.components.player;
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 me.unurled.sacredrealms.sr.components.attributes.Attribute;
import org.bukkit.Bukkit;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.potion.PotionEffect;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class SRPlayer {
@Expose
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) {
this.uuid = player.getUniqueId();
this.inventory = player.getInventory();
this.attributes = new HashMap<>();
this.potionEffects = (List<PotionEffect>) player.getActivePotionEffects();
}
@NotNull
@ -27,64 +43,35 @@ public class SRPlayer {
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 setInventory(Inventory inventory) {
this.inventory = inventory;
}
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);
@NotNull
public Inventory getInventory() {
return inventory;
}
return;
}
}
pdc.set(new NamespacedKey("sr", attribute.getID()), attribute.getType(), value);
@NotNull
public HashMap<Attribute, Double> getAttributes() {
return attributes;
}
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
@ -93,20 +80,37 @@ public class SRPlayer {
if (p == null) {
return null;
}
return p.getInventory()
Object o = p.getInventory()
.getItemInMainHand()
.getItemMeta()
.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);
if (p == null) {
return;
}
if (!attribute.getType().getComplexType().isInstance(value)) {
if (checkIfValueIsOutOfBoundsForAttribute(attribute, value, p))
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)) {
double d = (double) value;
@ -124,32 +128,42 @@ public class SRPlayer {
+ attribute.getMaxValue()
+ " Value: "
+ d);
return;
return true;
}
} 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 false;
}
return;
}
public void removeHandItemAttribute(@NotNull Attribute attribute) {
Player p = Bukkit.getPlayer(uuid);
if (p == null) {
return;
}
ItemStack i = p.getInventory().getItemInMainHand();
ItemMeta im = i.getItemMeta();
im.getPersistentDataContainer()
.set(new NamespacedKey("sr", attribute.getID()), attribute.getType(), value);
im.lore();
im.getPersistentDataContainer().remove(attribute.getKey());
i.setItemMeta(im);
}
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;
}
}