Skip to content

Commit 0dd0de8

Browse files
committed
1 parent a70d82f commit 0dd0de8

File tree

3 files changed

+52
-26
lines changed

3 files changed

+52
-26
lines changed

src/main/java/org/maxgamer/quickshop/api/shop/Shop.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,4 +606,10 @@ default boolean isShopSign(@NotNull Sign sign) {
606606
}
607607
}
608608

609+
/**
610+
* Check if this shop is free shop
611+
*
612+
* @return Free Shop
613+
*/
614+
boolean isFreeShop();
609615
}

src/main/java/org/maxgamer/quickshop/listener/PlayerListener.java

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.bukkit.event.inventory.InventoryCloseEvent;
3535
import org.bukkit.event.player.*;
3636
import org.bukkit.inventory.EquipmentSlot;
37+
import org.bukkit.inventory.Inventory;
3738
import org.bukkit.inventory.ItemStack;
3839
import org.bukkit.util.BlockIterator;
3940
import org.jetbrains.annotations.NotNull;
@@ -193,39 +194,16 @@ private void postTrade(PlayerInteractEvent e) {
193194
final AbstractEconomy eco = plugin.getEconomy();
194195
final double price = shop.getPrice();
195196
final double money = plugin.getEconomy().getBalance(p.getUniqueId(), shop.getLocation().getWorld(), shop.getCurrency());
196-
197+
final Inventory playerInventory = p.getInventory();
197198
if (shop.isSelling()) {
198-
int itemAmount = Math.min(shop.getRemainingSpace(), (int) Math.floor(money / price));
199-
if (!shop.isUnlimited()) {
200-
itemAmount = Math.min(itemAmount, shop.getRemainingStock());
201-
}
202-
if (itemAmount < 0) {
203-
itemAmount = 0;
204-
}
199+
int itemAmount = getPlayerCanBuy(shop, money, price, playerInventory);
205200
if (shop.isStackingShop()) {
206201
plugin.text().of(p, "how-many-buy-stack", Integer.toString(shop.getItem().getAmount()), Integer.toString(itemAmount)).send();
207202
} else {
208203
plugin.text().of(p, "how-many-buy", Integer.toString(itemAmount)).send();
209204
}
210205
} else {
211-
final double ownerBalance = eco.getBalance(shop.getOwner(), shop.getLocation().getWorld(), shop.getCurrency());
212-
int items = shop.getRemainingStock();
213-
final int ownerCanAfford = (int) (ownerBalance / shop.getPrice());
214-
215-
if (!shop.isUnlimited()) {
216-
// Amount check player amount and shop empty slot
217-
items = Math.min(items, shop.getRemainingSpace());
218-
// Amount check player selling item total cost and the shop owner's balance
219-
items = Math.min(items, ownerCanAfford);
220-
} else if (plugin.getConfiguration().getBoolean("shop.pay-unlimited-shop-owners")) {
221-
// even if the shop is unlimited, the config option pay-unlimited-shop-owners is set to
222-
// true,
223-
// the unlimited shop owner should have enough money.
224-
items = Math.min(items, ownerCanAfford);
225-
}
226-
if (items < 0) {
227-
items = 0;
228-
}
206+
int items = getPlayerCanSell(shop, money, price, playerInventory);
229207
if (shop.isStackingShop()) {
230208
plugin.text().of(p, "how-many-sell-stack", Integer.toString(shop.getItem().getAmount()), Integer.toString(items)).send();
231209
} else {
@@ -294,6 +272,43 @@ else if (e.useInteractedBlock() == Event.Result.ALLOW
294272
}
295273
}
296274

275+
private int getPlayerCanBuy(Shop shop, double money, double price, Inventory playerInventory) {
276+
if (shop.isFreeShop()) { // Free shop
277+
return Math.min(shop.getRemainingStock(), Util.countSpace(playerInventory, shop.getItem()));
278+
}
279+
int itemAmount = Math.min(shop.getRemainingSpace(), (int) Math.floor(money / price));
280+
if (!shop.isUnlimited()) {
281+
itemAmount = Math.min(itemAmount, shop.getRemainingStock());
282+
}
283+
if (itemAmount < 0) {
284+
itemAmount = 0;
285+
}
286+
return itemAmount;
287+
}
288+
289+
private int getPlayerCanSell(Shop shop, double money, double price, Inventory playerInventory) {
290+
if (shop.isFreeShop()) {
291+
return Math.min(shop.getRemainingSpace(), Util.countItems(playerInventory, shop.getItem()));
292+
}
293+
int items = shop.getRemainingStock();
294+
final int ownerCanAfford = (int) (money / price);
295+
if (!shop.isUnlimited()) {
296+
// Amount check player amount and shop empty slot
297+
items = Math.min(items, shop.getRemainingSpace());
298+
// Amount check player selling item total cost and the shop owner's balance
299+
items = Math.min(items, ownerCanAfford);
300+
} else if (plugin.getConfiguration().getBoolean("shop.pay-unlimited-shop-owners")) {
301+
// even if the shop is unlimited, the config option pay-unlimited-shop-owners is set to
302+
// true,
303+
// the unlimited shop owner should have enough money.
304+
items = Math.min(items, ownerCanAfford);
305+
}
306+
if (items < 0) {
307+
items = 0;
308+
}
309+
return items;
310+
}
311+
297312
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
298313
public void onInventoryClose(InventoryCloseEvent e) {
299314
try {

src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,11 @@ public void onUnload() {
582582
return name;
583583
}
584584

585+
@Override
586+
public boolean isFreeShop() {
587+
return this.price == 0.0d;
588+
}
589+
585590
@Override
586591
public @NotNull String ownerName() {
587592
return ownerName(false);

0 commit comments

Comments
 (0)