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.core.rp.achievement.condition;
13 
14 import java.util.HashMap;
15 import java.util.List;
16 import java.util.Map;
17 
18 import games.stendhal.common.parser.Sentence;
19 import games.stendhal.server.entity.Entity;
20 import games.stendhal.server.entity.npc.ChatCondition;
21 import games.stendhal.server.entity.player.Player;
22 
23 
24 /**
25  * Checks if a player has bought a number of items.
26  */
27 public class BoughtNumberOfCondition implements ChatCondition {
28 
29 	final Map<String, Integer> itemList;
30 
31 
32 	/**
33 	 * Constructor to check a single item.
34 	 *
35 	 * @param itemName
36 	 * 		Name of the item.
37 	 * @param count
38 	 * 		Minimum required amount.
39 	 */
BoughtNumberOfCondition(final String itemName, final int count)40 	public BoughtNumberOfCondition(final String itemName, final int count) {
41 		itemList = new HashMap<>();
42 		itemList.put(itemName, count);
43 	}
44 
45 	/**
46 	 * Constructor to check multiple items.
47 	 *
48 	 * @param count
49 	 * 		Minimum required amount.
50 	 * @param itemName
51 	 * 		Names of the items.
52 	 */
BoughtNumberOfCondition(final int count, final String... itemName)53 	public BoughtNumberOfCondition(final int count, final String... itemName) {
54 		itemList = new HashMap<>();
55 		for (final String name: itemName) {
56 			itemList.put(name, count);
57 		}
58 	}
59 
60 	/**
61 	 * Constructor to check multiple items.
62 	 *
63 	 * @param count
64 	 * 		Minimum required amount.
65 	 * @param items
66 	 * 		List of item names.
67 	 */
BoughtNumberOfCondition(final int count, final List<String> items)68 	public BoughtNumberOfCondition(final int count, final List<String> items) {
69 		itemList = new HashMap<>();
70 		for (final String name: items) {
71 			itemList.put(name, count);
72 		}
73 	}
74 
75 	/**
76 	 * Constructor to check multiple items of different amounts.
77 	 *
78 	 * @param items
79 	 * 		List of item & the minimum required amount for each.
80 	 */
BoughtNumberOfCondition(final Map<String, Integer> items)81 	public BoughtNumberOfCondition(final Map<String, Integer> items) {
82 		itemList = items;
83 	}
84 
85 	@Override
fire(Player player, Sentence sentence, Entity npc)86 	public boolean fire(Player player, Sentence sentence, Entity npc) {
87 		for (final String item: itemList.keySet()) {
88 			final int required = itemList.get(item);
89 			final int actual = player.getQuantityOfBoughtItems(item);
90 
91 			if (actual < required) {
92 				return false;
93 			}
94 		}
95 
96 		return true;
97 	}
98 
99 
100 	@Override
toString()101 	public String toString() {
102 		final StringBuilder sb = new StringBuilder("BoughtNumberOf [");
103 
104 		final int itemCount = itemList.size();
105 		int idx = 0;
106 		for (final String item: itemList.keySet()) {
107 			sb.append(item + "=" + itemList.get(item));
108 			if (idx < itemCount - 1) {
109 				sb.append(",");
110 			}
111 			idx++;
112 		}
113 
114 		sb.append("]");
115 		return sb.toString();
116 	}
117 
118 	@Override
hashCode()119 	public int hashCode() {
120 		return 47 * itemList.hashCode();
121 	}
122 
123 	@Override
equals(final Object obj)124 	public boolean equals(final Object obj) {
125 		if (!(obj instanceof BoughtNumberOfCondition)) {
126 			return false;
127 		}
128 
129 		final BoughtNumberOfCondition other = (BoughtNumberOfCondition) obj;
130 		return itemList.equals(other.itemList);
131 	}
132 }
133