Files
src
build.gradle.kts
manifest.kod
settings.gradle.kts
Code
5 223
src/main/kotlin/com/kodari/scythe/ScythePlugin.kt
package com.kodari.scythe
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.format.NamedTextColor
import net.kyori.adventure.text.format.TextDecoration
import org.bukkit.Material
import org.bukkit.NamespacedKey
import org.bukkit.Particle
import org.bukkit.SoundCategory
import org.bukkit.attribute.Attribute
import org.bukkit.attribute.AttributeModifier
import org.bukkit.enchantments.Enchantment
import org.bukkit.entity.Player
import org.bukkit.event.EventHandler
import org.bukkit.event.EventPriority
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.ItemStack
import org.bukkit.inventory.meta.ItemMeta
import org.bukkit.inventory.ShapedRecipe
import org.bukkit.persistence.PersistentDataType
import org.bukkit.potion.PotionEffect
import org.bukkit.potion.PotionEffectType
import org.bukkit.scheduler.BukkitTask
import org.bukkit.util.Vector
import org.bukkit.plugin.java.JavaPlugin
import kotlin.math.max
class ScythePlugin : JavaPlugin(), Listener {
private lateinit var scytheKey: NamespacedKey
private lateinit var recipeKey: NamespacedKey
private val managedEffects = mutableMapOf<java.util.UUID, MutableSet<PotionEffectType>>()
private val dashCooldowns = mutableMapOf<java.util.UUID, Long>()
private var effectsTask: BukkitTask? = null
override fun onEnable() {
scytheKey = NamespacedKey(this, "scythe")
recipeKey = NamespacedKey(this, "scythe_recipe")
server.pluginManager.registerEvents(this, this)
registerScytheRecipe()
effectsTask = server.scheduler.runTaskTimer(this, Runnable(::updateEffects), 0L, 10L)
}
override fun onDisable() {
effectsTask?.cancel()
managedEffects.keys.toList().forEach { uuid ->
server.getPlayer(uuid)?.let(::removeManagedEffects)
}
managedEffects.clear()
dashCooldowns.clear()
}
@EventHandler(ignoreCancelled = true)
fun onScytheUse(event: PlayerInteractEvent) {
if (event.action != Action.RIGHT_CLICK_AIR && event.action != Action.RIGHT_CLICK_BLOCK) return
if (!isScythe(event.item)) return
event.isCancelled = true
val player = event.player
val now = System.currentTimeMillis()
if (now < (dashCooldowns[player.uniqueId] ?: 0L)) return
dashCooldowns[player.uniqueId] = now + 2000L
val direction = player.location.direction.normalize()
direction.y = direction.y.coerceIn(-0.2, 0.6)
player.velocity = direction.multiply(1.8)
player.world.spawnParticle(Particle.CLOUD, player.location, 12, 0.25, 0.15, 0.25, 0.02)
player.world.playSound(player.location, "minecraft:entity.player.attack.sweep", SoundCategory.PLAYERS, 1f, 1.2f)
}
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
fun onScytheAttack(event: EntityDamageByEntityEvent) {
val player = event.damager as? Player ?: return
if (!isScythe(player.inventory.itemInMainHand)) return
if (player.isFlying || player.isGliding || player.isInsideVehicle) return
if (player.fallDistance <= 1.5f) return
val fallDistance = player.fallDistance.toDouble()
event.damage += max(0.0, fallDistance * 2.5).coerceAtMost(40.0)
player.fallDistance = 0f
event.entity.world.spawnParticle(
Particle.CRIT,
event.entity.location.add(Vector(0.0, 1.0, 0.0)),
20,
0.3,
0.3,
0.3,
0.05
)
player.world.playSound(
player.location,
"minecraft:item.mace.smash_ground",
SoundCategory.PLAYERS,
1f,
1f
)
}
private fun updateEffects() {
server.onlinePlayers.forEach { player ->
if (isHoldingScythe(player)) {
applyManagedEffects(player)
} else {
removeManagedEffects(player)
}
}
}
private fun applyManagedEffects(player: Player) {
val effects = managedEffects.getOrPut(player.uniqueId) { mutableSetOf() }
val desiredEffects = listOf(
PotionEffectType.STRENGTH,
PotionEffectType.SPEED,
PotionEffectType.INVISIBILITY
)
desiredEffects.forEach { type ->
val current = player.getPotionEffect(type)
if (current == null || (type in effects && current.amplifier == 0 && current.duration < 20)) {
player.addPotionEffect(PotionEffect(type, 30, 0, false, false, true), true)
effects += type
}
}
}
private fun removeManagedEffects(player: Player) {
managedEffects.remove(player.uniqueId)?.forEach(player::removePotionEffect)
}
private fun isHoldingScythe(player: Player): Boolean =
isScythe(player.inventory.itemInMainHand) || isScythe(player.inventory.itemInOffHand)
private fun isScythe(item: ItemStack?): Boolean =
item != null && item.type == Material.NETHERITE_HOE &&
item.itemMeta?.persistentDataContainer?.has(scytheKey, PersistentDataType.BYTE) == true
private fun registerScytheRecipe() {
val recipe = ShapedRecipe(recipeKey, createScythe())
recipe.shape("NN ", " SN", " S")
recipe.setIngredient('N', Material.NETHERITE_INGOT)
recipe.setIngredient('S', Material.STICK)
server.addRecipe(recipe)
}
private fun createScythe(): ItemStack {
val item = ItemStack(Material.NETHERITE_HOE)
val meta: ItemMeta = item.itemMeta
meta.displayName(
Component.text("Scythe", NamedTextColor.DARK_PURPLE)
.decoration(TextDecoration.ITALIC, false)
)
meta.lore(
listOf(
Component.text("A weapon for striking from above.", NamedTextColor.GRAY),
Component.text("Strength, Speed, and Invisibility", NamedTextColor.LIGHT_PURPLE)
)
)
meta.isUnbreakable = true
meta.addEnchant(Enchantment.UNBREAKING, 3, true)
meta.addAttributeModifier(
Attribute.ATTACK_DAMAGE,
AttributeModifier(
NamespacedKey(this, "scythe_attack_damage"),
7.0,
AttributeModifier.Operation.ADD_NUMBER,
org.bukkit.inventory.EquipmentSlotGroup.MAINHAND
)
)
meta.persistentDataContainer.set(scytheKey, PersistentDataType.BYTE, 1)
item.itemMeta = meta
return item
}
}