mirror of
https://github.com/Auxilor/EcoSkills.git
synced 2026-01-02 22:02:19 +00:00
Finished stat modifier API
This commit is contained in:
@@ -3,8 +3,13 @@ package com.willfp.ecoskills.api;
|
||||
import com.willfp.ecoskills.effects.Effect;
|
||||
import com.willfp.ecoskills.skills.Skill;
|
||||
import com.willfp.ecoskills.stats.Stat;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public interface EcoSkillsAPI {
|
||||
/**
|
||||
@@ -78,6 +83,51 @@ public interface EcoSkillsAPI {
|
||||
int getStatLevel(@NotNull Player player,
|
||||
@NotNull Stat stat);
|
||||
|
||||
/**
|
||||
* Add a stat modifier to an item.
|
||||
*
|
||||
* @param itemStack The item.
|
||||
* @param modifier The modifier.
|
||||
*/
|
||||
void addStatModifier(@NotNull ItemStack itemStack,
|
||||
@NotNull StatModifier modifier);
|
||||
|
||||
/**
|
||||
* Remove a stat modifier from an item.
|
||||
*
|
||||
* @param itemStack The item.
|
||||
* @param modifier The modifier.
|
||||
*/
|
||||
void removeStatModifier(@NotNull ItemStack itemStack,
|
||||
@NotNull StatModifier modifier);
|
||||
|
||||
/**
|
||||
* Get stat modifier keys on an item.
|
||||
*
|
||||
* @param itemStack The item.
|
||||
* @return The modifier keys.
|
||||
*/
|
||||
Set<NamespacedKey> getStatModifierKeys(@NotNull ItemStack itemStack);
|
||||
|
||||
/**
|
||||
* Get stat modifiers on an item.
|
||||
*
|
||||
* @param itemStack The item.
|
||||
* @return The modifiers.
|
||||
*/
|
||||
Set<StatModifier> getStatModifiers(@NotNull ItemStack itemStack);
|
||||
|
||||
/**
|
||||
* Get stat modifier on an item.
|
||||
*
|
||||
* @param itemStack The item.
|
||||
* @param key The key
|
||||
* @return The modifier.
|
||||
*/
|
||||
@Nullable
|
||||
StatModifier getStatModifier(@NotNull ItemStack itemStack,
|
||||
@NotNull NamespacedKey key);
|
||||
|
||||
/**
|
||||
* Get the instance of the API.
|
||||
*
|
||||
|
||||
@@ -12,11 +12,6 @@ public class StatModifier {
|
||||
*/
|
||||
private final NamespacedKey key;
|
||||
|
||||
/**
|
||||
* The key.
|
||||
*/
|
||||
private final NamespacedKey slotKey;
|
||||
|
||||
/**
|
||||
* The stat.
|
||||
*/
|
||||
@@ -45,7 +40,6 @@ public class StatModifier {
|
||||
final int amount,
|
||||
@NotNull final EquipmentSlot... slot) {
|
||||
this.key = key;
|
||||
this.slotKey = NamespacedKeyUtils.create(key.getNamespace(), key.getKey() + "_slot");
|
||||
this.stat = stat;
|
||||
this.amount = amount;
|
||||
this.slots = slot;
|
||||
@@ -60,15 +54,6 @@ public class StatModifier {
|
||||
return key;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the slot key.
|
||||
*
|
||||
* @return The key.
|
||||
*/
|
||||
public NamespacedKey getSlotKey() {
|
||||
return slotKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the stat.
|
||||
*
|
||||
|
||||
@@ -1,32 +1,91 @@
|
||||
package com.willfp.ecoskills
|
||||
|
||||
import com.google.common.collect.ImmutableSet
|
||||
import com.willfp.eco.util.NamespacedKeyUtils
|
||||
import com.willfp.ecoskills.api.StatModifier
|
||||
import com.willfp.ecoskills.effects.Effect
|
||||
import com.willfp.ecoskills.skills.Skill
|
||||
import com.willfp.ecoskills.stats.Stat
|
||||
import com.willfp.ecoskills.stats.Stats
|
||||
import org.bukkit.NamespacedKey
|
||||
import org.bukkit.entity.Player
|
||||
import org.bukkit.inventory.EquipmentSlot
|
||||
import org.bukkit.inventory.ItemStack
|
||||
import org.bukkit.inventory.meta.ItemMeta
|
||||
import org.bukkit.persistence.PersistentDataAdapterContext
|
||||
import org.bukkit.persistence.PersistentDataContainer
|
||||
import org.bukkit.persistence.PersistentDataHolder
|
||||
import org.bukkit.persistence.PersistentDataType
|
||||
|
||||
private val modifierKey: NamespacedKey = NamespacedKeyUtils.create("ecoskills", "modifiers")
|
||||
private val statKey: NamespacedKey = NamespacedKeyUtils.create("ecoskills", "stat")
|
||||
private val amountKey: NamespacedKey = NamespacedKeyUtils.create("ecoskills", "amount")
|
||||
private val slotsKey: NamespacedKey = NamespacedKeyUtils.create("ecoskills", "slots")
|
||||
|
||||
private fun getModifiersTag(meta: ItemMeta): PersistentDataContainer {
|
||||
val container = meta.persistentDataContainer
|
||||
val context = container.adapterContext
|
||||
return container.getOrDefault(modifierKey, PersistentDataType.TAG_CONTAINER, context.newPersistentDataContainer())
|
||||
}
|
||||
|
||||
fun ItemStack.addStatModifier(modifier: StatModifier) {
|
||||
val meta = this.itemMeta ?: return
|
||||
|
||||
val container = meta.persistentDataContainer
|
||||
|
||||
val context = container.adapterContext
|
||||
|
||||
val modifiers = container.getOrDefault(modifierKey, PersistentDataType.TAG_CONTAINER, context.newPersistentDataContainer())
|
||||
val modifiers = getModifiersTag(meta)
|
||||
|
||||
modifiers.remove(modifier.key)
|
||||
modifiers.remove(modifier.slotKey)
|
||||
modifiers.set(modifier.key, PersistentDataType.INTEGER, modifier.amount)
|
||||
modifiers.set(modifier.slotKey, PersistentDataType.STRING, modifier.slots.map { slot -> slot.name }.toTypedArray().joinToString { "," })
|
||||
|
||||
val modifierTag = modifiers.adapterContext.newPersistentDataContainer()
|
||||
|
||||
modifierTag.set(statKey, PersistentDataType.STRING, modifier.stat.id)
|
||||
modifierTag.set(amountKey, PersistentDataType.INTEGER, modifier.amount)
|
||||
modifierTag.set(slotsKey, PersistentDataType.STRING, modifier.slots.map { slot -> slot.name }.toTypedArray().joinToString { "," })
|
||||
|
||||
modifiers.set(modifier.key, PersistentDataType.TAG_CONTAINER, modifierTag)
|
||||
|
||||
this.itemMeta = meta
|
||||
}
|
||||
|
||||
fun ItemStack.removeStatModifier(modifier: StatModifier) {
|
||||
val meta = this.itemMeta ?: return
|
||||
val modifiers = getModifiersTag(meta)
|
||||
|
||||
modifiers.remove(modifier.key)
|
||||
|
||||
this.itemMeta = meta
|
||||
}
|
||||
|
||||
fun ItemStack.getStatModifierKeys(): MutableSet<NamespacedKey> {
|
||||
val meta = this.itemMeta ?: return HashSet()
|
||||
val modifiers = getModifiersTag(meta)
|
||||
|
||||
return modifiers.keys
|
||||
}
|
||||
|
||||
fun ItemStack.getStatModifiers(): MutableSet<StatModifier> {
|
||||
val keys = HashSet<StatModifier>()
|
||||
for (modifier in this.getStatModifierKeys().stream().map { key -> this.getStatModifier(key) }) {
|
||||
if (modifier != null) {
|
||||
keys.add(modifier)
|
||||
}
|
||||
}
|
||||
return keys
|
||||
}
|
||||
|
||||
fun ItemStack.getStatModifier(key: NamespacedKey): StatModifier? {
|
||||
val meta = this.itemMeta ?: return null
|
||||
val modifiers = getModifiersTag(meta)
|
||||
|
||||
return if (modifiers.has(key, PersistentDataType.TAG_CONTAINER)) {
|
||||
val modifierTag = modifiers.get(key, PersistentDataType.TAG_CONTAINER)!!
|
||||
|
||||
val stat = Stats.getByID(modifierTag.get(statKey, PersistentDataType.STRING)!!)!!
|
||||
val amount = modifierTag.get(amountKey, PersistentDataType.INTEGER)!!
|
||||
val slots = modifierTag.get(slotsKey, PersistentDataType.STRING)!!.split(",")
|
||||
.map { s -> EquipmentSlot.valueOf(s) }
|
||||
.toCollection(ArrayList())
|
||||
|
||||
StatModifier(key, stat, amount, *slots.toTypedArray())
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,9 @@ import com.willfp.ecoskills.*
|
||||
import com.willfp.ecoskills.effects.Effect
|
||||
import com.willfp.ecoskills.skills.Skill
|
||||
import com.willfp.ecoskills.stats.Stat
|
||||
import org.bukkit.NamespacedKey
|
||||
import org.bukkit.entity.Player
|
||||
import org.bukkit.inventory.ItemStack
|
||||
|
||||
object EcoSkillsAPIImpl: EcoSkillsAPI {
|
||||
override fun getSkillLevel(player: Player, skill: Skill): Int {
|
||||
@@ -34,4 +36,24 @@ object EcoSkillsAPIImpl: EcoSkillsAPI {
|
||||
override fun getStatLevel(player: Player, stat: Stat): Int {
|
||||
return player.getStatLevel(stat)
|
||||
}
|
||||
|
||||
override fun addStatModifier(itemStack: ItemStack, modifier: StatModifier) {
|
||||
itemStack.addStatModifier(modifier)
|
||||
}
|
||||
|
||||
override fun removeStatModifier(itemStack: ItemStack, modifier: StatModifier) {
|
||||
itemStack.removeStatModifier(modifier)
|
||||
}
|
||||
|
||||
override fun getStatModifierKeys(itemStack: ItemStack): MutableSet<NamespacedKey> {
|
||||
return itemStack.getStatModifierKeys()
|
||||
}
|
||||
|
||||
override fun getStatModifiers(itemStack: ItemStack): MutableSet<StatModifier> {
|
||||
return itemStack.getStatModifiers()
|
||||
}
|
||||
|
||||
override fun getStatModifier(itemStack: ItemStack, key: NamespacedKey): StatModifier? {
|
||||
return itemStack.getStatModifier(key)
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,34 @@
|
||||
package com.willfp.ecoskills.effects.effects
|
||||
|
||||
import com.willfp.eco.util.NumberUtils
|
||||
import com.willfp.ecoskills.effects.Effect
|
||||
import com.willfp.ecoskills.getEffectLevel
|
||||
import org.bukkit.entity.Player
|
||||
import org.bukkit.event.EventHandler
|
||||
import org.bukkit.event.EventPriority
|
||||
import org.bukkit.event.entity.EntityDamageEvent
|
||||
|
||||
class EffectSeamlessMovement: Effect(
|
||||
"seamless_movement"
|
||||
) {
|
||||
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
|
||||
fun handle(event: EntityDamageEvent) {
|
||||
val player = event.entity
|
||||
|
||||
if (player !is Player) {
|
||||
return
|
||||
}
|
||||
|
||||
if (event.cause != EntityDamageEvent.DamageCause.FALL) {
|
||||
return
|
||||
}
|
||||
|
||||
val level = player.getEffectLevel(this)
|
||||
|
||||
val chance = config.getDouble("chance-per-level") * level
|
||||
|
||||
if (NumberUtils.randFloat(0.0, 100.0) < chance) {
|
||||
event.isCancelled = true
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user