diff --git a/src/games/stendhal/server/entity/npc/SpeakerNPC.java b/src/games/stendhal/server/entity/npc/SpeakerNPC.java index 02a00ab45af..f980488f5ea 100644 --- a/src/games/stendhal/server/entity/npc/SpeakerNPC.java +++ b/src/games/stendhal/server/entity/npc/SpeakerNPC.java @@ -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); diff --git a/src/games/stendhal/server/entity/npc/fsm/Engine.java b/src/games/stendhal/server/entity/npc/fsm/Engine.java index 266ea9d92d5..0bc80d966a6 100644 --- a/src/games/stendhal/server/entity/npc/fsm/Engine.java +++ b/src/games/stendhal/server/entity/npc/fsm/Engine.java @@ -1,6 +1,6 @@ // $Id$ /*************************************************************************** - * (C) Copyright 2003-2023 - Stendhal * + * (C) Copyright 2003-2024 - Stendhal * *************************************************************************** *************************************************************************** * * @@ -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.