thedutchpianoman_

Make a plugin for Java Paper 26.1.2 that when someone hits someone with a sword, mace, anything else that they get in combat for 10 seconds, also make it so that when someone throws a wind charge at somebody they also get in combat, by the way when i mean in combat i mean they both get in combat. Also make it so that when any kind of explosion made by somebody and it hits somebody else they both get in combat, example: Somebody uses crystals and explodes them and somebody is next to the crystal. Another example: Somebody uses TNT Minecarts to explode them on their victim.

Kodari AI
KodariMon at 6:35 pm 2
CombatTagSystem
CombatTagSystem is a Paper plugin that tags both attacker and victim as "in combat" for 10 seconds whenever combat is initiated. It detects melee hits with swords, maces, or any other item, wind charge projectiles, and explosions caused by players (including end crystals and TNT minecarts). When tagged, both parties receive a configurable combat status, preventing unfair escape or logout during PvP encounters. The plugin listens to entity damage events, projectile hits, and explosion events to accurately attribute combat to the responsible player, ensuring both sides are tagged simultaneously. It provides a lightweight, efficient solution for enforcing combat logging rules in survival or minigame servers.

Created the combat-tag plugin. Both players are tagged for 10 seconds after player-caused damage; /combat shows the remaining time.

Explosions are attributed when the responsible player can be identified. Logging out clears the tag; there’s no logout penalty.

thedutchpianoman_

Make it so that when you're in combat that there comes up a placeholder above your hotbar that says you're in combat. Make everything customizable, in config.yml. Like the color for the timer and how long the timer should be.

Kodari AI
KodariMon at 6:37 pm 1
  • Add a configurable action-bar countdown for both combatants.

  • Move the duration and combat messages into config.yml.

Added an action-bar countdown for both players. In config.yml, you can customize the combat duration, timer color, action-bar text, and chat messages.

Restart the server after editing the config.

thedutchpianoman_

Also make it so that when you're in combat that you can't use specific commands. You can edit those commands in config.yml.

Kodari AI
KodariMon at 6:43 pm 1
  • Block configured commands, including their arguments and namespaced forms, while a player is in combat.

  • Add the command list and denial message to config.yml.

Added configurable command blocking during combat. Edit blocked-commands and messages.command-blocked in config.yml, then restart the server.

On an existing installation, add those settings to your current config; the default file won’t overwrite it.

thedutchpianoman_

So when i die i'm still in combat, can you make it so that it doesn't happen? Also when i try to use blocked commands, make it so that when you try to do that that it says you cant use that command but not in the chat but in the placeholder hotbar, also play a villager no sound when you try to use blocked commands. Also make it so that when you leave, you die. Atleast when you're in combat.

Kodari AI
KodariTue at 5:28 pm 2
  1. Clear combat when a player dies.

  2. Show blocked-command feedback above the hotbar and play a villager sound.

  3. Kill players who disconnect during combat.

  • Combat now clears on death.

  • Blocked commands show the configured message above the hotbar and play the villager “no” sound.

  • Players who disconnect during combat are killed.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
settings.gradle
Code
5 313
src/main/java/com/kodari/combattag/CombatTagPlugin.java
package com.kodari.combattag;

import com.cryptomorin.xseries.XSound;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.UUID;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.block.Block;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.entity.TNTPrimed;
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.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityPlaceEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.projectiles.ProjectileSource;

public final class CombatTagPlugin extends JavaPlugin implements Listener {
    private final Map<UUID, Long> combat = new HashMap<>();
    private final Map<UUID, Long> commandFeedback = new HashMap<>();
    private final Map<Location, BlockUse> explosiveUses = new HashMap<>();
    private NamespacedKey ownerKey;

    @Override
    public void onEnable() {
        saveDefaultConfig();
        ownerKey = new NamespacedKey(this, "explosive-owner");
        Bukkit.getPluginManager().registerEvents(this, this);
        Bukkit.getScheduler().runTaskTimer(this, this::expireCombat, 1L, 5L);
    }

    @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
    public void onPlace(EntityPlaceEvent event) {
        Player player = event.getPlayer();
        if (player != null) {
            rememberOwner(event.getEntity(), player);
        }
    }

    @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
    public void onExplosiveBlockUse(PlayerInteractEvent event) {
        Block block = event.getClickedBlock();
        if (event.getAction() != Action.RIGHT_CLICK_BLOCK || block == null) {
            return;
        }
        String type = block.getType().name();
        if (type.endsWith("_BED") || type.equals("RESPAWN_ANCHOR")) {
            long now = System.currentTimeMillis();
            explosiveUses.entrySet().removeIf(entry -> now - entry.getValue().time > 2_000L);
            explosiveUses.put(block.getLocation(), new BlockUse(event.getPlayer().getUniqueId(), now));
        }
    }

    @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
    public void onEntityDamage(EntityDamageByEntityEvent event) {
        Entity damager = event.getDamager();
        Entity causing = event.getDamageSource().getCausingEntity();
        Player attacker = causing instanceof Player ? (Player) causing : resolvePlayer(damager);

        if (!(event.getEntity() instanceof Player victim)) {
            if (attacker != null && (event.getEntity().getType().name().equals("END_CRYSTAL")
                    || event.getEntity().getType().name().equals("TNT_MINECART"))) {
                rememberOwner(event.getEntity(), attacker);
            }
            return;
        }
        tagBoth(attacker, victim);
    }

    @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
    public void onBlockExplosionDamage(EntityDamageEvent event) {
        if (event instanceof EntityDamageByEntityEvent || !(event.getEntity() instanceof Player victim)
                || event.getCause() != EntityDamageEvent.DamageCause.BLOCK_EXPLOSION) {
            return;
        }
        Location origin = event.getDamageSource().getDamageLocation();
        if (origin == null || origin.getWorld() == null) {
            return;
        }
        long now = System.currentTimeMillis();
        BlockUse closest = null;
        double closestDistance = 4.0;
        for (Map.Entry<Location, BlockUse> entry : explosiveUses.entrySet()) {
            if (now - entry.getValue().time > 2_000L || !origin.getWorld().equals(entry.getKey().getWorld())) {
                continue;
            }
            double distance = origin.distanceSquared(entry.getKey().clone().add(0.5, 0.5, 0.5));
            if (distance <= closestDistance) {
                closestDistance = distance;
                closest = entry.getValue();
            }
        }
        if (closest != null) {
            tagBoth(Bukkit.getPlayer(closest.player), victim);
        }
    }

    private void rememberOwner(Entity entity, Player player) {
        entity.getPersistentDataContainer().set(ownerKey, PersistentDataType.STRING, player.getUniqueId().toString());
    }

    private Player resolvePlayer(Entity entity) {
        if (entity instanceof Player player) {
            return player;
        }
        if (entity instanceof Projectile projectile) {
            ProjectileSource shooter = projectile.getShooter();
            if (shooter instanceof Player player) {
                return player;
            }
        }
        if (entity instanceof TNTPrimed tnt && tnt.getSource() != null && tnt.getSource() != entity) {
            Player source = resolvePlayer(tnt.getSource());
            if (source != null) {
                return source;
            }
        }
        String owner = entity.getPersistentDataContainer().get(ownerKey, PersistentDataType.STRING);
        if (owner == null) {
            return null;
        }
        try {
            return Bukkit.getPlayer(UUID.fromString(owner));
        } catch (IllegalArgumentException ignored) {
            return null;
        }
    }

    private void tagBoth(Player attacker, Player victim) {
        if (attacker == null || attacker.getUniqueId().equals(victim.getUniqueId())) {
            return;
        }
        long duration = Math.max(1L, getConfig().getLong("combat-duration-seconds", 10L));
        long until = System.currentTimeMillis() + Math.min(duration, Integer.MAX_VALUE) * 1000L;
        tag(attacker, until);
        tag(victim, until);
    }

    private void tag(Player player, long until) {
        Long previous = combat.put(player.getUniqueId(), until);
        if (previous == null || previous <= System.currentTimeMillis()) {
            sendMessage(player, "messages.combat-start", "&cYou are in combat for {time} seconds.", (until - System.currentTimeMillis() + 999) / 1000);
        }
        showActionBar(player, (until - System.currentTimeMillis() + 999) / 1000);
    }

    private void expireCombat() {
        long now = System.currentTimeMillis();
        Iterator<Map.Entry<UUID, Long>> iterator = combat.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<UUID, Long> entry = iterator.next();
            if (entry.getValue() <= now) {
                Player player = Bukkit.getPlayer(entry.getKey());
                if (player != null) {
                    if (getConfig().getBoolean("action-bar.enabled", true)) {
                        player.sendActionBar(net.kyori.adventure.text.Component.empty());
                    }
                    sendMessage(player, "messages.combat-end", "&aYou are no longer in combat.", 0);
                }
                iterator.remove();
            } else {
                Player player = Bukkit.getPlayer(entry.getKey());
                if (player != null) {
                    showActionBar(player, (entry.getValue() - now + 999) / 1000);
                }
            }
        }
    }

    private void showActionBar(Player player, long seconds) {
        if (!getConfig().getBoolean("action-bar.enabled", true)
                || commandFeedback.getOrDefault(player.getUniqueId(), 0L) > System.currentTimeMillis()) {
            return;
        }
        String format = getConfig().getString("action-bar.text", "&cYou are in combat: {timer-color}{time}&c seconds");
        String timerColor = getConfig().getString("action-bar.timer-color", "&e");
        String text = format.replace("{timer-color}", timerColor).replace("{time}", Long.toString(seconds));
        player.sendActionBar(LegacyComponentSerializer.legacyAmpersand().deserialize(text));
    }

    private void sendMessage(CommandSender sender, String path, String fallback, long seconds) {
        String text = getConfig().getString(path, fallback).replace("{time}", Long.toString(seconds));
        if (!text.isEmpty()) {