-
Notifications
You must be signed in to change notification settings - Fork 3
Add stats to your relics
ForgottenArbiter edited this page Jun 6, 2020
·
1 revision
The Relic Stats mod supports displaying stats for custom relics. In order to add custom stats to your relic, make sure it implements the following methods:
// The string which is displayed in the stats window (uses Default StS formatting, like NL for a line break)
public String getStatsDescription() {}
// The string which is displayed if extended stats are turned on (per combat, per turn)
public String getExtendedStatsDescription(int totalCombats, int totalTurns) {}
// Called to reset your relic's stats for a new run
public void resetStats() {}
// Returns a JSON object that will save your stats
public JsonElement onSaveStats() {}
// Loads the stats from the object returned by onSaveStats()
public void onLoadStats(JsonElement jsonElement) {}
As long as your relic is registered with BaseMod.addRelic
or BaseMod.addRelicToCustomPool
, and implements the above five methods, its stats should display in game if Relic Stats is loaded, without needing Relic Stats as a dependency.
Due to the implementation of Relic Stats, methods will be called on the instance that is passed to BaseMod.addRelic
or BaseMod.addRelicToCustomPool
. If you want the stats to display correctly, make sure that instance is always updated with the correct stats, or all instances share the same stats. See an example below.
public class MyRelic extends CustomRelic {
HashMap<String, Integer> stats = new HashMap<>();
private static final String ENERGY_STAT = "Energy obtained: ";
private static final String PER_COMBAT_STRING = " NL Per combat: ";
private static final String PER_TURN_STRING = " NL Per turn: ";
public MyRelic() {
super("OP Relic", "artOfWar.png", RelicTier.RARE, LandingSound.HEAVY);
resetStats();
}
@Override
public String getUpdatedDescription() {
return DESCRIPTIONS[0];
}
public String getStatsDescription() {
return ENERGY_STAT + stats.get(ENERGY_STAT);
}
public String getExtendedStatsDescription(int totalCombats, int totalTurns) {
// You would just return getStatsDescription() if you don't want to display per-combat and per-turn stats
StringBuilder builder = new StringBuilder();
builder.append(getStatsDescription());
float stat = (float)stats.get(ENERGY_STAT);
// Relic Stats truncates these extended stats to 3 decimal places, so we do the same
DecimalFormat perTurnFormat = new DecimalFormat("#.###");
builder.append(PER_TURN_STRING);
builder.append(perTurnFormat.format(stat / Math.max(totalTurns, 1)));
builder.append(PER_COMBAT_STRING);
builder.append(perTurnFormat.format(stat / Math.max(totalCombats, 1)));
return builder.toString();
}
public void resetStats() {
stats.put(ENERGY_STAT, 0);
}
public JsonElement onSaveStats() {
// An array makes more sense if you want to store more than one stat
Gson gson = new Gson();
ArrayList<Integer> statsToSave = new ArrayList<>();
statsToSave.add(stats.get(ENERGY_STAT));
return gson.toJsonTree(statsToSave);
}
public void onLoadStats(JsonElement jsonElement) {
if (jsonElement != null) {
JsonArray jsonArray = jsonElement.getAsJsonArray();
stats.put(ENERGY_STAT, jsonArray.get(0).getAsInt());
} else {
resetStats();
}
}
@Override
public void onCardDraw(AbstractCard card) {
flash();
addToBot(new RelicAboveCreatureAction(AbstractDungeon.player, this));
addToBot(new GainEnergyAction(1));
stats.put(ENERGY_STAT, stats.get(ENERGY_STAT) + 1);
}
@Override
public AbstractRelic makeCopy() {
// Relic Stats will always query the stats from the instance passed to BaseMod.addRelic()
// Therefore, we make sure all copies share the same stats by copying the HashMap.
MyRelic newRelic = new MyRelic();
newRelic.stats = this.stats;
return newRelic;
}
}