Solved Retrieving a serialized ItemStack from YAML

Discussion in 'Plugin Development' started by Alshain01, Oct 8, 2013.

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

    Alshain01

    I'm wondering what is the appropriate way to do this. I'm serializing an ItemStack and storing it in YAML. I need to retrieve and deserialize it, but I can only receive in generics which I have trouble with casting. How can I accomplish this without suppressing warnings?

    Below is the constructor where I should be deserializing. The passed in parameter is a direct read from YAML and the tradeItems are a class storage array.

    Code:java
    1. protected Trade(List<Map<?, ?>> items) {
    2. int i = 0;
    3. for(Map<?, ?> m : items) {
    4. tradeItems[i] = ItemStack.deserialize((Map<String, Object>) m);
    5. i++;
    6. }
    7. }[/i]
     
  2. Offline

    Garris0n

    What warnings?
     
  3. Offline

    Alshain01

    Type safety unchecked cast warning on (Map<String, Object>) m

    I'm not incredibly familiar with java generics and how to handle them appropriately. Is there another way to cast that?
     
  4. Offline

    metalhedd

    Not sure if you've already eliminated this possiblity, or just weren't aware.. but ItemStack is ConfigurationSerializable. you can read and write ItemStacks to Config's natively. eg:

    Code:java
    1.  
    2. // set it
    3. getConfig().set("wool", new ItemStack(Material.WOOL));
    4. // get it
    5. ItemStack samewool = getConfig().getItemStack("wool");
    6.  
     
  5. Offline

    Alshain01

    No, I was not aware. That allows me to use my ItemStack contructor that I already have. Thank you very much.
     
Thread Status:
Not open for further replies.

Share This Page