com.tera.gapi.spawn.template.CSpawnData.java Source code

Java tutorial

Introduction

Here is the source code for com.tera.gapi.spawn.template.CSpawnData.java

Source

/**
 * This file is part of tera-api.
 *
 * tera-api 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 3 of the License, or
 * (at your option) any later version.
    
 * tera-api 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 tera-api.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.tera.gapi.spawn.template;

import java.util.Collection;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;

/**
 * @author ATracer
 * @since 0.1.0
 */
public class CSpawnData implements SpawnData {

    private static final Logger log = LoggerFactory.getLogger(CSpawnData.class);

    private Multimap<Integer, SpawnTemplate> spawnTemplates = Multimaps
            .synchronizedSetMultimap(HashMultimap.<Integer, SpawnTemplate>create());

    private Multimap<Integer, SpawnTemplate> worldMapSpawns = Multimaps
            .synchronizedSetMultimap(HashMultimap.<Integer, SpawnTemplate>create());

    @Override
    public SpawnTemplate getObject(int npcId) {
        Collection<SpawnTemplate> spawns = spawnTemplates.get(npcId);
        return spawns.size() != 0 ? Iterables.get(spawns, 0) : null;
    }

    @Override
    public Collection<SpawnTemplate> getSpawnsForMap(int mapId) {
        return worldMapSpawns.get(mapId);
    }

    @Override
    public int size() {
        return spawnTemplates.size();
    }

    @Override
    public Collection<SpawnTemplate> values() {
        return spawnTemplates.values();
    }

    /**
     * @param templates
     */
    public CSpawnData(List<? extends SpawnTemplate> templates) {
        for (SpawnTemplate template : templates) {
            spawnTemplates.put(template.getNpcId(), template);
        }
        for (SpawnTemplate template : templates) {
            worldMapSpawns.put(template.getMapId(), template);
        }
        printWorldMapSpawnStatistics();
    }

    @Override
    public SpawnTemplate getTemplate(int npcId) {
        return getObject(npcId);
    }

    private void printWorldMapSpawnStatistics() {
        for (Integer mapId : worldMapSpawns.keys()) {
            log.info("World map: {}, spawn count {}", mapId, worldMapSpawns.get(mapId).size());
        }
    }

}