Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD, modid = XLifeHealth.MOD_ID)
- public class HealthEvents {
- public static final ResourceLocation BLUE_HEARTS = new ResourceLocation("xlifehealth:textures/gui/blue_hearts.png");
- public static final ResourceLocation GREEN_HEARTS = new ResourceLocation("xlifehealth:textures/gui/green_hearts.png");
- public static final ResourceLocation ORANGE_HEARTS = new ResourceLocation("xlifehealth:textures/gui/orange_hearts.png");
- public static final ResourceLocation PINK_HEARTS = new ResourceLocation("xlifehealth:textures/gui/pink_hearts.png");
- public static final ResourceLocation PURPLE_HEARTS = new ResourceLocation("xlifehealth:textures/gui/purple_hearts.png");
- public static final ResourceLocation YELLOW_HEARTS = new ResourceLocation("xlifehealth:textures/gui/yellow_hearts.png");
- public static final ResourceLocation CYAN_HEARTS = new ResourceLocation("xlifehealth:textures/gui/cyan_hearts.png");
- public static final ResourceLocation LIME_HEARTS = new ResourceLocation("xlifehealth:textures/gui/lime_hearts.png");
- public static final ResourceLocation BLACK_HEARTS = new ResourceLocation("xlifehealth:textures/gui/black_hearts.png");
- private float maxHealth = 0;
- public float getMaxHealth() {
- return maxHealth;
- }
- public void setMaxHealth(float maxHealth) {
- this.maxHealth = maxHealth;
- }
- @SubscribeEvent
- public void onPlayerDie(LivingDeathEvent event) {
- if (event.getEntityLiving() instanceof PlayerEntity) {
- PlayerEntity player = (PlayerEntity) event.getEntityLiving();
- setMaxHealth(player.getMaxHealth());
- }
- }
- @SubscribeEvent
- public void onRespawn(PlayerEvent.PlayerRespawnEvent event) {
- PlayerEntity player = event.getPlayer();
- SecurityCheck.securityCheck(player);
- setHealthAfterDeath(player);
- setMaxHealth(player.getMaxHealth());
- }
- @SubscribeEvent
- public void onEnterNether(PlayerEvent.PlayerChangedDimensionEvent event) {
- PlayerEntity player = event.getPlayer();
- SecurityCheck.securityCheck(player);
- applyMaxHealthModifier(player, 0f);
- }
- @SubscribeEvent
- public void onFirstJoin(PlayerEvent.PlayerLoggedInEvent event){
- PlayerEntity player = event.getPlayer();
- CompoundNBT entityData = player.getPersistentData();
- if(!entityData.getBoolean("xlifehealth.firstJoin") && player.getMaxHealth() == 20f) {
- entityData.putBoolean("xlifehealth.firstJoin", true);
- removeMaxHealthModifiers(player);
- applyMaxHealthModifier(player, -18f);
- }
- }
- public void setHealthAfterDeath(PlayerEntity player) {
- if (getMaxHealth() >= 2 && getMaxHealth() <= 18) {
- float maxHealth = getMaxHealth();
- int lives = (int) (10 - (maxHealth / 2));
- int modifierAmount = (lives * 2 - 2) - ((lives * 2 - 2) + (lives * 2 - 2));
- removeMaxHealthModifiers(player);
- applyMaxHealthModifier(player, modifierAmount);
- if (lives >= 2) {
- SendMessage.sendMessage(player, TextFormatting.RED + "You have " + lives + " lives remaining . . .");
- }
- if (lives == 1) {
- SendMessage.sendMessage(player, TextFormatting.RED + "You have 1 life remaining . . .");
- }
- } else if (getMaxHealth() == 20) {
- player.setGameType(GameType.SPECTATOR);
- SendMessage.sendMessage(player,TextFormatting.RED + "You died with 10 hearts remaining . . .");
- removeMaxHealthModifiers(player);
- applyMaxHealthModifier(player, 0);
- } else {
- removeMaxHealthModifiers(player);
- applyMaxHealthModifier(player, -18);
- }
- }
- @SubscribeEvent
- public void setHeartColors(RenderGameOverlayEvent event) {
- if (event.getType() == RenderGameOverlayEvent.ElementType.HEALTH) {
- Minecraft mc = Minecraft.getInstance();
- if (getMaxHealth() == 4) {
- mc.getTextureManager().bindTexture(BLUE_HEARTS);
- }
- else if (getMaxHealth() == 6) {
- mc.getTextureManager().bindTexture(GREEN_HEARTS);
- }
- else if (getMaxHealth() == 8) {
- mc.getTextureManager().bindTexture(ORANGE_HEARTS);
- }
- else if (getMaxHealth() == 10) {
- mc.getTextureManager().bindTexture(PINK_HEARTS);
- }
- else if (getMaxHealth() == 12) {
- mc.getTextureManager().bindTexture(PURPLE_HEARTS);
- }
- else if (getMaxHealth() == 14) {
- mc.getTextureManager().bindTexture(YELLOW_HEARTS);
- }
- else if (getMaxHealth() == 16) {
- mc.getTextureManager().bindTexture(CYAN_HEARTS);
- }
- else if (getMaxHealth() == 18) {
- mc.getTextureManager().bindTexture(LIME_HEARTS);
- }
- else if (getMaxHealth() == 20) {
- mc.getTextureManager().bindTexture(BLACK_HEARTS);
- }
- }
- }
- }
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- public class HealthModifier {
- public static void applyMaxHealthModifier(PlayerEntity player, float amount) {
- ModifiableAttributeInstance attribute = player.getAttribute(Attributes.MAX_HEALTH);
- attribute.applyPersistentModifier(new AttributeModifier("MaxHealth", amount, AttributeModifier.Operation.ADDITION));
- player.setHealth(player.getMaxHealth());
- }
- public static void removeMaxHealthModifiers(PlayerEntity player) {
- ModifiableAttributeInstance iAttributeInstance = player.getAttribute(Attributes.MAX_HEALTH);
- Collection<AttributeModifier> modifiers = iAttributeInstance.getModifierListCopy();
- for (AttributeModifier modifier : modifiers) {
- if (modifier.getName().equals("MaxHealth")) {
- iAttributeInstance.removeModifier(modifier);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement