|
34 | 34 | import org.bukkit.event.inventory.InventoryCloseEvent; |
35 | 35 | import org.bukkit.event.player.*; |
36 | 36 | import org.bukkit.inventory.EquipmentSlot; |
| 37 | +import org.bukkit.inventory.Inventory; |
37 | 38 | import org.bukkit.inventory.ItemStack; |
38 | 39 | import org.bukkit.util.BlockIterator; |
39 | 40 | import org.jetbrains.annotations.NotNull; |
@@ -193,39 +194,16 @@ private void postTrade(PlayerInteractEvent e) { |
193 | 194 | final AbstractEconomy eco = plugin.getEconomy(); |
194 | 195 | final double price = shop.getPrice(); |
195 | 196 | final double money = plugin.getEconomy().getBalance(p.getUniqueId(), shop.getLocation().getWorld(), shop.getCurrency()); |
196 | | - |
| 197 | + final Inventory playerInventory = p.getInventory(); |
197 | 198 | 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); |
205 | 200 | if (shop.isStackingShop()) { |
206 | 201 | plugin.text().of(p, "how-many-buy-stack", Integer.toString(shop.getItem().getAmount()), Integer.toString(itemAmount)).send(); |
207 | 202 | } else { |
208 | 203 | plugin.text().of(p, "how-many-buy", Integer.toString(itemAmount)).send(); |
209 | 204 | } |
210 | 205 | } 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); |
229 | 207 | if (shop.isStackingShop()) { |
230 | 208 | plugin.text().of(p, "how-many-sell-stack", Integer.toString(shop.getItem().getAmount()), Integer.toString(items)).send(); |
231 | 209 | } else { |
@@ -294,6 +272,43 @@ else if (e.useInteractedBlock() == Event.Result.ALLOW |
294 | 272 | } |
295 | 273 | } |
296 | 274 |
|
| 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 | + |
297 | 312 | @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) |
298 | 313 | public void onInventoryClose(InventoryCloseEvent e) { |
299 | 314 | try { |
|
0 commit comments