Java tutorial
/* * CrimsonGlow is an adult computer roleplaying game with spanking content. * Copyright (C) 2015 Andrew Russell * * This program 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. * * This program 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 this program. If not, see <http://www.gnu.org/licenses/>. */ package com.spankingrpgs.scarletmoon.loader; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.spankingrpgs.model.GameState; import com.spankingrpgs.model.items.Item; import com.spankingrpgs.model.items.ItemFactory; import com.spankingrpgs.model.loader.Loader; import com.spankingrpgs.util.EnumUtils; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; /** * Loads items. Items follow the following JSON format: * { * "name": "skirt", * "description": "A pretty skirt", * "equip slots": ["lower body"], * "risque": 1, * "type": "skirt" * } * * and the equivalent YAML format: * * * name: skirt * description: A pretty skirt * equip slots: [lower body] * risque: 1 * type: skirt * * Note that everything after "description" are optional. */ public class ItemLoader implements Loader { private static final Logger LOG = Logger.getLogger(ItemLoader.class.getName()); private static final ObjectMapper JSON_PARSER = new ObjectMapper(); private final ItemFactory itemFactory; /** * Builds an object that can hydrate a String in a format supported by Jackson into an {@link Item}. * * @param itemFactory The factory to use to build items * @param parser The parser to use to parse the String containing item information */ public ItemLoader(ItemFactory itemFactory, ObjectMapper parser) { this.itemFactory = itemFactory; } /** * Builds an object that can hydrate a JSON String in the format described in the class docs into an {@link Item}. * * @param itemFactory The factory to use to build items */ public ItemLoader(ItemFactory itemFactory) { this.itemFactory = itemFactory; } @Override public void load(Collection<String> data, GameState state) { data.stream().forEach(datum -> loadItem(datum, state)); } /** * Hydrates the passed item data string into an item, and loads it into the specified state. * * @param datum The item data to be hydrated * @param state The state to load the hydrated item into */ private void loadItem(String datum, GameState state) { LOG.fine(String.format("Loading item: %s", datum)); try { JsonNode jsonData = JSON_PARSER.readValue(datum, JsonNode.class); Item item = itemFactory.create(jsonData.get("name").asText(), jsonData.get("description").asText(), jsonData.get("type").asText(), hydrateEquipSlots(jsonData.get("equip slots")), jsonData.get("risque").asInt()); state.addItem(item.getName(), item); } catch (IOException e) { LOG.log(Level.SEVERE, e.getMessage()); throw new IllegalArgumentException(e); } } private List<String> hydrateEquipSlots(JsonNode equipSlots) { if (equipSlots == null) { return Collections.emptyList(); } List<String> equipSlotNames = new ArrayList<>(); Iterator<JsonNode> equipSlotsIterator = equipSlots.elements(); while (equipSlotsIterator.hasNext()) { equipSlotNames .add(EnumUtils.nameToEnum(Collections.singleton(equipSlotsIterator.next().asText())).get(0)); } return equipSlotNames; } }