Skip to content

Commit

Permalink
Refactor ActionExecutor to use player UUID and cleanup context
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverSchlueter committed Jan 11, 2025
1 parent 7a1d961 commit b40666f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import de.oliver.fancynpcs.api.actions.ActionTrigger;
import de.oliver.fancynpcs.api.actions.NpcAction;
import de.oliver.fancynpcs.api.actions.types.BlockUntilDoneAction;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
* Context for executing a sequence of NPC actions initiated by different triggers.
Expand All @@ -30,7 +32,7 @@ public class ActionExecutionContext {
/**
* The player involved in the action, may be null if no player is involved.
*/
private final @Nullable Player player;
private final @Nullable UUID player;

/**
* A list of NpcActionData instances representing the sequence of actions
Expand Down Expand Up @@ -60,7 +62,7 @@ public class ActionExecutionContext {
* @param npc the NPC that the action is being executed on
* @param player the player involved in the action, may be null if no player is involved
*/
public ActionExecutionContext(ActionTrigger trigger, Npc npc, @Nullable Player player) {
public ActionExecutionContext(ActionTrigger trigger, Npc npc, @Nullable UUID player) {
this.trigger = trigger;
this.npc = npc;
this.player = player;
Expand Down Expand Up @@ -164,10 +166,18 @@ public List<NpcAction.NpcActionData> getActions() {
return actions;
}

public @Nullable Player getPlayer() {
public UUID getPlayerUUID() {
return player;
}

public @Nullable Player getPlayer() {
if (player == null) {
return null;
}

return Bukkit.getPlayer(player);
}

public int getActionIndex() {
return actionIndex;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,18 @@ public static void execute(ActionTrigger trigger, Npc npc, Player player) {
}
}

ActionExecutionContext context = new ActionExecutionContext(trigger, npc, player);
ActionExecutionContext context = new ActionExecutionContext(trigger, npc, player.getUniqueId());
runningContexts.put(key, context);

FancyNpcsPlugin.get().newThread("FancyNpcs-ActionExecutor", () -> {
while (context.hasNext()) {
context.runNext();
}
context.terminate();

runningContexts.remove(key);
}).start();

}

private static String getKey(ActionTrigger trigger, Npc npc, Player player) {
Expand Down

0 comments on commit b40666f

Please sign in to comment.