F

If you have a crystals/anchors and the player your against in 100 blocks doesnt have crystals or anchors they will be nerfed otherwise do the normal crystal damage Or if ur a crystal pvper against a Cart pvper if they are using a crossbow the crossbow will be enchanted with flame till they die or go too far out. 1. Maybe like 40% I want it to do damage but not be like using a wooden sword 2. 100 blocks 3. Yes 4. 1.21.11 5. Instead of range you should make it the player we damaged instead that way you could track them so if they left/teleported or ran way remove the crystal nerf/crossbow ench

Kodari AI
KodariOct 9 at 12:02 pm 1
CrystalCombatBalancer
Balances Minecraft PvP combat by tracking player damage events. When a player damages another player, the system checks if the victim has crystals or anchors within a 100-block radius. If the victim lacks these items, crystal damage against them is reduced by 40%. Additionally, if a crystal PvPer fights a crossbow user, the crossbow receives a flame enchantment that persists until the victim dies or moves beyond tracking range. The plugin tracks damaged players to apply or remove nerfs based on their location and equipment status.
  1. Create the Maven/Paper 1.21.11 Kotlin plugin skeleton.

  2. Reduce crystal/anchor damage to 40% only when the damaged player has neither crystals nor anchors.

  3. Track crystal-PvP opponents and temporarily add Flame to a minecart PvPer’s crossbows; remove it on death, quit, teleport, or separation beyond 100 blocks.

Created the Maven Paper 1.21.11 plugin. Crystal and respawn-anchor damage is reduced to 40% when the damaged player has neither crystals nor anchors. When an explosion can be attributed to a crystal PvPer, a minecart-carrying opponent with a crossbow temporarily gets Flame on their crossbows; the enchantment is removed when the tracked fight ends due to death, disconnect, teleport, or separation beyond 100 blocks.

The minecart-PvPer check uses whether the opponent carries a minecart or TNT minecart.

Kodari can make mistakes. Review important code before using.
Files
src
manifest.kod
pom.xml
Code
4 322
src/main/kotlin/com/kodari/crystalbalance/CrystalBalancePlugin.kt
package com.kodari.crystalbalance

import org.bukkit.Bukkit
import org.bukkit.Location
import org.bukkit.Material
import org.bukkit.NamespacedKey
import org.bukkit.enchantments.Enchantment
import org.bukkit.entity.EnderCrystal
import org.bukkit.entity.Item
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.block.BlockExplodeEvent
import org.bukkit.event.entity.EntityDamageByEntityEvent
import org.bukkit.event.entity.EntityDamageEvent
import org.bukkit.event.entity.EntityExplodeEvent
import org.bukkit.event.entity.PlayerDeathEvent
import org.bukkit.event.player.PlayerInteractEvent
import org.bukkit.event.player.PlayerQuitEvent
import org.bukkit.event.player.PlayerTeleportEvent
import org.bukkit.persistence.PersistentDataType
import org.bukkit.plugin.java.JavaPlugin
import java.util.UUID

class CrystalBalancePlugin : JavaPlugin(), Listener {
    private data class CrystalUse(val worldId: UUID, val location: Location, val timestamp: Long)
    private data class ExplosionMark(val location: Location, val attackerId: UUID?)
    private data class FightKey(val crystalPlayerId: UUID, val targetId: UUID)
    private data class ActiveFight(val targetId: UUID, var grantsFlame: Boolean)

    private val crystalUses = mutableMapOf<UUID, CrystalUse>()
    private val explosionMarks = mutableListOf<ExplosionMark>()
    private val fights = mutableMapOf<FightKey, ActiveFight>()
    private lateinit var temporaryFlameKey: NamespacedKey

    override fun onEnable() {
        temporaryFlameKey = NamespacedKey(this, "temporary_flame")
        server.pluginManager.registerEvents(this, this)
        server.scheduler.runTaskTimer(this, Runnable {
            expireCrystalUses()
            removeDistantFights()
        }, 20L, 20L)
    }

    override fun onDisable() {
        fights.clear()
        removeTemporaryFlameFromAllItems()
    }

    @EventHandler(ignoreCancelled = true)
    fun onCrystalUse(event: PlayerInteractEvent) {
        if (event.action != Action.RIGHT_CLICK_BLOCK && event.action != Action.RIGHT_CLICK_AIR) return
        val item = event.item ?: return
        if (item.type != Material.END_CRYSTAL && item.type != Material.RESPAWN_ANCHOR) return

        val location = event.clickedBlock?.location ?: event.player.location
        crystalUses[event.player.uniqueId] = CrystalUse(
            location.world.uid,
            location.clone(),
            System.currentTimeMillis()
        )
    }

    @EventHandler(ignoreCancelled = true)
    fun onCrystalExplosion(event: EntityExplodeEvent) {
        if (event.entity !is EnderCrystal) return
        recordExplosion(event.entity.location)
    }

    @EventHandler(ignoreCancelled = true)
    fun onAnchorExplosion(event: BlockExplodeEvent) {
        if (event.block.type != Material.RESPAWN_ANCHOR) return
        recordExplosion(event.block.location)
    }

    @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
    fun onPlayerDamage(event: EntityDamageEvent) {
        val target = event.entity as? Player ?: return
        val directCrystal = event is EntityDamageByEntityEvent && event.damager is EnderCrystal
        val explosionMark = findExplosionMark(target.location)
        val explosionDamage = event.cause == EntityDamageEvent.DamageCause.BLOCK_EXPLOSION ||
            event.cause == EntityDamageEvent.DamageCause.ENTITY_EXPLOSION
        if (!directCrystal && (!explosionDamage || explosionMark == null)) return

        if (!hasCrystalsOrAnchors(target)) {
            event.damage *= CRYSTAL_DAMAGE_MULTIPLIER
        }

        val attackerId = explosionMark?.attackerId ?: findRecentCrystalUser(target.location)
        val attacker = attackerId?.let(Bukkit::getPlayer) ?: return
        if (attacker.uniqueId == target.uniqueId) return
        trackFight(attacker, target)
    }

    @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
    fun onPlayerDeath(event: PlayerDeathEvent) {
        clearFightsFor(event.entity.uniqueId)
    }

    @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
    fun onPlayerQuit(event: PlayerQuitEvent) {
        clearFightsFor(event.player.uniqueId)
        crystalUses.remove(event.player.uniqueId)
    }

    @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
    fun onPlayerTeleport(event: PlayerTeleportEvent) {
        clearFightsFor(event.player.uniqueId)
    }

    private fun hasCrystalsOrAnchors(player: Player): Boolean = player.inventory.contents.any {
        it?.type == Material.END_CRYSTAL || it?.type == Material.RESPAWN_ANCHOR
    }

    private fun recordExplosion(location: Location) {
        val origin = location.clone()
        val mark = ExplosionMark(origin, findRecentCrystalUser(origin))
        explosionMarks += mark
        server.scheduler.runTaskLater(this, Runnable { explosionMarks.remove(mark) }, 2L)
    }

    private fun findExplosionMark(location: Location): ExplosionMark? = explosionMarks
        .asSequence()
        .filter { it.location.world.uid == location.world.uid }
        .filter { it.location.distanceSquared(location) <= EXPLOSION_MATCH_RADIUS_SQUARED }
        .minByOrNull { it.location.distanceSquared(location) }

    private fun findRecentCrystalUser(location: Location): UUID? {
        val now = System.currentTimeMillis()
        return crystalUses.entries
            .asSequence()
            .filter { now - it.value.timestamp <= CRYSTAL_USE_LIFETIME_MILLIS }
            .filter { it.value.worldId == location.world.uid }
            .filter { it.value.location.distanceSquared(location) <= CRYSTAL_USE_RADIUS_SQUARED }
            .minByOrNull { it.value.location.distanceSquared(location) }
            ?.key
    }

    private fun trackFight(crystalPlayer: Player, target: Player) {
        val key = FightKey(crystalPlayer.uniqueId, target.uniqueId)
        val fight = fights.getOrPut(key) { ActiveFight(target.uniqueId, grantsFlame = false) }
        if (isCartPvPer(target) && hasCrossbow(target)) {
            fight.grantsFlame = true
            applyTemporaryFlame(target)
        }
    }

    private fun isCartPvPer(player: Player): Boolean = player.inventory.contents.any {
        it?.type == Material.TNT_MINECART || it?.type == Material.MINECART
    }

    private fun hasCrossbow(player: Player): Boolean = player.inventory.contents.any {
        it?.type == Material.CROSSBOW
    }

    private fun applyTemporaryFlame(player: Player) {
        val inventory = player.inventory
        for (slot in 0 until inventory.size) {
            val item = inventory.getItem(slot) ?: continue
            if (item.type != Material.CROSSBOW || item.containsEnchantment(Enchantment.FLAME)) continue

            val meta = item.itemMeta
            meta.persistentDataContainer.set(temporaryFlameKey, PersistentDataType.BYTE, 1.toByte())
            item.itemMeta = meta
            item.addUnsafeEnchantment(Enchantment.FLAME, 1)
            inventory.setItem(slot, item)
        }
    }

    private fun removeTemporaryFlameFromAllItems() {
        Bukkit.getOnlinePlayers().forEach { player ->
            val inventory = player.inventory
            for (slot in 0 until inventory.size) {
                val item = inventory.getItem(slot) ?: continue
                if (removeTemporaryFlame(item)) inventory.setItem(slot, item)
            }
        }

        Bukkit.getWorlds().forEach { world ->
            world.entities.filterIsInstance<Item>().forEach { droppedItem ->
                val item = droppedItem.itemStack
                if (removeTemporaryFlame(item)) droppedItem.itemStack = item
            }
        }
    }

    private fun removeTemporaryFlame(item: org.bukkit.inventory.ItemStack): Boolean {
        val meta = item.itemMeta
        if (!meta.persistentDataContainer.has(temporaryFlameKey, PersistentDataType.BYTE)) return false
        meta.persistentDataContainer.remove(temporaryFlameKey)
        item.itemMeta = meta
        item.removeEnchantment(Enchantment.FLAME)
        return true
    }

    private fun clearFightsFor(playerId: UUID) {
        val removed = fights.entries
            .filter { it.key.crystalPlayerId == playerId || it.key.targetId == playerId }