44
55import com .eternalcode .commons .bukkit .ItemUtil ;
66import com .eternalcode .commons .scheduler .Scheduler ;
7+ import com .eternalcode .parcellockers .configuration .implementation .PluginConfig ;
78import com .eternalcode .parcellockers .content .ParcelContent ;
89import com .eternalcode .parcellockers .content .repository .ParcelContentRepository ;
910import com .eternalcode .parcellockers .notification .NoticeService ;
1920import java .util .UUID ;
2021import java .util .concurrent .CompletableFuture ;
2122import java .util .concurrent .TimeUnit ;
23+ import net .milkbowl .vault .economy .Economy ;
2224import org .bukkit .command .CommandSender ;
2325import org .bukkit .entity .Player ;
2426import org .bukkit .inventory .ItemStack ;
2527
2628public class ParcelServiceImpl implements ParcelService {
2729
30+ private static final String PARCEL_FEE_BYPASS_PERMISSION = "parcellockers.fee.bypass" ;
31+
2832 private final NoticeService noticeService ;
2933 private final ParcelRepository parcelRepository ;
3034 private final ParcelContentRepository parcelContentRepository ;
3135 private final Scheduler scheduler ;
36+ private final PluginConfig config ;
37+ private final Economy economy ;
3238
3339 private final Cache <UUID , Parcel > parcelsByUuid ;
3440 private final Cache <UUID , List <Parcel >> parcelsBySender ;
@@ -38,12 +44,14 @@ public ParcelServiceImpl(
3844 NoticeService noticeService ,
3945 ParcelRepository parcelRepository ,
4046 ParcelContentRepository parcelContentRepository ,
41- Scheduler scheduler
47+ Scheduler scheduler , PluginConfig config , Economy economy
4248 ) {
4349 this .noticeService = noticeService ;
4450 this .parcelRepository = parcelRepository ;
4551 this .parcelContentRepository = parcelContentRepository ;
4652 this .scheduler = scheduler ;
53+ this .config = config ;
54+ this .economy = economy ;
4755
4856 this .parcelsByUuid = Caffeine .newBuilder ()
4957 .expireAfterAccess (3 , TimeUnit .HOURS )
@@ -65,6 +73,32 @@ public ParcelServiceImpl(
6573
6674 @ Override
6775 public CompletableFuture <Void > send (Player sender , Parcel parcel , List <ItemStack > items ) {
76+ if (!sender .hasPermission (PARCEL_FEE_BYPASS_PERMISSION )) {
77+ double fee = switch (parcel .size ()) {
78+ case SMALL -> this .config .settings .smallParcelFee ;
79+ case MEDIUM -> this .config .settings .mediumParcelFee ;
80+ case LARGE -> this .config .settings .largeParcelFee ;
81+ };
82+
83+ if (fee > 0 ) {
84+ boolean success = this .economy .withdrawPlayer (sender , fee ).transactionSuccess ();
85+ if (!success ) {
86+ this .noticeService .create ()
87+ .notice (messages -> messages .parcel .insufficientFunds )
88+ .player (sender .getUniqueId ())
89+ .placeholder ("{AMOUNT}" , String .format ("%.2f" , fee ))
90+ .send ();
91+ return CompletableFuture .completedFuture (null );
92+ }
93+
94+ this .noticeService .create ()
95+ .notice (messages -> messages .parcel .feeWithdrawn )
96+ .player (sender .getUniqueId ())
97+ .placeholder ("{AMOUNT}" , String .format ("%.2f" , fee ))
98+ .send ();
99+ }
100+ }
101+
68102 return this .parcelRepository .save (parcel ).whenComplete ((unused , throwable ) -> {
69103 if (throwable != null ) {
70104 this .noticeService .create ()
0 commit comments