diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoarmor/sets/ArmorSet.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoarmor/sets/ArmorSet.kt index 7c9c971..a70f323 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoarmor/sets/ArmorSet.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoarmor/sets/ArmorSet.kt @@ -16,6 +16,7 @@ import com.willfp.ecoarmor.sets.ArmorUtils.setAdvanced import com.willfp.ecoarmor.sets.ArmorUtils.setTier import com.willfp.ecoarmor.upgrades.Tier import com.willfp.ecoarmor.upgrades.Tiers +import com.willfp.ecoarmor.util.PlayableSound import com.willfp.ecoarmor.util.notNullMapOf import com.willfp.libreforge.Holder import com.willfp.libreforge.conditions.Conditions @@ -23,13 +24,14 @@ import com.willfp.libreforge.conditions.ConfiguredCondition import com.willfp.libreforge.effects.ConfiguredEffect import com.willfp.libreforge.effects.Effects import org.bukkit.Bukkit +import org.bukkit.Sound import org.bukkit.inventory.ItemStack import org.bukkit.persistence.PersistentDataType import java.util.* import java.util.stream.Collectors class ArmorSet( - private val config: Config, + val config: Config, private val plugin: EcoPlugin ) { /** @@ -62,6 +64,39 @@ class ArmorSet( */ val advancementShardItem: ItemStack + /* + * Equip Sound + */ + val equipSound = if (config.getBool("sounds.equip.enabled")) { + PlayableSound( + Sound.valueOf(config.getString("sounds.equip.sound").uppercase()), + config.getDouble("sounds.equip.volume"), + config.getDouble("sounds.equip.pitch") + ) + } else null + + /* + * Advanced equip Sound + */ + val advancedEquipSound = if (config.getBool("sounds.equip.enabled")) { + PlayableSound( + Sound.valueOf(config.getString("sounds.equip.sound").uppercase()), + config.getDouble("sounds.equip.volume"), + config.getDouble("sounds.equip.pitch") + ) + } else null + + /* + * Unequip Sound + */ + val unequipSound = if (config.getBool("sounds.equip.enabled")) { + PlayableSound( + Sound.valueOf(config.getString("sounds.equip.sound").uppercase()), + config.getDouble("sounds.equip.volume"), + config.getDouble("sounds.equip.pitch") + ) + } else null + /** * Create a new Armor Set. */ @@ -132,7 +167,11 @@ class ArmorSet( advanced: Boolean ): ItemStack { val builder: ItemBuilder = ItemStackBuilder(Items.lookup(slotConfig.getString("item")).item) - .setDisplayName(if (advanced) slotConfig.getFormattedString("advancedName") else slotConfig.getFormattedString("name")) + .setDisplayName( + if (advanced) slotConfig.getFormattedString("advancedName") else slotConfig.getFormattedString( + "name" + ) + ) .addLoreLines(slotConfig.getFormattedStrings("lore").stream().map { s: String -> Display.PREFIX + s } .collect(Collectors.toList())) .addLoreLines { @@ -144,7 +183,11 @@ class ArmorSet( return@addLoreLines null } } - .setDisplayName { if (advanced) slotConfig.getFormattedString("advancedName") else slotConfig.getFormattedString("name") } + .setDisplayName { + if (advanced) slotConfig.getFormattedString("advancedName") else slotConfig.getFormattedString( + "name" + ) + } builder.writeMetaKey( plugin.namespacedKeyFactory.create("set"), PersistentDataType.STRING, diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoarmor/util/EffectListener.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoarmor/util/EffectListener.kt index 35c80a5..2bc028b 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoarmor/util/EffectListener.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoarmor/util/EffectListener.kt @@ -1,15 +1,79 @@ package com.willfp.ecoarmor.util import com.willfp.eco.core.events.ArmorChangeEvent +import com.willfp.ecoarmor.sets.ArmorSet +import com.willfp.ecoarmor.sets.ArmorUtils import com.willfp.libreforge.updateEffects +import org.bukkit.Material import org.bukkit.event.EventHandler import org.bukkit.event.Listener +import org.bukkit.inventory.ItemStack -class EffectListener: Listener { +class EffectListener : Listener { @EventHandler fun armorEquipListener(event: ArmorChangeEvent) { val player = event.player player.updateEffects() } -} \ No newline at end of file + + @EventHandler + fun handleSounds(event: ArmorChangeEvent) { + val player = event.player + + val new = getSetOnItems(event.after) ?: return + val newAdvanced = isAdvanced(event.after) + val prev = getSetOnItems(event.before) + val prevAdvanced = isAdvanced(event.before) + if (new == prev && newAdvanced == prevAdvanced) { + return + } + + if (new == prev) { + if (newAdvanced && !prevAdvanced) { + new.advancedEquipSound?.play(player) + } + } else { + prev?.unequipSound?.play(player) + if (newAdvanced) { + new.advancedEquipSound?.play(player) + } else { + new.equipSound?.play(player) + } + } + } + + private fun getSetOnItems(items: List): ArmorSet? { + var set: ArmorSet? = null + + for (item in items) { + if (item == null || item.type == Material.AIR) { + return null + } + + val found = ArmorUtils.getSetOnItem(item) ?: return null + + if (set != null && set != found) { + return null + } + + set = found + } + + return set + } + + private fun isAdvanced(items: List): Boolean { + for (item in items) { + if (item == null) { + return false + } + + if (!ArmorUtils.isAdvanced(item)) { + return false + } + } + + return true + } +} diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoarmor/util/PlayableSound.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoarmor/util/PlayableSound.kt new file mode 100644 index 0000000..a0de21f --- /dev/null +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoarmor/util/PlayableSound.kt @@ -0,0 +1,14 @@ +package com.willfp.ecoarmor.util + +import org.bukkit.Sound +import org.bukkit.entity.Player + +class PlayableSound( + private val sound: Sound, + private val pitch: Double, + val volume: Double +) { + fun play(player: Player) { + player.playSound(player.location, sound, volume.toFloat(), pitch.toFloat()) + } +} \ No newline at end of file diff --git a/eco-core/core-plugin/src/main/resources/ecoarmor.yml b/eco-core/core-plugin/src/main/resources/ecoarmor.yml index 404e826..639e756 100644 --- a/eco-core/core-plugin/src/main/resources/ecoarmor.yml +++ b/eco-core/core-plugin/src/main/resources/ecoarmor.yml @@ -22,6 +22,22 @@ sets: multiplier: 0.9 triggers: - take_damage + sounds: + equip: + enabled: false + sound: "" + volume: 1 + pitch: 1 + advancedEquip: + enabled: false + sound: "" + volume: 1 + pitch: 1 + unequip: + enabled: false + sound: "" + volume: 1 + pitch: 1 advancedLore: - '' - "&lADVANCED BONUS" @@ -196,6 +212,22 @@ sets: - trident_attack filters: onlyBosses: true + sounds: + equip: + enabled: false + sound: "" + volume: 1 + pitch: 1 + advancedEquip: + enabled: false + sound: "" + volume: 1 + pitch: 1 + unequip: + enabled: false + sound: "" + volume: 1 + pitch: 1 advancedLore: - '' - "&lADVANCED BONUS" @@ -371,6 +403,22 @@ sets: args: effect: regeneration level: 1 + sounds: + equip: + enabled: false + sound: "" + volume: 1 + pitch: 1 + advancedEquip: + enabled: false + sound: "" + volume: 1 + pitch: 1 + unequip: + enabled: false + sound: "" + volume: 1 + pitch: 1 advancedLore: - '' - "&lADVANCED BONUS" @@ -553,8 +601,22 @@ sets: multiplier: 1.75 triggers: - bow_attack - potionEffects: [] - advancedPotionEffects: [] + sounds: + equip: + enabled: false + sound: "" + volume: 1 + pitch: 1 + advancedEquip: + enabled: false + sound: "" + volume: 1 + pitch: 1 + unequip: + enabled: false + sound: "" + volume: 1 + pitch: 1 advancedLore: - '' - "&lADVANCED BONUS"