Skip to content

Commit d4716de

Browse files
committed
Combine load and unload
1 parent cbaabce commit d4716de

File tree

2 files changed

+43
-57
lines changed

2 files changed

+43
-57
lines changed

src/main/java/io/github/cccm5/CargoMain.java

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646

4747
import java.util.ArrayList;
4848
import java.util.List;
49+
import java.util.Set;
4950

5051
public class CargoMain extends JavaPlugin implements Listener {
5152
private static Economy economy;
@@ -167,8 +168,18 @@ public void unload(@NotNull Player player){
167168
return;
168169
}
169170

170-
TradableGUIItem finalItem = NPCUtil.getUnloadItem(nearbyMerchants, player.getInventory().getItemInMainHand().clone(), dtlTradersPlugin);
171-
if (finalItem == null || finalItem.getTradePrice() == 0.0) {
171+
Set<TradableGUIItem> items = NPCUtil.getItems(nearbyMerchants, player.getInventory().getItemInMainHand(), dtlTradersPlugin, "sell");
172+
TradableGUIItem finalItem = null;
173+
for (TradableGUIItem item : items) {
174+
if (finalItem == null) {
175+
finalItem = item;
176+
continue;
177+
}
178+
179+
if (item.getTradePrice() > finalItem.getTradePrice())
180+
finalItem = item;
181+
}
182+
if (finalItem == null || finalItem.getTradePrice() <= 0.0) {
172183
player.sendMessage(Config.ERROR_TAG + "You need to be holding a cargo item to do that!");
173184
return;
174185
}
@@ -213,14 +224,23 @@ public void load(@NotNull Player player){
213224
return;
214225
}
215226

216-
TradableGUIItem finalItem = NPCUtil.getLoadItem(nearbyMerchants, player.getInventory().getItemInMainHand(), dtlTradersPlugin);
217-
if (finalItem == null || finalItem.getTradePrice() == 0.0) {
227+
Set<TradableGUIItem> items = NPCUtil.getItems(nearbyMerchants, player.getInventory().getItemInMainHand(), dtlTradersPlugin, "buy");
228+
TradableGUIItem finalItem = null;
229+
for (TradableGUIItem item : items) {
230+
if (finalItem == null) {
231+
finalItem = item;
232+
continue;
233+
}
234+
235+
if (item.getTradePrice() < finalItem.getTradePrice())
236+
finalItem = item;
237+
}
238+
if (finalItem == null || finalItem.getTradePrice() <= 0.0) {
218239
player.sendMessage(Config.ERROR_TAG + "You need to be holding a cargo item to do that!");
219240
return;
220241
}
221242

222-
final ItemMeta meta = finalItem.getMainItem().getItemMeta();
223-
String itemName = meta.getDisplayName() != null && meta.getDisplayName().length() > 0 ? meta.getDisplayName() : finalItem.getMainItem().getType().name().toLowerCase();
243+
String itemName = finalItem.getMainItem().getItemMeta().getDisplayName() != null && finalItem.getMainItem().getItemMeta().getDisplayName().length() > 0 ? finalItem.getMainItem().getItemMeta().getDisplayName() : finalItem.getMainItem().getType().name().toLowerCase();
224244
if(!economy.has(player,finalItem.getTradePrice()*(1+Config.loadTax))){
225245
player.sendMessage(Config.ERROR_TAG + "You don't have enough money to buy any " + itemName + "!");
226246
return;

src/main/java/io/github/cccm5/util/NPCUtil.java

Lines changed: 17 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
import org.jetbrains.annotations.Nullable;
2121

2222
import java.util.ArrayList;
23+
import java.util.HashSet;
2324
import java.util.List;
25+
import java.util.Set;
2426

2527
public class NPCUtil {
2628
@NotNull
@@ -63,64 +65,28 @@ public static List<NPC> getNPCsInRange(MovecraftLocation center) {
6365
return result;
6466
}
6567

66-
@Nullable
67-
public static TradableGUIItem getUnloadItem(@NotNull List<NPC> nearbyMerchants, ItemStack compareItem, Main dtlTradersPlugin) {
68-
TradableGUIItem result = null;
69-
for (NPC cargoMerchant : nearbyMerchants) {
70-
if (result != null)
71-
break;
72-
String guiName = cargoMerchant.getTrait(TraderTrait.class).getGUIName();
73-
AGUI gui = dtlTradersPlugin.getGuiListService().getGUI(guiName);
74-
TradeGUI tradeGUI = (TradeGUI) gui;
75-
result = null;
76-
for (TradeGUIPage page : tradeGUI.getPages()) {
77-
if (page == null) continue;
78-
for (AGUIItem tempItem : page.getItems("sell")) {
79-
if (!(tempItem instanceof TradableGUIItem)) continue;
80-
if (tempItem.getMainItem().isSimilar(compareItem)) {
81-
if (tempItem.getMainItem().getAmount() > 1)
82-
continue;
83-
result = (TradableGUIItem) tempItem;
84-
break;
85-
}
86-
}
87-
if (result == null || result.getTradePrice() == 0.0) {
88-
return null;
89-
}
90-
}
91-
}
92-
return result;
93-
}
94-
95-
@Nullable
96-
public static TradableGUIItem getLoadItem(@NotNull List<NPC> nearbyMerchants, ItemStack compareItem, Main dtlTradersPlugin) {
97-
TradableGUIItem result = null;
98-
for (NPC cargoMerchant : nearbyMerchants) {
99-
String guiName = cargoMerchant.getTrait(TraderTrait.class).getGUIName();
68+
@NotNull
69+
public static Set<TradableGUIItem> getItems(@NotNull List<NPC> merchants, ItemStack item, Main dtlTradersPlugin, String shopMode) {
70+
Set<TradableGUIItem> result = new HashSet<>();
71+
for (NPC merchant : merchants) {
72+
String guiName = merchant.getTrait(TraderTrait.class).getGUIName();
10073
AGUI gui = dtlTradersPlugin.getGuiListService().getGUI(guiName);
101-
TradeGUI tradeGUI = (TradeGUI) gui;
102-
for (TradeGUIPage page : tradeGUI.getPages()) {
103-
if (page == null)
104-
continue;
74+
if (!(gui instanceof TradeGUI))
75+
continue;
10576

106-
for (AGUIItem tempItem : page.getItems("buy")) {
107-
if (!(tempItem instanceof TradableGUIItem))
77+
for (TradeGUIPage page : ((TradeGUI) gui).getPages()) {
78+
for (AGUIItem guiItem : page.getItems(shopMode)) {
79+
if (!(guiItem instanceof TradableGUIItem tradableItem))
80+
continue;
81+
if (!guiItem.getMainItem().isSimilar(item) || guiItem.getMainItem().getAmount() > 1)
10882
continue;
10983

110-
TradableGUIItem tradeItem = (TradableGUIItem) tempItem;
111-
if (tradeItem.getMainItem().isSimilar(compareItem)) {
112-
if (tempItem.getMainItem().getAmount() > 1)
113-
continue;
114-
115-
result = tradeItem;
116-
break;
84+
result.add(tradableItem);
85+
if (Config.debug) {
86+
CargoMain.getInstance().getLogger().info("Found for $" + tradableItem.getTradePrice() + " in " + merchant.getId() + "/" + page.getPageName());
11787
}
11888
}
119-
if (result != null)
120-
break;
12189
}
122-
if (result != null)
123-
break;
12490
}
12591
return result;
12692
}

0 commit comments

Comments
 (0)