Starsector API
Loading...
Searching...
No Matches
RemoveCommodity.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.campaign.rulecmd;
2
3import java.util.List;
4import java.util.Map;
5
6import com.fs.starfarer.api.Global;
7import com.fs.starfarer.api.campaign.InteractionDialogAPI;
8import com.fs.starfarer.api.campaign.rules.MemoryAPI;
9import com.fs.starfarer.api.util.Misc.Token;
10import com.fs.starfarer.api.util.MutableValue;
11
15public class RemoveCommodity extends BaseCommandPlugin {
16
17 public boolean execute(String ruleId, InteractionDialogAPI dialog, List<Token> params, Map<String, MemoryAPI> memoryMap) {
18 if (dialog == null) return false;
19
20 String commodityId = params.get(0).getString(memoryMap);
21 float quantity = 0;
22 int next = 2;
23 if (params.get(1).isOperator()) {
24 quantity = -1 * params.get(2).getFloat(memoryMap);
25 next = 3;
26 } else {
27 quantity = params.get(1).getFloat(memoryMap);
28 }
29
30 quantity = -quantity;
31
32 boolean withText = Math.abs(quantity) >= 1;
33 if (dialog != null && params.size() >= next + 1) {
34 withText = params.get(next).getBoolean(memoryMap) && withText;
35 }
36
37 if (commodityId.equals("credits")) {
38 MutableValue credits = Global.getSector().getPlayerFleet().getCargo().getCredits();
39 if (quantity > 0) {
40 credits.add(quantity);
41 if (withText) {
42 AddRemoveCommodity.addCreditsGainText((int) quantity, dialog.getTextPanel());
43 }
44 } else {
45 credits.subtract(Math.abs(quantity));
46 if (credits.get() < 0) credits.set(0);
47 if (withText) {
48 AddRemoveCommodity.addCreditsLossText((int) Math.abs(quantity), dialog.getTextPanel());
49 }
50 }
51 } else {
52// if (quantity < 0) {
53// quantity = -1f * Math.max(Math.abs(quantity), Global.getSector().getPlayerFleet().getCargo().getCommodityQuantity(commodityId));
54// }
55 if (quantity > 0) {
56 Global.getSector().getPlayerFleet().getCargo().addCommodity(commodityId, quantity);
57 if (withText) {
58 AddRemoveCommodity.addCommodityGainText(commodityId, (int) quantity, dialog.getTextPanel());
59 }
60 } else {
61 Global.getSector().getPlayerFleet().getCargo().removeCommodity(commodityId, Math.abs(quantity));
62 if (withText) {
63 AddRemoveCommodity.addCommodityLossText(commodityId, (int) Math.abs(quantity), dialog.getTextPanel());
64 }
65 }
66 }
67
68 if (!"credits".equals(commodityId)) {
69 // update $supplies, $fuel, etc if relevant
71 }
72
73 return true;
74 }
75
76
77}
78
79
80
static SectorAPI getSector()
Definition Global.java:59
static void addCommodityLossText(String commodityId, int quantity, TextPanelAPI text)
static void addCreditsGainText(int credits, TextPanelAPI text)
static void addCommodityGainText(String commodityId, int quantity, TextPanelAPI text)
static void addCreditsLossText(int credits, TextPanelAPI text)
boolean execute(String ruleId, InteractionDialogAPI dialog, List< Token > params, Map< String, MemoryAPI > memoryMap)