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 */
017package net.tridentsdk.server.data.block;
018
019import net.tridentsdk.base.Block;
020import net.tridentsdk.base.Substance;
021import net.tridentsdk.base.SubstanceColor;
022import net.tridentsdk.meta.block.ColorMeta;
023import net.tridentsdk.meta.component.Meta;
024import net.tridentsdk.meta.component.MetaCollection;
025
026/**
027 * Represents data held by a wool block
028 *
029 * @author The TridentSDK Team
030 */
031public class ColorMetaImpl implements ColorMeta {
032    private volatile SubstanceColor color = SubstanceColor.WHITE;
033
034    @Override
035    public void setColor(SubstanceColor color) {
036        this.color = color;
037    }
038
039    @Override
040    public SubstanceColor color() {
041        return color;
042    }
043
044    @Override
045    public Substance[] applyTo(MetaCollection collection) {
046        collection.putIfAbsent(ColorMeta.class, this);
047        return new Substance[]{Substance.WOOL, Substance.STAINED_GLASS, Substance.STAINED_GLASS_PANE, Substance.STAINED_CLAY};
048    }
049
050    @Override
051    public Meta<Block> decode(Block instance, float yaw, byte direction, byte cx, byte cy, byte cz, short damageValue) {
052        setColor(SubstanceColor.of((byte) damageValue));
053        return this;
054    }
055
056    @Override
057    public byte encode() {
058        return (byte) color.asInt();
059    }
060
061    @Override
062    public Meta<Block> make() {
063        return new ColorMetaImpl();
064    }
065}