Solved Set Book Pages

Discussion in 'Plugin Development' started by EnZiGuRi, Aug 15, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    EnZiGuRi

    Hello,

    I have the following data on a MySQL database:
    Item@WRITTEN_BOOK:BookAuthor@AName:BookTitle@BTitle:BookGeneration@ORIGINAL:BookPageAmount@4:BookPage1@P1:BookPage2@P2:BookPage3@P3:BookPage4@P4

    I want to get all pages and add it to the book. When i run this code the "List<String> pages = new ArrayList<String>();" is always empty and when i get the book in-game i can only get the 1 page with the text of the last one.

    Thanks

    Code:
    String[] serializedItemStack = serializedItem.split(":");
            for (String itemInfo : serializedItemStack) {
                String[] itemAttribute = itemInfo.split("@");
                if (auxAttribute[0].contains("BookPage")) {
                    BookMeta bookPages = (BookMeta) itemStack.getItemMeta();
                    List<String> pages = new ArrayList<String>();
                    String PageContent;
                    for (int i = 1; i <= Amount; i++) {
                        //String PageContent = new String();
                        if (auxAttribute[0].equals("BookPage" + i)) {
                            PageContent = String.valueOf(auxAttribute[1])
                                    .toString();
                            if (DebugMode == true) {
                                nplayer.sendMessage(ChatColor.GREEN
                                        + "PageAmount: " + pages.toString());
                                nplayer.sendMessage(ChatColor.GREEN
                                        + "PageContent: " + PageContent + "Loop:"
                                        + i);
                            }
                            pages.add(PageContent);
                        }
                    }
    
                    bookPages.setPages(pages);
                    itemStack.setItemMeta(bookPages);
                }
    
     
  2. Offline

    Zombie_Striker

    @EnZiGuRi
    First, follow java naming convetnions. All methods need to start with a lowercase letter.

    Now, have you debugged? Are sure all the line are being read? If there are lines not being read, at which line does it stop reading? If they all are being read, at hat point does it stop working? If there a logic error in your code?
     
  3. Offline

    EnZiGuRi

    Iam debugging it using messages to player (in-game), and It loops well, returns me all loops values but at the end i get the book with only one page with the text of the last one
     
  4. Offline

    Zombie_Striker

    @EnZiGuRi
    Have many times does it loop? What is "pageContent" equal to each time it loops? Does the list's size increase every time you add a new page?

    [edit]
    Have you tried using the "addPage()" method? Does that work?
     
  5. Offline

    EnZiGuRi

    @Zombie_Striker
    Yes, for that book i set a message for loop number, for page content and for the list.. I get the following output (in-game)

    List: []
    Loop: 1
    Page 1: P1

    List: []
    Loop: 2
    Page 2: P2

    List: []
    Loop: 3
    Page 3: P3

    List: []
    Loop: 4
    Page 4: P4
     
  6. Offline

    Zombie_Striker

    What does this mean? Is this a representation of what is in the array? If so, it looks like it is not being updated.
     
  7. Offline

    EnZiGuRi

    the List: [] is the content i add using "pages.add(PageContent);". This is supposed to add a value to "List<String> pages = new ArrayList<String>();"
    about the .addPage(), i already tried it and it gives me console errors.

    Note: Iam thinking about changing this method of saving/reading ItemStacks to "Base64" to avoid this type of issues. What do u think about it?
     
  8. Offline

    Zombie_Striker

    Can you post this error?
     
  9. Offline

    EnZiGuRi

    For some reason ".addPage()" worked now. Maybe i had something wrong... I did lot of changes after i tried this way...

    Thanks a lot for your help.

    Heres the final code:

    Code:
    if (auxAttribute[0].contains("BookPage")) {
                    BookMeta bookPages = (BookMeta) itemStack.getItemMeta();
                    String PageContent;
                    for (int i = 1; i <= Amount; i++) {
                        if (auxAttribute[0].equals("BookPage" + i)) {
                            PageContent = String.valueOf(auxAttribute[1])
                                    .toString();
                            if (DebugMode == true) {
                                nplayer.sendMessage(ChatColor.GREEN
                                        + "PageAmount: " + bookPages.toString());
                                nplayer.sendMessage(ChatColor.GREEN
                                        + "PageContent: " + PageContent + "Loop:"
                                        + i);
                            }
                            bookPages.addPage(PageContent);
                        }
                    }
                    itemStack.setItemMeta(bookPages);
                }
    
     
Thread Status:
Not open for further replies.

Share This Page