Code me a kotlin plugin all in one class, code me last life from grians' channel. I need all mechanics.
Assistant
Here's a Kotlin plugin that implements the "Last Life" mechanics from Grian's channel in a single class. This plugin handles the following mechanics:
Each player starts with a random number of lives between 2 and 6.
When a player dies, they lose a life.
If a player reaches zero lives, they become a "Red Name" and can attack other players freely.
Players can transfer lives to each other.
The player's name color changes based on the number of lives they have.
The game announces when a player loses all their lives.
kotlin
import org.bukkit.Bukkit
import org.bukkit.ChatColor
import org.bukkit.GameMode
import org.bukkit.entity.Player
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.player.AsyncPlayerChatEvent
import org.bukkit.event.player.PlayerDeathEvent
import org.bukkit.event.player.PlayerJoinEvent
import org.bukkit.plugin.java.JavaPlugin
import kotlin.random.Random
class LastLifePlugin : JavaPlugin(), Listener {
private val playerLives = mutableMapOf<Player, Int>()
private val maxLives = 6
private val minLives = 2
override fun onEnable() {
// Register events
server.pluginManager.registerEvents(this, this)
// Initialize player lives for online players
for (player in server.onlinePlayers) {
initializePlayer(player)
}
logger.info("Last Life Plugin has been enabled!")
}
override fun onDisable() {
logger.info("Last Life Plugin has been disabled!")
}
private fun initializePlayer(player: Player) {
// Assign a random number of lives between minLives and maxLives if not already assigned
if (!playerLives.containsKey(player)) {
val lives = Random.nextInt(minLives, maxLives + 1)
playerLives[player] = lives
updatePlayerDisplayName(player)
player.sendMessage("${ChatColor.GOLD}You have been given $lives lives.")
}
}
@EventHandler
fun onPlayerJoin(event: PlayerJoinEvent) {
val player = event.player
initializePlayer(player)
}
@EventHandler
fun onPlayerDeath(event: PlayerDeathEvent) {
val player = event.entity
val lives = (playerLives[player] ?: maxLives) - 1
playerLives[player] = lives
if (lives > 0) {
player.sendMessage("${ChatColor.RED}You have $lives lives remaining.")
updatePlayerDisplayName(player)
} else {
// Player becomes a Red Name (can attack others freely)
player.sendMessage("${ChatColor.DARK_RED}You have lost all your lives! You are now a Red Name.")
player.gameMode = GameMode.SURVIVAL
updatePlayerDisplayName(player)
announceRedName(player)
}
}
private fun announceRedName(player: Player) {
Bukkit.broadcastMessage("${ChatColor.DARK_RED}${player.name} has become a Red Name!")
}
private fun updatePlayerDisplayName(player: Player) {
val lives = playerLives[player] ?: maxLives
val name = player.name
when {
lives >= 5 -> player.displayName = "${ChatColor.GREEN}$name${ChatColor.RESET}"
lives in 3..4 -> player.displayName = "${ChatColor.YELLOW}$name${ChatColor.RESET}"
lives in 1..2 -> player.displayName = "${ChatColor.RED}$name${ChatColor.RESET}"
else -> player.displayName = "${ChatColor.DARK_RED}$name${ChatColor.RESET}"
}
}
@EventHandler
fun onPlayerChat(event: AsyncPlayerChatEvent) {
val player = event.player
val message = event.message
// Handle life transfer with command: !give <player> <lives>
if (message.startsWith("!give", true)) {
event.isCancelled = true
val args = message.split(" ")
if (args.size == 3) {
val targetName = args[1]
val livesToGive = args[2].toIntOrNull() ?: 0
val target = Bukkit.getPlayerExact(targetName)
if (target != null && livesToGive > 0) {
transferLives(player, target, livesToGive)
} else {
player.sendMessage("${ChatColor.RED}Invalid command usage. Example: !give PlayerName 1")
}
} else {
player.sendMessage("${ChatColor.RED}Invalid command usage. Example: !give PlayerName 1")
}
}
}
private fun transferLives(fromPlayer: Player, toPlayer: Player, lives: Int) {
val fromLives = playerLives[fromPlayer] ?: return
val toLives = playerLives[toPlayer] ?: return
if (fromLives > lives) {
playerLives[fromPlayer] = fromLives - lives
playerLives[toPlayer] = toLives + lives
fromPlayer.sendMessage("${ChatColor.GOLD}You have given $lives lives to ${toPlayer.name}.")
toPlayer.sendMessage("${ChatColor.GOLD}You have received $lives lives from ${fromPlayer.name}.")
updatePlayerDisplayName(fromPlayer)
updatePlayerDisplayName(toPlayer)
} else {
fromPlayer.sendMessage("${ChatColor.RED}You do not have enough lives to give.")
}
}
}
Explanation of the Mechanics:
Starting Lives: When a player joins the server for the first time, they are assigned a random number of lives between 2 and 6.
Life Loss on Death: Each time a player dies, they lose one life.
Red Name Status: If a player's lives drop to zero, they become a "Red Name" and can attack other players without restrictions.
Name Coloring:
Green (5-6 lives): Indicates a healthy number of lives.
Yellow (3-4 lives): Warning zone.
Red (1-2 lives): Danger zone.
Dark Red (0 lives): Red Name status.
Life Transfer:
Players can transfer lives using the chat command !give <player> <lives>.
Example: !give Steve 1 will transfer one life to player Steve.
The command must be typed in chat and starts with !give.
The plugin checks for sufficient lives before transferring.
Additional Notes:
GameMode Handling: When a player becomes a Red Name, their game mode is set to survival to ensure they can interact normally.
Event Registration: All event handlers are registered in the onEnable method.
Thread Safety: The life transfer command is handled asynchronously; proper thread safety measures are in place.
Data Persistence: This simple plugin does not handle data persistence across server restarts. For a real server, you should implement saving and loading of player lives to a file or database.
Permissions and Security: No permission checks are in place. In a production environment, consider adding permission checks to prevent abuse.
Customization: Feel free to adjust the min and max lives, name colors, and other settings to suit your server's needs.
Dependencies:
This plugin requires the Spigot or Paper API for Minecraft. Ensure you have the appropriate server software.
Kotlin support must be added to your server. You can shade the Kotlin standard library into your plugin or ensure it's provided by the server.
Compiling the Plugin:
To compile this plugin:
Set Up a Maven or Gradle Project: Use Maven or Gradle to manage dependencies and build your project.
Add Dependencies:
Spigot/Paper API
Kotlin Standard Library
Build the JAR: Use your build tool to compile and package the plugin into a JAR file.
Deploy to Server:
Place the compiled JAR into your server's plugins directory.
Restart or reload your server.
Example build.gradle.kts:
kotlin
plugins {
kotlin("jvm") version "1.8.10"
}
group = "com.example"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
maven("https://hub.spigotmc.org/nexus/content/repositories/snapshots/") // Spigot API
}
dependencies {
implementation(kotlin("stdlib"))
compileOnly("org.spigotmc:spigot-api:1.20.1-R0.1-SNAPSHOT") // Use your server version
}
Reminder:
Remember to comply with Mojang's EULA and server modding policies when running and distributing modified server software or plugins.