How to handle Enchanted Books?

Discussion in 'Plugin Development' started by UltraMC, Dec 22, 2012.

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

    UltraMC

    In recent 1.4.6 update Enchanted Books was introducet to the game.

    How to handle them in Bukkit?

    Following code takes and gives enchanted items to player, along with the regular blocks and potions. But books, are loosing their enchantment!

    Book's enchant glow stays, its ID is not changed, but effect is lost.

    Code:
    /odbierz - is for download item from database
    /sprzedaj - is for upload item to database
    Code:java
    1. if (cmd.getName().equalsIgnoreCase("odbierz")) {// on Command: ' /kupione '
    2. if (sender instanceof Player) {
    3.  
    4. Player player = (Player) sender;
    5. String playerName = player.getName();
    6. int isHandEmpty = player.getItemInHand().getTypeId();
    7.  
    8. if ( isHandEmpty != 0 ){
    9.  
    10. player.sendMessage( ChatColor.RED + "Aby odebrac przedmiot musisz miec wolne rece..." );
    11.  
    12. } else {
    13.  
    14. String MySQLresult = getFromMySQL( playerName, isHandEmpty );
    15.  
    16. if ( MySQLresult != "" ) {
    17. String[] splits = MySQLresult.split(":");
    18.  
    19. String itemIDtmp = splits[0];
    20. String itemSUBtmp = splits[1];
    21. String itemQTYtmp = splits[2];
    22. String itemDURtmp = splits[3];
    23. String itemENCH = splits[4];
    24.  
    25. int itemID = Integer.parseInt( itemIDtmp );
    26. byte itemSUB = Byte.parseByte( itemSUBtmp );
    27. int itemQTY = Integer.parseInt( itemQTYtmp );
    28. short itemDUR = Short.parseShort( itemDURtmp );
    29.  
    30. player.setItemInHand( new ItemStack( itemID , itemQTY , itemDUR, itemSUB ) );
    31.  
    32. if ( itemENCH.length() > 5 ) {
    33.  
    34. String[] explode = itemENCH.split(",");
    35.  
    36. for (String s : explode) {
    37.  
    38. String[] enchantINFO = s.split("\\.");
    39.  
    40. Enchantment enchantTYPE = Enchantment.getByName(enchantINFO[0]);
    41. int enchantLVL = Integer.parseInt(enchantINFO[1]);
    42.  
    43. player.getItemInHand().addEnchantment( enchantTYPE , enchantLVL );
    44.  
    45. }
    46. }
    47.  
    48. String itemNAME = player.getItemInHand().getType() + "";
    49.  
    50. player.sendMessage( ChatColor.GREEN + "Odbierasz " + ChatColor.AQUA + itemNAME + " " + itemQTY + " szt." );
    51. } else {
    52.  
    53. player.sendMessage( ChatColor.RED + "Brak przedmiotow do odebrania..." );
    54.  
    55. }
    56. }
    57.  
    58. }
    59. return true;
    60.  
    61. } else if ( cmd.getName().equalsIgnoreCase("sprzedaj") ) {
    62.  
    63. if ( sender instanceof Player ) {// on Command: ' /sprzedaj '
    64.  
    65. Player player = (Player) sender;
    66. String playerName = player.getName();
    67. int isHandEmpty = player.getItemInHand().getTypeId();
    68.  
    69. if ( isHandEmpty == 0 ){
    70.  
    71. player.sendMessage( ChatColor.RED + "Nie mozesz sprzedac powietrza..." );
    72.  
    73. } else {
    74.  
    75. String itemNAME = player.getItemInHand().getType() + "";
    76. int itemID = player.getItemInHand().getTypeId();
    77. int itemSUB = player.getItemInHand().getData().getData();
    78. int itemQTY = player.getItemInHand().getAmount();
    79. String itemDATA = itemID + ":" + itemSUB;
    80. int itemDUR = player.getItemInHand().getDurability();
    81. String itemENCH = "";
    82.  
    83. Map<Enchantment, Integer> map_ItemENCH = player.getItemInHand().getEnchantments();
    84. String explode0 = map_ItemENCH + "";
    85.  
    86. if ( explode0.length() > 5 ) {
    87.  
    88. String explode1 = explode0.replace("{", "");
    89. String explode2 = explode1.replace("}", "");
    90. String explode3 = explode2.replace("Enchantment", "");
    91. String explode4 = explode3.replace("[", ",");
    92. String[] explode5 = explode4.split(", ,");
    93.  
    94. for (String s : explode5) {
    95.  
    96. String[] explode6 = s.split(", ");
    97. String[] explode7 = explode6[1].split("]=");
    98. String enchantmentTYPE = explode7[0];
    99. String enchantmentLEVEL = explode7[1];
    100.  
    101. itemENCH = itemENCH + enchantmentTYPE + "." + enchantmentLEVEL + ",";
    102. }
    103.  
    104. itemENCH = itemENCH.substring( 0, itemENCH.length() -1 );
    105.  
    106. }
    107.  
    108. if ( itemENCH.length() < 5) itemENCH = "XXX";
    109. String sendQuery = sendToMySQL( playerName, itemDATA, itemQTY, itemDUR, itemNAME, itemENCH );
    110.  
    111. player.sendMessage( sendQuery );
    112.  
    113. if ( sendQuery != "" ) {
    114.  
    115. player.setItemInHand(new ItemStack(Material.AIR, 0));
    116.  
    117. } else {
    118.  
    119. player.sendMessage( "Blad MySQL KOD: SWB1" );
    120.  
    121. }
    122. }
    123.  
    124. }
    125. return true;
    126.  
    127. }
    128.  
    129. return false;
    130. }
     
  2. Offline

    gamerzap

    Sorry, I haven't had a chance to mess with this yet. Although I think maybe the problem is that Enchanted Books aren't actually enchanted and it's just like a seperate thing. I'll test it right now.

    Yeah, it's not an actual enchantment. It's a seperate thing.

    I checked, and they enchantments are stored in a NBT List in "tag" called StoredEnchantments which is a list of compounds that each contain a short "id" and a short "level".

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  3. Offline

    UltraMC

    Got any tutorials, howtos or explanations about NBT Lists?
     
  4. Offline

    gamerzap

    While, you can get the item's tag with the ItemStack.getTag() method, which returns the items "tag" compound, and then you can do NBTCompound.getList("StoredEnchantments") to get the StoredEnchantments list, then later you can use the NBTCompound.setList("StoredEnchantments", <LIST YOU GOT EARLIER>) to set the new item's list...
    But I think I might have misconstrued your intentions with this plugin...

    An NBTList is basiically the NBT version of an ArrayList.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  5. Offline

    Wolvereness Bukkit Team Member

    Use the enchanted storage meta, it includes books. There are methods to manipulate the 'stored' enchantments.
     
Thread Status:
Not open for further replies.

Share This Page