Here is the complete code for the Minecraft plugin written in Kotlin. This plugin introduces two custom weapons, the 'Flame Sword' and the 'Frost Axe', each with unique effects and animations when used.
package com.example.customweapons
import org.bukkit.Bukkit
import org.bukkit.ChatColor
import org.bukkit.Material
import org.bukkit.NamespacedKey
import org.bukkit.Particle
import org.bukkit.enchantments.Enchantment
import org.bukkit.entity.Entity
import org.bukkit.entity.LivingEntity
import org.bukkit.entity.Player
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.player.PlayerInteractEvent
import org.bukkit.event.entity.EntityDamageByEntityEvent
import org.bukkit.event.entity.EntityDamageEvent.DamageCause
import org.bukkit.inventory.ItemFlag
import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.meta.ItemMeta
import org.bukkit.plugin.java.JavaPlugin
import org.bukkit.potion.PotionEffect
import org.bukkit.potion.PotionEffectType
class CustomWeaponsPlugin : JavaPlugin(), Listener {
// NamespacedKeys for identifying custom weapons
private val flameSwordKey = NamespacedKey(this, "flame_sword")
private val frostAxeKey = NamespacedKey(this, "frost_axe")
override fun onEnable() {
// Plugin startup logic
// Registering the event listeners
server.pluginManager.registerEvents(this, this)
// Giving the custom weapons to all online players (for testing purposes)
for (player in server.onlinePlayers) {
player.inventory.addItem(createFlameSword())
player.inventory.addItem(createFrostAxe())
}
logger.info("CustomWeaponsPlugin has been enabled!")
}
override fun onDisable() {
// Plugin shutdown logic
logger.info("CustomWeaponsPlugin has been disabled.")
}
/**
* Creates the custom Flame Sword item with unique meta data.
*/
private fun createFlameSword(): ItemStack {
val item = ItemStack(Material.DIAMOND_SWORD)
val meta = item.itemMeta
meta?.let {
// Setting the display name and lore
it.setDisplayName("${ChatColor.RED}Flame Sword")
it.lore = listOf("${ChatColor.GRAY}An ancient sword imbued with fire.")
// Adding a custom enchantment effect (optional)
it.addEnchant(Enchantment.FIRE_ASPECT, 2, true)
// Hiding enchantment info
it.addItemFlags(ItemFlag.HIDE_ENCHANTS)
// Setting custom persistent data to identify the item
val flameSwordKey = NamespacedKey(this, "flame_sword")
it.persistentDataContainer.set(flameSwordKey, org.bukkit.persistence.PersistentDataType.BYTE, 1.toByte())
item.itemMeta = it
}
return item
}
/**
* Creates the custom Frost Axe item with unique meta data.
*/
private fun createFrostAxe(): ItemStack {
val item = ItemStack(Material.DIAMOND_AXE)
val meta = item.itemMeta
meta?.let {
// Setting the display name and lore
it.setDisplayName("${ChatColor.AQUA}Frost Axe")
it.lore = listOf("${ChatColor.GRAY}An icy axe that chills enemies.")
// Adding a custom enchantment effect (optional)
it.addEnchant(Enchantment.DAMAGE_ALL, 3, true)
// Hiding enchantment info
it.addItemFlags(ItemFlag.HIDE_ENCHANTS)
// Setting custom persistent data to identify the item
val frostAxeKey = NamespacedKey(this, "frost_axe")
it.persistentDataContainer.set(frostAxeKey, org.bukkit.persistence.PersistentDataType.BYTE, 1.toByte())
item.itemMeta = it
}
return item
}
/**
* Event handler for player interactions (e.g., swinging a weapon).
*/
@EventHandler
fun onPlayerInteract(event: PlayerInteractEvent) {
val player = event.player
val item = event.item ?: return
val itemMeta = item.itemMeta ?: return
// Check if the item is the Flame Sword
if (itemMeta.persistentDataContainer.has(flameSwordKey, org.bukkit.persistence.PersistentDataType.BYTE)) {
// Display fire particle effect around the player
player.world.spawnParticle(Particle.FLAME, player.location.add(0.0, 1.0, 0.0), 30, 0.5, 0.5, 0.5, 0.0)
}
// Check if the item is the Frost Axe
if (itemMeta.persistentDataContainer.has(frostAxeKey, org.bukkit.persistence.PersistentDataType.BYTE)) {
// Display frost particle effect around the player
player.world.spawnParticle(Particle.SNOWFLAKE, player.location.add(0.0, 1.0, 0.0), 30, 0.5, 0.5, 0.5, 0.0)
}
}
/**
* Event handler for when an entity is damaged by another entity.
*/
@EventHandler
fun onEntityDamageByEntity(event: EntityDamageByEntityEvent) {
// Ensure the damager is a player
val damager = event.damager as? Player ?: return
val damaged = event.entity as? LivingEntity ?: return
val item = damager.inventory.itemInMainHand
val itemMeta = item.itemMeta ?: return
// Check if the item is the Flame Sword
if (itemMeta.persistentDataContainer.has(flameSwordKey, org.bukkit.persistence.PersistentDataType.BYTE)) {
// Set the damaged entity on fire
damaged.fireTicks = 100 // Sets the entity on fire for 5 seconds (20 ticks = 1 second)
// Additional flame effects
damaged.world.spawnParticle(Particle.FLAME, damaged.location.add(0.0, 1.0, 0.0), 20, 0.3, 0.5, 0.3, 0.01)
damaged.world.playSound(damaged.location, "entity.blaze.hit", 1f, 1f)
}
// Check if the item is the Frost Axe
if (itemMeta.persistentDataContainer.has(frostAxeKey, org.bukkit.persistence.PersistentDataType.BYTE)) {
// Apply slowness effect to the damaged entity
damaged.addPotionEffect(PotionEffect(PotionEffectType.SLOW, 100, 1)) // Slowness II for 5 seconds
// Additional frost effects
damaged.world.spawnParticle(Particle.SNOWFLAKE, damaged.location.add(0.0, 1.0, 0.0), 20, 0.3, 0.5, 0.3, 0.01)
damaged.world.playSound(damaged.location, "block.glass.break", 1f, 1f)
}
}
}
Explanation of the Code
Package and Imports
package com.example.customweapons
import org.bukkit.Bukkit
import org.bukkit.ChatColor
import org.bukkit.Material
import org.bukkit.NamespacedKey
import org.bukkit.Particle
import org.bukkit.enchantments.Enchantment
import org.bukkit.entity.Entity
import org.bukkit.entity.LivingEntity
import org.bukkit.entity.Player
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.player.PlayerInteractEvent
import org.bukkit.event.entity.EntityDamageByEntityEvent
import org.bukkit.event.entity.EntityDamageEvent.DamageCause
import org.bukkit.inventory.ItemFlag
import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.meta.ItemMeta
import org.bukkit.plugin.java.JavaPlugin
import org.bukkit.potion.PotionEffect
import org.bukkit.potion.PotionEffectType
- We import all the necessary classes from the Bukkit/Spigot API to handle events, create items, and apply effects.
Main Plugin Class
class CustomWeaponsPlugin : JavaPlugin(), Listener {
// ...
}
- The main plugin class
CustomWeaponsPlugin extends JavaPlugin and implements Listener to handle events.
Variables
// NamespacedKeys for identifying custom weapons
private val flameSwordKey = NamespacedKey(this, "flame_sword")
private val frostAxeKey = NamespacedKey(this, "frost_axe")
NamespacedKey is used to store custom data in item metadata, allowing us to identify custom items.
Plugin Lifecycle Methods
override fun onEnable() {
// Plugin startup logic
// Registering the event listeners
server.pluginManager.registerEvents(this, this)
// Giving the custom weapons to all online players (for testing purposes)
for (player in server.onlinePlayers) {
player.inventory.addItem(createFlameSword())
player.inventory.addItem(createFrostAxe())
}
logger.info("CustomWeaponsPlugin has been enabled!")
}
override fun onDisable() {
// Plugin shutdown logic
logger.info("CustomWeaponsPlugin has been disabled.")
}
onEnable() is called when the plugin is started. We register the event listeners and provide the custom weapons to online players for testing.
onDisable() is called when the plugin is stopped.
Creating Custom Weapons
Flame Sword
private fun createFlameSword(): ItemStack {
val item = ItemStack(Material.DIAMOND_SWORD)
val meta = item.itemMeta
meta?.let {
// Setting the display name and lore
it.setDisplayName("${ChatColor.RED}Flame Sword")
it.lore = listOf("${ChatColor.GRAY}An ancient sword imbued with fire.")
// Adding a custom enchantment effect (optional)
it.addEnchant(Enchantment.FIRE_ASPECT, 2, true)
// Hiding enchantment info
it.addItemFlags(ItemFlag.HIDE_ENCHANTS)
// Setting custom persistent data to identify the item
val flameSwordKey = NamespacedKey(this, "flame_sword")
it.persistentDataContainer.set(flameSwordKey, org.bukkit.persistence.PersistentDataType.BYTE, 1.toByte())
item.itemMeta = it
}
return item
}
- We create an
ItemStack of type DIAMOND_SWORD.
- We set custom metadata, including a display name, lore, enchantments, and a custom
NamespacedKey in the persistentDataContainer.
Frost Axe
private fun createFrostAxe(): ItemStack {
val item = ItemStack(Material.DIAMOND_AXE)
val meta = item.itemMeta
meta?.let {
// Setting the display name and lore
it.setDisplayName("${ChatColor.AQUA}Frost Axe")
it.lore = listOf("${ChatColor.GRAY}An icy axe that chills enemies.")
// Adding a custom enchantment effect (optional)
it.addEnchant(Enchantment.DAMAGE_ALL, 3, true)
// Hiding enchantment info
it.addItemFlags(ItemFlag.HIDE_ENCHANTS)
// Setting custom persistent data to identify the item
val frostAxeKey = NamespacedKey(this, "frost_axe")
it.persistentDataContainer.set(frostAxeKey, org.bukkit.persistence.PersistentDataType.BYTE, 1.toByte())
item.itemMeta = it
}
return item
}
- Similar to the Flame Sword, but we create a
DIAMOND_AXE with different metadata.
Event Listeners
Player Interaction Event
@EventHandler
fun onPlayerInteract(event: PlayerInteractEvent) {
val player = event.player
val item = event.item ?: return
val itemMeta = item.itemMeta ?: return
// Check if the item is the Flame Sword
if (itemMeta.persistentDataContainer.has(flameSwordKey, org.bukkit.persistence.PersistentDataType.BYTE)) {
// Display fire particle effect around the player
player.world.spawnParticle(Particle.FLAME, player.location.add(0.0, 1.0, 0.0), 30, 0.5, 0.5, 0.5, 0.0)
}
// Check if the item is the Frost Axe
if (itemMeta.persistentDataContainer.has(frostAxeKey, org.bukkit.persistence.PersistentDataType.BYTE)) {
// Display frost particle effect around the player
player.world.spawnParticle(Particle.SNOWFLAKE, player.location.add(0.0, 1.0, 0.0), 30, 0.5, 0.5, 0.5, 0.0)
}
}
- When a player interacts (e.g., swings a weapon), we check if the item is one of our custom weapons.
- We use the
persistentDataContainer to identify the item.
- We spawn appropriate particle effects at the player's location.
Entity Damage Event
@EventHandler
fun onEntityDamageByEntity(event: EntityDamageByEntityEvent) {
// Ensure the damager is a player
val damager = event.damager as? Player ?: return
val damaged = event.entity as? LivingEntity ?: return
val item = damager.inventory.itemInMainHand
val itemMeta = item.itemMeta ?: return
// Check if the item is the Flame Sword
if (itemMeta.persistentDataContainer.has(flameSwordKey, org.bukkit.persistence.PersistentDataType.BYTE)) {
// Set the damaged entity on fire
damaged.fireTicks = 100 // Sets the entity on fire for 5 seconds (20 ticks = 1 second)
// Additional flame effects
damaged.world.spawnParticle(Particle.FLAME, damaged.location.add(0.0, 1.0, 0.0), 20, 0.3, 0.5, 0.3, 0.01)
damaged.world.playSound(damaged.location, "entity.blaze.hit", 1f, 1f)
}
// Check if the item is the Frost Axe
if (itemMeta.persistentDataContainer.has(frostAxeKey, org.bukkit.persistence.PersistentDataType.BYTE)) {
// Apply slowness effect to the damaged entity
damaged.addPotionEffect(PotionEffect(PotionEffectType.SLOW, 100, 1)) // Slowness II for 5 seconds
// Additional frost effects
damaged.world.spawnParticle(Particle.SNOWFLAKE, damaged.location.add(0.0, 1.0, 0.0), 20, 0.3, 0.5, 0.3, 0.01)
damaged.world.playSound(damaged.location, "block.glass.break", 1f, 1f)
}
}
- When an entity is damaged by another entity, we check if the damager is a player holding one of our custom weapons.
- For the Flame Sword, we set the damaged entity on fire and spawn flame particles.
- For the Frost Axe, we apply a slowness effect to the damaged entity and spawn frost particles.
Performance Optimization
- We use the
persistentDataContainer to efficiently store and check custom item data.
- We avoid heavy computations in event handlers.
- Particle effects are kept to a reasonable number to prevent lag.
Compatibility
- The plugin uses the Spigot/Bukkit API and should be compatible with versions up to 1.20.
- The Kotlin code is compatible with Java 17, which is the required Java version for recent Minecraft servers.
Notes
- For actual deployment, you should remove the code that gives items to all online players automatically.
- Consider adding crafting recipes or commands for players to obtain the custom weapons.
- Ensure that your server is running a version compatible with the plugin and that Kotlin is properly set up.