Package com.kellerkindt.scs.internals

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

/**
* ShowCaseStandalone
* Copyright (C) 2012 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.util.UUID;

import net.minecraft.server.v1_4_R1.NBTTagCompound;
import net.minecraft.server.v1_4_R1.NBTTagList;
import net.minecraft.server.v1_4_R1.NBTTagString;

@Deprecated
public class NBTStorage extends Storage {
 
  private static final String tagStringAuthor    = "author";
  private static final String tagStringTitle    = "title";
  private static final String tagStringPages    = "pages";
  private static final String tagStringPagePref  = "page-";
 
  private static final String tagIntegerPagesSize = "pages";
 
//  private static final String STRING_NBT_BINARY  = "nbt-tag";


  private NBTTagCompound  tag        = null;
  private Storage      storage      = this;
 
  public NBTStorage (NBTTagCompound tag, int version, UUID uuid) {
    super(version, uuid);
   
    this.tag  = tag;
   
    saveNBT(storage, tag);
  }
 
  public NBTStorage (Storage storage) {
    super(storage);
   
    this.tag  = loadNBT(storage);
  }
 
  /**
   * @return  The saved tag
   */
  public NBTTagCompound getNBTTagCompound () {
    return this.tag;
  }
 

  /**
   * Saves every known NBT-tag to the storage
   * @param nbt
   */
  private void saveNBT (Storage storage, NBTTagCompound nbt) {
    if (nbt == null)
      return;
   
//    ByteArrayOutputStream  baos  = new ByteArrayOutputStream();
//    NBTBase.a(nbt, new DataOutputStream(baos));
//   
//    storage.setString(STRING_NBT_BINARY, baos.toString());
   
    String    author  = nbt.getString  (tagStringAuthor);
    String    title  = nbt.getString  (tagStringTitle);
    NBTTagList  pages  = nbt.getList  (tagStringPages);
   
    if (pages != null) {
      for (int i = 0; i < pages.size(); i++)
        setString(tagStringPagePref+i, ((NBTTagString)pages.get(i)).data);
      setInteger(tagIntegerPagesSize, pages.size());
    }
   
    if (title != null)
      setString(tagStringTitle, title);
   
    if (author != null)
      setString(tagStringAuthor, author);
  }
 
  /**
   * Loads the NBT with every known NBT-tag
   * @return
   */
  private NBTTagCompound loadNBT (Storage storage) {
   
//    String nbtBinary  = storage.getString(STRING_NBT_BINARY);
//   
//
//   
//    ShowCaseStandalone.slog(Level.INFO, "nbtBinary="+nbtBinary);
//   
//    if (nbtBinary != null) {
//     
//      ByteArrayInputStream  bais  = new ByteArrayInputStream(nbtBinary.getBytes());
//      return (NBTTagCompound)NBTBase.b(new DataInputStream(bais));
//    }
   
   
    String   author  = storage.getString  (tagStringAuthor);
    String   title  = storage.getString  (tagStringTitle);
    Integer pSize  = storage.getInteger(tagIntegerPagesSize);
   
    NBTTagCompound compound  = new NBTTagCompound();
   
    if (pSize != null) {
      NBTTagList  list  = new NBTTagList();
      for (int i = 0; i < pSize; i++) {
        String       page  = storage.getString(tagStringPagePref+i);
       
        if (page != null) {
          NBTTagString tag  = new NBTTagString(page);
          tag.data      = page;
          list.add(tag);
        }
      }
     
      compound.set(tagStringPages, list);
    }
   
    if (title != null)
      compound.setString(tagStringTitle, title);
   
    if (author != null)
      compound.setString(tagStringAuthor, author);
   
   
    return compound;
  }
 
  /**
   * @return  The Book-Title or null
   */
  public String getBookTitle () {
    return getString(tagStringTitle);
  }
}
TOP

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

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.