Skip to content

Commit

Permalink
improved onUpdate event
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDrOfDoctoring committed Dec 7, 2023
1 parent c9c8993 commit 0876713
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 22 deletions.
18 changes: 2 additions & 16 deletions src/api/java/de/teamlapen/vampirism/api/event/ActionEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ 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 @@ -105,32 +104,19 @@ public void setCooldown(int cooldown) {
/**
* 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.
*/
@HasResult
public static class ActionUpdateEvent extends ActionEvent {
private final int remainingDuration;
private boolean shouldDeactivate;
public ActionUpdateEvent(@NotNull IFactionPlayer<?> factionPlayer, @NotNull IAction<?> action, int remainingDuration, boolean shouldDeactivate) {
public ActionUpdateEvent(@NotNull IFactionPlayer<?> factionPlayer, @NotNull IAction<?> action, int remainingDuration) {
super(factionPlayer, action);
this.remainingDuration = remainingDuration;
this.shouldDeactivate = shouldDeactivate;
}
/**
* @return The remaining duration of the action, in ticks.
*/
public int getRemainingDuration() {
return this.remainingDuration;
}
/**
* @return Whether the lasting action should be deactivated
*/
public boolean shouldDeactivate() {
return this.shouldDeactivate;
}
/**
* @param deactivate If set to true, the action will deactivate. If set to false, the action will not deactivate at all on that update.
*/
public void setDeactivation(boolean deactivate) {
this.shouldDeactivate = deactivate;
}

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Entity;
import net.minecraftforge.eventbus.api.Event;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -427,9 +428,20 @@ public boolean updateActions() {

dirty = true;
} else {
boolean shouldDeactivate= action.onUpdate(player);
boolean shouldReallyDeactivate = VampirismEventFactory.fireActionUpdateEvent(player, action, newtimer, shouldDeactivate);
if (shouldReallyDeactivate) {
/**
* If the event result is DENY, the lasting action will always be deactivated next tick and won't call {@link de.teamlapen.vampirism.api.entity.player.actions.ILastingAction#onUpdate(de.teamlapen.vampirism.api.entity.player.IFactionPlayer)}.
* If the event result is ALLOW, the lasting action will call onUpdate, but the return value will be ignored.
* If its the default, there will be no change, onUpdate will be called and deactivated if it should.
*/
boolean shouldDeactivate = true;
Event.Result eventResult = VampirismEventFactory.fireActionUpdateEvent(player, action, newtimer);
if(eventResult != Event.Result.DENY) {
shouldDeactivate = action.onUpdate(player);
}
if(eventResult == Event.Result.ALLOW) {
shouldDeactivate = false;
}
if (shouldDeactivate) {
entry.setValue(1); //Value of means they are deactivated next tick and onUpdate is not called again
} else {
ModStats.updateActionTime(player.getRepresentingPlayer(), action);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ public static int fireActionDeactivatedEvent(@NotNull IFactionPlayer<?> factionP
MinecraftForge.EVENT_BUS.post(event);
return event.getCooldown();
}
public static boolean fireActionUpdateEvent(@NotNull IFactionPlayer<?> factionPlayer, @NotNull IAction<?> action, int remainingDuration, boolean shouldDeactivate) {
ActionEvent.ActionUpdateEvent event = new ActionEvent.ActionUpdateEvent(factionPlayer, action, remainingDuration, shouldDeactivate);
public static Event.Result fireActionUpdateEvent(@NotNull IFactionPlayer<?> factionPlayer, @NotNull IAction<?> action, int remainingDuration) {
ActionEvent.ActionUpdateEvent event = new ActionEvent.ActionUpdateEvent(factionPlayer, action, remainingDuration);
MinecraftForge.EVENT_BUS.post(event);
return event.shouldDeactivate();
return event.getResult();
}

}

0 comments on commit 0876713

Please sign in to comment.