Files
src
build.gradle.kts
manifest.kod
settings.gradle.kts
Code
5 208
src/main/kotlin/com/kodari/dabot/DabotPlugin.kt
package com.kodari.dabot
import org.bukkit.Bukkit
import org.bukkit.ChatColor
import org.bukkit.Location
import org.bukkit.Material
import org.bukkit.NamespacedKey
import org.bukkit.World
import org.bukkit.WorldCreator
import org.bukkit.attribute.Attribute
import org.bukkit.block.Block
import org.bukkit.command.Command
import org.bukkit.command.CommandSender
import org.bukkit.entity.EntityType
import org.bukkit.entity.LivingEntity
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.entity.EntityDeathEvent
import org.bukkit.event.player.PlayerInteractEvent
import org.bukkit.event.player.PlayerMoveEvent
import org.bukkit.event.player.PlayerQuitEvent
import org.bukkit.inventory.EquipmentSlot
import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.ShapedRecipe
import org.bukkit.persistence.PersistentDataType
import org.bukkit.plugin.java.JavaPlugin
import java.util.UUID
class DabotPlugin : JavaPlugin(), Listener {
private val shardKey by lazy { NamespacedKey(this, "sculk_shard") }
private val returns = mutableMapOf<UUID, Location>()
private val cooldowns = mutableMapOf<UUID, Long>()
private lateinit var dimension: World
override fun onEnable() {
dimension = WorldCreator("dabot_dimension").createWorld()
?: throw IllegalStateException("Could not create the Dabot Dimension")
val shard = ItemStack(Material.ECHO_SHARD)
shard.itemMeta = shard.itemMeta.apply {
setDisplayName("${ChatColor.DARK_AQUA}Sculk Shard")
persistentDataContainer.set(shardKey, PersistentDataType.BYTE, 1.toByte())
}
Bukkit.addRecipe(ShapedRecipe(shardKey, shard).apply {
shape(" S ", "SES", " S ")
setIngredient('S', Material.SCULK)
setIngredient('E', Material.ECHO_SHARD)
})
server.pluginManager.registerEvents(this, this)
}
@EventHandler
fun onLight(event: PlayerInteractEvent) {
if (event.hand != EquipmentSlot.HAND || event.action != Action.RIGHT_CLICK_BLOCK) return
val item = event.item ?: return
if (item.itemMeta?.persistentDataContainer?.get(shardKey, PersistentDataType.BYTE) != 1.toByte()) return
val clicked = event.clickedBlock ?: return
if (clicked.type != Material.SCULK) return
event.isCancelled = true
val frame = findFrame(clicked, false)
if (frame == null) {
event.player.sendMessage("${ChatColor.RED}Build a 4-wide, 5-high sculk frame with a clear 2-by-3 interior.")
return
}
frame.forEach { it.type = Material.PURPLE_STAINED_GLASS }
if (event.player.gameMode != org.bukkit.GameMode.CREATIVE) item.amount -= 1
event.player.sendMessage("${ChatColor.DARK_AQUA}The Dabot portal awakens!")
}
@EventHandler
fun onMove(event: PlayerMoveEvent) {
val to = event.to
if (event.from.blockX == to.blockX && event.from.blockY == to.blockY && event.from.blockZ == to.blockZ && event.from.world == to.world) return
if (to.block.type != Material.PURPLE_STAINED_GLASS || findFrame(to.block, true) == null) return
val player = event.player
if ((cooldowns[player.uniqueId] ?: 0L) > System.currentTimeMillis()) return
cooldowns[player.uniqueId] = System.currentTimeMillis() + 5000
if (player.world == dimension) {
leave(player)
} else {
val origin = event.from.clone()
val spawn = dimension.spawnLocation.clone().add(0.5, 0.0, 0.5)
if (player.teleport(spawn)) {
returns[player.uniqueId] = origin
saveReturn(player.uniqueId, origin)
spawnBoss()
player.sendMessage("${ChatColor.DARK_AQUA}You entered the Dabot Dimension! Use /dabot leave to return.")
}
}
}
private fun findFrame(block: Block, lit: Boolean): List<Block>? {
val world = block.world
for ((dx, dz) in listOf(1 to 0, 0 to 1)) {
for (horizontal in 0..3) for (vertical in 0..4) {
val x = block.x - horizontal * dx
val y = block.y - vertical
val z = block.z - horizontal * dz
if (y < world.minHeight || y + 4 >= world.maxHeight) continue
val inside = mutableListOf<Block>()
var valid = true
for (h in 0..3) for (v in 0..4) {
val candidate = world.getBlockAt(x + h * dx, y + v, z + h * dz)
if (h == 0 || h == 3 || v == 0 || v == 4) {
if (candidate.type != Material.SCULK) valid = false
} else {
if (if (lit) candidate.type != Material.PURPLE_STAINED_GLASS else !candidate.type.isAir) valid = false
inside.add(candidate)
}
}
if (valid) return inside
}
}
return null
}
private fun spawnBoss() {
if (dimension.entities.any { it.scoreboardTags.contains("dabot_boss") }) return
val spawn = dimension.spawnLocation
val x = spawn.blockX + 8
val z = spawn.blockZ + 8
val location = Location(dimension, x + 0.5, dimension.getHighestBlockYAt(x, z) + 1.0, z + 0.5)
val boss = dimension.spawnEntity(location, EntityType.WARDEN) as LivingEntity
boss.customName = "${ChatColor.DARK_AQUA}${ChatColor.BOLD}Dabot"
boss.isCustomNameVisible = true
boss.isPersistent = true
boss.removeWhenFarAway = false
boss.addScoreboardTag("dabot_boss")
boss.getAttribute(Attribute.MAX_HEALTH)?.baseValue = 500.0
boss.health = boss.getAttribute(Attribute.MAX_HEALTH)?.value ?: boss.health
}
@EventHandler
fun onBossDeath(event: EntityDeathEvent) {
if (!event.entity.scoreboardTags.contains("dabot_boss")) return
event.entity.world.players.forEach { it.sendMessage("${ChatColor.AQUA}Dabot has been defeated!") }
event.drops.add(ItemStack(Material.ECHO_SHARD, 4))
}
override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array<out String>): Boolean {
if (sender !is Player) {
sender.sendMessage("Only players can use this command.")
return true
}
if (args.size != 1 || !args[0].equals("leave", true)) return false
if (sender.world != dimension) {
sender.sendMessage("${ChatColor.RED}You are not in the Dabot Dimension.")
return true
}
leave(sender)
return true
}
private fun leave(player: Player) {
val destination = returns[player.uniqueId] ?: loadReturn(player.uniqueId) ?: server.worlds.first().spawnLocation
if (player.teleport(destination)) {
returns.remove(player.uniqueId)
config.set("returns.${player.uniqueId}", null)
saveConfig()
player.sendMessage("${ChatColor.AQUA}You left the Dabot Dimension.")
}
}
private fun saveReturn(id: UUID, location: Location) {
config.set("returns.$id", location)
saveConfig()
}
private fun loadReturn(id: UUID): Location? = config.getLocation("returns.$id")
@EventHandler
fun onQuit(event: PlayerQuitEvent) {
returns.remove(event.player.uniqueId)
cooldowns.remove(event.player.uniqueId)
}
}