Skip to content

Commit

Permalink
Methods to delete transition matching state, trigger, & condition
Browse files Browse the repository at this point in the history
  • Loading branch information
AntumDeluge committed Jun 13, 2024
1 parent d1c9618 commit 16a6731
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
49 changes: 49 additions & 0 deletions src/games/stendhal/server/entity/npc/SpeakerNPC.java
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,56 @@ public boolean del(final String label) {
return(engine.remove(label));
}

/**
* Removes matching transitions.
*
* @param state
* @param trigger
* @param condition
* @return
* {@code true} if at least one transition was removed.
*/
public boolean del(final ConversationStates state, final Expression trigger,
final ChatCondition condition) {
return engine.remove(state, trigger, condition);
}

/**
* Wrapper method.
*
* Lua struggles with overloaded methods, so create a wrapper with a different name.
*
* @param state
* @param trigger
* @param condition
* @return
* {@code true} if at least one transition was removed.
*/
public boolean removeTransition(final ConversationStates state, final Object trigger,
final ChatCondition condition) {
Expression expr;
if (trigger instanceof Expression) {
expr = (Expression) trigger;
} else if (trigger instanceof String) {
expr = new Expression(String.valueOf(trigger), ExpressionType.UNKNOWN);
} else if (trigger instanceof List) {
final List<?> l = (List<?>) trigger;
boolean res = false;
for (final Object obj: l) {
if (obj instanceof String) {
if (del(state, new Expression(String.valueOf(obj), ExpressionType.UNKNOWN), condition)) {
res = true;
}
}
}
return res;
} else {
logger.warn("Unknown trigger:" + trigger);
return false;
}

return del(state, expr, condition);
}

public void listenTo(final Player player, final String text) {
tell(player, text);
Expand Down
21 changes: 20 additions & 1 deletion src/games/stendhal/server/entity/npc/fsm/Engine.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// $Id$
/***************************************************************************
* (C) Copyright 2003-2023 - Stendhal *
* (C) Copyright 2003-2024 - Stendhal *
***************************************************************************
***************************************************************************
* *
Expand Down Expand Up @@ -341,6 +341,25 @@ public boolean remove(final String label) {
return res;
}

/**
* Removes matching transitions.
*
* @param state
* @param trigger
* @param condition
* @return
* {@code true} if at least one transition was removed.
*/
public boolean remove(final ConversationStates state, final Expression trigger,
final ChatCondition condition) {
Transition transition = get(state, trigger, condition);
while (transition != null) {
stateTransitionTable.remove(transition);
transition = get(state, trigger, condition);
}
return stateTransitionTable.indexOf(transition) < 0;
}

/**
* Create a collection of trigger expressions from trigger strings
* while checking for duplicate transitions.
Expand Down

0 comments on commit 16a6731

Please sign in to comment.