Jump to content

Recommended Posts

Posted

Hi,

 

I'm currently working on a custom book and want to create new pages on fly for it.

I've got myself and index which includes all chapters the book has and instead of using a scrollbar when more than 10 chapters

exists for this book, I want to add a new page to show the rest of the chapters.

So each site on the index page shows only 10 chapters and when you've got 100 chapters the index part of the book will be 10 pages long.

 

Here is what I've currently got:

 

        if(!this.chapters.isEmpty()) {
            int buttons = this.buttonList.size() - 1;
            int y = (this.top + 20) / this.chapters.size() + 10;
            for(int i = 0; i < this.chapters.size(); i++) {
                y += 10;
                GuiButtonChapter button = new GuiButtonChapter(i + buttons, this.left + 20, y, 100, 10, this.chapters.get(i));
                button.displayString = I18n.format(this.chapters.get(i).getTitle());
                this.buttonList.add(button);
            }
        }

 

GuiButtonChapter is the button used to display the chapter and to actually choose to read it.

Chapters is a ArrayList which get's loaded in from my API as other mods might want to add chapters to the book. This happens when the Gui object is created.

The top and left variables are used to determine the start point of the GUI.

 

Thx in advance.

Bektor

Developer of Primeval Forest.

Posted (edited)

Anyone got an idea as I'm stuck on this problem.

 

Just to mention, also tried this piece of code... well, it resulted in an index out of bounds exception

for(int z = 0; z < this.maxPages; z++) {
            for(int i = 0; i < this.chapters.size(); i++) {
                y += 10;
                if(!(this.chapters.size() > i + z)) {
                    System.out.println("max: " + maxPages);
                    System.out.println("chapters: " + chapters.size());
                    System.out.println("i: " + i);
                    System.out.println("z: " + z);
                    break;
                }
                GuiButtonChapter button = new GuiButtonChapter(i + buttons, this.left + 20, y, 100, 10, this.chapters.get(i + z));
                button.displayString = I18n.format(this.chapters.get(i).getTitle());
                this.buttonList.add(button);
            }
}

 

Quote

max: 2 // yeah, it should be 3 as chapters is 21 and only 10 per page...

chapters: 21

i: 20

z: 1

 

Also it does not work. Somehow it even seems to do some weird stuff when going a page back...

Edited by Bektor

Developer of Primeval Forest.

Posted (edited)

Math.ceil(chapters.size()/10f) should return the required amount of pages for the chapters.
But without an Idea how the rest of your code looks and how you handle the pages we cannot tell you anything more.

Edited by JTK222
Posted
4 minutes ago, JTK222 said:

Math.ceil(chapters.size()/10f) should return the required amount of pages for the chapters.
But without an Idea how the rest of your code looks and how you handle the pages we cannot tell you anything more.

Ah, ok. That works. (I guess my idea of fixing this would have been more resource intensive ^^)

 

I'm handling the pages by actually having an integer variable which counts on which page we are currenty.

Thats basically how I handle the pages.

    private int page = 0;
    private int maxPages = 0;
    private ArrayList<Chapter> chapters = new ArrayList<>(APIMod.chapters);

    protected void initGui() {
        if(!this.chapters.isEmpty()) {
            int buttons = this.buttonList.size() - 1;
            int y = (this.top + 20) / this.chapters.size() + 10;
            for(int i = 0; i < this.chapters.size(); i++) {
                y += 10;
                GuiButtonChapter button = new GuiButtonChapter(i + buttons, this.left + 20, y, 100, 10, this.chapters.get(i));
                button.displayString = I18n.format(this.chapters.get(i).getTitle());
                this.buttonList.add(button);
            }
        }
    }

public void drawScreen(int mouseX, int mouseY, float partialTicks) {
  super.drawScreen(mouseX, mouseY, partialTicks);
}

 

Developer of Primeval Forest.

Posted

So I guess you use the pages only for this?
And not for the chapters themselves?

If yes you should create a list of 10 buttons. and add a method to them on to change for which chapter they are.
And as you switch pages you would have just to change the chapters for the buttons.

Posted
17 hours ago, JTK222 said:

So I guess you use the pages only for this?
And not for the chapters themselves?

If yes you should create a list of 10 buttons. and add a method to them on to change for which chapter they are.
And as you switch pages you would have just to change the chapters for the buttons.

Yes, the chapters are a different GUI.

 

Hm... I'm just wondering if this is efficient to do it with a list as every button would require to update on the actionPerformed method:

  • displayString and translate it
  • the chapter from the list

 This would mean, on each click on the forward button or the backwards button the hole button and chapters list would have to be iterated over.

Developer of Primeval Forest.

Posted

Yes, you would need to iterate trough the buttons, but let's be serious even in case your chapters lists gets a few hundred entries you wouldn't even notice it. (Using this system for a Crafting System I have done).
But you wouldn't have to iterate trough the chapters, chapters.get(10 * page + buttonOffset); should be enough, you would just have to set buttonOffset for each button (first button 0 and 10th button 9).
Maybe with a small if statement to prevent an IndexOutOfBounds exception and it would be done.

	button.chapter = chapters.size > 10 * page + buttonOffset ? chapters.get(10*page +buttonOffset) : null;
	

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Reach Out To Rapid Digital: What sapp Info: +1 41 4 80 7 14 85 Email INFO: rap iddi gita lrecov ery @ exe cs. com Hello, my name is Jayson, and I’m 35 years old from the United Kingdom. My family and I recently endured an incredibly challenging experience that I wouldn’t wish on anyone. We became victims of a cryptocurrency investment fraud scheme that saw us lose a staggering $807,000 in USDT and Bitcoins. The fraudsters had created a convincing facade, and we were lured into investing, only to discover later that the platform was a complete scam. We were left devastated, not just financially, but emotionally, as we had trusted these people and believed in the legitimacy of the investment. After the initial shock wore off, we desperately searched for ways to recover the lost funds. It seemed like an impossible task, and we felt as though there was no hope. That’s when, by sheer luck, we stumbled across a post about Rapid Digital Recovery, a cryptocurrency and funds recovery organization with a proven track record in cybersecurity and fraud recovery. We decided to reach out to them, and from the first interaction, we were impressed with their professionalism and transparency. They explained the recovery process in detail and reassured us that they had the skills and expertise to track down the perpetrators and recover our funds. This gave us a renewed sense of hope, something we hadn’t felt in months. What truly stood out during our experience with Rapid Digital Recovery was their dedication to the recovery process. The team went above and beyond, using sophisticated tracking tools and cyber forensics to gather critical information. Within a matter of weeks, they had successfully located the funds and traced the scam back to the fraudsters responsible. They worked with the authorities to ensure the criminals were held accountable for their actions. To our relief, the team at Rapid Digital Recovery was able to recover every single penny we had lost. The funds were returned in full, and the sense of closure we felt was invaluable. We couldn’t have imagined such a positive outcome in the early stages of our recovery journey, and we are deeply grateful for the work they did. If you ever find yourself in a similar situation, I highly recommend contacting Rapid Digital Recovery. Their expertise, transparency, and dedication to their clients make them the go-to choice for anyone seeking to recover lost cryptocurrency or funds. They truly gave us back our financial future.  
    • This is my first time modding anything, so maybe just skill issue. I'm using Forge 54.0.12 and Temurin 21.0.5+11-LTS I wanted to create a custom keybind and to check whether it works I'd like to send a chat message. I tried using Minecraft.getInstance().player.sendSystemMessage(Component.literal("test")); but IntelliJ couldnt resolve sendSystemMessage(...). Since I saw people using it in earlier versions, I tried the same thing with 1.20.6(- 50.1.0), where it works fine, now I can't figure out if this is intentional and whether there are other options for sending chat messages. On that note, is there more documentation than https://docs.minecraftforge.net/en/1.21.x/? It seems very incomplete compared to something like the Oracle Java docs
    • Hi, i'm having this error and I wanna fix it. we try: -Reload drivers -Eliminate .minecraft -Eliminate Java -Restart launcher -Verify if minecraft is using gpu -Mods  in .minecraft is empty -Install the latest and recomended version of forge idk what i have to do, help me pls. the lastest log is: https://mclo.gs/WAMao8x  
    • Read the FAQ, Rule #2. (https://forums.minecraftforge.net/topic/125488-rules-and-frequently-asked-questions-faq/)  
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.