Files
src
build.gradle.kts
manifest.kod
settings.gradle.kts
Code
30 2,672
src/main/kotlin/com/monsmp/crates/CrateManager.kt
package com.monsmp.crates
import com.monsmp.MonSMP
import com.monsmp.utils.ItemBuilder
import org.bukkit.Location
import org.bukkit.Material
import org.bukkit.Particle
import org.bukkit.Sound
import org.bukkit.block.Block
import org.bukkit.block.Chest
import org.bukkit.configuration.file.YamlConfiguration
import org.bukkit.entity.Player
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.block.Action
import org.bukkit.event.player.PlayerInteractEvent
import org.bukkit.inventory.ItemFlag
import org.bukkit.inventory.ItemStack
import java.io.File
import java.util.*
class CrateManager(private val plugin: MonSMP) : Listener {
private val cratesFile: File = File(plugin.dataFolder, "crates.yml")
private val crates: MutableMap<String, CrateData> = mutableMapOf()
private val crateLocations: MutableMap<Location, String> = mutableMapOf()
data class CrateData(
val name: String,
val displayName: String,
val loot: MutableList<LootItem> = mutableListOf(),
val keyMaterial: Material = Material.TRIPWIRE_HOOK
)
data class LootItem(
val material: Material,
val amount: Int = 1,
val chance: Double = 10.0,
val displayName: String? = null
)
init {
loadCrates()
// Default loot tables
if (crates.isEmpty()) {
createDefaultCrate()
}
}
private fun createDefaultCrate() {
val voteCrate = CrateData("vote", "&b&lVote Crate")
voteCrate.loot.addAll(listOf(
LootItem(Material.DIAMOND, 5, 20.0),
LootItem(Material.IRON_INGOT, 32, 40.0),
LootItem(Material.GOLD_INGOT, 16, 35.0),
LootItem(Material.ENDER_PEARL, 8, 25.0),
LootItem(Material.EXPERIENCE_BOTTLE, 32, 30.0),
LootItem(Material.OBSIDIAN, 16, 20.0),
LootItem(Material.NETHERITE_SCRAP, 1, 5.0),
LootItem(Material.TOTEM_OF_UNDYING, 1, 2.0),
LootItem(Material.ENCHANTED_GOLDEN_APPLE, 1, 3.0),
LootItem(Material.SHULKER_BOX, 1, 8.0)
))
crates["vote"] = voteCrate
val rareCrate = CrateData("rare", "&6&lRare Crate")
rareCrate.loot.addAll(listOf(
LootItem(Material.DIAMOND, 16, 25.0),
LootItem(Material.NETHERITE_INGOT, 1, 10.0),
LootItem(Material.NETHERITE_SCRAP, 3, 15.0),
LootItem(Material.ENCHANTED_GOLDEN_APPLE, 2, 15.0),
LootItem(Material.TOTEM_OF_UNDYING, 2, 10.0),
LootItem(Material.EXPERIENCE_BOTTLE, 64, 20.0),
LootItem(Material.HEART_OF_THE_SEA, 1, 2.0),
LootItem(Material.ELYTRA, 1, 1.0),
LootItem(Material.TRIDENT, 1, 3.0),
LootItem(Material.SHULKER_SHELL, 4, 8.0)
))
crates["rare"] = rareCrate
}
fun loadCrates() {
crates.clear()
crateLocations.clear()
if (!cratesFile.exists()) {
saveCrates()
return
}
val config = YamlConfiguration.loadConfiguration(cratesFile)
// Load crate locations
val locationsSection = config.getConfigurationSection("locations")
if (locationsSection != null) {
for (key in locationsSection.getKeys(false)) {
val parts = key.split(",")
if (parts.size == 4) {
val world = plugin.server.getWorld(parts[0])
if (world != null) {
val loc = Location(world, parts[1].toDouble(), parts[2].toDouble(), parts[3].toDouble())
crateLocations[loc] = locationsSection.getString(key) ?: continue
}
}
}
}
// Load crate definitions
val cratesSection = config.getConfigurationSection("crates")
if (cratesSection != null) {
for (key in cratesSection.getKeys(false)) {
val path = "crates.$key"
val displayName = config.getString("$path.display-name", "&b$key Crate") ?: "&b$key Crate"
val crate = CrateData(key, displayName)
val lootSection = config.getConfigurationSection("$path.loot")
if (lootSection != null) {
for (lootKey in lootSection.getKeys(false)) {
val lootPath = "$path.loot.$lootKey"
try {
val material = Material.valueOf(config.getString("$lootPath.material", "STONE") ?: "STONE")
val amount = config.getInt("$lootPath.amount", 1)
val chance = config.getDouble("$lootPath.chance", 10.0)
val displayName_ = config.getString("$lootPath.display-name")
crate.loot.add(LootItem(material, amount, chance, displayName_))
} catch (_: IllegalArgumentException) {}
}
}
crates[key] = crate
}
}
}
fun saveCrates() {
val config = YamlConfiguration()
// Save locations
for ((loc, name) in crateLocations) {
val key = "${loc.world?.name ?: "world"},${loc.blockX},${loc.blockY},${loc.blockZ}"
config.set("locations.$key", name)
}
// Save crate definitions
for ((name, crate) in crates) {
val path = "crates.$name"
config.set("$path.display-name", crate.displayName)
for ((i, loot) in crate.loot.withIndex()) {
val lootPath = "$path.loot.loot_$i"
config.set("$lootPath.material", loot.material.name)
config.set("$lootPath.amount", loot.amount)
config.set("$lootPath.chance", loot.chance)
if (loot.displayName != null) {
config.set("$lootPath.display-name", loot.displayName)
}
}
}
config.save(cratesFile)
}
fun createCrate(name: String, location: Location) {
val block = location.block
block.type = Material.CHEST
if (block.state is Chest) {
crateLocations[location] = name.lowercase()
saveCrates()
// Create default crate if doesn't exist
if (!crates.containsKey(name.lowercase())) {
val crate = CrateData(name.lowercase(), "&b&l${name.replaceFirstChar { it.uppercase() }} Crate")
crate.loot.addAll(listOf(
LootItem(Material.DIAMOND, 3, 25.0),
LootItem(Material.IRON_INGOT, 16, 40.0),
LootItem(Material.EXPERIENCE_BOTTLE, 16, 30.0),
LootItem(Material.GOLDEN_APPLE, 3, 20.0)
))
crates[name.lowercase()] = crate
saveCrates()
}
}
}
fun createKey(crateName: String): ItemStack? {
val crate = crates[crateName.lowercase()] ?: return null
return ItemBuilder.start(crate.keyMaterial)
.name(plugin.colorUtils.colorize("&6&l${crateName.replaceFirstChar { it.uppercase() }} Key"))
.lore(
plugin.colorUtils.colorize("&7Right-click a ${crateName.uppercase()} crate"),
plugin.colorUtils.colorize("&7to open it!"),
"",
plugin.colorUtils.colorize("&eCrate: ${crateName.replaceFirstChar { it.uppercase() }}")
)
.flags(ItemFlag.HIDE_ATTRIBUTES)
.build()
}
fun getCrates(): Map<String, CrateData> = crates.toMap()
fun handleInteract(player: Player, block: Block) {
val loc = block.location
val crateName = crateLocations[loc] ?: return
if (block.type != Material.CHEST && block.type != Material.ENDER_CHEST) return