<commit_before>package com.punchline.javalib.entities.components.generic;

import com.punchline.javalib.entities.Entity;
import com.punchline.javalib.entities.EntityWorld;
import com.punchline.javalib.entities.components.ComponentManager;
import com.punchline.javalib.entities.components.abstracted.Stat;

/**
 * A component describing an Entity's health. When health becomes 0, the Entity will be deleted.
 * @author MadcowD
 * @created Jul 23, 2013
 */
public class Health extends Stat {
	
	/**
	 * Callback for when an Entity dies.
	 * @author Nathaniel
	 * @created Jul 24, 2013
	 */
	public interface HealthEventCallback {
		
		/**
		 * Called when the owner of this component runs out of health.
		 * @param owner The owner of this component.
		 * @param world The world that Entity owner belongs to.
		 */
		public void invoke(Entity owner, EntityWorld world);
		
	}
	
	private Entity owner;
	private EntityWorld world;
	
	/**
	 * Whether a health bar should be rendered above this component's owner.
	 * The health bar will only render if the EntityWorld contains a HealthRenderSystem.
	 */
	public boolean render = false;
	
	/**
	 * Constructs a Health component.
	 * @param owner The Entity that owns this component.
	 * @param world The EntityWorld that owns Entity owner.
	 * @param max The max amount of health.
	 */
	public Health(Entity owner, EntityWorld world, float max) {
		super(max);
		
		this.owner = owner;
		this.world = world;
	}
	
	@Override
	public void drain(double amount) {
		if (isEmpty()) return; //Already dead, don't drain.
		
		super.drain(amount);
		
		if (isEmpty()) { //Dead
			
			if (onDeath != null)
				onDeath.invoke(owner, world);
			
			owner.delete();
			
			return;
		}
		
		if (amount > 0) { //Damaged
			
			if (onDamage != null)
				onDamage.invoke(owner, world);
			
		}
	}

	@Override
	public void fill(double amount) {
		super.fill(amount);
		
		if (amount > 0) { //Healed
			
			if (onHeal != null)
				onHeal.invoke(owner, world);
			
		}
	}

	/**
	 * Callback for when the Entity that owns this component dies.
	 */
	public HealthEventCallback onDeath;

	/**
	 * Callback for when the Entity that owns this component takes damage.
	 */
	public HealthEventCallback onDamage;
	
	/**
	 * Callback for when the Entity that owns this component is healed.
	 */
	public HealthEventCallback onHeal;
	
	@Override
	public void onAdd(ComponentManager container) { }

	@Override
	public void onRemove(ComponentManager container) { }
	
}<commit_msg>Health components now use EventHandlers instead of a specialized callback.<commit_after>package com.punchline.javalib.entities.components.generic;

import com.punchline.javalib.entities.Entity;
import com.punchline.javalib.entities.EntityWorld;
import com.punchline.javalib.entities.components.ComponentManager;
import com.punchline.javalib.entities.components.abstracted.Stat;
import com.punchline.javalib.entities.events.EventHandler;

/**
 * A component describing an Entity's health. When health becomes 0, the Entity will be deleted.
 * @author MadcowD
 * @created Jul 23, 2013
 */
public class Health extends Stat {
	
	private Entity owner;
	private EntityWorld world;
	
	/**
	 * Whether a health bar should be rendered above this component's owner.
	 * The health bar will only render if the EntityWorld contains a HealthRenderSystem.
	 */
	public boolean render = false;
	
	/**
	 * Invoked when the Entity that owns this component dies.
	 */
	public final EventHandler onDeath = new EventHandler();

	/**
	 * Invoked when the Entity that owns this component takes damage.
	 */
	public final EventHandler onDamage = new EventHandler();
	
	/**
	 * Invoked when the Entity that owns this component is healed.
	 */
	public final EventHandler onHeal = new EventHandler();
	
	/**
	 * Constructs a Health component.
	 * @param owner The Entity that owns this component.
	 * @param world The EntityWorld that owns Entity owner.
	 * @param max The max amount of health.
	 */
	public Health(Entity owner, EntityWorld world, float max) {
		super(max);
		
		this.owner = owner;
		this.world = world;
	}
	
	@Override
	public void drain(double amount) {
		if (isEmpty()) return; //Already dead, don't drain.
		
		super.drain(amount);
		
		if (isEmpty()) { //Dead
			
			if (onDeath != null)
				onDeath.invoke(owner, world);
			
			owner.delete();
			
			return;
		}
		
		if (amount > 0) { //Damaged
			
			if (onDamage != null)
				onDamage.invoke(owner, world);
			
		}
	}

	@Override
	public void fill(double amount) {
		super.fill(amount);
		
		if (amount > 0) { //Healed
			
			if (onHeal != null)
				onHeal.invoke(owner, world);
			
		}
	}
	
	@Override
	public void onAdd(ComponentManager container) { }

	@Override
	public void onRemove(ComponentManager container) { }
	
}
{'fraction_non_alphanumeric': 0.09798573227024758, 'fraction_numerical': 0.005035669324381032, 'mean_word_length': 3.306233062330623, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6274053', 'n_tokens_mistral': 1617, 'n_tokens_neox': 1370, 'n_words': 477}