BookMeta not working correctly?

Discussion in 'Plugin Development' started by ferrybig, Dec 24, 2012.

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

    ferrybig

    Code:java
    1. private void updateBookState(BookMeta book, int infoPage,
    2. List<String> errors, FireworkMeta fireworkmeta, boolean lolMode, BlockDispenseEvent evt)
    3. {
    4.  
    5. BlockState state = evt.getBlock().getState();
    6. if (state instanceof InventoryHolder)
    7. {
    8. InventoryHolder inventoryHolder = (InventoryHolder) state;
    9. ItemStack[] items = inventoryHolder.getInventory().getContents();
    10. for (int i = 0; i < items.length; i++)
    11. {
    12. if (items[i ] != null && items[i ].isSimilar(evt.getItem()))
    13. {
    14. logInfo("Found match!"); // << is correct printed
    15. items[i ] = evt.getItem();
    16. this.writeErrorLogToBook(book, infoPage, errors, fireworkmeta.getPower(), lolMode);
    17. items[i ].setItemMeta(book);
    18.  
    19. inventoryHolder.getInventory().setContents(items);
    20. state.update();
    21. { // Start of debug block
    22. BlockState debug = evt.getBlock().getState();
    23. InventoryHolder debug1 = (InventoryHolder) debug;
    24. ItemStack[] debug2 = debug1.getInventory().getContents();
    25. ItemMeta debug3 = debug2[i ].getItemMeta();
    26. BookMeta debug4 = (BookMeta) debug3;
    27. String debug5 = debug4.getPage(1); // Books start whit index 1, arrays in java whit 0
    28. logInfo(debug5); // Debug printed is the contents of the book I want
    29. } // end of debug block
    30. break;
    31. }
    32. }
    33. }
    34.  
    35. }

    Ok, what I want is that my book that is inside a dispenser must be updated while the dispense event is happening. I am not able to archieve this. I tried manny methodes to accomplish this, but none of them worked, above you see the methode I tried the last. I tested it at the following builds: 2569 and 2571. I need at least build 2569 for another bug I expierenced, I already solved it by searching on the forums, but I get now this but. I am sure of the following things:
    * The methode is called
    * The correct item is found inside the dispenser
    * The contents of variable 'debug5' are the correct contents of the book I need
    * Al the debug messages are printed
    * I used state.update() to update the contents of the dispenser
    * .equals() did not work to find the item in the dispenser, so I used isSimilar() (But it shouldn't matter as theres only 1 item inside the dispenser)

    I hoping you're guys can point me out where I did my mistake, because I looked over and over, but I am not able to find it...
     
  2. Offline

    fireblast709

    What kind of event, as this method uses a huge load of parameters you do not use
     
  3. Offline

    ferrybig

    I use it inside an ItemDispenseEvent
    I generate a firework depending on the contents of the book, and then I want to write some contents back into the book like the last time it fired, the firework that the plugin makes are fired, the debug statements show the correct things, but the book keep holding its contents like before it got shot
     
  4. Offline

    fireblast709

    ferrybig you should use item.setItemMeta(some_changed_meta) afterwards, as getItemMeta returns a copy of the current meta.
     
  5. Offline

    ferrybig

    The methode this.writeErrorLogToBook is chancing the contents of the book:
    Code:java
    1. private void writeErrorLogToBook(BookMeta book, int infoPageIndex, List<String> errorList, int fireworkPower, boolean lolMode)
    2. {
    3. StringBuilder infoBuilder = new StringBuilder();
    4. infoBuilder.append("[FIREWORK]\n");
    5. if (lolMode)
    6. {
    7. infoBuilder.append("LOL=true\n\n");
    8. }
    9. infoBuilder.append("P=").append(fireworkPower).append("\n\n");
    10. infoBuilder.append("[INFO] Last time shot at: ").append(DateUtils.now()).append("\n\n");
    11. for (String error : errorList)
    12. {
    13. infoBuilder.append("[Warning] ").append(error);
    14. }
    15. logInfo("Updated book!"); // << This debug is working
    16. logInfo(infoBuilder.toString());
    17. List<String> pages = new ArrayList<String>(book.getPages());
    18. pages.set(infoPageIndex, infoBuilder.toString().trim());
    19. book.setPages(pages);
    20. }
     
  6. Offline

    fireblast709

    You should apply the ItemMeta to the ItemStack as well
    Code:
    someBook.setItemMeta(someBookMeta);
     
  7. Offline

    ferrybig

    I did:
    Code:java
    1.  
    2. this.writeErrorLogToBook(book, infoPage, errors, fireworkmeta.getPower(), lolMode);
    3. items[i ].setItemMeta(book);
    4.  


    I just thinked, if I cancel the BlockDispenceEvent, would the item be restored? to see if this was causing my problem, I delayed the item.setItemMeta() and now it works whitout anny problems, thx for the help
     
  8. Offline

    fireblast709

    Mmh ok. And np :p
     
Thread Status:
Not open for further replies.

Share This Page