[Tutorial] TacoSerialization Tutorials

Discussion in 'Resources' started by KILL3RTACO, Sep 1, 2013.

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

    KILL3RTACO

    Don't know what TacoSerialization is? Find out here.
    Don't know what serialization is? Google it.

    TacoSerialization has specific classes that handle different parts of the serialization process. Some classes even depend on others, for example the InventorySerialization class depends on SingleItemSerialization (for pretty obvious reasons).

    Note: Any 'serialize' method should also have a version with 'AsString' at the end of the method's name.
    Note: All classes have 'serialize' methods as well as 'get' AND/OR 'set' AND/OR 'apply' methods.

    BookSerialization

    This class helps you store different book data. The data serialized depends on what type of book meta you supply.

    The serialization process for a book is different depending on the type of book. Book an Quills and Written books are similar, they each store the pages of the book. Written Books will also store the title and author of the book. Enchanted Books are simple, the enchantment(s) (not sure if enchanted books can have multiple enchantments, but if so all enchantments are serialized) is one string.

    To serialize an Enchanted Book:
    Code:java
    1. //'items' is an ItemStack
    2. if(items.getType == Material.ENCHANTED_BOOK){ //test if the ItemStack is an enchanted book
    3. EnchantmentStorageMeta meta = (EnchantmentStorageMeta) items.getItemMeta();
    4. JSONObject serializedMeta = BookSerialization.serializeEnchantedBookMeta(meta); //JSONObject version
    5. String metaAsString = BookSerialization.serializeEnchantedBookMetaAsString(meta); //String version
    6. //do what you like
    7. }


    To serialize a Book and Quill or a Written Book:
    Code:java
    1. //'items' is an ItemStack
    2. if(items.getType() == Material.BOOK_AND_QUILL || items.getType() == Material.WRITTEN_BOOK){
    3. BookMeta meta = (BookMeta) items.getItemMeta();
    4. JSONObject serializedBookMeta = BookSerialization.serializeBookMeta(meta); //JSONObject version
    5. String metaAsString = BookSerialization.serializeBookMetaAsString(meta); //String version
    6. }


    ColorSerialization

    Bukkit has created their own Color class, different then java.awt.Color. This Color class provides support for making a color from an RGB value as well as a BGR value (not sure why, but whatevs).
    Color.fromRGB(int, int, int) can be used to create a Color from an RGB value. Likewise, Color.fromBGR(int, int, int) can be used to create a Color from a BGR value.

    To serialize a Color:
    Code:java
    1. //'color' is a Color
    2. JSONObject serializedColor = ColorSerialization.serializeColor(color); //JSONObject version
    3. String colorAsString = ColorSerialization.serializeColorAsString(color); //String version


    EnchantmentSerialization
    If you're familiar with ChestShop, you should know that it has it's own way of making Enchantments into strings. I used to use this method, but the way I created allows the use of unsafe enchantments (level and material type ignored). It's also easier to use/debug.

    However, lets say you have a string that uses ChestShop's format. You can convert it with
    Code:java
    1. //'oldFormat' represents the string following ChestShop's method
    2. String newFormat = EnchantmentSerialization.convert(oldFormat);


    If you want to get the Enchantments from that same string you can use
    Code:java
    1. Map<Enchantment, Integer> enchantments = EnchantmentSerialization.convertAndGetEnchantments(oldFormat);


    To get the enchantment code (my version) from an Enchantment map:
    Code:java
    1. //'items' is an ItemStack that has enchantments
    2. String enchantCode = EnchantmentSerialization.serializeEnchantments(items.getItemMeta().getEnchants()); //items.getEnchantments() works as well


    To get enchantments from an enchantment code:
    Code:java
    1. Map<Enchantment, Integer> enchants = EnchantmentSerialization.getEnchantments(enchantCode);


    InventorySerialization
    InventorySerialization can take either an InventoryHolder instance, Inventory instance or an ItemStack array to serialize. If given a PlayerInventory, it cannot be an ItemStack array.

    To serialize an Inventory:
    Code:java
    1. //'inv' is an Inventory instance
    2. JSONArray serializedInventory = InventorySerialization.serializeInventory(inv); //JSONArray version
    3. String invAsString = InventorySerialization.serializeInventoryAsString(inv); //String version


    PlayerInventories have additional data (armor). To serialize a PlayerInventory:
    Code:java
    1. //'inv' is an PlayerInventory instance
    2. JSONObject serializedInventory = InventorySerialization.serializePlayerInventory(inv); //JSONObject version
    3. String invAsString = InventorySerialization.serializePlayerInventoryAsString(inv); //String


    LivingEntities
    Currently, TacoSerialization supports the serialization of Horses, Ocelots, and Wolves. However, With LivingEntitySerialization you can serialize that stats (health, name, etc. of any LivingEntity (if given a player, PlayerSerialization will be used instead).

    Any LivingEntity can be serialized with
    Code:java
    1. //'entity' is a LivingEntity
    2. JSONObject serializedEntity = serializedEntityLivingEntitySerialization.serializeEntity(entity); //JSONObject version
    3. String entityAsString = LivingEntitySerialization.serializeEntityAsString(entity); //String version


    If you want to be more specific, Horses, Ocelots, and Wolves can be serialized with
    Code:java
    1. //'entity' represents an instance of a Horse, Ocelot or Wolf, depending on your use
    2. JSONObject serializedEntity = <Type>Serialization.serialize<Type>(entity); //JSONObject version
    3. String entityAsString = <Type>Serialization.serialize<Type>AsString(entity); //String version

    Where <Type> is replaced with either Horse, Ocelot, or Wolf, respectively.

    LeatherArmorSerialization
    Leather armor is special. No, not that kind of special... Leather armor can have colors. And because of that, that color has to be saved as well.

    To serialize LeatherArmorMeta:
    Code:java
    1. //'items' is an ItemStack
    2. //The Util class is a class included with TacoSerialization
    3. if(Util.isLeatherArmor(items.getType)){
    4. LeatherArmorMeta meta = (LeatherArmorMeta) items.getitemMeta();
    5. JSONObject serializedMeta = LeatherArmorSerialization.serializeArmor(meta); //JSONObject version
    6. String metaAsString = LeatherArmorSerialization.serializeArmorAsString(meta); //String version
    7. }


    PlayerSerialization
    When players are serialized, their Ender Chest, Inventory (including armor) and stats (health, food, etc) are saved.

    To serialize a Player:
    Code:java
    1. //'player' is a Player
    2. JSONObject serializedPlayer = PlayerSerialization.serializePlayer(player); //JSONOBject version
    3. String playerAsString = PlayerSerialization.serializePlayerAsString(player); //String version


    PotionEffectSerialization
    PotionEffects are serialized similar to enchantments

    To serialize PotionEffects:
    Code:java
    1. //'effects' is a Collection<PotionEffect>
    2. String effectCode = PotionEffectSerialization.serializeEffects(effects);


    To get a Collection of PotionEffects from an effect code:
    Code:java
    1. //'effectCode' is a String representing potion effects
    2. Collection<PotionEffect> = PotionEffectSerialization.getPotionEffects(effectCode);


    SkullSerialization
    Skull serialization, like book serialization and leather armor serilaization, requires the ItemMeta to be cast first.

    To serialize a Skull:
    Code:java
    1. //the items below this line can be retrieved in any way
    2. ItemStack items = new ItemStack(Material.SKULL_ITEM); //only works with SKULL_ITEM
    3. SkullMeta meta = (SkullMeta) items.getItemMeta();
    4. JSONObject serializedMeta = SkullSerialization.serializeSkull(meta); //JSONObject version
    5. String metaAsString = SkullSerialization.serializeSkullAsString(meta); //String version



    This tutorial does not cover all the methods you can use. You can download the source from GitHub, or you can view the javadocs.

    Placeholder...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  2. Offline

    epicfacecreeper

    Your TacoSerialization link just goes to about:blank.
     
  3. Offline

    KILL3RTACO

    Fixed.
     
  4. Offline

    Jalau

    Can you add support for armor, lores, enchantments etc that a mob can wear? And CustomMobNames? So basicly everything a mob can have?
     
  5. Offline

    Ultimate_n00b


    As you can see here it does have mob support.
     
  6. Offline

    Jalau

    Yeah but does it support armors etc that a mob can have equiped?
    So that it will spawn it with the same armor and drop stats etc if i save it and spawn it again? Mobs like in Hypixel maps!
     
  7. Offline

    Ultimate_n00b

    It isn't that hard to serialize yourself (AKA Taco's doesn't have it).

    If you want an idea on how to serialize things, go ahead and look here.
    While you may not be able to use it directly, you could use it to store each item in an .yml file.
     
  8. Offline

    Deleted user

    KILL3RTACO

    Any idea what this Stack Trace is tryna say?

    Code:
    [14:56:34 WARN]: java.lang.NullPointerException
    [14:56:34 WARN]:        at me.blahberrys.savekit.handlers.KitHandler.loadKit(Kit
    Handler.java:93)
    [14:56:34 WARN]:        at me.blahberrys.savekit.Menus$1.onOptionClick(Menus.jav
    a:72)
    [14:56:34 WARN]:        at me.blahberrys.savekit.util.IconMenu.onInventoryClick(
    IconMenu.java:69)
    [14:56:34 WARN]:        at sun.reflect.GeneratedMethodAccessor3.invoke(Unknown S
    ource)
    [14:56:34 WARN]:        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unkno
    wn Source)
    [14:56:34 WARN]:        at java.lang.reflect.Method.invoke(Unknown Source)
    [14:56:34 WARN]:        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(Jav
    aPluginLoader.java:292)
    [14:56:34 WARN]:        at org.bukkit.plugin.RegisteredListener.callEvent(Regist
    eredListener.java:62)
    [14:56:34 WARN]:        at org.bukkit.plugin.SimplePluginManager.fireEvent(Simpl
    ePluginManager.java:501)
    [14:56:34 WARN]:        at org.bukkit.plugin.SimplePluginManager.callEvent(Simpl
    ePluginManager.java:486)
    [14:56:34 WARN]:        at net.minecraft.server.v1_7_R3.PlayerConnection.a(Playe
    rConnection.java:1363)
    [14:56:34 WARN]:        at net.minecraft.server.v1_7_R3.PacketPlayInWindowClick.
    a(SourceFile:32)
    [14:56:34 WARN]:        at net.minecraft.server.v1_7_R3.PacketPlayInWindowClick.
    handle(SourceFile:10)
    [14:56:34 WARN]:        at net.minecraft.server.v1_7_R3.NetworkManager.a(Network
    Manager.java:157)
    [14:56:34 WARN]:        at net.minecraft.server.v1_7_R3.ServerConnection.c(Sourc
    eFile:134)
    [14:56:34 WARN]:        at net.minecraft.server.v1_7_R3.MinecraftServer.v(Minecr
    aftServer.java:667)
    [14:56:34 WARN]:        at net.minecraft.server.v1_7_R3.DedicatedServer.v(Dedica
    tedServer.java:260)
    [14:56:34 WARN]:        at net.minecraft.server.v1_7_R3.MinecraftServer.u(Minecr
    aftServer.java:558)
    [14:56:34 WARN]:        at net.minecraft.server.v1_7_R3.MinecraftServer.run(Mine
    craftServer.java:469)
    [14:56:34 WARN]:        at net.minecraft.server.v1_7_R3.ThreadServerApplication.
    run(SourceFile:628)
    
     
  9. Offline

    chasechocolate

    Eballer48 NPE at KitHandler.java line 93
     
Thread Status:
Not open for further replies.

Share This Page