// Main plugin package declaration
package com.example.customweapons
// Import necessary Bukkit and Spigot APIs
import org.bukkit.Bukkit
import org.bukkit.Material
import org.bukkit.NamespacedKey
import org.bukkit.Particle
import org.bukkit.attribute.Attribute
import org.bukkit.command.Command
import org.bukkit.command.CommandSender
import org.bukkit.enchantments.Enchantment
import org.bukkit.entity.Entity
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.EntityDamageByEntityEvent
import org.bukkit.event.player.PlayerInteractEvent
import org.bukkit.inventory.ItemFlag
import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.ShapedRecipe
import org.bukkit.inventory.meta.ItemMeta
import org.bukkit.plugin.java.JavaPlugin
import org.bukkit.potion.PotionEffect
import org.bukkit.potion.PotionEffectType
// Main plugin class
class CustomWeaponsPlugin : JavaPlugin(), Listener {
// Declare custom weapons
lateinit var flameSword: ItemStack
lateinit var frostAxe: ItemStack
override fun onEnable() {
// Plugin startup logic
// Initialize custom weapons
flameSword = createFlameSword()
frostAxe = createFrostAxe()
// Register crafting recipes
registerRecipes()
// Register event listeners
server.pluginManager.registerEvents(this, this)
}
override fun onDisable() {
// Plugin shutdown logic
}
// Create the Flame Sword item
private fun createFlameSword(): ItemStack {
val item = ItemStack(Material.DIAMOND_SWORD)
val meta = item.itemMeta
meta?.setDisplayName("§cFlame Sword") // Set item name with color
meta?.lore = listOf("§6A sword imbued with fire") // Add item lore
meta?.addEnchant(Enchantment.FIRE_ASPECT, 2, true) // Add enchantment
meta?.addItemFlags(ItemFlag.HIDE_ENCHANTS) // Hide enchantments from lore
item.itemMeta = meta
return item
}
// Create the Frost Axe item
private fun createFrostAxe(): ItemStack {
val item = ItemStack(Material.DIAMOND_AXE)
val meta = item.itemMeta
meta?.setDisplayName("§bFrost Axe") // Set item name with color
meta?.lore = listOf("§3An axe of eternal ice") // Add item lore
meta?.addEnchant(Enchantment.DAMAGE_ALL, 3, true) // Add enchantment
meta?.addItemFlags(ItemFlag.HIDE_ENCHANTS) // Hide enchantments from lore
item.itemMeta = meta
return item
}
// Register custom crafting recipes
private fun registerRecipes() {
// Crafting recipe for Flame Sword
val flameSwordRecipe = ShapedRecipe(NamespacedKey(this, "flame_sword"), flameSword)
flameSwordRecipe.shape(" B ", "BSB", " N ")
flameSwordRecipe.setIngredient('B', Material.BLAZE_POWDER)
flameSwordRecipe.setIngredient('S', Material.DIAMOND_SWORD)
flameSwordRecipe.setIngredient('N', Material.NETHER_STAR)
server.addRecipe(flameSwordRecipe)
// Crafting recipe for Frost Axe
val frostAxeRecipe = ShapedRecipe(NamespacedKey(this, "frost_axe"), frostAxe)
frostAxeRecipe.shape("II ", "IS ", " N ")
frostAxeRecipe.setIngredient('I', Material.ICE)
frostAxeRecipe.setIngredient('S', Material.DIAMOND_AXE)
frostAxeRecipe.setIngredient('N', Material.NETHER_STAR)
server.addRecipe(frostAxeRecipe)
}
// Listen for player interactions to handle custom animations
@EventHandler
fun onPlayerInteract(event: PlayerInteractEvent) {
val player = event.player
val action = event.action
// Check if player is swinging the weapon
if (action == Action.LEFT_CLICK_AIR || action == Action.LEFT_CLICK_BLOCK) {
val item = player.inventory.itemInMainHand
val meta = item.itemMeta
// Check if item is the Flame Sword
if (meta != null && meta.displayName == "§cFlame Sword") {
// Spawn fire particles 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.01)
}
// Check if item is the Frost Axe
if (meta != null && meta.displayName == "§bFrost Axe") {
// Spawn snow particles around the player
player.world.spawnParticle(Particle.SNOWBALL, player.location.add(0.0, 1.0, 0.0), 30, 0.5, 0.5, 0.5, 0.01)
}
}
}
// Listen for entity damage events to apply weapon effects
@EventHandler
fun onEntityDamage(event: EntityDamageByEntityEvent) {
val damager = event.damager
// Check if damager is a player
if (damager is Player) {
val player = damager
val item = player.inventory.itemInMainHand
val meta = item.itemMeta
val entity = event.entity
// Check if item is the Flame Sword
if (meta != null && meta.displayName == "§cFlame Sword") {
// Set the target entity on fire
if (entity is LivingEntity) {
entity.fireTicks = 100 // Fire duration in ticks
}
}
// Check if item is the Frost Axe
if (meta != null && meta.displayName == "§bFrost Axe") {
// Apply slowness effect to the target entity
if (entity is LivingEntity) {
entity.addPotionEffect(PotionEffect(PotionEffectType.SLOW, 100, 1)) // Duration and amplifier
}
}
}
}
// Handle commands
override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array<out String>): Boolean {
// Command to give weapons to player
if (command.name.equals("giveweapon", ignoreCase = true)) {
// Check if sender is a player
if (sender is Player) {
val player = sender
// Check if player is an operator
if (player.isOp) {
if (args.isNotEmpty()) {
when (args[0].lowercase()) {
"flamesword" -> {
player.inventory.addItem(flameSword)
player.sendMessage("§aYou have received a Flame Sword!")
return true
}
"frostaxe" -> {
player.inventory.addItem(frostAxe)
player.sendMessage("§aYou have received a Frost Axe!")
return true
}
else -> {
player.sendMessage("§cInvalid weapon name. Use 'flamesword' or 'frostaxe'.")
return true
}
}
} else {
player.sendMessage("§cUsage: /giveweapon <weaponname>")
return true
}
} else {
player.sendMessage("§cYou do not have permission to use this command.")
return true
}
} else {
sender.sendMessage("This command can only be used by players.")
return true
}
}
return false
}
}
Explanation and Comments:
Additional Notes:
This plugin introduces two powerful custom weapons with special effects and challenging crafting recipes, enhancing the gameplay experience. By following best practices and including comprehensive comments, the code should be maintainable and understandable for future development or customization.