Files
src
build.gradle.kts
Code
12 814
src/main/kotlin/com/boxpvp/managers/ChestManager.kt
package com.boxpvp.managers
import com.boxpvp.BoxPvPEventsPlugin
import com.boxpvp.model.EventType
import org.bukkit.Location
import org.bukkit.Material
import org.bukkit.block.Chest
import org.bukkit.enchantments.Enchantment
import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.meta.ItemMeta
import kotlin.random.Random
class ChestManager(private val plugin: BoxPvPEventsPlugin) {
private var chestLocation: Location? = null
fun spawnChest(eventType: EventType) {
val world = plugin.server.getWorld(plugin.config.getString("event-world", "world")!!)
?: run {
plugin.logger.warning("World not found!")
return
}
val x = plugin.config.getInt("chest.x", 13)
val y = plugin.config.getInt("chest.y", -56)
val z = plugin.config.getInt("chest.z", 36)
val location = Location(world, x.toDouble(), y.toDouble(), z.toDouble())
removeChest()
location.block.type = Material.CHEST
val blockState = location.block.state as? Chest ?: return
val inventory = blockState.inventory
inventory.clear()
fillLoot(inventory, eventType)
blockState.update()
chestLocation = location
plugin.logger.info("Event chest spawned at $x, $y, $z for event: ${eventType.displayName}")
}
private fun fillLoot(inventory: org.bukkit.inventory.Inventory, eventType: EventType) {
val lootItems = mutableListOf<ItemStack>()
val netheriteMin = plugin.config.getInt("loot.netherite-blocks.min", 8)
val netheriteMax = plugin.config.getInt("loot.netherite-blocks.max", 64)
val netheriteCount = Random.nextInt(netheriteMin, netheriteMax + 1)
val netheriteItem = ItemStack(Material.NETHERITE_BLOCK, netheriteCount)
val netheriteMeta = netheriteItem.itemMeta
netheriteMeta?.addEnchant(Enchantment.MENDING, 1, true)
netheriteItem.itemMeta = netheriteMeta
lootItems.add(netheriteItem)
val beaconMin = plugin.config.getInt("loot.beacons.min", 5)
val beaconMax = plugin.config.getInt("loot.beacons.max", 50)
val beaconCount = Random.nextInt(beaconMin, beaconMax + 1)
distributeItems(lootItems, Material.BEACON, beaconCount)
val gaMin = plugin.config.getInt("loot.golden-apples.min", 5)
val gaMax = plugin.config.getInt("loot.golden-apples.max", 20)
val gaCount = Random.nextInt(gaMin, gaMax + 1)
distributeItems(lootItems, Material.GOLDEN_APPLE, gaCount)
val egaMin = plugin.config.getInt("loot.enchanted-golden-apples.min", 1)
val egaMax = plugin.config.getInt("loot.enchanted-golden-apples.max", 5)
val egaCount = Random.nextInt(egaMin, egaMax + 1)
lootItems.add(ItemStack(Material.ENCHANTED_GOLDEN_APPLE, egaCount))
val speedMin = plugin.config.getInt("loot.speed-potions.min", 3)
val speedMax = plugin.config.getInt("loot.speed-potions.max", 10)
val speedCount = Random.nextInt(speedMin, speedMax + 1)
lootItems.add(createPotion(org.bukkit.potion.PotionType.SWIFTNESS, speedCount))
val strengthMin = plugin.config.getInt("loot.strength-potions.min", 3)
val strengthMax = plugin.config.getInt("loot.strength-potions.max", 10)
val strengthCount = Random.nextInt(strengthMin, strengthMax + 1)
lootItems.add(createPotion(org.bukkit.potion.PotionType.STRENGTH, strengthCount))
val fireMin = plugin.config.getInt("loot.fire-resistance-potions.min", 2)
val fireMax = plugin.config.getInt("loot.fire-resistance-potions.max", 8)
val fireCount = Random.nextInt(fireMin, fireMax + 1)
lootItems.add(createPotion(org.bukkit.potion.PotionType.FIRE_RESISTANCE, fireCount))
if (eventType.isMythic()) {
lootItems.add(ItemStack(Material.NETHERITE_INGOT, Random.nextInt(5, 20)))
lootItems.add(ItemStack(Material.ENCHANTED_GOLDEN_APPLE, Random.nextInt(2, 8)))
}
val slots = (0 until inventory.size).toMutableList()
slots.shuffle()
var slotIndex = 0
for (item in lootItems) {
if (slotIndex >= slots.size) break
inventory.setItem(slots[slotIndex++], item)
}
}
private fun distributeItems(list: MutableList<ItemStack>, material: Material, total: Int) {
var remaining = total
while (remaining > 0) {
val stackSize = minOf(remaining, material.maxStackSize)
list.add(ItemStack(material, stackSize))
remaining -= stackSize
}
}
private fun createPotion(type: org.bukkit.potion.PotionType, amount: Int): ItemStack {
val potion = ItemStack(Material.POTION, amount.coerceAtMost(64))
val meta = potion.itemMeta as? org.bukkit.inventory.meta.PotionMeta ?: return potion
meta.basePotionType = type
potion.itemMeta = meta
return potion
}
fun removeChest() {
val loc = chestLocation ?: return
if (loc.block.type == Material.CHEST) {
loc.block.type = Material.AIR
}
chestLocation = null
}
fun getChestLocation(): Location? = chestLocation
fun isChestLocation(location: Location): Boolean {
val loc = chestLocation ?: return false
return loc.world == location.world &&
loc.blockX == location.blockX &&
loc.blockY == location.blockY &&
loc.blockZ == location.blockZ
}
}