-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented a ConditionalAction for use in rewardWith()
- Loading branch information
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
src/games/stendhal/server/entity/npc/action/ConditionalAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |