This commit is contained in:
parent
1380e7479a
commit
5ffb921150
7 changed files with 174 additions and 78 deletions
|
@ -1,8 +1,7 @@
|
|||
package me.unurled.sacredrealms.sr.commands.admin;
|
||||
|
||||
import static me.unurled.sacredrealms.sr.utils.Component.comp;
|
||||
import static me.unurled.sacredrealms.sr.utils.NumberParser.format;
|
||||
import static me.unurled.sacredrealms.sr.utils.NumberParser.parse;
|
||||
import static me.unurled.sacredrealms.sr.utils.SRPlayerUtils.syncSRToPlayer;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
@ -81,7 +80,7 @@ public class AttributeCommand implements TabExecutor {
|
|||
|
||||
if (args[0].equalsIgnoreCase("set")) {
|
||||
try {
|
||||
double value = parse(args[3]);
|
||||
double value = Double.parseDouble(args[3]);
|
||||
if (value < attribute.getMinValue() || value > attribute.getMaxValue()) {
|
||||
sender.sendMessage(
|
||||
"Value out of bounds. Min: "
|
||||
|
@ -94,7 +93,10 @@ public class AttributeCommand implements TabExecutor {
|
|||
if (player == null) return true;
|
||||
player.setHandItemAttribute(attribute, value);
|
||||
sender.sendMessage(
|
||||
"Set " + attribute.getName() + " to " + format(value) + " for " + target.getName());
|
||||
"Set " + attribute.getName() + " to " + value + " for " + target.getName());
|
||||
// update player
|
||||
player.setItemAttributes(attribute, target.getInventory().getItemInMainHand(), value);
|
||||
syncSRToPlayer(player, target);
|
||||
} catch (NumberFormatException e) {
|
||||
sender.sendMessage("Invalid value.");
|
||||
}
|
||||
|
@ -113,6 +115,8 @@ public class AttributeCommand implements TabExecutor {
|
|||
SRPlayer player = getSrPlayer(sender, target);
|
||||
if (player == null) return true;
|
||||
player.removeHandItemAttribute(attribute);
|
||||
player.removeItemAttributes(attribute, target.getInventory().getItemInMainHand());
|
||||
|
||||
sender.sendMessage("Removed " + attribute.getName() + " for " + target.getName());
|
||||
} else {
|
||||
sender.sendMessage("Usage: /attribute <set|get|remove> <player> <attribute> [value]");
|
||||
|
@ -135,7 +139,7 @@ public class AttributeCommand implements TabExecutor {
|
|||
|
||||
if (args[0].equalsIgnoreCase("set")) {
|
||||
try {
|
||||
double value = parse(args[3]);
|
||||
double value = Double.parseDouble(args[3]);
|
||||
if (value < attribute.getMinValue() || value > attribute.getMaxValue()) {
|
||||
sender.sendMessage(
|
||||
"Value out of bounds. Min: "
|
||||
|
@ -148,7 +152,9 @@ public class AttributeCommand implements TabExecutor {
|
|||
if (player == null) return true;
|
||||
player.setAttribute(attribute, value);
|
||||
sender.sendMessage(
|
||||
"Set " + attribute.getName() + " to " + format(value) + " for " + target.getName());
|
||||
"Set " + attribute.getName() + " to " + value + " for " + target.getName());
|
||||
player.setItemAttributes(attribute, target.getInventory().getItemInMainHand(), value);
|
||||
syncSRToPlayer(player, target);
|
||||
} catch (NumberFormatException e) {
|
||||
sender.sendMessage("Invalid value.");
|
||||
}
|
||||
|
@ -166,6 +172,8 @@ public class AttributeCommand implements TabExecutor {
|
|||
SRPlayer player = getSrPlayer(sender, target);
|
||||
if (player == null) return true;
|
||||
player.removeAttribute(attribute);
|
||||
player.removeItemAttributes(attribute, target.getInventory().getItemInMainHand());
|
||||
syncSRToPlayer(player, target);
|
||||
sender.sendMessage("Removed " + attribute.getName() + " for " + target.getName());
|
||||
} else {
|
||||
sender.sendMessage("Usage: /attribute <set|get|remove> <player> <attribute> [value]");
|
||||
|
@ -211,29 +219,6 @@ public class AttributeCommand implements TabExecutor {
|
|||
attributes.removeIf(s -> !s.startsWith(args[2]));
|
||||
return attributes;
|
||||
}
|
||||
if (args.length == 4 && args[0].equalsIgnoreCase("set")) {
|
||||
Attribute attribute;
|
||||
try {
|
||||
attribute = Attribute.valueOf(args[2].toUpperCase());
|
||||
} catch (IllegalArgumentException e) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
if (parse(args[3]) > attribute.getMaxValue() || parse(args[3]) < attribute.getMinValue()) {
|
||||
return null;
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
List<String> values = new java.util.ArrayList<>();
|
||||
double currentValue = attribute.getMinValue();
|
||||
while (currentValue <= attribute.getMaxValue()) {
|
||||
values.add(String.valueOf(format(currentValue)));
|
||||
currentValue *= 10;
|
||||
}
|
||||
return values;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,8 @@ package me.unurled.sacredrealms.sr.components.item;
|
|||
import java.util.HashMap;
|
||||
import me.unurled.sacredrealms.sr.managers.Manager;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
|
||||
@SuppressWarnings("EmptyMethod")
|
||||
public class ItemManager extends Manager {
|
||||
|
@ -33,6 +35,16 @@ public class ItemManager extends Manager {
|
|||
return items.get(id);
|
||||
}
|
||||
|
||||
public Item getItem(ItemStack item) {
|
||||
if (item == null) return null;
|
||||
if (item.getItemMeta() == null) return null;
|
||||
if (item.getItemMeta().getPersistentDataContainer().has(ID, PersistentDataType.STRING)) {
|
||||
return getItem(
|
||||
item.getItemMeta().getPersistentDataContainer().get(ID, PersistentDataType.STRING));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void removeItem(String id) {
|
||||
items.remove(id);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package me.unurled.sacredrealms.sr.utils;
|
||||
|
||||
import static me.unurled.sacredrealms.sr.utils.Component.comp;
|
||||
import static me.unurled.sacredrealms.sr.utils.SRPlayerUtils.syncSRToPlayer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
@ -9,6 +10,8 @@ import java.util.concurrent.atomic.AtomicReference;
|
|||
import me.unurled.sacredrealms.sr.components.attributes.Attribute;
|
||||
import me.unurled.sacredrealms.sr.components.item.Item;
|
||||
import me.unurled.sacredrealms.sr.components.item.ItemManager;
|
||||
import me.unurled.sacredrealms.sr.components.player.PlayerManager;
|
||||
import me.unurled.sacredrealms.sr.components.player.SRPlayer;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.EntityEquipment;
|
||||
|
@ -19,7 +22,7 @@ import org.jetbrains.annotations.NotNull;
|
|||
|
||||
@SuppressWarnings("UnusedReturnValue")
|
||||
public class Items {
|
||||
public static @NotNull Double getTotalAttribute(Player player, Attribute attribute) {
|
||||
public static @NotNull Double getTotalAttribute(@NotNull Player player, Attribute attribute) {
|
||||
return getTotalAttribute(player.getEquipment(), attribute);
|
||||
}
|
||||
|
||||
|
@ -51,18 +54,17 @@ public class Items {
|
|||
return i.getAttribute(attribute);
|
||||
}
|
||||
}
|
||||
if (p.has(attribute.getKey(), PersistentDataType.DOUBLE)) {
|
||||
try {
|
||||
return p.get(attribute.getKey(), PersistentDataType.DOUBLE);
|
||||
} catch (Exception e) {
|
||||
return 0.0;
|
||||
}
|
||||
}
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
/**
|
||||
* <b><color of the rarity>rarity <dark_gray>Description: blablabla <dark_gray>blabbla
|
||||
* <dark_gray>blabla <gray>Attributes: <gray>➤ Health: <green>+100 <gray>➤ Strength: <red>+10
|
||||
* <gray>➤ Mana: <dark_aqua>+10 <gray>Abilities: <gray>➤ Ability 1,<yellow> On Rightclick unleash
|
||||
* a powerful attack <dark_gray>cooldown: 10s, <dark_gray>damage: 100, <dark_gray>mana cost:
|
||||
* <dark_aqua>50 <gray>➤ Ability 2,<yellow> On Rightclick unleash a powerful attack
|
||||
* <dark_gray>cooldown: 10s, <dark_gray>damage: 100, <dark_gray>mana cost: <dark_aqua>50
|
||||
* <dark_purple>Enchantments: <blue>➤ Enchantment <if max level gold>1 <blue>➤ Enchantment <if max
|
||||
* level gold>2
|
||||
*
|
||||
* @param stack the itemstack
|
||||
* @return the itemstack with lore
|
||||
|
@ -118,4 +120,36 @@ public class Items {
|
|||
|
||||
return stack;
|
||||
}
|
||||
|
||||
public static void updatePlayer(@NotNull Player player, ItemStack oldItem, ItemStack newItem) {
|
||||
PlayerManager pm = (PlayerManager) PlayerManager.getInstance(PlayerManager.class);
|
||||
if (pm == null) {
|
||||
return;
|
||||
}
|
||||
SRPlayer p = pm.getPlayer(player.getUniqueId());
|
||||
if (p == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
ItemManager im = (ItemManager) ItemManager.getInstance(ItemManager.class);
|
||||
if (im == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// remove status of old Item
|
||||
if (oldItem != null && !oldItem.getType().equals(Material.AIR)) {
|
||||
for (Attribute a : Attribute.values()) {
|
||||
p.removeItemAttributes(a, oldItem);
|
||||
}
|
||||
}
|
||||
|
||||
// add status of new Item
|
||||
if (newItem != null && !newItem.getType().equals(Material.AIR)) {
|
||||
for (Attribute a : Attribute.values()) {
|
||||
p.setItemAttributes(a, newItem, getAttribute(newItem, a));
|
||||
}
|
||||
}
|
||||
|
||||
syncSRToPlayer(p, player);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,33 +0,0 @@
|
|||
package me.unurled.sacredrealms.sr.utils;
|
||||
|
||||
public class NumberParser {
|
||||
public static double parse(String input) {
|
||||
input = input.toLowerCase().trim();
|
||||
double multiplier = 1.0;
|
||||
|
||||
if (input.endsWith("k")) {
|
||||
multiplier = 1e3;
|
||||
input = input.substring(0, input.length() - 1);
|
||||
} else if (input.endsWith("m")) {
|
||||
multiplier = 1e6;
|
||||
input = input.substring(0, input.length() - 1);
|
||||
} else if (input.endsWith("b")) {
|
||||
multiplier = 1e9;
|
||||
input = input.substring(0, input.length() - 1);
|
||||
}
|
||||
|
||||
return Double.parseDouble(input) * multiplier;
|
||||
}
|
||||
|
||||
public static String format(double number) {
|
||||
if (number >= 1e9) {
|
||||
return String.format("%.2fb", number / 1e9);
|
||||
} else if (number >= 1e6) {
|
||||
return String.format("%.2fm", number / 1e6);
|
||||
} else if (number >= 1e3) {
|
||||
return String.format("%.2fk", number / 1e3);
|
||||
} else {
|
||||
return String.format("%.2f", number);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -3,6 +3,7 @@ package me.unurled.sacredrealms.sr.utils;
|
|||
import java.util.List;
|
||||
import me.unurled.sacredrealms.sr.components.attributes.Attribute;
|
||||
import me.unurled.sacredrealms.sr.components.player.SRPlayer;
|
||||
import org.bukkit.attribute.AttributeInstance;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.potion.PotionEffect;
|
||||
|
||||
|
@ -17,7 +18,17 @@ public class SRPlayerUtils {
|
|||
|
||||
// agility (speed)
|
||||
// default is 100 so 0.2
|
||||
float value = (float) (sr.getAttribute(Attribute.AGILITY) / 500.0);
|
||||
double items = sr.getTotalItemAttribute(Attribute.AGILITY);
|
||||
double speed = sr.getAttribute(Attribute.AGILITY);
|
||||
float value = (float) ((speed + items) / 500.0f);
|
||||
value = Math.min(value, 1.0f);
|
||||
value = Math.max(value, 0f);
|
||||
AttributeInstance attribute1 =
|
||||
p.getAttribute(org.bukkit.attribute.Attribute.GENERIC_MOVEMENT_SPEED);
|
||||
if (attribute1 != null) attribute1.setBaseValue(value);
|
||||
AttributeInstance attribute2 =
|
||||
p.getAttribute(org.bukkit.attribute.Attribute.GENERIC_FLYING_SPEED);
|
||||
if (attribute2 != null) attribute2.setBaseValue(value);
|
||||
p.setFlySpeed(value);
|
||||
p.setWalkSpeed(value);
|
||||
|
||||
|
@ -27,7 +38,10 @@ public class SRPlayerUtils {
|
|||
|
||||
// health
|
||||
// default is 100 so 20
|
||||
p.setHealth(sr.getAttribute(Attribute.HEALTH) / 5);
|
||||
double health =
|
||||
(sr.getAttribute(Attribute.HEALTH) + sr.getTotalItemAttribute(Attribute.HEALTH)) / 5;
|
||||
AttributeInstance attribute = p.getAttribute(org.bukkit.attribute.Attribute.GENERIC_MAX_HEALTH);
|
||||
if (attribute != null) attribute.setBaseValue(health);
|
||||
p.sendHealthUpdate();
|
||||
p.updateInventory();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue