-
-
Notifications
You must be signed in to change notification settings - Fork 0
Add parcel fees #144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Add parcel fees #144
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -59,16 +59,19 @@ | |
| import java.util.ArrayList; | ||
| import java.util.stream.Stream; | ||
| import net.kyori.adventure.text.minimessage.MiniMessage; | ||
| import net.milkbowl.vault.economy.Economy; | ||
| import org.bstats.bukkit.Metrics; | ||
| import org.bukkit.Server; | ||
| import org.bukkit.command.CommandSender; | ||
| import org.bukkit.plugin.RegisteredServiceProvider; | ||
| import org.bukkit.plugin.java.JavaPlugin; | ||
|
|
||
| public final class ParcelLockers extends JavaPlugin { | ||
|
|
||
| private LiteCommands<CommandSender> liteCommands; | ||
| private SkullAPI skullAPI; | ||
| private DatabaseManager databaseManager; | ||
| private Economy economy; | ||
|
|
||
| @Override | ||
| public void onEnable() { | ||
|
|
@@ -84,6 +87,12 @@ public void onEnable() { | |
| NoticeService noticeService = new NoticeService(messageConfig, miniMessage); | ||
| Scheduler scheduler = new BukkitSchedulerImpl(this); | ||
|
|
||
| if (!this.setupEconomy()) { | ||
| this.getLogger().severe("No economy provider registered! Disabling..."); | ||
| server.getPluginManager().disablePlugin(this); | ||
| return; | ||
| } | ||
|
Comment on lines
+90
to
+94
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The instantiation of I am commenting here as this is the closest related change, but the actual fix is in |
||
|
|
||
| DatabaseManager databaseManager = new DatabaseManager(config, this.getLogger(), this.getDataFolder()); | ||
| this.databaseManager = databaseManager; | ||
|
|
||
|
|
@@ -207,6 +216,18 @@ public void onDisable() { | |
| this.skullAPI.shutdown(); | ||
| } | ||
| } | ||
|
|
||
| private boolean setupEconomy() { | ||
| if (this.getServer().getPluginManager().getPlugin("Vault") == null) { | ||
| return false; | ||
| } | ||
| RegisteredServiceProvider<Economy> rsp = this.getServer().getServicesManager().getRegistration(Economy.class); | ||
| if (rsp == null) { | ||
| return false; | ||
| } | ||
| this.economy = rsp.getProvider(); | ||
| return this.economy != null; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |||||
|
|
||||||
| import com.eternalcode.commons.bukkit.ItemUtil; | ||||||
| import com.eternalcode.commons.scheduler.Scheduler; | ||||||
| import com.eternalcode.parcellockers.configuration.implementation.PluginConfig; | ||||||
| import com.eternalcode.parcellockers.content.ParcelContent; | ||||||
| import com.eternalcode.parcellockers.content.repository.ParcelContentRepository; | ||||||
| import com.eternalcode.parcellockers.notification.NoticeService; | ||||||
|
|
@@ -19,16 +20,21 @@ | |||||
| import java.util.UUID; | ||||||
| import java.util.concurrent.CompletableFuture; | ||||||
| import java.util.concurrent.TimeUnit; | ||||||
| import net.milkbowl.vault.economy.Economy; | ||||||
| import org.bukkit.command.CommandSender; | ||||||
| import org.bukkit.entity.Player; | ||||||
| import org.bukkit.inventory.ItemStack; | ||||||
|
|
||||||
| public class ParcelServiceImpl implements ParcelService { | ||||||
|
|
||||||
| private static final String PARCEL_FEE_BYPASS_PERMISSION = "parcellockers.fee.bypass"; | ||||||
|
|
||||||
| private final NoticeService noticeService; | ||||||
| private final ParcelRepository parcelRepository; | ||||||
| private final ParcelContentRepository parcelContentRepository; | ||||||
| private final Scheduler scheduler; | ||||||
| private final PluginConfig config; | ||||||
| private final Economy economy; | ||||||
|
|
||||||
| private final Cache<UUID, Parcel> parcelsByUuid; | ||||||
| private final Cache<UUID, List<Parcel>> parcelsBySender; | ||||||
|
|
@@ -38,12 +44,14 @@ public ParcelServiceImpl( | |||||
| NoticeService noticeService, | ||||||
| ParcelRepository parcelRepository, | ||||||
| ParcelContentRepository parcelContentRepository, | ||||||
| Scheduler scheduler | ||||||
| Scheduler scheduler, PluginConfig config, Economy economy | ||||||
| ) { | ||||||
|
Comment on lines
44
to
48
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The constructor signature has been updated. This is a breaking change, and the call site in Example fix in // ...
ParcelService parcelService = new ParcelServiceImpl(
noticeService,
parcelRepository,
parcelContentRepository,
scheduler,
config, // add this
this.economy // add this
);
// ... |
||||||
| this.noticeService = noticeService; | ||||||
| this.parcelRepository = parcelRepository; | ||||||
| this.parcelContentRepository = parcelContentRepository; | ||||||
| this.scheduler = scheduler; | ||||||
| this.config = config; | ||||||
| this.economy = economy; | ||||||
|
|
||||||
| this.parcelsByUuid = Caffeine.newBuilder() | ||||||
| .expireAfterAccess(3, TimeUnit.HOURS) | ||||||
|
|
@@ -65,6 +73,32 @@ public ParcelServiceImpl( | |||||
|
|
||||||
| @Override | ||||||
| public CompletableFuture<Void> send(Player sender, Parcel parcel, List<ItemStack> items) { | ||||||
| if (!sender.hasPermission(PARCEL_FEE_BYPASS_PERMISSION)) { | ||||||
| double fee = switch (parcel.size()) { | ||||||
| case SMALL -> this.config.settings.smallParcelFee; | ||||||
| case MEDIUM -> this.config.settings.mediumParcelFee; | ||||||
| case LARGE -> this.config.settings.largeParcelFee; | ||||||
| }; | ||||||
|
|
||||||
| if (fee > 0) { | ||||||
| boolean success = this.economy.withdrawPlayer(sender, fee).transactionSuccess(); | ||||||
| if (!success) { | ||||||
| this.noticeService.create() | ||||||
| .notice(messages -> messages.parcel.insufficientFunds) | ||||||
| .player(sender.getUniqueId()) | ||||||
| .placeholder("{AMOUNT}", String.format("%.2f", fee)) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using
Suggested change
|
||||||
| .send(); | ||||||
| return CompletableFuture.completedFuture(null); | ||||||
| } | ||||||
|
|
||||||
| this.noticeService.create() | ||||||
| .notice(messages -> messages.parcel.feeWithdrawn) | ||||||
| .player(sender.getUniqueId()) | ||||||
| .placeholder("{AMOUNT}", String.format("%.2f", fee)) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using
Suggested change
|
||||||
| .send(); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| return this.parcelRepository.save(parcel).whenComplete((unused, throwable) -> { | ||||||
| if (throwable != null) { | ||||||
| this.noticeService.create() | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For better readability and to adhere to Gradle best practices, it's recommended to use the
mavenCentral()method instead of a hardcoded URL for the Maven Central repository.