Skip to content

Commit

Permalink
merge preactivate and activate action events
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDrOfDoctoring committed Dec 7, 2023
1 parent 5037590 commit 14b3f30
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 26 deletions.
15 changes: 4 additions & 11 deletions src/api/java/de/teamlapen/vampirism/api/event/ActionEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ public ActionEvent(@NotNull IFactionPlayer<?> factionPlayer, @NotNull IAction<?>
}

/**
* Posted after an action fires. Use this to modify the cooldown or duration of an event
* Posted before an action fires. Use this to modify the cooldown or duration of action, or to prevent the action from activating.
*/
@Cancelable
public static class ActionActivatedEvent extends ActionEvent {

private int cooldown;
Expand All @@ -56,7 +57,7 @@ public void setCooldown(int cooldown) {
this.cooldown = cooldown;
}
/**
* @return The original duration of the action, this will return 0 if the action does not implement ILastingAction.
* @return The original duration of the action, this will return -1 if the action does not implement ILastingAction.
*/
public int getDuration() {
return duration;
Expand All @@ -71,6 +72,7 @@ public void setDuration(int duration) {
/**
* Posted when an action deactivates, either when deactivated manually or when out of time. As regular actions instantly deactivate, this only fires for actions that implement ILastingAction.
*/

public static class ActionDeactivatedEvent extends ActionEvent {
private final int remainingDuration;
private int cooldown;
Expand Down Expand Up @@ -131,14 +133,5 @@ public void setDeactivation(boolean deactivate) {
}

}
/**
* Posted before a player activates an action, can be used to prevent an action being fired.
*/
@Cancelable
public static class PreActionActivatedEvent extends ActionEvent {
public PreActionActivatedEvent(@NotNull IFactionPlayer<?> factionPlayer, @NotNull IAction<?> action) {
super(factionPlayer, action);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,7 @@ public void saveToNbt(@NotNull CompoundTag nbt) {

/**
* After server receives action toggle packet this is called.
* Actions here can be cancelled through {@link de.teamlapen.vampirism.api.event.ActionEvent.PreActionActivatedEvent}
* Actions can have their cooldown changed, or if a lasting action their duration changed as well through {@link de.teamlapen.vampirism.api.event.ActionEvent.ActionActivatedEvent}
* Actions can be cancelled, have their cooldown changed, or if a lasting action their duration changed as well through {@link de.teamlapen.vampirism.api.event.ActionEvent.ActionActivatedEvent}
* @param action Action being toggled
* @param context Context holding Block/Entity the player was looking at when activating if any
*
Expand All @@ -310,18 +309,20 @@ public IAction.PERM toggleAction(@NotNull IAction<T> action, IAction.ActivationC
if (this.player.getRepresentingPlayer().isSpectator()) return IAction.PERM.DISALLOWED;
if (!isActionUnlocked(action)) return IAction.PERM.NOT_UNLOCKED;
if (!isActionAllowedPermission(action)) return IAction.PERM.PERMISSION_DISALLOWED;
boolean isCancelled = VampirismEventFactory.firePreActionActivatedEvent(player, action);
if(isCancelled) return IAction.PERM.DISALLOWED;

IAction.PERM r = action.canUse(player);
if (r == IAction.PERM.ALLOWED) {

/**
* Only lasting actions have a cooldown, so regular actions will return a duration of -1.
*/
int duration = -1;
if (action instanceof ILastingAction) {
duration = ((ILastingAction<T>) action).getDuration(player);
}
ActionEvent.ActionActivatedEvent activationEvent = VampirismEventFactory.fireActionActivatedEvent(player, action, action.getCooldown(player), duration);
if(activationEvent.isCanceled()) return IAction.PERM.DISALLOWED;
if (action.onActivated(player, context)) {
ModStats.updateActionUsed(player.getRepresentingPlayer(), action);
int duration = 0;
if (action instanceof ILastingAction) {
duration = ((ILastingAction<T>) action).getDuration(player);
}
ActionEvent.ActionActivatedEvent activationEvent = VampirismEventFactory.fireActionActivatedEvent(player, action, action.getCooldown(player), duration);
//Even though lasting actions do not activate their cooldown until they deactivate
//we probably want to keep this here so that they are edited by one event.
int cooldown = activationEvent.getCooldown();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,6 @@ public static boolean fireMakeAggressive(@NotNull ITotem totem, @NotNull Village
MinecraftForge.EVENT_BUS.post(event);
return event;
}
public static boolean firePreActionActivatedEvent(@NotNull IFactionPlayer<?> factionPlayer, @NotNull IAction<?> action) {
ActionEvent.PreActionActivatedEvent event = new ActionEvent.PreActionActivatedEvent(factionPlayer, action);
MinecraftForge.EVENT_BUS.post(event);
return event.isCanceled();
}
public static @NotNull ActionEvent.ActionActivatedEvent fireActionActivatedEvent(@NotNull IFactionPlayer<?> factionPlayer, @NotNull IAction<?> action, int cooldown, int duration) {
ActionEvent.ActionActivatedEvent event = new ActionEvent.ActionActivatedEvent(factionPlayer, action, cooldown, duration);
MinecraftForge.EVENT_BUS.post(event);
Expand Down

0 comments on commit 14b3f30

Please sign in to comment.