Skip to content
This repository has been archived by the owner on Mar 7, 2024. It is now read-only.

Commit

Permalink
Expose PEnchantment#isSpawnBlacklisted
Browse files Browse the repository at this point in the history
  • Loading branch information
gmitch215 committed Dec 28, 2023
1 parent 570a891 commit 7bc6291
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,16 @@ interface PEnchantment : BiConsumer<Event, Int>, Keyed {
*/
get() = PlasmaConfig.config.disabledEnchantments.contains(this)

val isSpawnBlacklisted: Boolean
/**
* Whether or not this PEnchantment is blacklisted from spawning.
* @return true if blacklisted, false otherwise
*/
get() {
if (PlasmaConfig.config.whitelistedSpawnEnchantments.isNotEmpty())
return !PlasmaConfig.config.whitelistedSpawnEnchantments.contains(this)

return PlasmaConfig.config.blacklistedSpawnEnchantments.contains(this)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import org.bukkit.event.player.PlayerFishEvent
import org.bukkit.event.player.PlayerInteractEvent
import org.bukkit.inventory.BlockInventoryHolder
import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.MerchantInventory
import org.bukkit.inventory.MerchantRecipe
import org.bukkit.loot.LootTables
import org.bukkit.loot.Lootable
Expand Down Expand Up @@ -40,17 +39,9 @@ class SpawnEvents(private val plugin: PlasmaEnchants) : Listener {

// Extension Util

private inline val PEnchantment.isBlacklisted: Boolean
get() {
if (plugin.whitelistedSpawnEnchantments.isNotEmpty())
return !plugin.whitelistedSpawnEnchantments.contains(this)

return plugin.blacklistedSpawnEnchantments.contains(this)
}

private inline val PEnchantment.isFishingBlacklisted: Boolean
get() {
if (isBlacklisted) return true
if (isSpawnBlacklisted) return true

if (plugin.enchantmentSpawnFishingWhitelistedEnchants.isNotEmpty())
return !plugin.enchantmentSpawnFishingWhitelistedEnchants.contains(this)
Expand Down Expand Up @@ -90,7 +81,7 @@ class SpawnEvents(private val plugin: PlasmaEnchants) : Listener {

fun <T : Enum<T>> getRandomEnchantment(fishing: Boolean = false, configuration: EnchantmentChanceConfiguration<T>? = null): PEnchantment? =
plugin.enchantments.filter {
!it.isDisabled && !it.isBlacklisted && (configuration?.isAllowed(it) ?: true) && !(fishing && it.isFishingBlacklisted)
!it.isDisabled && !it.isSpawnBlacklisted && (configuration?.isAllowed(it) ?: true) && !(fishing && it.isFishingBlacklisted)
}.randomOrNull()

fun <T: Enum<T>> generateEnchantmentBook(min: Int = plugin.enchantmentSpawnMiningMaxLevel, max: Int = plugin.enchantmentSpawnMaxLevel, fishing: Boolean = false, configuration: EnchantmentChanceConfiguration<T>? = null): ItemStack? {
Expand All @@ -106,7 +97,7 @@ class SpawnEvents(private val plugin: PlasmaEnchants) : Listener {
return enchant?.generateBook(r.nextInt(min0, max0 + 1))
}

fun isAllowed(type: Any, blacklist: List<Any>, whitelist: List<Any>): Boolean {
fun <T> isAllowed(type: T, blacklist: List<T>, whitelist: List<T>): Boolean {
if (whitelist.isNotEmpty())
return whitelist.contains(type)

Expand Down

0 comments on commit 7bc6291

Please sign in to comment.