Files
src
build.gradle.kts
manifest.kod
Code
4 193
src/main/kotlin/com/kodari/floatie/FloatiePlugin.kt
package com.kodari.floatie
import org.bukkit.Bukkit
import org.bukkit.Material
import org.bukkit.block.Block
import org.bukkit.block.data.BlockData
import org.bukkit.entity.ArmorStand
import org.bukkit.entity.BlockDisplay
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.EquipmentSlot
import org.bukkit.inventory.ItemStack
import org.bukkit.persistence.PersistentDataType
import org.bukkit.plugin.java.JavaPlugin
import org.bukkit.util.Vector
import org.bukkit.NamespacedKey
class FloatiePlugin : JavaPlugin(), Listener {
private lateinit var floatieKey: NamespacedKey
private lateinit var steeringWheelKey: NamespacedKey
private lateinit var pilotKey: NamespacedKey
private val activeCrafts = mutableMapOf<java.util.UUID, ActiveCraft>()
override fun onEnable() {
floatieKey = NamespacedKey(this, "floatie_block")
steeringWheelKey = NamespacedKey(this, "steering_wheel")
pilotKey = NamespacedKey(this, "floatie_pilot")
server.pluginManager.registerEvents(this, this)
getCommand("floatie")?.setExecutor { sender, _, _, _ ->
val player = sender as? org.bukkit.entity.Player
if (player == null) {
sender.sendMessage("Only players can use this command.")
return@setExecutor true
}
player.inventory.addItem(createFloatieBlock(), createSteeringWheel())
player.sendMessage("§aYou received a Floatie Block and a Steering Wheel.")
true
}
}
override fun onDisable() {
activeCrafts.values.toList().forEach(::restoreCraft)
activeCrafts.clear()
}
@EventHandler
fun onPlayerInteract(event: PlayerInteractEvent) {
if (event.hand != EquipmentSlot.HAND) return
if (event.action != Action.RIGHT_CLICK_AIR && event.action != Action.RIGHT_CLICK_BLOCK) return
if (!isSteeringWheel(event.item)) return
event.isCancelled = true
val player = event.player
val currentPilot = player.vehicle as? ArmorStand
if (currentPilot?.persistentDataContainer?.get(pilotKey, PersistentDataType.BYTE) == 1.toByte()) {
moveCraft(currentPilot, player)
return
}
val floatieBlock = findFloatieBlock(player, event.clickedBlock) ?: run {
player.sendMessage("§eStand on an ordinary block with a Floatie Block directly underneath it, then use the wheel.")
return
}
boardCraft(floatieBlock, player)
}
private fun findFloatieBlock(player: org.bukkit.entity.Player, clickedBlock: Block?): Block? {
val blockBelowPlayer = player.location.block.getRelative(0, -1, 0)
val floatieBelowPlayer = blockBelowPlayer.getRelative(0, -1, 0)
if (blockBelowPlayer.type != Material.AIR && isFloatieBlock(floatieBelowPlayer)) {
return floatieBelowPlayer
}
if (clickedBlock != null && isFloatieBlock(clickedBlock) && clickedBlock.getRelative(0, 1, 0).type != Material.AIR) {
return clickedBlock
}
return null
}
private fun boardCraft(floatieBlock: Block, player: org.bukkit.entity.Player) {
val upperBlock = floatieBlock.getRelative(0, 1, 0)
if (upperBlock.type == Material.AIR || !floatieBlock.world.getNearbyEntities(floatieBlock.location, 0.9, 1.9, 0.9).isEmpty()) {
player.sendMessage("§eMake sure the ordinary block is directly above the Floatie Block and clear of other entities.")
return
}
val floatieData = floatieBlock.blockData.clone()
val upperData = upperBlock.blockData.clone()
val world = floatieBlock.world
val baseLocation = floatieBlock.location
val floatieDisplay = world.spawn(baseLocation, BlockDisplay::class.java)
floatieDisplay.block = floatieData
val upperDisplay = world.spawn(baseLocation.clone().add(0.0, 1.0, 0.0), BlockDisplay::class.java)
upperDisplay.block = upperData
val pilot = world.spawn(baseLocation.clone().add(0.5, 0.0, 0.5), ArmorStand::class.java)
pilot.isInvisible = true
pilot.isMarker = true
pilot.setGravity(false)
pilot.isInvulnerable = true
pilot.persistentDataContainer.set(pilotKey, PersistentDataType.BYTE, 1)
floatieBlock.setType(Material.AIR, false)
upperBlock.setType(Material.AIR, false)
activeCrafts[pilot.uniqueId] = ActiveCraft(pilot, floatieDisplay, upperDisplay, floatieData, upperData)
pilot.addPassenger(player)
player.sendMessage("§aYou're piloting the floating block. Use the wheel again to move where you're looking.")
}
private fun moveCraft(pilot: ArmorStand, player: org.bukkit.entity.Player) {
val craft = activeCrafts[pilot.uniqueId]
if (craft == null) {
player.leaveVehicle()
return
}
val direction = player.location.direction.setY(0.0).normalize()
if (direction.lengthSquared() == 0.0) return
val movement = direction.multiply(0.75)
craft.pilot.teleport(craft.pilot.location.add(movement))
craft.floatieDisplay.teleport(craft.floatieDisplay.location.add(movement))
craft.upperDisplay.teleport(craft.upperDisplay.location.add(movement))
}
private fun restoreCraft(craft: ActiveCraft) {
val base = craft.floatieDisplay.location.block
val upper = base.getRelative(0, 1, 0)
if (base.type == Material.AIR) base.setBlockData(craft.floatieData, false)
if (upper.type == Material.AIR) upper.setBlockData(craft.upperData, false)
craft.pilot.remove()
craft.floatieDisplay.remove()
craft.upperDisplay.remove()
}
private fun createFloatieBlock(): ItemStack = ItemStack(Material.LIME_CONCRETE).apply {
itemMeta = itemMeta?.apply {
setDisplayName("§bFloatie Block")
lore = listOf("§7Place under an ordinary block to make it float.")
persistentDataContainer.set(floatieKey, PersistentDataType.BYTE, 1)
}
}
private fun createSteeringWheel(): ItemStack = ItemStack(Material.WARPED_FUNGUS_ON_A_STICK).apply {
itemMeta = itemMeta?.apply {
setDisplayName("§6Steering Wheel")
lore = listOf("§7Use while standing on a Floatie Block to board.", "§7Use again to move in the direction you're looking.")
persistentDataContainer.set(steeringWheelKey, PersistentDataType.BYTE, 1)
}
}
private fun isSteeringWheel(item: ItemStack?): Boolean =
item?.itemMeta?.persistentDataContainer?.get(steeringWheelKey, PersistentDataType.BYTE) == 1.toByte()
private fun isFloatieBlock(block: Block): Boolean = block.type == Material.LIME_CONCRETE
private data class ActiveCraft(
val pilot: ArmorStand,
val floatieDisplay: BlockDisplay,
val upperDisplay: BlockDisplay,
val floatieData: BlockData,
val upperData: BlockData
)
}