Advertisement
jayhillx

HealthEvents.class

Oct 21st, 2020
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.82 KB | None | 0 0
  1. @Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD, modid = XLifeHealth.MOD_ID)
  2. public class HealthEvents {
  3.  
  4. private float maxHealth = 0;
  5.  
  6. public float getMaxHealth() {
  7. return maxHealth;
  8. }
  9. public void setMaxHealth(float maxHealth) {
  10. this.maxHealth = maxHealth;
  11. }
  12.  
  13. @SubscribeEvent
  14. public void onPlayerDie(LivingDeathEvent event) {
  15. if (event.getEntityLiving() instanceof PlayerEntity) {
  16. PlayerEntity player = (PlayerEntity) event.getEntityLiving();
  17. setMaxHealth(player.getMaxHealth());
  18. }
  19. }
  20.  
  21. @SubscribeEvent
  22. public void onRespawn(PlayerEvent.PlayerRespawnEvent event) {
  23. PlayerEntity player = event.getPlayer();
  24. SecurityCheck.securityCheck(player);
  25. setHealthAfterDeath(player);
  26.  
  27. }
  28.  
  29. @SubscribeEvent
  30. public void onEnterNether(PlayerEvent.PlayerChangedDimensionEvent event) {
  31. PlayerEntity player = event.getPlayer();
  32. SecurityCheck.securityCheck(player);
  33. applyMaxHealthModifier(player, 0f);
  34. }
  35.  
  36. @SubscribeEvent
  37. public void onFirstJoin(PlayerEvent.PlayerLoggedInEvent event){
  38. PlayerEntity player = event.getPlayer();
  39. CompoundNBT entityData = player.getPersistentData();
  40. if(!entityData.getBoolean("xlifehealth.firstJoin") && player.getMaxHealth() == 20f) {
  41. entityData.putBoolean("xlifehealth.firstJoin", true);
  42. removeMaxHealthModifiers(player);
  43. applyMaxHealthModifier(player, -18f);
  44. SendMessage.sendMessage(player, TextFormatting.YELLOW + "=====================================");
  45. SendMessage.sendMessage(player, " ");
  46. SendMessage.sendMessage(player, TextFormatting.GOLD + "Welcome to X Life!");
  47. SendMessage.sendMessage(player, " ");
  48. SendMessage.sendMessage(player, TextFormatting.WHITE + "You'll start off with only one heart.");
  49. SendMessage.sendMessage(player, TextFormatting.WHITE + "Every time you die, a heart will be added.");
  50. SendMessage.sendMessage(player, TextFormatting.WHITE + "When you die with 10 hearts, you will be banned!");
  51. SendMessage.sendMessage(player, " ");
  52. SendMessage.sendMessage(player, TextFormatting.GOLD + "Good Luck!");
  53. SendMessage.sendMessage(player, " ");
  54. SendMessage.sendMessage(player, TextFormatting.YELLOW + "=====================================");
  55. }
  56. }
  57.  
  58. public void setHealthAfterDeath(PlayerEntity player) {
  59. if (getMaxHealth() >= 2 && getMaxHealth() <= 18) {
  60. float maxHealth = getMaxHealth();
  61. int lives = (int) (10 - (maxHealth / 2));
  62. int modifierAmount = (lives * 2 - 2) - ((lives * 2 - 2) + (lives * 2 - 2));
  63. removeMaxHealthModifiers(player);
  64. applyMaxHealthModifier(player, modifierAmount);
  65. if (lives < 10) {
  66. if (lives == 9) {
  67. SendMessage.sendMessage(player, TextFormatting.RED + "You have 9 lives remaining . . .");
  68. setBlueHearts();
  69. }
  70. if (lives == 8) {
  71. SendMessage.sendMessage(player, TextFormatting.RED + "You have 8 lives remaining . . .");
  72. setGreenHearts();
  73. }
  74. if (lives == 7) {
  75. SendMessage.sendMessage(player, TextFormatting.RED + "You have 7 lives remaining . . .");
  76. }
  77. if (lives == 6) {
  78. SendMessage.sendMessage(player, TextFormatting.RED + "You have 6 lives remaining . . .");
  79. }
  80. if (lives == 5) {
  81. SendMessage.sendMessage(player, TextFormatting.RED + "You have 5 lives remaining . . .");
  82. }
  83. if (lives == 4) {
  84. SendMessage.sendMessage(player, TextFormatting.RED + "You have 4 lives remaining . . .");
  85. }
  86. if (lives == 3) {
  87. SendMessage.sendMessage(player, TextFormatting.RED + "You have 3 lives remaining . . .");
  88. }
  89. if (lives == 2) {
  90. SendMessage.sendMessage(player, TextFormatting.RED + "You have 2 lives remaining . . .");
  91. }
  92. if (lives == 1) {
  93. SendMessage.sendMessage(player, TextFormatting.DARK_RED + "You have 1 life remaining . . .");
  94. }
  95. }
  96. } else if (getMaxHealth() == 20) {
  97. player.setGameType(GameType.SPECTATOR);
  98. SendMessage.sendMessage(player,TextFormatting.RED + "You have died with 10 hearts remaining.");
  99. SendMessage.sendMessage(player,TextFormatting.RED + "You have been put in spectator mode.");
  100. removeMaxHealthModifiers(player);
  101. applyMaxHealthModifier(player, 0);
  102. } else {
  103. removeMaxHealthModifiers(player);
  104. applyMaxHealthModifier(player, -18);
  105. }
  106. }
  107.  
  108. public static final ResourceLocation BLUE_HEARTS = new ResourceLocation("xlifehealth:textures/gui/blue_hearts.png");
  109. public static final ResourceLocation GREEN_HEARTS = new ResourceLocation("xlifehealth:textures/gui/green_hearts.png");
  110. public static final ResourceLocation ORANGE_HEARTS = new ResourceLocation("xlifehealth:textures/gui/orange_hearts.png");
  111. public static final ResourceLocation PINK_HEARTS = new ResourceLocation("xlifehealth:textures/gui/pink_hearts.png");
  112. public static final ResourceLocation PURPLE_HEARTS = new ResourceLocation("xlifehealth:textures/gui/purple_hearts.png");
  113. public static final ResourceLocation YELLOW_HEARTS = new ResourceLocation("xlifehealth:textures/gui/yellow_hearts.png");
  114. public static final ResourceLocation CYAN_HEARTS = new ResourceLocation("xlifehealth:textures/gui/cyan_hearts.png");
  115. public static final ResourceLocation MAGENTA_HEARTS = new ResourceLocation("xlifehealth:textures/gui/magenta_hearts.png");
  116. public static final ResourceLocation BLACK_HEARTS = new ResourceLocation("xlifehealth:textures/gui/black_hearts.png");
  117.  
  118. @SubscribeEvent
  119. public void setHeartColors(RenderGameOverlayEvent.Pre evt) {
  120. if (evt.getType().equals(RenderGameOverlayEvent.ElementType.HEALTH)) {
  121. evt.setCanceled(true);
  122. Minecraft mc = Minecraft.getInstance();
  123. mc.getTextureManager().bindTexture(BLUE_HEARTS);
  124. mc.getTextureManager().bindTexture(GREEN_HEARTS);
  125. mc.getTextureManager().bindTexture(ORANGE_HEARTS);
  126. mc.getTextureManager().bindTexture(PINK_HEARTS);
  127. mc.getTextureManager().bindTexture(PURPLE_HEARTS);
  128. mc.getTextureManager().bindTexture(YELLOW_HEARTS);
  129. mc.getTextureManager().bindTexture(CYAN_HEARTS);
  130. mc.getTextureManager().bindTexture(MAGENTA_HEARTS);
  131. mc.getTextureManager().bindTexture(BLACK_HEARTS);
  132. }
  133. }
  134.  
  135. public void setBlueHearts() {
  136. Minecraft mc = Minecraft.getInstance();
  137. mc.getTextureManager().bindTexture(BLUE_HEARTS);
  138. bind(BLUE_HEARTS);
  139. }
  140. public void setGreenHearts() {
  141. Minecraft mc = Minecraft.getInstance();
  142. mc.getTextureManager().bindTexture(GREEN_HEARTS);
  143. bind(GREEN_HEARTS);
  144. }
  145. public void setOrangeHearts() {
  146. Minecraft mc = Minecraft.getInstance();
  147. mc.getTextureManager().bindTexture(ORANGE_HEARTS);
  148. bind(ORANGE_HEARTS);
  149. }
  150. public void setPinkHearts() {
  151. Minecraft mc = Minecraft.getInstance();
  152. mc.getTextureManager().bindTexture(PINK_HEARTS);
  153. bind(PINK_HEARTS);
  154. }
  155. public void setPurpleHearts() {
  156. Minecraft mc = Minecraft.getInstance();
  157. mc.getTextureManager().bindTexture(PURPLE_HEARTS);
  158. bind(PURPLE_HEARTS);
  159. }
  160. public void setYellowHearts() {
  161. Minecraft mc = Minecraft.getInstance();
  162. mc.getTextureManager().bindTexture(YELLOW_HEARTS);
  163. bind(YELLOW_HEARTS);
  164. }
  165. public void setCyanHearts() {
  166. Minecraft mc = Minecraft.getInstance();
  167. mc.getTextureManager().bindTexture(CYAN_HEARTS);
  168. bind(CYAN_HEARTS);
  169. }
  170. public void setMagentaHearts() {
  171. Minecraft mc = Minecraft.getInstance();
  172. mc.getTextureManager().bindTexture(MAGENTA_HEARTS);
  173. bind(MAGENTA_HEARTS);
  174. }
  175. public void setBlackHearts() {
  176. Minecraft mc = Minecraft.getInstance();
  177. mc.getTextureManager().bindTexture(BLACK_HEARTS);
  178. bind(BLACK_HEARTS);
  179. }
  180.  
  181. private static void bind(ResourceLocation res) {
  182. Minecraft mc = Minecraft.getInstance();
  183. mc.getTextureManager().bindTexture(res);
  184. }
  185. }
  186.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement