Advertisement
jayhillx

LifeHistoryBook 01

Aug 19th, 2021
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.98 KB | None | 0 0
  1. =======================================================================================================================================
  2. //Life History Book Item
  3. public class LifeBookItem extends WrittenBookItem {
  4.  
  5. public LifeBookItem(Properties builder) {
  6. super(builder);
  7. }
  8.  
  9. @Nonnull
  10. public ActionResult<ItemStack> onItemRightClick(@Nonnull World world, PlayerEntity player, @Nonnull Hand hand) {
  11. ItemStack stack = player.getHeldItem(hand);
  12. final Minecraft mc = Minecraft.getInstance();
  13.  
  14. if (mc.player != null) {
  15. if (stack.getItem() == HealthItems.LIFE_HISTORY_BOOK.get()) {
  16.  
  17. mc.displayGuiScreen(new LifeBookScreen(new LifeBookScreen.BookInfo(stack)));
  18. player.addStat(Stats.ITEM_USED.get(this));
  19. }
  20. }
  21.  
  22. return ActionResult.func_233538_a_(stack, world.isRemote());
  23. }
  24.  
  25. }
  26. =======================================================================================================================================
  27.  
  28. @OnlyIn(Dist.CLIENT)
  29. public class LifeBookScreen extends Screen {
  30. public static final ResourceLocation BOOK_TEXTURES = new ResourceLocation("textures/gui/book.png");
  31. public ReadBookScreen.IBookInfo bookInfo;
  32. public int currPage;
  33. /** Holds a copy of the page text, split into page width lines */
  34. public List<IReorderingProcessor> cachedPageLines = Collections.emptyList();
  35. public int cachedPage = -1;
  36. public ITextComponent field_243344_s = StringTextComponent.EMPTY;
  37. public ChangePageButton buttonNextPage;
  38. public ChangePageButton buttonPreviousPage;
  39. /** Determines if a sound is played when the page is turned */
  40. public final boolean pageTurnSounds;
  41.  
  42. public LifeBookScreen(ReadBookScreen.IBookInfo bookInfoIn) {
  43. this(bookInfoIn, true);
  44. }
  45.  
  46. private LifeBookScreen(ReadBookScreen.IBookInfo bookInfoIn, boolean pageTurnSoundsIn) {
  47. super(NarratorChatListener.EMPTY);
  48. this.bookInfo = bookInfoIn;
  49. this.pageTurnSounds = pageTurnSoundsIn;
  50. }
  51.  
  52. protected void init() {
  53. this.addDoneButton();
  54. this.addChangePageButtons();
  55. }
  56.  
  57. protected void addDoneButton() {
  58. this.addButton(new Button(this.width / 2 - 100, 196, 200, 20, DialogTexts.GUI_DONE, (p_214161_1_) -> {
  59. if (this.minecraft != null) {
  60. this.minecraft.displayGuiScreen(null);
  61. }
  62. }));
  63. }
  64.  
  65. protected void addChangePageButtons() {
  66. int i = (this.width - 192) / 2;
  67. int j = 2;
  68. this.buttonNextPage = this.addButton(new ChangePageButton(i + 116, 159, true, (p_214159_1_) -> {
  69. this.nextPage();
  70. }, this.pageTurnSounds));
  71. this.buttonPreviousPage = this.addButton(new ChangePageButton(i + 43, 159, false, (p_214158_1_) -> {
  72. this.previousPage();
  73. }, this.pageTurnSounds));
  74. this.updateButtons();
  75. }
  76.  
  77. private int getPageCount() {
  78. return this.bookInfo.getPageCount();
  79. }
  80.  
  81. /** Moves the display back one page. */
  82. protected void previousPage() {
  83. if (this.currPage > 0) {
  84. --this.currPage;
  85. }
  86.  
  87. this.updateButtons();
  88. }
  89.  
  90. /** Moves the display forward one page. */
  91. protected void nextPage() {
  92. if (this.currPage < this.getPageCount() - 1) {
  93. ++this.currPage;
  94. }
  95. this.updateButtons();
  96. }
  97.  
  98. private void updateButtons() {
  99. this.buttonNextPage.visible = this.currPage < this.getPageCount() - 1;
  100. this.buttonPreviousPage.visible = this.currPage > 0;
  101. }
  102.  
  103. public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
  104. if (super.keyPressed(keyCode, scanCode, modifiers)) {
  105. return true;
  106. } else {
  107. switch(keyCode) {
  108. case 266:
  109. this.buttonPreviousPage.onPress();
  110. return true;
  111. case 267:
  112. this.buttonNextPage.onPress();
  113. return true;
  114. default:
  115. return false;
  116. }
  117. }
  118. }
  119.  
  120. @Override
  121. public void render(@Nonnull MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {
  122. this.renderBackground(matrixStack);
  123. RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
  124. if (this.minecraft != null) {
  125. this.minecraft.getTextureManager().bindTexture(BOOK_TEXTURES);
  126. }
  127. int i = (this.width - 192) / 2;
  128. int j = 2;
  129. this.blit(matrixStack, i, 2, 0, 0, 192, 192);
  130. if (this.cachedPage != this.currPage) {
  131. ITextProperties itextproperties = this.bookInfo.func_238806_b_(this.currPage);
  132. this.cachedPageLines = this.font.trimStringToWidth(itextproperties, 114);
  133. this.field_243344_s = new TranslationTextComponent("book.pageIndicator", this.currPage + 1, Math.max(this.getPageCount(),
  134. 1));
  135. }
  136.  
  137. this.cachedPage = this.currPage;
  138. int i1 = this.font.getStringPropertyWidth(this.field_243344_s);
  139. this.font.func_243248_b(matrixStack, this.field_243344_s, (float)(i - i1 + 192 - 44), 18.0F, 0);
  140. int k = Math.min(128 / 9, this.cachedPageLines.size());
  141.  
  142. for(int l = 0; l < k; ++l) {
  143. IReorderingProcessor ireorderingprocessor = this.cachedPageLines.get(l);
  144. this.font.func_238422_b_(matrixStack, ireorderingprocessor, (float)(i + 36), (float)(32 + l * 9), 0);
  145. }
  146.  
  147. Style style = this.style(mouseX, mouseY);
  148. if (style != null) {
  149. this.renderComponentHoverEffect(matrixStack, style, mouseX, mouseY);
  150. }
  151.  
  152. super.render(matrixStack, mouseX, mouseY, partialTicks);
  153. }
  154.  
  155. @Nullable
  156. public Style style(double p_238805_1_, double p_238805_3_) {
  157. if (this.cachedPageLines.isEmpty()) {
  158. return null;
  159. } else {
  160. int i = MathHelper.floor(p_238805_1_ - (double)((this.width - 192) / 2) - 36.0D);
  161. int j = MathHelper.floor(p_238805_3_ - 2.0D - 30.0D);
  162. if (i >= 0 && j >= 0) {
  163. int k = Math.min(128 / 9, this.cachedPageLines.size());
  164. if (i <= 114 && j < 9 * k + k) {
  165. int l = j / 9;
  166. if (l < this.cachedPageLines.size()) {
  167. IReorderingProcessor ireorderingprocessor = this.cachedPageLines.get(l);
  168. return this.minecraft.fontRenderer.getCharacterManager().func_243239_a(ireorderingprocessor, i);
  169. } else {
  170. return null;
  171. }
  172. } else {
  173. return null;
  174. }
  175. } else {
  176. return null;
  177. }
  178. }
  179. }
  180.  
  181. public static List<String> nbtPagesToStrings(CompoundNBT p_214157_0_) {
  182. ListNBT listnbt = p_214157_0_.getList("pages", 8).copy();
  183. ImmutableList.Builder<String> builder = ImmutableList.builder();
  184.  
  185. for(int i = 0; i < listnbt.size(); ++i) {
  186. builder.add(listnbt.getString(i));
  187. }
  188.  
  189. return builder.build();
  190. }
  191.  
  192. @OnlyIn(Dist.CLIENT)
  193. public static class BookInfo implements ReadBookScreen.IBookInfo {
  194. private final List<String> pages;
  195. final Minecraft mc = Minecraft.getInstance();
  196.  
  197. public BookInfo(ItemStack stack) {
  198. this.pages = LifeBookInfo.writtenInfo(Objects.requireNonNull(mc.player), stack);
  199. }
  200.  
  201. /** Returns the size of the book */
  202. public int getPageCount() {
  203. return this.pages.size();
  204. }
  205.  
  206. @Nonnull
  207. public ITextProperties func_230456_a_(int pages) {
  208. String s = this.pages.get(pages);
  209.  
  210. ITextProperties properties = ITextComponent.Serializer.getComponentFromJson(s);
  211. if (properties != null) {
  212. return properties;
  213. }
  214.  
  215. return ITextProperties.func_240652_a_(s);
  216. }
  217. }
  218.  
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement