Package com.kellerkindt.scs.internals

Source Code of com.kellerkindt.scs.internals.ConfigurationSerializationStorage

/**
* ShowCaseStandalone
* Copyright (C) 2013 Kellerkindt <copyright at kellerkindt.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.kellerkindt.scs.internals;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;

import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.configuration.serialization.ConfigurationSerialization;
import org.bukkit.inventory.meta.ItemMeta;

/**
* This class allows to save and load ItemMeta data in clear XML-text
* @author kellerkindt <michael at kellerkindt.com>
*/
public class ConfigurationSerializationStorage extends Storage {
 
 
  private ConfigurationSerializable value;
 
  public ConfigurationSerializationStorage (int storageVersion, UUID uuid, ConfigurationSerializable value) {
    super(storageVersion, uuid);

    this.value  = value;
    this.saveItemMeta();
  }
 
  public ConfigurationSerializationStorage (Storage storage) {
    super(storage);
  }

  /**
   * @return The ConfigurationSerializable that is stored in this Storage
   */
  public ConfigurationSerializable getConfigurationSerializable () {
    // load the meta, if not loaded yet
    if (value == null) {
      loadConfigurationSerializable();
    }
   
    return value;
  }
 
  /**
   * Loads the ItemMeta from this Storage
   */
  public void loadConfigurationSerializable () {
   
    Map<String, Object>   serialized  = new HashMap<String, Object>();
   
    // load all storages
    for (String key : getStorageKeys()) {
      // collect the information
      Storage storage = getStorage(key);
     
      if (storage != null) {
        serialized.put(key, new ConfigurationSerializationStorage(storage).getConfigurationSerializable());
      }
    }
   
    // load all lists
    for (String key : getListKeys()) {
      // collect the information
      List<?> list = getList(key);
     
      if (list != null) {
        serialized.put(key, list);
      }
    }
   
    // load all Doubles
    for (String key : getDoubleKeys()) {
      // collect the information
      Double value = getDouble(key);
     
      if (value != null) {
        serialized.put(key, value);
      }
    }
   
    // load all integers
    for (String key : getIntegerKeys()) {
      // collect the information
      Integer value = getInteger(key);
     
      if (value != null) {
        serialized.put(key, value);
      }
    }
   
    // load all strings
    for (String key : getStringKeys()) {
      // collect the information
      String value = getString(key);
     
      if (value != null) {
        serialized.put(key, value);
      }
    }
   
    // load all Booleans
    for (String key : getBooleanKeys()) {
      // collect the information
      Boolean value = getBoolean(key);
     
      if (value != null) {
        serialized.put(key, value);
      }
    }
   
   
    // load
    value = ConfigurationSerialization.deserializeObject(serialized);
  }
 
  /**
   * Saves the ItemMeta into this Storage
   */
  public void saveItemMeta () {
   
    Map<String, Object> serialized  = new HashMap<String, Object>();
   
   
    serialized.putAll(value.serialize());
   
    // debug output
    for (String key : serialized.keySet()) {
      System.out.println (key+"--->"+serialized.get(key));
    }
   
   
    if (serialized.containsKey("meta-type") && !serialized.containsKey("==")) {
      serialized.put("==", "ItemMeta");
//      serialized.remove("meta-key");
    }
   
    // debug output
    for (String key : serialized.keySet()) {
      System.out.println (key+"--->"+serialized.get(key));
    }
   
   
    try {

      YamlConfiguration   conf = new YamlConfiguration();
      File        dest = new File("test.yaml");
     
//      conf.addDefaults(serialized);
      conf.set("test", value);
//      conf.createSection("test", serialized);
      conf.save(dest);
     
     
      System.out.println ("Deserialized: "+ConfigurationSerialization.deserializeObject(serialized));

      System.out.println ("Saved to " + dest.getAbsolutePath());
     
     
    } catch (IOException ioe) {
      ioe.printStackTrace();
    }
   
   
    for (Entry<String, Object> entry : serialized.entrySet()) {
     
      String key    = entry.getKey();
      Object value  = entry.getValue();

      System.out.println ();
      System.out.println ("key:   "+key);
      System.out.println ("value: "+value);
     
     

     
      // add a storage for the ItemMeta
      if (value instanceof ConfigurationSerializable) {
        System.out.println ("is ConfigurationSerializable");
        setStorage(key, new ConfigurationSerializationStorage(getVersion(), UUID.randomUUID(), (ConfigurationSerializable)value));
      }
     
     
     
     
      else if (value instanceof List) {
        List<Object> list = new ArrayList<Object>();
       
        for (Object o : (List<?>)value) {
          if (o instanceof ConfigurationSerializable) {
            list.add(new ConfigurationSerializationStorage(getVersion(), UUID.randomUUID(), (ConfigurationSerializable)o));
          } else {
            list.add(o);
          }
        }
       
        System.out.println ("is List");
        setList(key, list);
      }
     
      else if (value instanceof Double) {
        setDouble(key, (Double)value);
      }
     
      else if (value instanceof Integer) {
        setInteger(key, (Integer)value);
      }
     
      else if (value instanceof String) {
        setString(key, (String)value);
      }
     
      else if (value instanceof Boolean) {
        setBoolean(key, (Boolean)value);
      }

      // in conflict with ItemMeta
//      // unprobably
//      else if (value instanceof Storage) {
//        setStorage(key, (Storage)value);
//      }
    }
  }
}
TOP

Related Classes of com.kellerkindt.scs.internals.ConfigurationSerializationStorage

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.