/*
 * Trident - A Multithreaded Server Alternative
 * Copyright 2014 The TridentSDK Team
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.tridentsdk.meta.nbt;

import com.google.common.base.Charsets;
import net.tridentsdk.util.TridentLogger;

import java.io.DataOutput;
import java.io.IOException;
import java.util.List;

/**
 * @author The TridentSDK Team
 */
public class NBTEncoder {
    final DataOutput output;

    public NBTEncoder(DataOutput output) {
        this.output = output;
    }

    public void encode(CompoundTag tag) throws NBTException {
        try {
            this.writeTag(tag);
        } catch (IOException e) {
            TridentLogger.error(new NBTException("IO Error encoding the NBT Data", e));
        }
    }

    private void writeCompoundTag(CompoundTag tag) throws IOException {
        for (NBTTag inner : tag.listTags()) {
            this.writeTag(inner);
        }
        //Write Tag_End to signify end of Compound
        this.output.writeByte(TagType.END.id());
    }

    private void writeListTag(ListTag tag) throws IOException {
        //Write inner tag-type id
        this.output.writeByte(tag.getInnerType().id());

        List<NBTTag> innerTags = tag.listTags();
        this.output.writeInt(innerTags.size());

        for (NBTTag inner : innerTags) {
            this.writeTag(inner);
        }
    }

    private void writeTag(NBTTag tag) throws IOException {
        this.output.writeByte(tag.type().id());

        if (tag.hasName()) {
            this.writeString(tag.name());
        }

        switch (tag.type()) {
            case BYTE:
                this.output.writeByte((int) tag.asType(ByteTag.class).value());
                break;

            case SHORT:
                this.output.writeShort((int) tag.asType(ShortTag.class).value());
                break;

            case INT:
                this.output.writeInt(tag.asType(IntTag.class).value());
                break;

            case LONG:
                this.output.writeLong(tag.asType(LongTag.class).value());
                break;

            case FLOAT:
                this.output.writeFloat(tag.asType(FloatTag.class).value());
                break;

            case DOUBLE:
                this.output.writeDouble(tag.asType(DoubleTag.class).value());
                break;

            case BYTE_ARRAY:
                byte[] barray = tag.asType(ByteArrayTag.class).value();
                this.output.writeInt(barray.length);
                this.output.write(barray);
                break;

            case STRING:
                this.writeString(tag.asType(StringTag.class).value());
                break;

            case LIST:
                this.writeListTag(tag.asType(ListTag.class));
                break;

            case COMPOUND:
                this.writeCompoundTag(tag.asType(CompoundTag.class));
                break;

            case INT_ARRAY:
                int[] iarray = tag.asType(IntArrayTag.class).value();
                this.output.writeInt(iarray.length);
                for (int anIarray : iarray) {
                    this.output.writeInt(anIarray);
                }
                break;

            default:
                //Shouldn't/Can't happen
                break;
        }
    }

    private void writeString(String s) throws IOException {
        this.output.writeShort(s.length());
        this.output.write(s.getBytes(Charsets.UTF_8));
    }
}

@@ -1780,16 +1780,20 @@
 s.size()
+ - 1
 );%0A%0A    
@@ -1852,32 +1852,46 @@
 s.writeTag(inner
+, false, false
 );%0A        %7D%0A   
@@ -1886,32 +1886,132 @@
        %7D%0A    %7D%0A%0A
+    public void writeTag(NBTTag tag) throws IOException %7B%0A        writeTag(tag, true, true);%0A    %7D%0A%0A
     private void
@@ -2022,32 +2022,58 @@
 teTag(NBTTag tag
+, boolean name, boolean id
 ) throws IOExcep
@@ -2071,32 +2071,54 @@
 s IOException %7B%0A
+        if (id) %7B%0A    
         this.out
@@ -2140,32 +2140,42 @@
 ag.type().id());
+%0A        %7D
 %0A%0A        if (ta
@@ -2185,16 +2185,24 @@
 asName()
+ && name
 ) %7B%0A    
@@ -2234,24 +2234,144 @@
 tag.name());
+%0A%0A            if(tag.name.equals(%22ChunkSection%22))%0A                TridentLogger.log(%22Wrote ChunkSection tag with name%22);
 %0A        %7D%0A%0A

Fix world saving