This commit is contained in:
parent
1380e7479a
commit
5ffb921150
7 changed files with 174 additions and 78 deletions
|
@ -2,6 +2,7 @@ package me.unurled.sacredrealms.sr.components.player;
|
|||
|
||||
import static me.unurled.sacredrealms.sr.utils.Logger.error;
|
||||
|
||||
import com.destroystokyo.paper.event.player.PlayerArmorChangeEvent;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import java.util.HashMap;
|
||||
|
@ -13,10 +14,13 @@ import me.unurled.sacredrealms.sr.data.gson.InventorySerializer;
|
|||
import me.unurled.sacredrealms.sr.data.gson.PotionEffectDeserializer;
|
||||
import me.unurled.sacredrealms.sr.data.gson.PotionEffectSerializer;
|
||||
import me.unurled.sacredrealms.sr.managers.Manager;
|
||||
import me.unurled.sacredrealms.sr.utils.Items;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.player.PlayerItemHeldEvent;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.potion.PotionEffect;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
@ -87,6 +91,9 @@ public class PlayerManager extends Manager {
|
|||
.create();
|
||||
String json = dh.get(PLAYER_KEY + e.getPlayer().getUniqueId());
|
||||
SRPlayer srPlayer = gson.fromJson(json, SRPlayer.class);
|
||||
if (srPlayer == null) {
|
||||
srPlayer = new SRPlayer(e.getPlayer());
|
||||
}
|
||||
|
||||
addPlayer(srPlayer);
|
||||
|
||||
|
@ -119,4 +126,29 @@ public class PlayerManager extends Manager {
|
|||
|
||||
removePlayer(e.getPlayer().getUniqueId());
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerInventorySlotChangeEvent(@NotNull PlayerItemHeldEvent e) {
|
||||
SRPlayer player = getPlayer(e.getPlayer().getUniqueId());
|
||||
if (player == null) {
|
||||
return;
|
||||
}
|
||||
player.setInventory(e.getPlayer().getInventory());
|
||||
|
||||
ItemStack oldItem = e.getPlayer().getInventory().getItem(e.getPreviousSlot());
|
||||
ItemStack newItem = e.getPlayer().getInventory().getItem(e.getNewSlot());
|
||||
|
||||
Items.updatePlayer(e.getPlayer(), oldItem, newItem);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerArmorChange(PlayerArmorChangeEvent e) {
|
||||
SRPlayer player = getPlayer(e.getPlayer().getUniqueId());
|
||||
if (player == null) {
|
||||
return;
|
||||
}
|
||||
player.setInventory(e.getPlayer().getInventory());
|
||||
|
||||
Items.updatePlayer(e.getPlayer(), e.getOldItem(), e.getNewItem());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,12 +1,15 @@
|
|||
package me.unurled.sacredrealms.sr.components.player;
|
||||
|
||||
import static me.unurled.sacredrealms.sr.utils.Items.lore;
|
||||
import static me.unurled.sacredrealms.sr.utils.Logger.warn;
|
||||
|
||||
import com.google.gson.annotations.Expose;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import me.unurled.sacredrealms.sr.components.attributes.Attribute;
|
||||
import me.unurled.sacredrealms.sr.components.item.Item;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
|
@ -19,15 +22,16 @@ import org.jetbrains.annotations.Nullable;
|
|||
|
||||
public class SRPlayer {
|
||||
|
||||
@Expose private final UUID uuid;
|
||||
@Expose private final HashMap<Attribute, Double> attributes;
|
||||
@Expose private final List<PotionEffect> potionEffects;
|
||||
@Expose private UUID uuid = null;
|
||||
@Expose private HashMap<Attribute, Double> attributes = new HashMap<>();
|
||||
private HashMap<Attribute, HashMap<ItemStack, Double>> itemAttributes = new HashMap<>();
|
||||
@Expose private List<PotionEffect> potionEffects;
|
||||
@Expose private Inventory inventory;
|
||||
|
||||
public SRPlayer(@NotNull Player player) {
|
||||
this.uuid = player.getUniqueId();
|
||||
this.inventory = player.getInventory();
|
||||
this.attributes = new HashMap<>();
|
||||
this.itemAttributes = new HashMap<>();
|
||||
this.potionEffects = (List<PotionEffect>) player.getActivePotionEffects();
|
||||
}
|
||||
|
||||
|
@ -95,7 +99,9 @@ public class SRPlayer {
|
|||
ItemStack i = p.getInventory().getItemInMainHand();
|
||||
ItemMeta im = i.getItemMeta();
|
||||
im.getPersistentDataContainer().set(attribute.getKey(), PersistentDataType.DOUBLE, value);
|
||||
im.lore();
|
||||
i.setItemMeta(im);
|
||||
lore(i);
|
||||
p.getInventory().setItemInMainHand(i);
|
||||
}
|
||||
|
||||
private boolean checkIfValueIsOutOfBoundsForAttribute(
|
||||
|
@ -150,6 +156,7 @@ public class SRPlayer {
|
|||
|
||||
@NotNull
|
||||
public List<PotionEffect> getPotionEffects() {
|
||||
if (potionEffects == null) potionEffects = new ArrayList<>();
|
||||
return potionEffects;
|
||||
}
|
||||
|
||||
|
@ -157,4 +164,49 @@ public class SRPlayer {
|
|||
potionEffects.clear();
|
||||
potionEffects.addAll(effects);
|
||||
}
|
||||
|
||||
public void setItemAttributes(
|
||||
@NotNull Attribute attribute, @NotNull ItemStack item, @NotNull Double value) {
|
||||
if (itemAttributes == null) itemAttributes = new HashMap<>();
|
||||
if (itemAttributes.containsKey(attribute)) {
|
||||
itemAttributes.get(attribute).put(item, value);
|
||||
} else {
|
||||
HashMap<ItemStack, Double> map = new HashMap<>();
|
||||
map.put(item, value);
|
||||
itemAttributes.put(attribute, map);
|
||||
}
|
||||
}
|
||||
|
||||
public void removeItemAttributes(@NotNull Attribute attribute, @NotNull ItemStack item) {
|
||||
if (itemAttributes == null) itemAttributes = new HashMap<>();
|
||||
if (itemAttributes.containsKey(attribute)) {
|
||||
itemAttributes.get(attribute).remove(item);
|
||||
}
|
||||
}
|
||||
|
||||
public @Nullable Double getItemAttributes(@NotNull Attribute attribute, @NotNull ItemStack item) {
|
||||
if (itemAttributes == null) itemAttributes = new HashMap<>();
|
||||
if (itemAttributes.containsKey(attribute)) {
|
||||
return itemAttributes.get(attribute).get(item);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public @NotNull HashMap<ItemStack, Double> getItemAttributes(@NotNull Attribute attribute) {
|
||||
if (itemAttributes == null) itemAttributes = new HashMap<>();
|
||||
return itemAttributes.getOrDefault(attribute, new HashMap<>());
|
||||
}
|
||||
|
||||
public @NotNull HashMap<Attribute, Double> getItemAttributes(Item item) {
|
||||
if (itemAttributes == null) itemAttributes = new HashMap<>();
|
||||
HashMap<Attribute, Double> map = new HashMap<>();
|
||||
itemAttributes.forEach((k, v) -> map.put(k, v.get(item)));
|
||||
return map;
|
||||
}
|
||||
|
||||
public @NotNull Double getTotalItemAttribute(@NotNull Attribute attribute) {
|
||||
if (itemAttributes == null) itemAttributes = new HashMap<>();
|
||||
return itemAttributes.getOrDefault(attribute, new HashMap<>()).values().stream()
|
||||
.reduce(0.0, Double::sum);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue