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.item;
018
019import java.util.List;
020import java.util.Map;
021
022import com.google.common.collect.Maps;
023
024import net.tridentsdk.base.Enchantment;
025import net.tridentsdk.meta.item.EnchantedBookMeta;
026import net.tridentsdk.meta.nbt.NBTField;
027import net.tridentsdk.meta.nbt.NBTSerializable;
028import net.tridentsdk.meta.nbt.TagType;
029
030public class EnchantedBookMetaImpl implements EnchantedBookMeta, NBTSerializable {
031    @NBTField(name = "StoredEnchantments", type = TagType.LIST)
032    private List<NBTEnchantment> enchants;
033
034    @NBTField(name = "RepairCost", type = TagType.STRING)
035    private int repairCost = 0;
036
037    private Map<Enchantment, Short> enchantments = Maps.newConcurrentMap();
038
039    @Override
040    public void process() {
041        enchants.forEach(e -> enchantments.put(Enchantment.fromId(e.id), e.lvl));
042    }
043
044    @Override
045    public Map<Enchantment, Short> storedEnchantments() {
046        return this.enchantments;
047    }
048
049    @Override
050    public int repairCost() {
051        return this.repairCost;
052    }
053
054    protected static class NBTEnchantment implements NBTSerializable {
055        @NBTField(name = "id", type = TagType.SHORT)
056        short id;
057
058        @NBTField(name = "lvl", type = TagType.SHORT)
059        short lvl;
060    }
061}