1 /***************************************************************************
2  *                     Copyright © 2020 - Arianne                          *
3  ***************************************************************************
4  ***************************************************************************
5  *                                                                         *
6  *   This program is free software; you can redistribute it and/or modify  *
7  *   it under the terms of the GNU General Public License as published by  *
8  *   the Free Software Foundation; either version 2 of the License, or     *
9  *   (at your option) any later version.                                   *
10  *                                                                         *
11  ***************************************************************************/
12 package games.stendhal.server.entity.npc.condition;
13 
14 import org.apache.log4j.Logger;
15 
16 import games.stendhal.common.parser.Sentence;
17 import games.stendhal.server.entity.Entity;
18 import games.stendhal.server.entity.npc.ChatCondition;
19 import games.stendhal.server.entity.player.Player;
20 
21 
22 public class StatLevelComparisonCondition implements ChatCondition {
23 
24 	private final static Logger logger = Logger.getLogger(StatLevelComparisonCondition.class);
25 
26 	private final String statName;
27 	private final String comparisonOperator;
28 	private final int targetLevel;
29 
30 
StatLevelComparisonCondition(final String stat, final String operator, final int level)31 	public StatLevelComparisonCondition(final String stat, final String operator, final int level) {
32 		statName = stat;
33 		comparisonOperator = operator;
34 		targetLevel = level;
35 	}
36 
37 	@Override
fire(final Player player, final Sentence sentence, final Entity npc)38 	public boolean fire(final Player player, final Sentence sentence, final Entity npc) {
39 		if (!player.has(statName)) {
40 			logger.warn("Player stat not defined: " + statName);
41 			return false;
42 		}
43 
44 		final int statLevel = player.getInt(statName);
45 
46 		switch (comparisonOperator) {
47 		case "==":
48 			return statLevel == targetLevel;
49 		case "!=":
50 			return statLevel != targetLevel;
51 		case ">":
52 			return statLevel > targetLevel;
53 		case "<":
54 			return statLevel < targetLevel;
55 		case ">=":
56 			return statLevel >= targetLevel;
57 		case "<=":
58 			return statLevel <= targetLevel;
59 		default:
60 			return false;
61 		}
62 	}
63 }
64