Example usage for com.fasterxml.jackson.dataformat.yaml YAMLFactory createParser

List of usage examples for com.fasterxml.jackson.dataformat.yaml YAMLFactory createParser

Introduction

In this page you can find the example usage for com.fasterxml.jackson.dataformat.yaml YAMLFactory createParser.

Prototype

@Override
    public YAMLParser createParser(byte[] data) throws IOException, JsonParseException 

Source Link

Usage

From source file:com.github.fabioticconi.roguelite.systems.BootstrapSystem.java

public void loadBody(final String filename, final EntityEdit edit) throws IOException {
    final YAMLFactory factory = new YAMLFactory();
    final JsonParser parser = factory.createParser(new File(filename));

    int str = 0, agi = 0, con = 0, skin = 0, sight = 0;
    boolean herbivore = false, carnivore = false;

    parser.nextToken(); // START_OBJECT

    while (parser.nextToken() != null) {
        final String name = parser.getCurrentName();

        if (name == null)
            break;

        parser.nextToken(); // get in value

        System.out.println(name);

        if (name.equals("strength")) {
            str = parser.getIntValue();/*from w ww  .  j  a  v  a  2  s  .c o m*/
            edit.create(Strength.class).value = Util.ensureRange(str, -2, 2);
        } else if (name.equals("agility")) {
            agi = parser.getIntValue();
            edit.create(Agility.class).value = Util.ensureRange(agi, -2, 2);
        } else if (name.equals("constitution")) {
            con = parser.getIntValue();
            edit.create(Constitution.class).value = Util.ensureRange(con, -2, 2);
        } else if (name.equals("skin")) {
            skin = parser.getIntValue();
            edit.create(Skin.class).value = Util.ensureRange(skin, -2, 2);
        } else if (name.equals("sight")) {
            sight = parser.getIntValue();
            edit.create(Sight.class).value = Util.ensureRange(sight, 1, 18);
        } else if (name.equals("herbivore")) {
            herbivore = parser.getBooleanValue();

            if (herbivore)
                edit.create(Herbivore.class);
        } else if (name.equals("carnivore")) {
            carnivore = parser.getBooleanValue();

            if (carnivore)
                edit.create(Carnivore.class);
        }
    }

    // TODO check if neither herbivore nor carnivore? player is currently as such, for testing

    // Secondary Attributes
    final int size = Math.round((con - agi) / 2f);
    edit.create(Size.class).value = size;
    edit.create(Stamina.class).value = 5 + str + con;
    edit.create(Speed.class).value = (con - str - agi + 6) / 12f;

    // Tertiary Attributes
    edit.create(Hunger.class).value = (size / 2f) + 2f;
}