Skip to content

Commit

Permalink
add skill handler events
Browse files Browse the repository at this point in the history
  • Loading branch information
Cheaterpaul committed Jul 25, 2024
1 parent 7854ebd commit ca81127
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.mojang.datafixers.util.Either;
import de.teamlapen.vampirism.api.entity.factions.IFaction;
import de.teamlapen.vampirism.api.entity.factions.ISkillTree;
import de.teamlapen.vampirism.api.entity.player.IFactionPlayer;
import de.teamlapen.vampirism.api.entity.player.ISkillPlayer;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
Expand All @@ -15,7 +14,7 @@
/**
* Skill that tha unlocks abilities for a player.
*/
public interface ISkill<T extends IFactionPlayer<T> & ISkillPlayer<T>> extends ISkillLike<T> {
public interface ISkill<T extends ISkillPlayer<T>> extends ISkillLike<T> {
/**
* The description for this skill or null if there is no description.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public void setCancelMessage(@NotNull Component message) {
/**
* 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<T extends IFactionPlayer<T> & ISkillPlayer<T>> extends ActionEvent<T, ILastingAction<T>> {
public static class ActionDeactivatedEvent<T extends ISkillPlayer<T>> extends ActionEvent<T, ILastingAction<T>> {
private final int remainingDuration;
private int cooldown;
private boolean ignoreCooldown;
Expand Down Expand Up @@ -197,7 +197,7 @@ public void setFullCooldown(boolean fullCooldown) {
/**
* 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 {@link de.teamlapen.vampirism.api.entity.player.actions.ILastingAction}.
*/
public static class ActionUpdateEvent<T extends IFactionPlayer<T> & ISkillPlayer<T>> extends ActionEvent<T, ILastingAction<T>> {
public static class ActionUpdateEvent<T extends ISkillPlayer<T>> extends ActionEvent<T, ILastingAction<T>> {
private final int remainingDuration;
private boolean deactivate;
private boolean skipActionUpdate;
Expand Down
119 changes: 119 additions & 0 deletions src/api/java/de/teamlapen/vampirism/api/event/SkillEvents.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package de.teamlapen.vampirism.api.event;

import de.teamlapen.vampirism.api.entity.player.ISkillPlayer;
import de.teamlapen.vampirism.api.entity.player.skills.ISkill;
import de.teamlapen.vampirism.api.entity.player.skills.ISkillHandler;
import net.minecraft.core.Holder;
import net.minecraft.world.entity.player.Player;
import net.neoforged.bus.api.Event;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* Event related to any skill changes of players
*/
public abstract class SkillEvents<T extends ISkillPlayer<T>, Z extends ISkill<T>> extends Event {

@NotNull
private final T factionPlayer;
@NotNull
private final Holder<Z> skill;

@ApiStatus.Internal
public SkillEvents(@NotNull T skillPlayer, @NotNull Holder<Z> skill) {
this.factionPlayer = skillPlayer;
this.skill = skill;
}

/**
* @return The skill Player for which this event is fired
*/
public @NotNull ISkillPlayer<?> getFactionPlayer() {
return this.factionPlayer;
}

/**
* @return The skill the event is firing for.
*/
public @NotNull Holder<Z> skill() {
return this.skill;
}

/**
* @return The player for which this event is fired
*/
@NotNull
public Player getPlayer() {
return this.factionPlayer.asEntity();
}

/**
* This event is posted before the skill check is conducted.
* If {@link #result} is set using {@link #setResult(de.teamlapen.vampirism.api.entity.player.skills.ISkillHandler.Result)} the check will be skipped. and {@link #getResult()} will be used as the result.
*/
public static class SkillUnlockCheckEvent<T extends ISkillPlayer<T>> extends SkillEvents<T, ISkill<T>> {

@Nullable
private ISkillHandler.Result result;

@ApiStatus.Internal
@SuppressWarnings("unchecked")
public SkillUnlockCheckEvent(@NotNull T skillPlayer, @NotNull Holder<? extends ISkill<?>> skill) {
super(skillPlayer, (Holder<ISkill<T>>) skill);
}

/**
* Set the result of {@link de.teamlapen.vampirism.api.entity.player.skills.ISkillHandler#canSkillBeEnabled(net.minecraft.core.Holder)}
*/
public void setResult(@Nullable ISkillHandler.Result result) {
this.result = result;
}

/**
* The current result if this event
*/
@Nullable
public ISkillHandler.Result getResult() {
return this.result;
}

}

/**
* Fired when a skill is disabled for a player
*/
public static class SkillDisableEvent<T extends ISkillPlayer<T>> extends SkillEvents<T, ISkill<T>> {

@ApiStatus.Internal
@SuppressWarnings("unchecked")
public SkillDisableEvent(@NotNull T skillPlayer, @NotNull Holder<? extends ISkill<?>> skill) {
super(skillPlayer, (Holder<ISkill<T>>) skill);
}

}

/**
* Fired when a skill is enabled for a player
*/
public static class SkillEnableEvent<T extends ISkillPlayer<T>> extends SkillEvents<T, ISkill<T>> {

private final boolean fromLoading;

@ApiStatus.Internal
@SuppressWarnings("unchecked")
public SkillEnableEvent(@NotNull T factionPlayer, @NotNull Holder<? extends ISkill<?>> skill, boolean fromLoading) {
super(factionPlayer, (Holder<ISkill<T>>) skill);
this.fromLoading = fromLoading;
}

/**
* @return If the skill is enabled because the player is loaded from the save file
*/
public boolean isFromLoading() {
return fromLoading;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import de.teamlapen.vampirism.core.ModStats;
import de.teamlapen.vampirism.data.ISkillTreeData;
import de.teamlapen.vampirism.util.RegUtil;
import de.teamlapen.vampirism.util.VampirismEventFactory;
import net.minecraft.core.Holder;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.Registry;
Expand Down Expand Up @@ -62,6 +63,11 @@ public ISkillTreeData getTreeData() {

@Override
public @NotNull Result canSkillBeEnabled(@NotNull Holder<ISkill<?>> skill) {
var preResult = VampirismEventFactory.fireSkillUnlockCheckEvent(this.player, skill);
if (preResult != null) {
return preResult;
}

if (player.asEntity().getEffect(ModEffects.OBLIVION) != null) {
return Result.LOCKED_BY_PLAYER_STATE;
}
Expand Down Expand Up @@ -91,6 +97,7 @@ public ISkillTreeData getTreeData() {

public void disableAllSkills() {
for (Holder<ISkill<T>> skill : enabledSkills) {
VampirismEventFactory.fireSkillDisabledEvent(player, skill);
skill.value().onDisable(player);
}
enabledSkills.clear();
Expand All @@ -100,6 +107,7 @@ public void disableAllSkills() {
@Override
public void disableSkill(@NotNull Holder<ISkill<T>> skill) {
if (enabledSkills.remove(skill)) {
VampirismEventFactory.fireSkillDisabledEvent(player, skill);
skill.value().onDisable(player);
dirty = true;
}
Expand All @@ -108,6 +116,7 @@ public void disableSkill(@NotNull Holder<ISkill<T>> skill) {
@Override
public void enableSkill(@NotNull Holder<ISkill<T>> skill, boolean fromLoading) {
if (!enabledSkills.contains(skill)) {
VampirismEventFactory.fireSkillEnableEvent(player, skill, fromLoading);
skill.value().onEnable(player);
enabledSkills.add(skill);
if (!fromLoading) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@

import de.teamlapen.vampirism.api.entity.factions.IFactionPlayerHandler;
import de.teamlapen.vampirism.api.entity.factions.IPlayableFaction;
import de.teamlapen.vampirism.api.entity.player.IFactionPlayer;
import de.teamlapen.vampirism.api.entity.player.ISkillPlayer;
import de.teamlapen.vampirism.api.entity.player.actions.IAction;
import de.teamlapen.vampirism.api.entity.player.actions.ILastingAction;
import de.teamlapen.vampirism.api.entity.player.skills.ISkill;
import de.teamlapen.vampirism.api.entity.player.skills.ISkillHandler;
import de.teamlapen.vampirism.api.entity.player.vampire.IDrinkBloodContext;
import de.teamlapen.vampirism.api.entity.player.vampire.IVampirePlayer;
import de.teamlapen.vampirism.api.entity.vampire.IVampire;
import de.teamlapen.vampirism.api.event.ActionEvent;
import de.teamlapen.vampirism.api.event.BloodDrinkEvent;
import de.teamlapen.vampirism.api.event.PlayerFactionEvent;
import de.teamlapen.vampirism.api.event.VampirismVillageEvent;
import de.teamlapen.vampirism.api.event.*;
import de.teamlapen.vampirism.api.world.ITotem;
import net.minecraft.core.Holder;
import net.minecraft.world.entity.Mob;
Expand Down Expand Up @@ -87,24 +85,40 @@ public static boolean fireMakeAggressive(@NotNull ITotem totem, @NotNull Village

@SuppressWarnings("unchecked")
@NotNull
public static <T extends IFactionPlayer<T> & ISkillPlayer<T>> ActionEvent.ActionActivatedEvent<T> fireActionActivatedEvent(@NotNull T factionPlayer, @NotNull Holder<? extends IAction<T>> action, int cooldown, int duration) {
public static <T extends ISkillPlayer<T>> ActionEvent.ActionActivatedEvent<T> fireActionActivatedEvent(@NotNull T factionPlayer, @NotNull Holder<? extends IAction<T>> action, int cooldown, int duration) {
ActionEvent.ActionActivatedEvent<T> event = new ActionEvent.ActionActivatedEvent<>(factionPlayer, (Holder<IAction<T>>) action, cooldown, duration);
NeoForge.EVENT_BUS.post(event);
return event;
}

@SuppressWarnings("unchecked")
public static <T extends IFactionPlayer<T> & ISkillPlayer<T>> ActionEvent.ActionDeactivatedEvent<T> fireActionDeactivatedEvent(@NotNull T factionPlayer, @NotNull Holder<? extends ILastingAction<T>> action, int remainingDuration, int cooldown, boolean ignoreCooldown, boolean fullCooldown) {
public static <T extends ISkillPlayer<T>> ActionEvent.ActionDeactivatedEvent<T> fireActionDeactivatedEvent(@NotNull T factionPlayer, @NotNull Holder<? extends ILastingAction<T>> action, int remainingDuration, int cooldown, boolean ignoreCooldown, boolean fullCooldown) {
ActionEvent.ActionDeactivatedEvent<T> event = new ActionEvent.ActionDeactivatedEvent<>(factionPlayer, (Holder<ILastingAction<T>>) action, remainingDuration, cooldown, ignoreCooldown, fullCooldown);
NeoForge.EVENT_BUS.post(event);
return event;
}

@SuppressWarnings("unchecked")
public static <T extends IFactionPlayer<T> & ISkillPlayer<T>> ActionEvent.ActionUpdateEvent<T> fireActionUpdateEvent(@NotNull T factionPlayer, @NotNull Holder<? extends ILastingAction<T>> action, int remainingDuration) {
public static <T extends ISkillPlayer<T>> ActionEvent.ActionUpdateEvent<T> fireActionUpdateEvent(@NotNull T factionPlayer, @NotNull Holder<? extends ILastingAction<T>> action, int remainingDuration) {
ActionEvent.ActionUpdateEvent<T> event = new ActionEvent.ActionUpdateEvent<>(factionPlayer, (Holder<ILastingAction<T>>) action, remainingDuration);
NeoForge.EVENT_BUS.post(event);
return event;
}

public static <T extends ISkillPlayer<T>> ISkillHandler.Result fireSkillUnlockCheckEvent(@NotNull T factionPlayer, @NotNull Holder<? extends ISkill<?>> skill) {
var event = new SkillEvents.SkillUnlockCheckEvent<>(factionPlayer, skill);
NeoForge.EVENT_BUS.post(event);
return event.getResult();
}

public static <T extends ISkillPlayer<T>> void fireSkillDisabledEvent(@NotNull T factionPlayer, @NotNull Holder<? extends ISkill<?>> skill) {
var event = new SkillEvents.SkillDisableEvent<>(factionPlayer, skill);
NeoForge.EVENT_BUS.post(event);
}

public static <T extends ISkillPlayer<T>> void fireSkillEnableEvent(@NotNull T factionPlayer, @NotNull Holder<? extends ISkill<?>> skill, boolean fromLoading) {
var event = new SkillEvents.SkillEnableEvent<>(factionPlayer, skill, fromLoading);
NeoForge.EVENT_BUS.post(event);
}

}

0 comments on commit ca81127

Please sign in to comment.