Wrote this really quick for someone as a favor, figured I'd put it here for anyone who wants it
The ratio was 1:10000 for Platinum:Blood Money. You can configure this by changing the "exchangeRate" variable in both sections.
Be sure to do your own testing as this was written very quickly and every edge-case/validation may not be present. Worked as intended when I tested it, though!
This is done through dialogue, by the way. Make sure it's within the "new OptionsDialogue(" of an NPC's talk() method.
Code:
new Option("I'd like to exchange my Blood Money for Platinum Tokens", () -> {
if (!player.getInventory().hasId(BLOOD_MONEY)) { // If they don't have any blood money, return
player.dialogue(new NPCDialogue(npc, "You don't have any Blood Money to exchange."));
return;
}
int exchangeRate = 10000;
int bloodMoney = player.getInventory().findItem(BLOOD_MONEY).getAmount();
long platTokens = 0;
if (player.getInventory().hasId(PLATINUM_TOKEN)) // make platTokens equal to how many they have
platTokens = player.getInventory().findItem(PLATINUM_TOKEN).getAmount();
int totalBloodExchanges = Math.floorDiv(bloodMoney, exchangeRate);
if (totalBloodExchanges > 0) { // Make sure they have enough blood money to complete the exchange
if (platTokens + totalBloodExchanges <= Integer.MAX_VALUE) { // Make sure they can accept the amount of tokens
int freeSlots = player.getInventory().getFreeSlots();
if ((player.getInventory().hasId(PLATINUM_TOKEN) && player.getInventory().hasId(BLOOD_MONEY)) || freeSlots > 0) { // If they already have plat tokens and blood money, don't need to check freeSlots
player.getInventory().remove(BLOOD_MONEY, totalBloodExchanges * exchangeRate);
player.getInventory().add(PLATINUM_TOKEN, totalBloodExchanges);
} else {
player.dialogue(new NPCDialogue(npc, "You don't have enough inventory space."));
return;
}
}
else {
player.dialogue(new NPCDialogue(npc, "You're carrying too many Platinum Tokens to make this transaction."));
return;
}
}
else {
player.dialogue(new NPCDialogue(npc, "You need at least " + exchangeRate + " Blood Money to exchange for Platinum Tokens."));
return;
}
player.dialogue(new ItemDialogue().two(BLOOD_MONEY, PLATINUM_TOKEN, "The bank exchanges your blood money for platinum tokens."));
}),
new Option("I'd like to exchange my Platinum Tokens for Blood Money", () -> {
if (!player.getInventory().hasId(PLATINUM_TOKEN)) { // If they don't have any blood money, return
player.dialogue(new NPCDialogue(npc, "You don't have any Platinum Tokens to exchange."));
return;
}
int exchangeRate = 10000;
int platTokens = player.getInventory().findItem(PLATINUM_TOKEN).getAmount();
long bloodMoney = 0;
if (player.getInventory().hasId(BLOOD_MONEY)) // make bloodMoney equal to how many they have
bloodMoney = player.getInventory().findItem(BLOOD_MONEY).getAmount();
if (bloodMoney + ((long) platTokens * exchangeRate) <= Integer.MAX_VALUE) { // Make sure they can accept the amount of tokens
int freeSlots = player.getInventory().getFreeSlots();
if ((player.getInventory().hasId(PLATINUM_TOKEN) && player.getInventory().hasId(BLOOD_MONEY)) || freeSlots > 0) { // If they already have plat tokens and blood money, don't need to check freeSlots
player.getInventory().remove(PLATINUM_TOKEN, platTokens);
player.getInventory().add(BLOOD_MONEY, platTokens * exchangeRate);
} else {
player.dialogue(new NPCDialogue(npc, "You don't have enough inventory space."));
return;
}
}
else {
player.dialogue(new NPCDialogue(npc, "You won't be able to hold the amount of Blood Money you'd receive after completing this transaction"));
return;
}
player.dialogue(new ItemDialogue().two(BLOOD_MONEY, PLATINUM_TOKEN, "The bank exchanges your Platinum Tokens for Blood Money."));
}),