Problem with hitting an entity

Started by Kris on

Topic category: Help with Minecraft modding (Java Edition)

Joined Jul 2025
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Problem with hitting an entity
package net.mcreator.deltamined.procedures;

import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.event.entity.player.AttackEntityEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

@Mod.EventBusSubscriber
public class GetTpOnHitMobProcedure {

    @SubscribeEvent
    public static void onPlayerAttack(AttackEntityEvent event) {
        Player player = event.getEntity(); // The attacking player
        Entity target = event.getTarget(); // The entity being hit

        // Only proceed if the attack wasn't canceled (e.g., by another mod)
        if (!event.isCanceled()) {
            execute(player); // Pass the player to the execute method
        }
    }

    public static void execute(Entity entity) {
        if (entity == null || !(entity instanceof Player player)) 
            return;

        // Safely modify player variables
        player.getCapability(DeltaminedModVariables.PLAYER_VARIABLES).ifPresent(vars -> {
            vars.TensePoints += 1; // Increase points
            vars.syncPlayerVariables(player); // Sync to client
        });
    }
}
Here is my code i search up on forum how to do something on player hit and i saw code with same request so i copied it and it didnt work besides code seems correct