Skip to content
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

Improve StockHolder events #74

Merged
merged 11 commits into from
Oct 12, 2021
56 changes: 7 additions & 49 deletions api/src/main/java/net/okocraft/box/api/event/stock/StockEvent.java
Original file line number Diff line number Diff line change
@@ -1,66 +1,24 @@
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.
* <p>
* This event is triggered when a {@link StockHolder} is modified.
* A {@link StockHolderEvent} called when the stock of the {@link StockHolder} is modified.
* <p>
* 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}.
*
* @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}.
* <p>
* 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);
}
}
Original file line number Diff line number Diff line change
@@ -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{

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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}.
* <p>
* 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.");
}
}
}
Original file line number Diff line number Diff line change
@@ -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() +
"}";
}
}
Original file line number Diff line number Diff line change
@@ -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() +
"}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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() + "', " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}

Expand All @@ -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);
}
Expand Down