diff --git a/api/src/main/java/net/okocraft/box/api/event/stock/StockEvent.java b/api/src/main/java/net/okocraft/box/api/event/stock/StockEvent.java index 2f6a1785fe..1b62eab897 100644 --- a/api/src/main/java/net/okocraft/box/api/event/stock/StockEvent.java +++ b/api/src/main/java/net/okocraft/box/api/event/stock/StockEvent.java @@ -1,23 +1,17 @@ package net.okocraft.box.api.event.stock; -import net.okocraft.box.api.event.BoxEvent; +import net.okocraft.box.api.event.stockholder.StockHolderEvent; import net.okocraft.box.api.model.stock.StockHolder; -import net.okocraft.box.api.model.stock.UserStockHolder; import org.jetbrains.annotations.NotNull; -import java.util.Objects; - /** - * A class that represents a {@link StockHolder} related event. - *

- * This event is triggered when a {@link StockHolder} is modified. + * A {@link StockHolderEvent} called when the stock of the {@link StockHolder} is modified. *

- * Therefore, you need to be very careful not to call methods - * that cause changes to a {@link StockHolder} in the event, as this may result in an infinite loop. + * You need to be very careful not to call methods that cause changes to a {@link StockHolder} in the event, + * as this may result in an infinite loop. */ -public class StockEvent extends BoxEvent { - - private final StockHolder stockHolder; +// TODO: move StockEvent classes to api.event.stockholder in version 5.0.0 +public class StockEvent extends StockHolderEvent { /** * The constructor of {@link StockEvent}. @@ -25,42 +19,6 @@ public class StockEvent extends BoxEvent { * @param stockHolder the stockholder of the event */ public StockEvent(@NotNull StockHolder stockHolder) { - this.stockHolder = Objects.requireNonNull(stockHolder); - } - - /** - * Gets the stockholder. - * - * @return the stockholder - */ - public @NotNull StockHolder getStockHolder() { - return stockHolder; - } - - /** - * Checks if the {@link StockHolder} of this event is a {@link UserStockHolder}. - * - * @return whether the {@link StockHolder} is a {@link UserStockHolder} or not - */ - public boolean isUserStockHolder() { - return stockHolder instanceof UserStockHolder; - } - - /** - * Gets the {@link StockHolder} as a {@link UserStockHolder}. - *

- * You must check if the {@link StockHolder} of this event - * is {@link UserStockHolder} using {@link #isUserStockHolder()} - * before calling this method. - * - * @return the {@link UserStockHolder} - * @throws IllegalStateException if the {@link StockHolder} of this event is not {@link UserStockHolder} - */ - public @NotNull UserStockHolder getUserStockHolder() { - if (stockHolder instanceof UserStockHolder userStockHolder) { - return userStockHolder; - } else { - throw new IllegalStateException("The StockHolder of this event is not UserStockHolder."); - } + super(stockHolder); } } diff --git a/api/src/main/java/net/okocraft/box/api/event/stock/StockSaveEvent.java b/api/src/main/java/net/okocraft/box/api/event/stock/StockSaveEvent.java index 0405cadaca..87442bd888 100644 --- a/api/src/main/java/net/okocraft/box/api/event/stock/StockSaveEvent.java +++ b/api/src/main/java/net/okocraft/box/api/event/stock/StockSaveEvent.java @@ -1,11 +1,16 @@ package net.okocraft.box.api.event.stock; import net.okocraft.box.api.model.stock.StockHolder; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; /** * A {@link StockEvent} called when the stockholder has been saved. + * + * @deprecated use {@link net.okocraft.box.api.event.stockholder.StockHolderSaveEvent} */ +@Deprecated(forRemoval = true) +@ApiStatus.ScheduledForRemoval(inVersion = "4.2.0") public class StockSaveEvent extends StockEvent{ /** diff --git a/api/src/main/java/net/okocraft/box/api/event/stockholder/StockHolderEvent.java b/api/src/main/java/net/okocraft/box/api/event/stockholder/StockHolderEvent.java new file mode 100644 index 0000000000..bb34eb0d95 --- /dev/null +++ b/api/src/main/java/net/okocraft/box/api/event/stockholder/StockHolderEvent.java @@ -0,0 +1,61 @@ +package net.okocraft.box.api.event.stockholder; + +import net.okocraft.box.api.event.BoxEvent; +import net.okocraft.box.api.model.stock.StockHolder; +import net.okocraft.box.api.model.stock.UserStockHolder; +import org.jetbrains.annotations.NotNull; + +import java.util.Objects; + +/** + * A class that represents a {@link StockHolder} related event. + */ +public class StockHolderEvent extends BoxEvent { + + private final StockHolder stockHolder; + + /** + * The constructor of {@link StockHolderEvent}. + * + * @param stockHolder the stockholder of the event + */ + public StockHolderEvent(@NotNull StockHolder stockHolder) { + this.stockHolder = Objects.requireNonNull(stockHolder); + } + + /** + * Gets the stockholder. + * + * @return the stockholder + */ + public @NotNull StockHolder getStockHolder() { + return stockHolder; + } + + /** + * Checks if the {@link StockHolder} of this event is a {@link UserStockHolder}. + * + * @return whether the {@link StockHolder} is a {@link UserStockHolder} or not + */ + public boolean isUserStockHolder() { + return stockHolder instanceof UserStockHolder; + } + + /** + * Gets the {@link StockHolder} as a {@link UserStockHolder}. + *

+ * You must check if the {@link StockHolder} of this event + * is {@link UserStockHolder} using {@link #isUserStockHolder()} + * before calling this method. + * + * @return the {@link UserStockHolder} + * @throws IllegalStateException if the {@link StockHolder} of this event is not {@link UserStockHolder} + */ + public @NotNull UserStockHolder getUserStockHolder() { + if (stockHolder instanceof UserStockHolder userStockHolder) { + return userStockHolder; + } else { + throw new IllegalStateException("The StockHolder of this event is not UserStockHolder."); + } + } +} diff --git a/api/src/main/java/net/okocraft/box/api/event/stockholder/StockHolderLoadEvent.java b/api/src/main/java/net/okocraft/box/api/event/stockholder/StockHolderLoadEvent.java new file mode 100644 index 0000000000..db354b18fe --- /dev/null +++ b/api/src/main/java/net/okocraft/box/api/event/stockholder/StockHolderLoadEvent.java @@ -0,0 +1,26 @@ +package net.okocraft.box.api.event.stockholder; + +import net.okocraft.box.api.model.stock.StockHolder; +import org.jetbrains.annotations.NotNull; + +/** + * A {@link StockHolderEvent} called when the stockholder is loaded. + */ +public class StockHolderLoadEvent extends StockHolderEvent { + + /** + * The constructor of {@link StockHolderLoadEvent}. + * + * @param stockHolder the loaded stockholder + */ + public StockHolderLoadEvent(@NotNull StockHolder stockHolder) { + super(stockHolder); + } + + @Override + public String toString() { + return "StockHolderLoadEvent{" + + "stockholder=" + getStockHolder() + + "}"; + } +} diff --git a/api/src/main/java/net/okocraft/box/api/event/stockholder/StockHolderSaveEvent.java b/api/src/main/java/net/okocraft/box/api/event/stockholder/StockHolderSaveEvent.java new file mode 100644 index 0000000000..3959bce0e9 --- /dev/null +++ b/api/src/main/java/net/okocraft/box/api/event/stockholder/StockHolderSaveEvent.java @@ -0,0 +1,29 @@ +package net.okocraft.box.api.event.stockholder; + +import net.okocraft.box.api.BoxProvider; +import net.okocraft.box.api.event.stock.StockSaveEvent; +import net.okocraft.box.api.model.stock.StockHolder; +import org.jetbrains.annotations.NotNull; + +/** + * A {@link StockHolderEvent} called when the stockholder is saved. + */ +public class StockHolderSaveEvent extends StockHolderEvent { + + /** + * The constructor of {@link StockHolderSaveEvent}. + * + * @param stockHolder the saved stockholder + */ + public StockHolderSaveEvent(@NotNull StockHolder stockHolder) { + super(stockHolder); + BoxProvider.get().getEventBus().callEvent(new StockSaveEvent(stockHolder)); // for compatibility + } + + @Override + public String toString() { + return "StockHolderSaveEvent{" + + "stockholder=" + getStockHolder() + + "}"; + } +} diff --git a/core/src/main/java/net/okocraft/box/core/listener/DebugListener.java b/core/src/main/java/net/okocraft/box/core/listener/DebugListener.java index a619c687a8..2577e32933 100644 --- a/core/src/main/java/net/okocraft/box/core/listener/DebugListener.java +++ b/core/src/main/java/net/okocraft/box/core/listener/DebugListener.java @@ -7,8 +7,8 @@ import net.okocraft.box.api.event.player.PlayerUnloadEvent; import net.okocraft.box.api.event.stock.StockDecreaseEvent; import net.okocraft.box.api.event.stock.StockIncreaseEvent; -import net.okocraft.box.api.event.stock.StockSaveEvent; import net.okocraft.box.api.event.stock.StockSetEvent; +import net.okocraft.box.api.event.stockholder.StockHolderSaveEvent; import org.jetbrains.annotations.NotNull; public class DebugListener { @@ -62,7 +62,7 @@ private void handleEvent(@NotNull BoxEvent event) { return; } - if (event instanceof StockSaveEvent saveEvent) { + if (event instanceof StockHolderSaveEvent saveEvent) { printLog( saveEvent.getEventName() + "{" + "stockholderName='" + saveEvent.getStockHolder().getName() + "', " + diff --git a/core/src/main/java/net/okocraft/box/core/listener/StockHolderListener.java b/core/src/main/java/net/okocraft/box/core/listener/StockHolderListener.java index 14b1c08d4c..93fbe29720 100644 --- a/core/src/main/java/net/okocraft/box/core/listener/StockHolderListener.java +++ b/core/src/main/java/net/okocraft/box/core/listener/StockHolderListener.java @@ -7,7 +7,6 @@ import net.okocraft.box.api.event.stock.StockDecreaseEvent; import net.okocraft.box.api.event.stock.StockEvent; import net.okocraft.box.api.event.stock.StockIncreaseEvent; -import net.okocraft.box.api.event.stock.StockSaveEvent; import net.okocraft.box.api.event.stock.StockSetEvent; import net.okocraft.box.api.model.stock.UserStockHolder; import net.okocraft.box.api.model.user.BoxUser; @@ -70,7 +69,6 @@ private void saveModifiedStockHolders(@NotNull AutoSaveStartEvent task) { private void save(@NotNull UserStockHolder userStockHolder) { try { BoxProvider.get().getStockManager().saveUserStock(userStockHolder).join(); - BoxProvider.get().getEventBus().callEvent(new StockSaveEvent(userStockHolder)); } catch (Exception e) { Optional.of(userStockHolder.getUser()) .map(BoxUser::getUUID) diff --git a/core/src/main/java/net/okocraft/box/core/model/manager/BoxStockManager.java b/core/src/main/java/net/okocraft/box/core/model/manager/BoxStockManager.java index 85925182b4..fdec8d6c5f 100644 --- a/core/src/main/java/net/okocraft/box/core/model/manager/BoxStockManager.java +++ b/core/src/main/java/net/okocraft/box/core/model/manager/BoxStockManager.java @@ -1,5 +1,8 @@ package net.okocraft.box.core.model.manager; +import net.okocraft.box.api.BoxProvider; +import net.okocraft.box.api.event.stockholder.StockHolderLoadEvent; +import net.okocraft.box.api.event.stockholder.StockHolderSaveEvent; import net.okocraft.box.api.model.manager.StockManager; import net.okocraft.box.api.model.stock.UserStockHolder; import net.okocraft.box.api.model.user.BoxUser; @@ -26,11 +29,15 @@ public BoxStockManager(@NotNull StockStorage stockStorage) { Objects.requireNonNull(user); return CompletableFuture.supplyAsync(() -> { + UserStockHolder stockHolder; try { - return stockStorage.loadUserStockHolder(user); + stockHolder = stockStorage.loadUserStockHolder(user); } catch (Exception e) { throw new RuntimeException("Could not load user stock holder (" + user.getUUID() + ")", e); } + + BoxProvider.get().getEventBus().callEvent(new StockHolderLoadEvent(stockHolder)); + return stockHolder; }, executor); } @@ -41,6 +48,7 @@ public BoxStockManager(@NotNull StockStorage stockStorage) { return CompletableFuture.runAsync(() -> { try { stockStorage.saveUserStockHolder(stockHolder); + BoxProvider.get().getEventBus().callEvent(new StockHolderSaveEvent(stockHolder)); } catch (Exception e) { throw new RuntimeException("Could not save user stock holder (" + stockHolder.getUser().getUUID() + ")", e); }