Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- =======================================================================================================================================
- //Life History Book Item
- public class LifeBookItem extends WrittenBookItem {
- public LifeBookItem(Properties builder) {
- super(builder);
- }
- @Nonnull
- public ActionResult<ItemStack> onItemRightClick(@Nonnull World world, PlayerEntity player, @Nonnull Hand hand) {
- ItemStack stack = player.getHeldItem(hand);
- final Minecraft mc = Minecraft.getInstance();
- if (mc.player != null) {
- if (stack.getItem() == HealthItems.LIFE_HISTORY_BOOK.get()) {
- mc.displayGuiScreen(new LifeBookScreen(new LifeBookScreen.BookInfo(stack)));
- player.addStat(Stats.ITEM_USED.get(this));
- }
- }
- return ActionResult.func_233538_a_(stack, world.isRemote());
- }
- }
- =======================================================================================================================================
- @OnlyIn(Dist.CLIENT)
- public class LifeBookScreen extends Screen {
- public static final ResourceLocation BOOK_TEXTURES = new ResourceLocation("textures/gui/book.png");
- public ReadBookScreen.IBookInfo bookInfo;
- public int currPage;
- /** Holds a copy of the page text, split into page width lines */
- public List<IReorderingProcessor> cachedPageLines = Collections.emptyList();
- public int cachedPage = -1;
- public ITextComponent field_243344_s = StringTextComponent.EMPTY;
- public ChangePageButton buttonNextPage;
- public ChangePageButton buttonPreviousPage;
- /** Determines if a sound is played when the page is turned */
- public final boolean pageTurnSounds;
- public LifeBookScreen(ReadBookScreen.IBookInfo bookInfoIn) {
- this(bookInfoIn, true);
- }
- private LifeBookScreen(ReadBookScreen.IBookInfo bookInfoIn, boolean pageTurnSoundsIn) {
- super(NarratorChatListener.EMPTY);
- this.bookInfo = bookInfoIn;
- this.pageTurnSounds = pageTurnSoundsIn;
- }
- protected void init() {
- this.addDoneButton();
- this.addChangePageButtons();
- }
- protected void addDoneButton() {
- this.addButton(new Button(this.width / 2 - 100, 196, 200, 20, DialogTexts.GUI_DONE, (p_214161_1_) -> {
- if (this.minecraft != null) {
- this.minecraft.displayGuiScreen(null);
- }
- }));
- }
- protected void addChangePageButtons() {
- int i = (this.width - 192) / 2;
- int j = 2;
- this.buttonNextPage = this.addButton(new ChangePageButton(i + 116, 159, true, (p_214159_1_) -> {
- this.nextPage();
- }, this.pageTurnSounds));
- this.buttonPreviousPage = this.addButton(new ChangePageButton(i + 43, 159, false, (p_214158_1_) -> {
- this.previousPage();
- }, this.pageTurnSounds));
- this.updateButtons();
- }
- private int getPageCount() {
- return this.bookInfo.getPageCount();
- }
- /** Moves the display back one page. */
- protected void previousPage() {
- if (this.currPage > 0) {
- --this.currPage;
- }
- this.updateButtons();
- }
- /** Moves the display forward one page. */
- protected void nextPage() {
- if (this.currPage < this.getPageCount() - 1) {
- ++this.currPage;
- }
- this.updateButtons();
- }
- private void updateButtons() {
- this.buttonNextPage.visible = this.currPage < this.getPageCount() - 1;
- this.buttonPreviousPage.visible = this.currPage > 0;
- }
- public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
- if (super.keyPressed(keyCode, scanCode, modifiers)) {
- return true;
- } else {
- switch(keyCode) {
- case 266:
- this.buttonPreviousPage.onPress();
- return true;
- case 267:
- this.buttonNextPage.onPress();
- return true;
- default:
- return false;
- }
- }
- }
- @Override
- public void render(@Nonnull MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {
- this.renderBackground(matrixStack);
- RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
- if (this.minecraft != null) {
- this.minecraft.getTextureManager().bindTexture(BOOK_TEXTURES);
- }
- int i = (this.width - 192) / 2;
- int j = 2;
- this.blit(matrixStack, i, 2, 0, 0, 192, 192);
- if (this.cachedPage != this.currPage) {
- ITextProperties itextproperties = this.bookInfo.func_238806_b_(this.currPage);
- this.cachedPageLines = this.font.trimStringToWidth(itextproperties, 114);
- this.field_243344_s = new TranslationTextComponent("book.pageIndicator", this.currPage + 1, Math.max(this.getPageCount(),
- 1));
- }
- this.cachedPage = this.currPage;
- int i1 = this.font.getStringPropertyWidth(this.field_243344_s);
- this.font.func_243248_b(matrixStack, this.field_243344_s, (float)(i - i1 + 192 - 44), 18.0F, 0);
- int k = Math.min(128 / 9, this.cachedPageLines.size());
- for(int l = 0; l < k; ++l) {
- IReorderingProcessor ireorderingprocessor = this.cachedPageLines.get(l);
- this.font.func_238422_b_(matrixStack, ireorderingprocessor, (float)(i + 36), (float)(32 + l * 9), 0);
- }
- Style style = this.style(mouseX, mouseY);
- if (style != null) {
- this.renderComponentHoverEffect(matrixStack, style, mouseX, mouseY);
- }
- super.render(matrixStack, mouseX, mouseY, partialTicks);
- }
- @Nullable
- public Style style(double p_238805_1_, double p_238805_3_) {
- if (this.cachedPageLines.isEmpty()) {
- return null;
- } else {
- int i = MathHelper.floor(p_238805_1_ - (double)((this.width - 192) / 2) - 36.0D);
- int j = MathHelper.floor(p_238805_3_ - 2.0D - 30.0D);
- if (i >= 0 && j >= 0) {
- int k = Math.min(128 / 9, this.cachedPageLines.size());
- if (i <= 114 && j < 9 * k + k) {
- int l = j / 9;
- if (l < this.cachedPageLines.size()) {
- IReorderingProcessor ireorderingprocessor = this.cachedPageLines.get(l);
- return this.minecraft.fontRenderer.getCharacterManager().func_243239_a(ireorderingprocessor, i);
- } else {
- return null;
- }
- } else {
- return null;
- }
- } else {
- return null;
- }
- }
- }
- public static List<String> nbtPagesToStrings(CompoundNBT p_214157_0_) {
- ListNBT listnbt = p_214157_0_.getList("pages", 8).copy();
- ImmutableList.Builder<String> builder = ImmutableList.builder();
- for(int i = 0; i < listnbt.size(); ++i) {
- builder.add(listnbt.getString(i));
- }
- return builder.build();
- }
- @OnlyIn(Dist.CLIENT)
- public static class BookInfo implements ReadBookScreen.IBookInfo {
- private final List<String> pages;
- final Minecraft mc = Minecraft.getInstance();
- public BookInfo(ItemStack stack) {
- this.pages = LifeBookInfo.writtenInfo(Objects.requireNonNull(mc.player), stack);
- }
- /** Returns the size of the book */
- public int getPageCount() {
- return this.pages.size();
- }
- @Nonnull
- public ITextProperties func_230456_a_(int pages) {
- String s = this.pages.get(pages);
- ITextProperties properties = ITextComponent.Serializer.getComponentFromJson(s);
- if (properties != null) {
- return properties;
- }
- return ITextProperties.func_240652_a_(s);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement