Skip to content

Commit 32cd596

Browse files
committed
Add parcel fees
1 parent bbe51ed commit 32cd596

3 files changed

Lines changed: 52 additions & 1 deletion

File tree

‎src/main/java/com/eternalcode/parcellockers/configuration/implementation/MessageConfig.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,14 @@ public static class ParcelMessages extends OkaeriConfig {
111111
.chat("&2✔ &aParcel deleted successfully.")
112112
.sound(Sound.ENTITY_ITEM_BREAK.key())
113113
.build();
114+
public Notice insufficientFunds = Notice.builder()
115+
.chat("&4✘ &cYou do not have enough funds to send this parcel! Required: &6${AMOUNT}&c.")
116+
.sound(Sound.ENTITY_VILLAGER_NO.key())
117+
.build();
118+
public Notice feeWithdrawn = Notice.builder()
119+
.chat("&2✔ &a${AMOUNT} has been withdrawn from your account to cover the parcel sending fee.")
120+
.sound(Sound.ENTITY_EXPERIENCE_ORB_PICKUP.key())
121+
.build();
114122

115123
@Comment({"", "# The parcel info message." })
116124
public Notice parcelInfoMessages = Notice.builder()

‎src/main/java/com/eternalcode/parcellockers/configuration/implementation/PluginConfig.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ public static class Settings extends OkaeriConfig {
6161

6262
@Comment({"", "# Maximum number of parcels that can be stored in a single locker"})
6363
public int maxParcelsPerLocker = 30;
64+
65+
@Comment({"", "# Small parcel fee in in-game currency"})
66+
public double smallParcelFee = 10.0;
67+
68+
@Comment({"", "# Medium parcel fee in in-game currency"})
69+
public double mediumParcelFee = 25.0;
70+
71+
@Comment({"", "# Large parcel fee in in-game currency"})
72+
public double largeParcelFee = 50.0;
6473
}
6574

6675
public static class GuiSettings extends OkaeriConfig {

‎src/main/java/com/eternalcode/parcellockers/parcel/ParcelServiceImpl.java‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import com.eternalcode.commons.bukkit.ItemUtil;
66
import com.eternalcode.commons.scheduler.Scheduler;
7+
import com.eternalcode.parcellockers.configuration.implementation.PluginConfig;
78
import com.eternalcode.parcellockers.content.ParcelContent;
89
import com.eternalcode.parcellockers.content.repository.ParcelContentRepository;
910
import com.eternalcode.parcellockers.notification.NoticeService;
@@ -19,16 +20,21 @@
1920
import java.util.UUID;
2021
import java.util.concurrent.CompletableFuture;
2122
import java.util.concurrent.TimeUnit;
23+
import net.milkbowl.vault.economy.Economy;
2224
import org.bukkit.command.CommandSender;
2325
import org.bukkit.entity.Player;
2426
import org.bukkit.inventory.ItemStack;
2527

2628
public 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

Comments
 (0)