001/*
002 * Trident - A Multithreaded Server Alternative
003 * Copyright 2014 The TridentSDK Team
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *    http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package net.tridentsdk.meta.nbt;
019
020/**
021 * @author The TridentSDK Team
022 * @since 0.3-alpha-DP
023 */
024public class ListTagBuilder<B> {
025    private final TagContainer parent;
026    private final ListTag current;
027    private final B parentBuilder;
028    private final TagType type;
029
030    public ListTagBuilder(String name, TagContainer parent, B parentBuilder, TagType type) {
031        this.parent = parent;
032        this.parentBuilder = parentBuilder;
033        this.type = type;
034        this.current = new ListTag(name, type);
035        parent.addTag(this.current);
036    }
037
038    public CompoundTagBuilder<ListTagBuilder<B>> beginCompoundTag(String name) {
039        return new CompoundTagBuilder<>(name, this.current, this);
040    }
041
042    public B endListTag() {
043        return this.parentBuilder;
044    }
045
046    public ListTagBuilder<ListTagBuilder<B>> beginListTag(String name, TagType type) {
047        return new ListTagBuilder<>(name, this.current, this, type);
048    }
049
050    public ListTagBuilder<B> tag(Object value) {
051        switch (this.type) {
052            case BYTE:
053                if (value instanceof Byte) {
054                    this.current.addTag(new ByteTag(null).setValue((byte) value));
055                }
056                break;
057            case SHORT:
058                if (value instanceof Short) {
059                    this.current.addTag(new ShortTag(null).setValue((short) value));
060                }
061                break;
062            case INT:
063                if (value instanceof Integer) {
064                    this.current.addTag(new IntTag(null).setValue((int) value));
065                }
066                break;
067            case LONG:
068                if (value instanceof Long) {
069                    this.current.addTag(new LongTag(null).setValue((long) value));
070                }
071                break;
072            case FLOAT:
073                if (value instanceof Float) {
074                    this.current.addTag(new FloatTag(null).setValue((float) value));
075                }
076                break;
077            case DOUBLE:
078                if (value instanceof Double) {
079                    this.current.addTag(new DoubleTag(null).setValue((double) value));
080                }
081                break;
082            case BYTE_ARRAY:
083                if (value instanceof byte[]) {
084                    this.current.addTag(new ByteArrayTag(null).setValue((byte[]) value));
085                }
086                break;
087            case STRING:
088                if (value instanceof String) {
089                    this.current.addTag(new StringTag(null).setValue((String) value));
090                }
091                break;
092            case INT_ARRAY:
093                if (value instanceof int[]) {
094                    this.current.addTag(new IntArrayTag(null).setValue((int[]) value));
095                }
096                break;
097            default:
098                break;
099        }
100
101        return this;
102    }
103}