@@ -392,21 +392,21 @@ public void sellAllContent(Player player, org.bukkit.inventory.Inventory invento
392392 List <Pair <ItemStack , ItemButton >> buttons = this .itemButtons .stream ().map (button -> {
393393 ItemStack itemStack = button .getItemStack ().build (player , false );
394394 return new Pair <>(itemStack , button );
395- }).collect ( Collectors . toList () );
395+ }).toList ();
396396
397397 /* SCAN ITEMS */
398398 for (int slot = 0 ; slot != inventory .getContents ().length ; slot ++) {
399399 ItemStack itemStack = inventory .getContents ()[slot ];
400400 if (itemStack == null ) continue ;
401401
402402 Optional <ItemButton > optional = buttons .stream ().filter (e -> e .first .isSimilar (itemStack )).map (e -> e .second ).findFirst ();
403- if (! optional .isPresent ()) continue ;
403+ if (optional .isEmpty ()) continue ;
404404
405405 ItemButton itemButton = optional .get ();
406406 if (!itemButton .canSell ()) continue ;
407407
408408 double price = itemButton .getSellPrice (player , itemStack .getAmount ());
409- ShopAction shopAction = new ShopAction (itemStack , itemButton , price );
409+ ShopAction shopAction = new ShopAction (itemStack . clone () , itemButton , price );
410410 shopActions .add (shopAction );
411411 }
412412
@@ -424,6 +424,8 @@ public void sellAllContent(Player player, org.bukkit.inventory.Inventory invento
424424 ItemButton button = action .getItemButton ();
425425 ItemStack itemStack = action .getItemStack ();
426426
427+ int requestedAmount = itemStack .getAmount ();
428+ int actualSellAmount = requestedAmount ; // Amount that will actually be sold
427429 int newServerLimitAmount = 0 ;
428430 int newPlayerLimitAmount = 0 ;
429431 String material = button .getItemStack ().getMaterial ();
@@ -435,20 +437,41 @@ public void sellAllContent(Player player, org.bukkit.inventory.Inventory invento
435437 /* SERVER LIMIT */
436438 if (optionalServer .isPresent ()) {
437439 Limit serverSellLimit = optionalServer .get ();
438- newServerLimitAmount = serverSellLimit .getAmount () + itemStack .getAmount ();
439- if (newServerLimitAmount > serverSellLimit .getLimit ()) return ;
440+ int currentServerAmount = serverSellLimit .getAmount ();
441+ int maxCanSell = serverSellLimit .getLimit () - currentServerAmount ;
442+
443+ if (maxCanSell <= 0 ) {
444+ action .setTotalAmount (0 );
445+ return ;
446+ }
447+ actualSellAmount = Math .min (actualSellAmount , maxCanSell );
448+ newServerLimitAmount = currentServerAmount + actualSellAmount ;
440449 }
441450 /* END SERVER LIMIT */
442451
443452 /* PLAYER LIMIT */
444453 if (optionalPlayer .isPresent ()) {
445454 Limit playerSellLimit = optionalPlayer .get ();
446455 Optional <PlayerLimit > optional = limiterManager .getLimit (player );
447- newPlayerLimitAmount = optional .map (e -> e .getSellAmount (material )).orElse (0 ) + itemStack .getAmount ();
448- if (newPlayerLimitAmount > playerSellLimit .getLimit ()) return ;
456+ int currentPlayerAmount = optional .map (e -> e .getSellAmount (material )).orElse (0 );
457+ int maxCanSell = playerSellLimit .getLimit () - currentPlayerAmount ;
458+
459+ if (maxCanSell <= 0 ) {
460+ action .setTotalAmount (0 );
461+ return ;
462+ }
463+ actualSellAmount = Math .min (actualSellAmount , maxCanSell );
464+ newPlayerLimitAmount = currentPlayerAmount + actualSellAmount ;
449465 }
450466 /* END PLAYER LIMIT */
451467
468+ if (actualSellAmount != requestedAmount ) {
469+ itemStack .setAmount (actualSellAmount );
470+ double actualPrice = button .getSellPrice (player , actualSellAmount );
471+ action .setPrice (actualPrice );
472+ }
473+ action .setTotalAmount (actualSellAmount );
474+
452475 /* UPDATE LIMIT VALUES */
453476 if (optionalServer .isPresent ()) optionalServer .get ().setAmount (newServerLimitAmount );
454477 if (newPlayerLimitAmount > 0 )
@@ -457,8 +480,7 @@ public void sellAllContent(Player player, org.bukkit.inventory.Inventory invento
457480
458481 /* REMOVE ITEMS AND UPDATE MONEY */
459482 prices .put (button .getEconomy (), prices .getOrDefault (button .getEconomy (), 0.0 ) + action .getPrice ());
460- // inventory.remove(itemStack);
461- InventoryUtils .removeItem (inventory , itemStack , itemStack .getAmount ());
483+ InventoryUtils .removeItem (inventory , itemStack , actualSellAmount );
462484 });
463485
464486 if (prices .isEmpty ()) {
@@ -469,23 +491,25 @@ public void sellAllContent(Player player, org.bukkit.inventory.Inventory invento
469491 List <ShopAction > fixedShopActions = new ArrayList <>();
470492
471493 shopActions .forEach (action -> {
494+ if (action .getTotalAmount () == 0 ) return ;
495+
472496 Optional <ShopAction > optional = fixedShopActions .stream ().filter (e -> e .getItemStack ().isSimilar (action .getItemStack ())).findFirst ();
473497 if (optional .isPresent ()) {
474498 ShopAction currentAction = optional .get ();
475499 currentAction .setPrice (currentAction .getPrice () + action .getPrice ());
476- currentAction .getItemStack (). setAmount ( currentAction . getItemStack (). getAmount () + action .getItemStack (). getAmount ());
500+ currentAction .addAmount ( action .getTotalAmount ());
477501 } else fixedShopActions .add (action );
478502 });
479503
480504 var translationManager = this .plugin .getTranslationManager ();
481505
482- String results = toList (fixedShopActions .stream ().map (action -> getMessage (Message .SELL_ALL_INFO , "%amount%" , action .getItemStack (). getAmount (), "%item%" , translationManager .translateItemStack (action .getItemStack ()), "%price%" , action .getItemButton ().getEconomy ().format (transformPrice (action .getPrice ()), action .getPrice ()))).collect (Collectors .toList ()), Message .SELL_ALL_COLOR_SEPARATOR .msg (), Message .SELL_ALL_COLOR_INFO .msg ());
506+ String results = toList (fixedShopActions .stream ().map (action -> getMessage (Message .SELL_ALL_INFO , "%amount%" , action .getTotalAmount (), "%item%" , translationManager .translateItemStack (action .getItemStack ()), "%price%" , action .getItemButton ().getEconomy ().format (transformPrice (action .getPrice ()), action .getPrice ()))).collect (Collectors .toList ()), Message .SELL_ALL_COLOR_SEPARATOR .msg (), Message .SELL_ALL_COLOR_INFO .msg ());
483507 if (results == null ) {
484508 Logger .info ("Error with results on sellall !" );
485509 player .sendMessage ("§cError with results on sellall !" );
486510 }
487511
488- String resultsReason = toList (fixedShopActions .stream ().map (action -> getMessage (Config .depositAllLine , "%amount%" , action .getItemStack (). getAmount (), "%item%" , translationManager .translateItemStack (action .getItemStack ()), "%price%" , action .getItemButton ().getEconomy ().format (transformPrice (action .getPrice ()), action .getPrice ()))).collect (Collectors .toList ()), "" , "" );
512+ String resultsReason = toList (fixedShopActions .stream ().map (action -> getMessage (Config .depositAllLine , "%amount%" , action .getTotalAmount (), "%item%" , translationManager .translateItemStack (action .getItemStack ()), "%price%" , action .getItemButton ().getEconomy ().format (transformPrice (action .getPrice ()), action .getPrice ()))).collect (Collectors .toList ()), "" , "" );
489513 prices .forEach ((economy , price ) -> economy .depositMoney (player , price , Config .depositAllReason .replace ("%items%" , resultsReason == null ? "" : resultsReason )));
490514
491515 message (this .plugin , player , Message .SELL_ALL_MESSAGE , "%items%" , results == null ? "ERROR" : results );
@@ -510,7 +534,7 @@ public void sellAllContent(Player player, org.bukkit.inventory.Inventory invento
510534 public void sellHand (Player player , int amount ) {
511535
512536 ItemStack itemInHand = player .getItemInHand (); // Use old method for 1.8 support
513- if (itemInHand == null || itemInHand .getType ().equals (Material .AIR )) {
537+ if (itemInHand .getType ().equals (Material .AIR )) {
514538 message (this .plugin , player , Message .SELL_HAND_AIR );
515539 return ;
516540 }
0 commit comments