Starsector API
Loading...
Searching...
No Matches
BaseSpecialItemPlugin.java
Go to the documentation of this file.
1package com.fs.starfarer.api.campaign.impl.items;
2
3import java.awt.Color;
4import java.util.ArrayList;
5import java.util.List;
6import java.util.Random;
7
8import org.json.JSONException;
9
10import com.fs.starfarer.api.Global;
11import com.fs.starfarer.api.campaign.CargoStackAPI;
12import com.fs.starfarer.api.campaign.CargoTransferHandlerAPI;
13import com.fs.starfarer.api.campaign.SpecialItemPlugin;
14import com.fs.starfarer.api.campaign.SpecialItemSpecAPI;
15import com.fs.starfarer.api.campaign.SubmarketPlugin.TransferAction;
16import com.fs.starfarer.api.campaign.econ.MarketAPI;
17import com.fs.starfarer.api.campaign.econ.SubmarketAPI;
18import com.fs.starfarer.api.combat.ShipHullSpecAPI;
19import com.fs.starfarer.api.impl.campaign.ids.Tags;
20import com.fs.starfarer.api.loading.FighterWingSpecAPI;
21import com.fs.starfarer.api.loading.WeaponSpecAPI;
22import com.fs.starfarer.api.ui.LabelAPI;
23import com.fs.starfarer.api.ui.TooltipMakerAPI;
24import com.fs.starfarer.api.util.Highlights;
25import com.fs.starfarer.api.util.Misc;
26
28
31 protected String itemId;
32
33
34 public String getId() {
35 return itemId;
36 }
37 public void setId(String id) {
38 this.itemId = id;
40 }
41 public void init(CargoStackAPI stack) {
42 this.stack = stack;
43 }
44
46
47 }
48
49 public boolean hasRightClickAction() {
50 return false;
51 }
52
54 return true;
55 }
56
57
58 public void createTooltip(TooltipMakerAPI tooltip, boolean expanded, CargoTransferHandlerAPI transferHandler, Object stackSource) {
59 boolean useGray = true;
60 if (getClass() == BaseSpecialItemPlugin.class) {
61 useGray = false;
62 }
63 createTooltip(tooltip, expanded, transferHandler, stackSource, useGray);
64 }
65 public void createTooltip(TooltipMakerAPI tooltip, boolean expanded, CargoTransferHandlerAPI transferHandler, Object stackSource, boolean useGray) {
66 float opad = 10f;
67
68 tooltip.addTitle(getName());
69
70 String design = getDesignType();
71 Misc.addDesignTypePara(tooltip, design, opad);
72
73 if (!spec.getDesc().isEmpty()) {
74 Color c = Misc.getTextColor();
75 if (useGray) c = Misc.getGrayColor();
76 tooltip.addPara(spec.getDesc(), c, opad);
77 }
78
79 if (getClass() == BaseSpecialItemPlugin.class) {
80 addCostLabel(tooltip, opad, transferHandler, stackSource);
81 }
82 }
83
84 public float getTooltipWidth() {
85 return 450;
86 }
87
88 public boolean isTooltipExpandable() {
89 return false;
90 }
91
92 public String getName() {
93 return spec.getName();
94 }
95
96 public int getPrice(MarketAPI market, SubmarketAPI submarket) {
97 if (spec != null) return (int) spec.getBasePrice();
98 return 0;
99 }
100
101 protected float getItemPriceMult() {
102 return Global.getSettings().getFloat("blueprintPriceOriginalItemMult");
103 }
104
105 public void render(float x, float y, float w, float h, float alphaMult, float glowMult, SpecialItemRendererAPI renderer) {
106
107 }
108
109
110
111 protected void addCostLabel(TooltipMakerAPI tooltip, float pad, CargoTransferHandlerAPI transferHandler, Object stackSource) {
112 ItemCostLabelData data = getCostLabelData(stack, transferHandler, stackSource);
113
114 LabelAPI label = tooltip.addPara(data.text, pad);
115 if (data.highlights != null) {
116 label.setHighlight(data.highlights.getText());
117 label.setHighlightColors(data.highlights.getColors());
118 }
119 }
120
121
122 public static class ItemCostLabelData {
123 public String text;
124 public Highlights highlights;
125 }
126
127 protected ItemCostLabelData getCostLabelData(CargoStackAPI stack, CargoTransferHandlerAPI transferHandler, Object stackSource) {
128 String text = "";
129 String highlight = null;
130 Highlights highlights = null;
131 Color highlightColor = Misc.getHighlightColor();
132 TransferAction action = TransferAction.PLAYER_BUY;
133 if (transferHandler != null && stackSource == transferHandler.getManifestOne()) {
134 action = TransferAction.PLAYER_SELL;
135 }
136
137
138 if (action == TransferAction.PLAYER_SELL && stack.getSpecialItemSpecIfSpecial() != null) {
140 if (spec.hasTag(Tags.MISSION_ITEM)) {
141 text = "Can not remove item";
142 highlight = text;
143 highlights = new Highlights();
144 highlights.append(text, Misc.getNegativeHighlightColor());
145
146 ItemCostLabelData data = new ItemCostLabelData();
147 data.text = text;// + ".";
148 data.highlights = highlights;
149
150 return data;
151 }
152 }
153
154
155
156 if (transferHandler != null && transferHandler.getSubmarketTradedWith() != null &&
157 transferHandler.getSubmarketTradedWith().isIllegalOnSubmarket(stack, action)) {
158 highlightColor = Misc.getNegativeHighlightColor();
159 //text = "Illegal to trade on the " + transferHandler.getSubmarketTradedWith().getNameOneLine() + " here";
160 text = transferHandler.getSubmarketTradedWith().getPlugin().getIllegalTransferText(stack, action);
161 highlight = text;
162 highlights = transferHandler.getSubmarketTradedWith().getPlugin().getIllegalTransferTextHighlights(stack, TransferAction.PLAYER_BUY);
163 } else {
164 if (stackSource != null && transferHandler != null && !transferHandler.isNoCost()) {
165 if (stackSource == transferHandler.getManifestOne()) {
166 int cost = (int)transferHandler.computeCurrentSingleItemSellCost(stack);
167 //text = "Sells for: " + Misc.getWithDGS(cost) + " credits per unit";
168 text = "Sells for: " + Misc.getDGSCredits(cost) + " per unit.";
169 highlight = "" + Misc.getDGSCredits(cost);
170 } else {
171 int cost = (int)transferHandler.computeCurrentSingleItemBuyCost(stack);
172 //text = "Price: " + Misc.getWithDGS(cost) + " credits per unit";
173 text = "Price: " + Misc.getDGSCredits(cost) + " per unit.";
174 highlight = "" + Misc.getDGSCredits(cost);
175 }
176 } else {
177 int cost = (int) stack.getBaseValuePerUnit();
178 //float mult = Global.getSettings().getFloat("nonEconItemSellPriceMult");
179 //cost *= mult;
180 //text = "Base value: " + Misc.getWithDGS(cost) + " credits per unit";
181 text = "Base value: " + Misc.getDGSCredits(cost) + " per unit.";
182 highlight = "" + Misc.getDGSCredits(cost);
183 }
184 }
185
186 if (highlights == null) {
187 highlights = new Highlights();
188 highlights.setText(highlight);
189 highlights.setColors(highlightColor);
190 }
191
192 ItemCostLabelData data = new ItemCostLabelData();
193 data.text = text;// + ".";
194 data.highlights = highlights;
195
196 return data;
197 }
198
199 public String resolveDropParamsToSpecificItemData(String params, Random random) throws JSONException {
200 return "";
201 }
202
203
204
205
206 protected static interface BlueprintLister {
207 boolean isKnown(String id);
208 String getNoun(int num);
209 String getName(String id);
210 }
211
212 protected void addShipList(TooltipMakerAPI tooltip, String title, List<String> hulls, int max, float opad) {
213 addBlueprintList(tooltip, title, hulls, max, opad, new BlueprintLister(){
214 public boolean isKnown(String id) {
215 return Global.getSector().getPlayerFaction().knowsShip(id);
216 }
217 public String getNoun(int num) {
218 if (num == 1) return "hull";
219 return "hulls";
220 }
221 public String getName(String id) {
222 ShipHullSpecAPI spec = Global.getSettings().getHullSpec(id);
223 return spec.getNameWithDesignationWithDashClass();
224 }
225 });
226 }
227
228 protected void addWeaponList(TooltipMakerAPI tooltip, String title, List<String> weapons, int max, float opad) {
229 addBlueprintList(tooltip, title, weapons, max, opad, new BlueprintLister(){
230 public boolean isKnown(String id) {
231 return Global.getSector().getPlayerFaction().knowsWeapon(id);
232 }
233 public String getNoun(int num) {
234 if (num == 1) return "weapon";
235 return "weapons";
236 }
237 public String getName(String id) {
238 WeaponSpecAPI spec = Global.getSettings().getWeaponSpec(id);
239 return spec.getWeaponName();
240 }
241 });
242 }
243
244 protected void addFighterList(TooltipMakerAPI tooltip, String title, List<String> wings, int max, float opad) {
245 addBlueprintList(tooltip, title, wings, max, opad, new BlueprintLister(){
246 public boolean isKnown(String id) {
247 return Global.getSector().getPlayerFaction().knowsFighter(id);
248 }
249 public String getNoun(int num) {
250 if (num == 1) return "fighter LPC";
251 return "fighter LPCs";
252 }
253 public String getName(String id) {
254 FighterWingSpecAPI spec = Global.getSettings().getFighterWingSpec(id);
255 return spec.getWingName();
256 }
257 });
258 }
259
260 protected void addBlueprintList(TooltipMakerAPI tooltip, String title, List<String> ids, int max, float opad,
261 BlueprintLister lister) {
262
263 Color b = Misc.getButtonTextColor();
264 Color g = Misc.getGrayColor();
265
266 tooltip.addPara(title, opad);
267
268 String tab = " ";
269 float small = 5f;
270 float pad = small;
271
272 int left = ids.size();
273
274 List<String> copy = new ArrayList<String>();
275 for (String id : ids) {
276 if (!lister.isKnown(id)) copy.add(id);
277 }
278 for (String id : ids) {
279 if (lister.isKnown(id)) copy.add(id);
280 }
281
282 ids = copy;
283 for (String id : ids) {
284 boolean known = lister.isKnown(id);
285
286 if (known) {
287 tooltip.addPara(tab + lister.getName(id) + " (known)", g, pad);
288 } else {
289 tooltip.addPara(tab + lister.getName(id), b, pad);
290 }
291 left--;
292 pad = 3f;
293 if (ids.size() - left >= max - 1) break;
294 }
295 if (ids.isEmpty()) {
296 tooltip.addPara(tab + "None", pad);
297 }
298 if (left > 0) {
299 String noun = lister.getNoun(left);
300 tooltip.addPara(tab + "... and %s other " + noun + "", pad, Misc.getHighlightColor(), "" + left);
301 }
302 }
303 public String getDesignType() {
304 return spec.getManufacturer();
305 }
307 return spec;
308 }
309
310}
311
312
313
314
315
316
317
static SettingsAPI getSettings()
Definition Global.java:51
static SectorAPI getSector()
Definition Global.java:59
ItemCostLabelData getCostLabelData(CargoStackAPI stack, CargoTransferHandlerAPI transferHandler, Object stackSource)
void addCostLabel(TooltipMakerAPI tooltip, float pad, CargoTransferHandlerAPI transferHandler, Object stackSource)
void addWeaponList(TooltipMakerAPI tooltip, String title, List< String > weapons, int max, float opad)
void addBlueprintList(TooltipMakerAPI tooltip, String title, List< String > ids, int max, float opad, BlueprintLister lister)
void addShipList(TooltipMakerAPI tooltip, String title, List< String > hulls, int max, float opad)
void createTooltip(TooltipMakerAPI tooltip, boolean expanded, CargoTransferHandlerAPI transferHandler, Object stackSource, boolean useGray)
void createTooltip(TooltipMakerAPI tooltip, boolean expanded, CargoTransferHandlerAPI transferHandler, Object stackSource)
void addFighterList(TooltipMakerAPI tooltip, String title, List< String > wings, int max, float opad)
void render(float x, float y, float w, float h, float alphaMult, float glowMult, SpecialItemRendererAPI renderer)
SpecialItemSpecAPI getSpecialItemSpec(String itemId)
ShipHullSpecAPI getHullSpec(String hullId)
FighterWingSpecAPI getFighterWingSpec(String wingId)
WeaponSpecAPI getWeaponSpec(String weaponId)
SpecialItemSpecAPI getSpecialItemSpecIfSpecial()
float computeCurrentSingleItemSellCost(CargoStackAPI stack)
float computeCurrentSingleItemBuyCost(CargoStackAPI stack)