Advertisement
jayhillx

HealthBookUtils

Sep 13th, 2021
5,182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.87 KB | None | 0 0
  1. package com.jayhill.xlife.common.util;
  2.  
  3. import com.jayhill.xlife.common.capability.stats.IStatsCapability;
  4. import com.jayhill.xlife.common.capability.stats.StatsCapability;
  5. import com.jayhill.xlife.common.capability.time.ITimeCapability;
  6. import com.jayhill.xlife.common.capability.time.TimeCapability;
  7. import net.minecraft.entity.player.PlayerEntity;
  8. import net.minecraft.nbt.StringNBT;
  9.  
  10. /** This adds the players life information to a book. */
  11. @SuppressWarnings("all")
  12. public class HealthBookUtils {
  13.  
  14.     /** These pages are set when the players hearts increase. */
  15.     public static StringNBT setPages(PlayerEntity player, int pages) {
  16.         return StringNBT.valueOf(title(getPages(player, pages)));
  17.     }
  18.  
  19.     /** Gets the pages written in the book. */
  20.     private static String getPages(PlayerEntity player, int pages) {
  21.         IStatsCapability stats = player.getCapability(StatsCapability.STATS_CAPABILITY).orElse(null);
  22.         String[] time = stats.getTime();
  23.         String[] cause = stats.getCause();
  24.  
  25.         if (pages == 1) {
  26.             if (player.getMaxHealth() == 2.0F) {
  27.                 return (living(player, 1));
  28.             } else if (player.getMaxHealth() == 4.0F) {
  29.                 return lasted(1, time[0], cause[0]) + "," + living(player, 2);
  30.             } else if (player.getMaxHealth() == 6.0F) {
  31.                 return lasted(1, time[0], cause[0]) + "," + lasted(2, time[1], cause[1]) + "," + living(player, 3);
  32.             } else {
  33.                 return lasted(1, time[0], cause[0]) + "," + lasted(2, time[1], cause[1]) + "," + lasted(3, time[2], cause[2]);
  34.             }
  35.         } else if (pages == 2) {
  36.             if (player.getMaxHealth() == 8.0F) {
  37.                 return (living(player, 4));
  38.             } else if (player.getMaxHealth() == 10.0F) {
  39.                 return lasted(4, time[3], cause[3]) + "," + living(player, 5);
  40.             } else if (player.getMaxHealth() == 12.0F) {
  41.                 return lasted(4, time[3], cause[3]) + "," + lasted(5, time[4], cause[4]) + "," + living(player, 6);
  42.             } else {
  43.                 return lasted(4, time[3], cause[3]) + "," + lasted(5, time[4], cause[4]) + "," + lasted(6, time[5], cause[5]);
  44.             }
  45.         } else if (pages == 3) {
  46.             if (player.getMaxHealth() == 14.0F) {
  47.                 return (living(player, 7));
  48.             } else if (player.getMaxHealth() == 16.0F) {
  49.                 return lasted(7, time[6], cause[6]) + "," + living(player, 8);
  50.             } else if (player.getMaxHealth() == 18.0F) {
  51.                 return lasted(7, time[6], cause[6]) + "," + lasted(8, time[7], cause[7]) + "," + living(player, 9);
  52.             } else {
  53.                 return lasted(7, time[6], cause[6]) + "," + lasted(8, time[7], cause[7]) + "," + lasted(9, time[8], cause[8]);
  54.             }
  55.         } else if (pages == 4) {
  56.             if (player.getMaxHealth() == 20.0F) {
  57.                 return (living(player, 10));
  58.             } else {
  59.                 return lasted(10, time[9], cause[9]);
  60.             }
  61.         } else {
  62.             return null;
  63.         }
  64.     }
  65.  
  66.     private static String title(String page) {
  67.         return "{\"extra\":[{\"bold\":true,\"text\":\"Life History\"},{\"text\":\"\\n\\n\"}," + page + "],\"text\":\"\"}";
  68.     }
  69.  
  70.     private static String living(PlayerEntity player, int hearts) {
  71.         return hearts(hearts) + ",{\"text\":\"\\n\"},{\"color\":\"black\",\"text\":\"" + time(player) + "\"}";
  72.     }
  73.  
  74.     private static String lasted(int hearts, String timeArray, String causeArray) {
  75.         return hearts(hearts) + ",{\"text\":\"\\n\"},{\"color\":\"black\",\"text\":\"Lasted " + timeArray + "\\nEnded by \"},{\"color\":\"dark_red\",\"hoverEvent\":{\"action\":\"show_text\",\"contents\":{\"text\":\"Something\"}},\"text\":\"" + causeArray + "\"},{\"text\":\"\\n\\n\"}";
  76.     }
  77.  
  78.     /** Returns how long the player has been living. */
  79.     private static String time(PlayerEntity player) {
  80.         ITimeCapability time = player.getCapability(TimeCapability.TIME_CAPABILITY).orElse(null);
  81.  
  82.         if (time.getTime() == 1) {
  83.             return "Living 1 second";
  84.         } else if (time.getTime() < 60) {
  85.             return "Living " + time.getTime() + " seconds";
  86.         } else if (time.getTime() >= 60 && time.getTime() < 120) {
  87.             return "Living 1 minute";
  88.         } else if (time.getTime() >= 120 && time.getTime() < 3600) {
  89.             return "Living " + time.getTime() / 60 + " minutes";
  90.         } else if (time.getTime() >= 3600 && time.getTime() < 7200) {
  91.             return "Living 1 hour";
  92.         } else {
  93.             return "Living " + time.getTime() / 3600 + " hours";
  94.         }
  95.     }
  96.  
  97.     private static String hearts(float hearts) {
  98.         char h = 10084;
  99.  
  100.         if (hearts == 1) {
  101.             return "{\"color\":\"red\",\"text\":\"" + h + "\"}";
  102.         } else if (hearts == 2) {
  103.             return "{\"color\":\"blue\",\"text\":\"" + h+h + "\"}";
  104.         } else if (hearts == 3) {
  105.             return "{\"color\":\"dark_green\",\"text\":\"" + h+h+h + "\"}";
  106.         } else if (hearts == 4) {
  107.             return "{\"color\":\"gold\",\"text\":\"" + h+h+h+h + "\"}";
  108.         } else if (hearts == 5) {
  109.             return "{\"color\":\"light_purple\",\"text\":\"" + h+h+h+h+h + "\"}";
  110.         } else if (hearts == 6) {
  111.             return "{\"color\":\"dark_purple\",\"text\":\"" + h+h+h+h+h+h + "\"}";
  112.         } else if (hearts == 7) {
  113.             return "{\"color\":\"yellow\",\"text\":\"" + h+h+h+h+h+h+h + "\"}";
  114.         } else if (hearts == 8) {
  115.             return "{\"color\":\"dark_aqua\",\"text\":\"" + h+h+h+h+h+h+h+h + "\"}";
  116.         } else if (hearts == 9) {
  117.             return "{\"color\":\"green\",\"text\":\"" + h+h+h+h+h+h+h+h+h + "\"}";
  118.         } else if (hearts == 10) {
  119.             return "{\"color\":\"black\",\"text\":\"" + h+h+h+h+h+h+h+h+h+h + "\"}";
  120.         } else {
  121.             return "";
  122.         }
  123.     }
  124.  
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement