Advertisement
jayhillx

healthevents

Jan 1st, 2021
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.28 KB | None | 0 0
  1. @Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD, modid = XLifeHealth.MOD_ID)
  2. public class HealthEvents {
  3. public static final ResourceLocation BLUE_HEARTS = new ResourceLocation("xlifehealth:textures/gui/blue_hearts.png");
  4. public static final ResourceLocation GREEN_HEARTS = new ResourceLocation("xlifehealth:textures/gui/green_hearts.png");
  5. public static final ResourceLocation ORANGE_HEARTS = new ResourceLocation("xlifehealth:textures/gui/orange_hearts.png");
  6. public static final ResourceLocation PINK_HEARTS = new ResourceLocation("xlifehealth:textures/gui/pink_hearts.png");
  7. public static final ResourceLocation PURPLE_HEARTS = new ResourceLocation("xlifehealth:textures/gui/purple_hearts.png");
  8. public static final ResourceLocation YELLOW_HEARTS = new ResourceLocation("xlifehealth:textures/gui/yellow_hearts.png");
  9. public static final ResourceLocation CYAN_HEARTS = new ResourceLocation("xlifehealth:textures/gui/cyan_hearts.png");
  10. public static final ResourceLocation LIME_HEARTS = new ResourceLocation("xlifehealth:textures/gui/lime_hearts.png");
  11. public static final ResourceLocation BLACK_HEARTS = new ResourceLocation("xlifehealth:textures/gui/black_hearts.png");
  12.  
  13. private float maxHealth = 0;
  14.  
  15. public float getMaxHealth() {
  16. return maxHealth;
  17. }
  18.  
  19. public void setMaxHealth(float maxHealth) {
  20. this.maxHealth = maxHealth;
  21. }
  22.  
  23. @SubscribeEvent
  24. public void onPlayerDie(LivingDeathEvent event) {
  25. if (event.getEntityLiving() instanceof PlayerEntity) {
  26. PlayerEntity player = (PlayerEntity) event.getEntityLiving();
  27. setMaxHealth(player.getMaxHealth());
  28. }
  29. }
  30.  
  31. @SubscribeEvent
  32. public void onRespawn(PlayerEvent.PlayerRespawnEvent event) {
  33. PlayerEntity player = event.getPlayer();
  34. SecurityCheck.securityCheck(player);
  35. setHealthAfterDeath(player);
  36. setMaxHealth(player.getMaxHealth());
  37. }
  38.  
  39. @SubscribeEvent
  40. public void onEnterNether(PlayerEvent.PlayerChangedDimensionEvent event) {
  41. PlayerEntity player = event.getPlayer();
  42. SecurityCheck.securityCheck(player);
  43. applyMaxHealthModifier(player, 0f);
  44. }
  45.  
  46. @SubscribeEvent
  47. public void onFirstJoin(PlayerEvent.PlayerLoggedInEvent event){
  48. PlayerEntity player = event.getPlayer();
  49. CompoundNBT entityData = player.getPersistentData();
  50. if(!entityData.getBoolean("xlifehealth.firstJoin") && player.getMaxHealth() == 20f) {
  51. entityData.putBoolean("xlifehealth.firstJoin", true);
  52. removeMaxHealthModifiers(player);
  53. applyMaxHealthModifier(player, -18f);
  54. }
  55. }
  56.  
  57. public void setHealthAfterDeath(PlayerEntity player) {
  58. if (getMaxHealth() >= 2 && getMaxHealth() <= 18) {
  59. float maxHealth = getMaxHealth();
  60. int lives = (int) (10 - (maxHealth / 2));
  61. int modifierAmount = (lives * 2 - 2) - ((lives * 2 - 2) + (lives * 2 - 2));
  62. removeMaxHealthModifiers(player);
  63. applyMaxHealthModifier(player, modifierAmount);
  64. if (lives >= 2) {
  65. SendMessage.sendMessage(player, TextFormatting.RED + "You have " + lives + " lives remaining . . .");
  66. }
  67. if (lives == 1) {
  68. SendMessage.sendMessage(player, TextFormatting.RED + "You have 1 life remaining . . .");
  69. }
  70. } else if (getMaxHealth() == 20) {
  71. player.setGameType(GameType.SPECTATOR);
  72. SendMessage.sendMessage(player,TextFormatting.RED + "You died with 10 hearts remaining . . .");
  73. removeMaxHealthModifiers(player);
  74. applyMaxHealthModifier(player, 0);
  75. } else {
  76. removeMaxHealthModifiers(player);
  77. applyMaxHealthModifier(player, -18);
  78. }
  79. }
  80.  
  81. @SubscribeEvent
  82. public void setHeartColors(RenderGameOverlayEvent event) {
  83. if (event.getType() == RenderGameOverlayEvent.ElementType.HEALTH) {
  84. Minecraft mc = Minecraft.getInstance();
  85. if (getMaxHealth() == 4) {
  86. mc.getTextureManager().bindTexture(BLUE_HEARTS);
  87. }
  88. else if (getMaxHealth() == 6) {
  89. mc.getTextureManager().bindTexture(GREEN_HEARTS);
  90. }
  91. else if (getMaxHealth() == 8) {
  92. mc.getTextureManager().bindTexture(ORANGE_HEARTS);
  93. }
  94. else if (getMaxHealth() == 10) {
  95. mc.getTextureManager().bindTexture(PINK_HEARTS);
  96. }
  97. else if (getMaxHealth() == 12) {
  98. mc.getTextureManager().bindTexture(PURPLE_HEARTS);
  99. }
  100. else if (getMaxHealth() == 14) {
  101. mc.getTextureManager().bindTexture(YELLOW_HEARTS);
  102. }
  103. else if (getMaxHealth() == 16) {
  104. mc.getTextureManager().bindTexture(CYAN_HEARTS);
  105. }
  106. else if (getMaxHealth() == 18) {
  107. mc.getTextureManager().bindTexture(LIME_HEARTS);
  108. }
  109. else if (getMaxHealth() == 20) {
  110. mc.getTextureManager().bindTexture(BLACK_HEARTS);
  111. }
  112. }
  113. }
  114. }
  115. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  116. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  117. public class HealthModifier {
  118.  
  119. public static void applyMaxHealthModifier(PlayerEntity player, float amount) {
  120. ModifiableAttributeInstance attribute = player.getAttribute(Attributes.MAX_HEALTH);
  121. attribute.applyPersistentModifier(new AttributeModifier("MaxHealth", amount, AttributeModifier.Operation.ADDITION));
  122. player.setHealth(player.getMaxHealth());
  123. }
  124.  
  125. public static void removeMaxHealthModifiers(PlayerEntity player) {
  126. ModifiableAttributeInstance iAttributeInstance = player.getAttribute(Attributes.MAX_HEALTH);
  127. Collection<AttributeModifier> modifiers = iAttributeInstance.getModifierListCopy();
  128. for (AttributeModifier modifier : modifiers) {
  129. if (modifier.getName().equals("MaxHealth")) {
  130. iAttributeInstance.removeModifier(modifier);
  131. }
  132. }
  133. }
  134. }
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement