Skip to content

Commit

Permalink
Hotfix + commission check
Browse files Browse the repository at this point in the history
  • Loading branch information
jsbursik committed Dec 22, 2020
1 parent f47c89f commit 0d948ca
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 15 deletions.
Binary file modified jars/Dynamic_Tariffs.jar
Binary file not shown.
5 changes: 3 additions & 2 deletions settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"dt_percents":[30,15,10,5,2,0],

"dt_percents":[30,25,20,15,10,5],
"dt_commission":true,
"dt_commModifier":5,
"dt_whitelist":["ancyra_market","derinkuyu_market","sindria","volturn","cruor",
"nortia","umbra","chicomoztoc","coatl","tigra_city","eventide",
"orthrus","sphinx","asharu","jangala","corvus_IIIa","raesvelg",
Expand Down
12 changes: 9 additions & 3 deletions src/dynamictariffs/DynamicTariffsPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ public void onGameLoad(boolean newGame) {
new TariffUtil();
SettingsUtil.readSettings();
for(String market : SettingsUtil.whitelist){
TariffUtil.modifyTariff(EconUtil.getMarket(market));
if(EconUtil.marketExists(market)){
TariffUtil.modifyTariff(EconUtil.getMarket(market));
}
}
log.info("DynamicTariffs: Loaded");
}
Expand All @@ -31,7 +33,9 @@ public void onGameLoad(boolean newGame) {
@Override
public void beforeGameSave() {
for(String market : SettingsUtil.whitelist){
EconUtil.unmodTariffs(market);
if(EconUtil.marketExists(market)){
EconUtil.unmodTariffs(market);
}
}
log.info("DynamicTariffs: Unmodified all Tariffs");
}
Expand All @@ -42,7 +46,9 @@ public void beforeGameSave() {
@Override
public void afterGameSave() {
for(String market : SettingsUtil.whitelist){
TariffUtil.modifyTariff(EconUtil.getMarket(market));
if(EconUtil.marketExists(market)){
TariffUtil.modifyTariff(EconUtil.getMarket(market));
}
}
log.info("DynamicTariffs: Remodified Tariffs");
}
Expand Down
22 changes: 22 additions & 0 deletions src/dynamictariffs/util/EconUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

import com.fs.starfarer.api.campaign.RepLevel;
import com.fs.starfarer.api.impl.campaign.ids.Factions;
import com.fs.starfarer.api.util.Misc;

import java.util.List;

/*
* This Class is just here to shorthand some common tasks when
Expand Down Expand Up @@ -33,5 +36,24 @@ public static RepLevel getRepLevel(MarketAPI market){
public static void unmodTariffs(String marketID){
getMarket(marketID).getTariff().unmodify("dynamictariffs");
}
/*
* Scaedumar found a flaw, need to check if a market exists first
*/
public static boolean marketExists(String marketID){
MarketAPI market = getMarket(marketID);
List<MarketAPI> markets = Global.getSector().getEconomy().getMarketsCopy();
if(markets.contains(market)){
return true;
} else {
return false;
}
}
/*
* Need to check if the player is commissioned with the given market faction
*/
public static boolean isCommissioned(MarketAPI market){
String faction = market.getFactionId();
return faction.equals(Misc.getCommissionFactionId());
}

}
4 changes: 4 additions & 0 deletions src/dynamictariffs/util/SettingsUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class SettingsUtil {

public static int[] percents = new int[6];
public static ArrayList<String> whitelist = new ArrayList<String>();
public static Boolean commission = false; // States whether or not you want Tariffs to also be modified by commission
public static Integer commModifier = 0; // This will be how much the tariff should change based on commission
/*
* This reads in the settings.json and populates the percents and whitelist arrays
*/
Expand All @@ -27,6 +29,8 @@ public static void readSettings(){
JSONObject modSettings = settings.loadJSON("settings.json", "dynamictariffs");
JSONArray jsonPercents = modSettings.getJSONArray("dt_percents");
JSONArray jsonWhitelist = modSettings.getJSONArray("dt_whitelist");
commission = modSettings.getBoolean("dt_commission");
commModifier = modSettings.getInt("dt_commModifier");
for(int i = 0; i < jsonPercents.length(); i++){
percents[i] = jsonPercents.getInt(i);
}
Expand Down
27 changes: 17 additions & 10 deletions src/dynamictariffs/util/TariffUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public void reportPlayerOpenedMarket(MarketAPI market) {
RepLevel rep = EconUtil.getRepLevel(market);
log.info("DynamicTariffs: " + rep + " " + market.getTariff().getModifiedValue());
}

/*
* This modifies the Tariff of a given Market based on Rep
*/
Expand All @@ -35,22 +34,22 @@ public static void modifyTariff(MarketAPI market){
switch(rep)
{
case SUSPICIOUS:
flat = getOffset(percents[0]);
flat = getOffset(percents[0], market);
break;
case NEUTRAL:
flat = getOffset(percents[1]);
flat = getOffset(percents[1], market);
break;
case FAVORABLE:
flat = getOffset(percents[2]);
flat = getOffset(percents[2], market);
break;
case WELCOMING:
flat = getOffset(percents[3]);
flat = getOffset(percents[3], market);
break;
case FRIENDLY:
flat = getOffset(percents[4]);
flat = getOffset(percents[4], market);
break;
case COOPERATIVE:
flat = getOffset(percents[5]);
flat = getOffset(percents[5], market);
break;
default:
// This is to stop it warning me about the untradeable rep levels
Expand All @@ -59,10 +58,18 @@ public static void modifyTariff(MarketAPI market){
market.getTariff().modifyFlat("dynamictariffs", flat);
}
/*
* This takes a number like 40, subtracts the vanilla tariff
* of 30 to get the offset, then turns it into something like 0.1f
* This takes a number like 40, subtracts the vanilla tariff
* of 30 to get the offset, then turns it into something like 0.1f
* If commissioned it will subtract the commission percent as well
*/
public static float getOffset(float percent) {
public static float getOffset(int percent, MarketAPI market) {
if(SettingsUtil.commission && EconUtil.isCommissioned(market)){
int result = percent - 30 - SettingsUtil.commModifier;
if(result < -30){
return -30 / 100f;
}
return result / 100f;
}
return (percent - 30) / 100f;
}
/*
Expand Down

0 comments on commit 0d948ca

Please sign in to comment.