Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.sixteencolorgames.rpg.pathfindertools.misc; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.sixteencolorgames.rpg.pathfindertools.PathfinderTools; import com.sixteencolorgames.rpg.pathfindertools.enums.Ability; import com.sixteencolorgames.rpg.pathfindertools.enums.Skill; import java.util.ArrayList; import java.util.Map.Entry; /** * Holds the various races and alternate traits for them in static. * * Holds a specific race and alternate traits for a character. * * @author oa10712 */ public class Race { public static ArrayList<JsonObject> races = new ArrayList();//All races built by the parser public static ArrayList<JsonObject> altTraits = new ArrayList();//All alternate traits built by the parser private JsonObject main = null; private JsonObject alternate = null; private Ability choice = null;//Will be used for rraces that get to pick an ability to add a +2 to private String replacedTrait; public final static JsonObject DEF = new JsonObject(); /** * setup the default race/alternate trait object */ static { JsonObject deftrt = new JsonObject(); deftrt.addProperty("title", "Default"); DEF.addProperty("title", "Default"); DEF.add("traits", deftrt); DEF.addProperty("replace", "null"); } /** * Initialize a default race object */ public Race() { main = DEF; alternate = DEF; } /** * Set the base race and clears the alternate traits for this instance. * * @param base */ public void setMain(JsonObject base) { main = base; alternate = DEF; // processTraits(); } /** * Set the alternate racial trait for this instance * * @param alt */ public void setAlternate(JsonObject alt) { alternate = alt; replacedTrait = alternate.get("replace").getAsString(); processTraits(); } /** * Gets the racial ability modifier for a specified ability * * @param a The ability to check * @return the modifier for the specified ability */ public int getScoreMod(Ability a) { if (choice == a) { return 2; } try { return ((JsonObject) main.get("stats")).get(a.getAbbrivation()).getAsInt(); } catch (Exception ex) { return 0; } } /** * Returns the possible alternate traits for the current race * * @return a list of valid alternate traits */ public ArrayList<JsonObject> getAlternates() { ArrayList<JsonObject> ret = new ArrayList(); ret.add(DEF); altTraits.forEach((JsonObject t) -> { try { if (t.get("race").getAsString().equals(main.get("title").getAsString())) { ret.add(t); } } catch (Exception ex) { System.out.println(t); System.out.println(main); ex.printStackTrace(); } }); return ret; } /** * WIP. Gets the race segment of the creature summary * * @return A String containing a segment of the summary pertaining to the * race */ public String getSummary() { String ret = "Race\n"; ret += main.get("title").getAsString() + "\n"; for (JsonObject o : getTraits()) { ret += o.get("title").getAsString() + "\n"; ret += " " + o.get("desc").getAsString() + "\n"; } return ret; } /** * Gets the traits for the current race and replaced traits if an alternate * trait is selected * * @return The final traits after all replacements are completed */ public ArrayList<JsonObject> getTraits() { ArrayList<JsonObject> ret = new ArrayList(); for (JsonElement e : main.get("traits").getAsJsonArray()) { JsonObject o = e.getAsJsonObject(); if (!o.get("title").getAsString().equals(replacedTrait)) { ret.add(o); } } if (alternate != null && alternate != DEF) { ret.add(alternate.get("traits").getAsJsonObject()); } return ret; } /** * Returns the main race choice * * @return */ public JsonObject getMain() { return main; } /** * Returns the alternate trait race choice * * @return */ public JsonObject getAlternate() { return alternate; } public void setChoice(Ability ability) { choice = ability; } private void processTraits() { getTraits().forEach((JsonObject trait) -> { trait.entrySet().forEach((entry) -> { String key = entry.getKey(); if ("skill.extra".equals(key)) { PathfinderTools.instance.charBuild.skills.skillExtra .append(entry.getValue().getAsString() + "\n"); } else if (key.startsWith("skill.")) { Skill skill = Skill.valueOf(key.substring(6).toUpperCase()); PathfinderTools.instance.charBuild.skills.racialMod.get(skill) .setValue(entry.getValue().getAsInt()); } }); }); } }