Saving item enchantments in database.

Discussion in 'Plugin Development' started by EnZiGuRi, Jul 17, 2014.

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

    EnZiGuRi

    Hey, Im making a plugin that deposit items in MySQL database for later withdraw by a player but i dont know howto save the enchantments...

    to save the item meta/enchantments in Database i need to convert it to string... and i cant convert it back to ItemMeta... -.- Is there any good way to make this?

    This is my code:

    Thanks.

    Code:java
    1. public static void DepositAction(Player player, ItemStack deposititem) throws SQLException{
    2. String playerName = player.getDisplayName();
    3. int itemID = deposititem.getTypeId();
    4. short itemSubID = deposititem.getDurability();
    5. String itemName = deposititem.getType().name();
    6. int itemAmount = deposititem.getAmount();
    7. String itemMeta = deposititem.getItemMeta().toString();
    8. String itemEnchantment = deposititem.getEnchantments().toString();
    9.  
    10. if (deposititem.getItemMeta().toString().contains("ENCHANTED_META") && deposititem.getEnchantments().toString().contentEquals("{}")){
    11. player.sendMessage(ChatColor.DARK_GREEN + "[DEBUG] " + ChatColor.WHITE + "BOOK!");
    12. }
    13. if (deposititem.getItemMeta().toString().contains("UNSPECIFIC_META") && deposititem.getEnchantments().toString().contains("Enchantment")){
    14. player.sendMessage(ChatColor.DARK_GREEN + "[DEBUG] " + ChatColor.WHITE + "ITEM!");
    15. ItemStack myItem = new ItemStack(itemID, itemAmount, itemSubID);
    16. // I want to add here the enchantments/itemmeta(for books)...
    17. player.getInventory().addItem(myItem);
    18. player.updateInventory();
    19. }
    20. if(itemID != 0){
    21. mysql.depositItemDB(playerName, itemID, itemSubID, itemName, itemAmount, itemMeta, itemEnchantment);
    22. }
    23. if (mysql.mysql_success() == true){
    24. player.setItemInHand(null);
    25. }
    26. }
     
  2. Offline

    EcMiner

    EnZiGuRi likes this.
  3. Offline

    EnZiGuRi

    cool, im currently testing it because i dont want to "set" an inventory. My plugin its a item transfer from a server to another server, so i want to keep the current inventory items in the target server...

    Thanks anyways for that link. it can be the soluction, i feel it :D
     
  4. Offline

    EcMiner

    Only thing you might want to add is a displayname and lore saver to the current serialization, if you need help with that, ask me if you want :)
     
  5. Offline

    EnZiGuRi

    Well, and enchanted books ^^ I will continue when i come from work, but if you know how to add it i will appreciate (for me will be a pain, learning java now ahah). I changed it a bit for my use... ItemStack instead of whole inventory.... Here is my current code:
    Thanks for the awesome help, this saved me a lot of work and works perfectly.
    Code:java
    1.  
    2. public class ItemStackSerializer {
    3. @SuppressWarnings("deprecation")
    4. public String ItemStackToString (ItemStack itemStack){
    5. String serialization = null;
    6.  
    7. if (itemStack != null){
    8. String serializedItemStack = new String();
    9.  
    10. String itemStackType = String.valueOf(itemStack.getType().getId());
    11. serializedItemStack += "Item@" + itemStackType;
    12.  
    13. if (itemStack.getDurability() != 0){
    14. String itemStackDurability = String.valueOf(itemStack.getDurability());
    15. serializedItemStack += ":d@" + itemStackDurability;
    16. }
    17.  
    18. if (itemStack.getAmount() != 1){
    19. String itemStackAmount = String.valueOf(itemStack.getAmount());
    20. serializedItemStack += ":a@" + itemStackAmount;
    21. }
    22.  
    23. Map<Enchantment,Integer> itemStackEnch = itemStack.getEnchantments();
    24. if (itemStackEnch.size() > 0){
    25. for (Entry<Enchantment,Integer> ench : itemStackEnch.entrySet()){
    26. serializedItemStack += ":e@" + ench.getKey().getId() + "@" + ench.getValue();
    27. }
    28. }
    29.  
    30. serialization = serializedItemStack;
    31. }
    32. return serialization;
    33. }
    34.  
    35. @SuppressWarnings("deprecation")
    36. public ItemStack StringToItemStack (String serializedItem, Player player){
    37. ItemStack itemStack = null;
    38. Boolean createdItemStack = false;
    39. String[] serializedItemStack = serializedItem.split(":");
    40. for (String itemInfo : serializedItemStack){
    41. String[] itemAttribute = itemInfo.split("@");
    42. if (itemAttribute[0].equals("Item")){
    43. itemStack = new ItemStack(Material.getMaterial(Integer.valueOf(itemAttribute[1])));
    44. createdItemStack = true;
    45. }
    46. else if (itemAttribute[0].equals("d") && createdItemStack){
    47. itemStack.setDurability(Short.valueOf(itemAttribute[1]));
    48. }
    49. else if (itemAttribute[0].equals("a") && createdItemStack){
    50. itemStack.setAmount(Integer.valueOf(itemAttribute[1]));
    51. }
    52. else if (itemAttribute[0].equals("e") && createdItemStack){
    53. itemStack.addEnchantment(Enchantment.getById(Integer.valueOf(itemAttribute[1])), Integer.valueOf(itemAttribute[2]));
    54. }
    55. }
    56. ItemStack unserializedItem = itemStack;
    57. return unserializedItem;
    58. }
    59. }
     
  6. Offline

    EnZiGuRi

    With the awesome help of @EcMiner this issue is solved. For who want something like this here is my final code. It supports item enchantments and book stored enchantments!

    Code:java
    1.  
    2. public class ItemStackSerializer {
    3. @SuppressWarnings("deprecation")
    4. public String ItemStackToString (ItemStack itemStack, Player player){
    5. String serialization = null;
    6.  
    7. if (itemStack != null){
    8. String serializedItemStack = new String();
    9.  
    10. String itemStackType = String.valueOf(itemStack.getType().getId());
    11. serializedItemStack += "Item@" + itemStackType;
    12.  
    13. if (itemStack.getDurability() != 0){
    14. String itemStackDurability = String.valueOf(itemStack.getDurability());
    15. serializedItemStack += ":d@" + itemStackDurability;
    16. }
    17.  
    18. if (itemStack.getAmount() != 1){
    19. String itemStackAmount = String.valueOf(itemStack.getAmount());
    20. serializedItemStack += ":a@" + itemStackAmount;
    21. }
    22.  
    23. Map<Enchantment,Integer> itemStackEnch = itemStack.getEnchantments();
    24. if (itemStackEnch.size() > 0){
    25. for (Entry<Enchantment,Integer> ench : itemStackEnch.entrySet()){
    26. serializedItemStack += ":e@" + ench.getKey().getId() + "@" + ench.getValue();
    27. }
    28. }
    29.  
    30. if (itemStack.getType() == Material.ENCHANTED_BOOK){
    31. EnchantmentStorageMeta bookmeta = (EnchantmentStorageMeta)itemStack.getItemMeta();
    32. Map<Enchantment, Integer> enchantments = bookmeta.getStoredEnchants();
    33. if (enchantments.size() > 0){
    34. for(Entry<Enchantment,Integer> bookenchants : enchantments.entrySet()){
    35. player.sendMessage(ChatColor.DARK_GREEN + "[Debug] " + ChatColor.WHITE + bookenchants);
    36. serializedItemStack += ":m@" + bookenchants.getKey().getId() + "@" + bookenchants.getValue();
    37. }
    38. }
    39. }
    40.  
    41. serialization = serializedItemStack;
    42. }
    43. return serialization;
    44. }
    45.  
    46. @SuppressWarnings("deprecation")
    47. public ItemStack StringToItemStack (String serializedItem, Player player){
    48. ItemStack itemStack = null;
    49. Boolean createdItemStack = false;
    50. String[] serializedItemStack = serializedItem.split(":");
    51. for (String itemInfo : serializedItemStack){
    52. String[] itemAttribute = itemInfo.split("@");
    53. if (itemAttribute[0].equals("Item")){
    54. itemStack = new ItemStack(Material.getMaterial(Integer.valueOf(itemAttribute[1])));
    55. createdItemStack = true;
    56. }
    57. else if (itemAttribute[0].equals("d") && createdItemStack){
    58. itemStack.setDurability(Short.valueOf(itemAttribute[1]));
    59. }
    60. else if (itemAttribute[0].equals("a") && createdItemStack){
    61. itemStack.setAmount(Integer.valueOf(itemAttribute[1]));
    62. }
    63. else if (itemAttribute[0].equals("e") && createdItemStack){
    64. itemStack.addEnchantment(Enchantment.getById(Integer.valueOf(itemAttribute[1])), Integer.valueOf(itemAttribute[2]));
    65. }
    66. else if (itemAttribute[0].equals("m") && createdItemStack){
    67. EnchantmentStorageMeta itemStackMeta = (EnchantmentStorageMeta)itemStack.getItemMeta();
    68. itemStackMeta.addStoredEnchant(Enchantment.getById(Integer.valueOf(itemAttribute[1])), Integer.valueOf(itemAttribute[2]), true);
    69. itemStack.setItemMeta(itemStackMeta);
    70. }
    71. }
    72. ItemStack unserializedItem = itemStack;
    73. return unserializedItem;
    74. }
    75. }
    76.  
     
    maxmar628 likes this.
Thread Status:
Not open for further replies.

Share This Page