Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void onEnable() {
UserValidationService userValidationService = new UserValidator();
UserManager userManager = new UserManagerImpl(userRepository, userValidationService);
LockerValidationService lockerValidationService = new LockerValidator();
LockerManager lockerManager = new LockerManager(lockerRepository, lockerValidationService);
LockerManager lockerManager = new LockerManager(config, lockerRepository, lockerValidationService, parcelRepository);
ParcelContentManager parcelContentManager = new ParcelContentManager(parcelContentRepository);
ItemStorageManager itemStorageManager = new ItemStorageManager(itemStorageRepository);
DeliveryManager deliveryManager = new DeliveryManager(deliveryRepository);
Expand All @@ -131,6 +131,7 @@ public void onEnable() {
GuiManager guiManager = new GuiManager(
config,
scheduler,
noticeService,
parcelService,
lockerManager,
userManager,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ public static class ParcelMessages extends OkaeriConfig {
.chat("&4✘ &cThe parcel destination locker is not set!")
.sound(Sound.ENTITY_ENDERMAN_AMBIENT.key())
.build();
public Notice lockerFull = Notice.builder()
.chat("&4✘ &cThe destination locker is full! Please select another locker.")
.sound(Sound.ENTITY_VILLAGER_NO.key())
.build();
public Notice illegalItem = Notice.builder()
.chat("&4✘ &cThe parcel contains illegal items that cannot be sent. ({ITEMS})")
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ public static class Settings extends OkaeriConfig {

@Comment({"", "# Parcel sending duration for priority parcels"})
public Duration priorityParcelSendDuration = Duration.ofSeconds(30);

@Comment({"", "# Maximum number of parcels that can be stored in a single locker"})
public int maxParcelsPerLocker = 30;
}

public static class GuiSettings extends OkaeriConfig {
Expand Down
44 changes: 29 additions & 15 deletions src/main/java/com/eternalcode/parcellockers/gui/GuiManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.eternalcode.parcellockers.itemstorage.ItemStorageManager;
import com.eternalcode.parcellockers.locker.Locker;
import com.eternalcode.parcellockers.locker.LockerManager;
import com.eternalcode.parcellockers.notification.NoticeService;
import com.eternalcode.parcellockers.parcel.Parcel;
import com.eternalcode.parcellockers.parcel.ParcelService;
import com.eternalcode.parcellockers.parcel.task.ParcelSendTask;
Expand All @@ -28,6 +29,7 @@ public class GuiManager {

private final PluginConfig config;
private final Scheduler scheduler;
private final NoticeService noticeService;
private final ParcelService parcelService;
private final LockerManager lockerManager;
private final UserManager userManager;
Expand All @@ -36,7 +38,7 @@ public class GuiManager {
private final DeliveryManager deliveryManager;

public GuiManager(
PluginConfig config, Scheduler scheduler, ParcelService parcelService,
PluginConfig config, Scheduler scheduler, NoticeService noticeService, ParcelService parcelService,
LockerManager lockerManager,
UserManager userManager,
ItemStorageManager itemStorageManager,
Expand All @@ -45,6 +47,7 @@ public GuiManager(
) {
this.config = config;
this.scheduler = scheduler;
this.noticeService = noticeService;
this.parcelService = parcelService;
this.lockerManager = lockerManager;
this.userManager = userManager;
Expand All @@ -54,20 +57,31 @@ public GuiManager(
}

public void sendParcel(Player sender, Parcel parcel, List<ItemStack> items) {
Duration delay = parcel.priority()
? this.config.settings.priorityParcelSendDuration
: this.config.settings.parcelSendDuration;
this.parcelService.send(sender, parcel, items);
this.deliveryManager.create(parcel.uuid(), Instant.now().plus(delay));
this.parcelContentManager.create(parcel.uuid(), items);

ParcelSendTask task = new ParcelSendTask(
parcel,
this.parcelService,
this.deliveryManager
);

this.scheduler.runLaterAsync(task, delay);
// Check if the locker is full before sending
this.lockerManager.isLockerFull(parcel.destinationLocker()).thenAccept(isFull -> {
if (isFull) {
this.noticeService.create()
.notice(messages -> messages.parcel.lockerFull)
.player(sender.getUniqueId())
.send();
return;
}

Duration delay = parcel.priority()
? this.config.settings.priorityParcelSendDuration
: this.config.settings.parcelSendDuration;
this.parcelService.send(sender, parcel, items);
this.deliveryManager.create(parcel.uuid(), Instant.now().plus(delay));
this.parcelContentManager.create(parcel.uuid(), items);

ParcelSendTask task = new ParcelSendTask(
parcel,
this.parcelService,
this.deliveryManager
);

this.scheduler.runLaterAsync(task, delay);
});
}

public void collectParcel(Player player, Parcel parcel) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.eternalcode.parcellockers.locker;

import com.eternalcode.parcellockers.configuration.implementation.PluginConfig;
import com.eternalcode.parcellockers.locker.repository.LockerRepository;
import com.eternalcode.parcellockers.locker.validation.LockerValidationService;
import com.eternalcode.parcellockers.notification.NoticeService;
import com.eternalcode.parcellockers.parcel.repository.ParcelRepository;
import com.eternalcode.parcellockers.shared.Page;
import com.eternalcode.parcellockers.shared.PageResult;
import com.eternalcode.parcellockers.shared.Position;
Expand All @@ -19,15 +21,24 @@

public class LockerManager {

private final PluginConfig config;
private final LockerRepository lockerRepository;
private final LockerValidationService validationService;
private final ParcelRepository parcelRepository;

private final Cache<UUID, Locker> lockersByUUID;
private final Cache<Position, Locker> lockersByPosition;

public LockerManager(LockerRepository lockerRepository, LockerValidationService validationService) {
public LockerManager(
PluginConfig config,
LockerRepository lockerRepository,
LockerValidationService validationService,
ParcelRepository parcelRepository
) {
this.config = config;
this.lockerRepository = lockerRepository;
this.validationService = validationService;
this.parcelRepository = parcelRepository;

this.lockersByUUID = Caffeine.newBuilder()
.expireAfterAccess(Duration.ofHours(2))
Expand Down Expand Up @@ -148,6 +159,11 @@ public CompletableFuture<Void> deleteAll(CommandSender sender, NoticeService not
});
}

public CompletableFuture<Boolean> isLockerFull(UUID lockerUuid) {
return this.parcelRepository.countByDestinationLocker(lockerUuid)
.thenApply(count -> count >= this.config.settings.maxParcelsPerLocker);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe You can add option to set infinite number of parcels in the locer with value like -1 ? So this would be an option not a must.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will add

}

private void cacheAll() {
this.lockerRepository.fetchAll()
.thenAccept(optionalLockers -> optionalLockers.ifPresent(lockers -> lockers.forEach(locker -> {
Expand All @@ -156,3 +172,4 @@ private void cacheAll() {
})));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public interface ParcelService {

CompletableFuture<PageResult<Parcel>> getByReceiver(UUID receiver, Page page);


CompletableFuture<Integer> delete(UUID uuid);

CompletableFuture<Integer> delete(Parcel parcel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ public CompletableFuture<PageResult<Parcel>> getByReceiver(UUID receiver, Page p
});
}


private void cacheAll() {
this.parcelRepository.fetchAll()
.thenAccept(optional -> optional.ifPresent(parcels -> parcels.forEach(this::cache)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public interface ParcelRepository {

CompletableFuture<PageResult<Parcel>> fetchByReceiver(UUID receiver, Page page);

CompletableFuture<Integer> countByDestinationLocker(UUID destinationLocker);

CompletableFuture<Integer> delete(Parcel parcel);

CompletableFuture<Integer> delete(UUID uuid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class ParcelRepositoryOrmLite extends AbstractRepositoryOrmLite implement

private static final String RECEIVER_COLUMN = "receiver";
private static final String SENDER_COLUMN = "sender";
private static final String DESTINATION_LOCKER_COLUMN = "destinationLocker";

public ParcelRepositoryOrmLite(DatabaseManager databaseManager, Scheduler scheduler) {
super(databaseManager, scheduler);
Expand Down Expand Up @@ -70,6 +71,17 @@ public CompletableFuture<PageResult<Parcel>> fetchByReceiver(UUID receiver, Page
return this.fetchByPaged(receiver, page, RECEIVER_COLUMN);
}

@Override
public CompletableFuture<Integer> countByDestinationLocker(UUID destinationLocker) {
return this.action(ParcelTable.class, dao -> {
long count = dao.queryBuilder()
.where()
.eq(DESTINATION_LOCKER_COLUMN, destinationLocker)
.countOf();
return (int) count;
});
}

private CompletableFuture<PageResult<Parcel>> fetchByPaged(UUID key, Page page, String column) {
return this.action(
ParcelTable.class, dao -> {
Expand Down
Loading