From ef4b11b2f4b9ac3fdecb76a627a3f9c8b58f8716 Mon Sep 17 00:00:00 2001 From: Hendrik Brummermann Date: Sun, 8 Oct 2023 09:02:27 +0200 Subject: [PATCH] implemented a ConditionalAction for use in rewardWith() --- .../entity/npc/action/ConditionalAction.java | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/games/stendhal/server/entity/npc/action/ConditionalAction.java diff --git a/src/games/stendhal/server/entity/npc/action/ConditionalAction.java b/src/games/stendhal/server/entity/npc/action/ConditionalAction.java new file mode 100644 index 00000000000..494dc1a894e --- /dev/null +++ b/src/games/stendhal/server/entity/npc/action/ConditionalAction.java @@ -0,0 +1,68 @@ +/*************************************************************************** + * (C) Copyright 2003-2023 - Stendhal * + *************************************************************************** + *************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ +package games.stendhal.server.entity.npc.action; + +import games.stendhal.common.parser.Sentence; +import games.stendhal.server.core.config.annotations.Dev; +import games.stendhal.server.core.config.annotations.Dev.Category; +import games.stendhal.server.entity.npc.ChatAction; +import games.stendhal.server.entity.npc.ChatCondition; +import games.stendhal.server.entity.npc.EventRaiser; +import games.stendhal.server.entity.player.Player; + +/** + * executes an actions, if and only if, a condition is met. + */ +@Dev(category=Category.IGNORE) +public class ConditionalAction implements ChatAction { + + private final ChatCondition condition; + private final ChatAction action; + + /** + * Creates a new ConditionalAction. + * + * @param action + * action to execute + */ + public ConditionalAction(final ChatCondition condition, final ChatAction action) { + this.condition = condition; + this.action = action; + } + + @Override + public void fire(final Player player, final Sentence sentence, final EventRaiser npc) { + if (condition.fire(player, sentence, player)) { + action.fire(player, sentence, npc); + } + } + + @Override + public String toString() { + return "ConditionalAction <" + condition + ", " + action + ">"; + } + + @Override + public int hashCode() { + return 8363 * condition.hashCode() * action.hashCode(); + } + + @Override + public boolean equals(final Object obj) { + if (!(obj instanceof ConditionalAction)) { + return false; + } + final ConditionalAction other = (ConditionalAction) obj; + return condition.equals(other.condition) && action.equals(other.action); + } + +}