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

Commit

Permalink
Update to v1.1.1 (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregory authored Dec 28, 2023
2 parents 1d3c9e2 + af13a73 commit 809aff4
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1273,4 +1273,76 @@ interface PlasmaConfig {
* @param value Maximum Anvil Cost
*/
set(value) { "max-anvil-cost"[configuration, configFile] = value }

var isEnchantmentBarteringEnabled: Boolean
/**
* Fetches whether or not bartering with piglins can yield enchanted books.
* @return true if enabled, false otherwise
*/
get() = "enchantments.bartering.enabled"[configuration, Boolean::class.java, true]
/**
* Sets whether or not bartering with piglins can yield enchanted books.
* @param value true if enabled, false otherwise
*/
set(value) { "enchantments.bartering.enabled"[configuration, configFile] = value }

var enchantmentBarteringChance: Double
/**
* Fetches the chance for a bartering trade to yield an enchanted book.
* @return Chance
*/
get() = "enchantments.bartering.chance"[configuration, Double::class.java, 0.05]
/**
* Sets the chance for a bartering trade to yield an enchanted book.
* @param value Chance
*/
set(value) { "enchantments.bartering.chance"[configuration, configFile] = value }

var blacklistedBarteringEnchantments: List<PEnchantment>
/**
* Fetches the list of enchantments that cannot be received through bartering.
* @return List of Enchantments
*/
get() = "enchantments.bartering.blacklisted-enchants"[configuration, List::class.java, listOf<String>()].mapNotNull { registry.getEnchantment(it.toString()) }
/**
* Sets the list of enchantments that cannot be received through bartering.
* @param value List of Enchantments
*/
set(value) { "enchantments.bartering.blacklisted-enchants"[configuration, configFile] = value.map { it.key.key } }

var whitelistedBarteringEnchantments: List<PEnchantment>
/**
* Fetches the list of enchantments that can only be received through bartering.
* @return List of Enchantments
*/
get() = "enchantments.bartering.whitelisted-enchants"[configuration, List::class.java, listOf<String>()].mapNotNull { registry.getEnchantment(it.toString()) }
/**
* Sets the list of enchantments that can only be received through bartering.
* @param value List of Enchantments
*/
set(value) { "enchantments.bartering.whitelisted-enchants"[configuration, configFile] = value.map { it.key.key } }

var enchantmentBarteringMinEnchantLevel: Int
/**
* Fetches the minimum enchantment level for enchanted books received through bartering.
* @return Minimum Level
*/
get() = "enchantments.bartering.min-level"[configuration, Int::class.java, 1]
/**
* Sets the minimum enchantment level for enchanted books received through bartering.
* @param value Minimum Level
*/
set(value) { "enchantments.bartering.min-level"[configuration, configFile] = value }

var enchantmentBarteringMaxEnchantLevel: Int
/**
* Fetches the maximum enchantment level for enchanted books received through bartering.
* @return Maximum Level
*/
get() = "enchantments.bartering.max-level"[configuration, Int::class.java] ?: Int.MAX_VALUE
/**
* Sets the maximum enchantment level for enchanted books received through bartering.
* @param value Maximum Level
*/
set(value) { "enchantments.bartering.max-level"[configuration, configFile] = value }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package us.teaminceptus.plasmaenchants.api.config

import com.google.common.collect.ImmutableMap
import org.bukkit.Material
import org.bukkit.configuration.MemorySection
import org.bukkit.configuration.file.FileConfiguration
import org.bukkit.enchantments.Enchantment
import org.bukkit.entity.EntityType
Expand Down Expand Up @@ -188,6 +187,20 @@ internal val CONFIG_MAP = ImmutableMap.builder<String, ConfigData>()
}}
)

.putSection("enchantments.bartering")
.put("enchantments.bartering.enabled", FileConfiguration::isBoolean, true)
.put("enchantments.bartering.chance", isNumber, 0.05)
.put("enchantments.bartering.blacklisted-enchants", FileConfiguration::isList, listOf<String>(),
{ value -> value.all { it.isEnchantment() } },
{ old -> old.filter { it.isEnchantment() } }
)
.put("enchantments.bartering.whitelisted-enchants", FileConfiguration::isList, listOf<String>(),
{ value -> value.all { it.isEnchantment() } },
{ old -> old.filter { it.isEnchantment() } }
)
.put("enchantments.bartering.min-level", FileConfiguration::isInt, "default")
.put("enchantments.bartering.max-level", FileConfiguration::isInt, "default")

// Artifact Configuration
.putSection("artifacts")
.put("artifacts.disabled-artifacts", FileConfiguration::isList, listOf<String>(),
Expand Down Expand Up @@ -273,7 +286,7 @@ private fun Any?.isArtifact(): Boolean {
}

private inline fun <K, reified CV> ImmutableMap.Builder<K, ConfigData>.put(
key: K,
key: K & Any,
noinline checker: (FileConfiguration, String) -> Boolean,
default: CV? = null,
crossinline validator: (CV) -> Boolean = { true },
Expand All @@ -285,7 +298,7 @@ private inline fun <K, reified CV> ImmutableMap.Builder<K, ConfigData>.put(
false)
)

private fun <T> ImmutableMap.Builder<T, ConfigData>.putSection(key: T) =
private fun <T> ImmutableMap.Builder<T, ConfigData>.putSection(key: T & Any) =
put(key, ConfigData(FileConfiguration::isConfigurationSection, null, { true }, { it }, true))

private fun <K, V> Map<K, V>.key(key: K, predicate: (V?) -> Boolean): Boolean = predicate(get(key))
Expand Down
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)
}

}
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ plugins {
}

val pGroup = "us.teaminceptus.plasmaenchants"
val pVersion = "1.1.0"
val pVersion = "1.1.1"
val pAuthor = "Team-Inceptus"

val jvmVersion: JavaVersion = JavaVersion.VERSION_11
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.bukkit.entity.Player
import org.bukkit.event.HandlerList
import org.bukkit.event.player.PlayerEvent
import org.bukkit.inventory.ItemStack
import org.bukkit.plugin.Plugin
import org.bukkit.scheduler.BukkitRunnable
import us.teaminceptus.plasmaenchants.api.*
import us.teaminceptus.plasmaenchants.api.artifacts.PArtifact
Expand Down Expand Up @@ -110,6 +111,11 @@ class PlasmaEnchants : JavaPlugin(), PlasmaConfig, PlasmaRegistry {
.forEach { if (it is PArtifact) register(it) }
} catch (ignored: ClassNotFoundException) {}

try {
val clazz = Class.forName("us.teaminceptus.plasmaenchants.events.Events$version")
clazz.getConstructor(Plugin::class.java).newInstance(this)
} catch (ignored: ClassNotFoundException) {}

if (major == version) break
}
}
Expand Down
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
24 changes: 24 additions & 0 deletions plugin/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,30 @@ enchantments:
# - boss_collector
config: []

# Enchanted Books received through Piglin Bartering
bartering:
# Whether you can receive enchantment books from bartering with piglins
enabled: true

# The chance of receiving an enchanted book from bartering with piglins
chance: 0.05

# A List of Enchantments that shouldn't be received through barterin
# If not empty, this overrides the global blacklisted enchantments
blacklisted-enchants: []

# A List of Enchantments that can only be received through bartering
# If this isn't empty, only these enchantments can be received by bartering and this overrides the global whitelisted enchantments
whitelisted-enchants: []

# The Minimum Enchantment Level for a fished enchanted book
# Set to 'default' to use the global default
min-level: default

# The Maximum Enchantment Level for a fished enchanted book
# Set to 'default' to use the global default
max-level: default

# Villager Trading for Enchantments Configuration
trades:
# Whether Wandering Traders can trade for artifacts
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package us.teaminceptus.plasmaenchants.events

import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.entity.PiglinBarterEvent
import org.bukkit.plugin.Plugin
import us.teaminceptus.plasmaenchants.api.PType
import us.teaminceptus.plasmaenchants.api.PlasmaConfig
import us.teaminceptus.plasmaenchants.api.artifacts.PArtifact
import java.security.SecureRandom

internal class Events1_16(plugin: Plugin) : Listener {

init {
plugin.server.pluginManager.registerEvents(this, plugin)
}

val r: SecureRandom = SecureRandom()

@EventHandler
fun barter(event: PiglinBarterEvent) {
if (r.nextDouble() > PlasmaConfig.config.enchantmentBarteringChance) return

val blacklist = PlasmaConfig.config.blacklistedBarteringEnchantments
val whitelist = PlasmaConfig.config.whitelistedBarteringEnchantments

val enchantment = PlasmaConfig.registry.enchantments
.filter { !it.isDisabled && !it.isSpawnBlacklisted }
.filter {
if (whitelist.isNotEmpty())
whitelist.contains(it)
else
!blacklist.contains(it)
}
.randomOrNull() ?: return

val minLevel = PlasmaConfig.config.enchantmentBarteringMinEnchantLevel.coerceAtLeast(1)
val maxLevel = PlasmaConfig.config.enchantmentBarteringMaxEnchantLevel.coerceIn(minLevel..(enchantment.maxLevel + 1))

val level = (if (minLevel >= maxLevel) minLevel else r.nextInt(minLevel, maxLevel)).coerceAtMost(enchantment.maxLevel)
event.outcome.add(enchantment.generateBook(level))
}

}

0 comments on commit 809aff4

Please sign in to comment.