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

Commit

Permalink
Add Enchantments from Piglin Bartering
Browse files Browse the repository at this point in the history
  • Loading branch information
gmitch215 committed Dec 28, 2023
1 parent ace3666 commit 570a891
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 3 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 @@ -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
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 570a891

Please sign in to comment.