forked from Codisimus/PhatLoots
-
Notifications
You must be signed in to change notification settings - Fork 17
/
LootCollection.java
353 lines (326 loc) · 12.4 KB
/
LootCollection.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package com.codisimus.plugins.phatloots.loot;
import com.codisimus.plugins.phatloots.PhatLoot;
import com.codisimus.plugins.phatloots.PhatLoots;
import com.codisimus.plugins.phatloots.PhatLootsConfig;
import com.codisimus.plugins.phatloots.util.PhatLootsUtil;
import com.codisimus.plugins.phatloots.gui.Button;
import com.codisimus.plugins.phatloots.gui.InventoryListener;
import java.util.*;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.configuration.serialization.SerializableAs;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
/**
* A Collection is a list of Loots which has a unique name
*
* @author Codisimus
*/
@SerializableAs("LootCollection")
public class LootCollection extends Loot {
public static boolean allowDuplicates;
public String name;
public int lowerNumberOfLoots;
public int upperNumberOfLoots;
private LinkedList<Loot> lootList;
/**
* Adds A LootCollection as Loot
*/
private static class AddCollectionButton extends Button {
private AddCollectionButton(ItemStack item) {
super(item);
}
@Override
public boolean onClick(ClickType type, Inventory inv, PhatLoot phatLoot, List<Loot> lootList) {
//Find a unique collection name for the PhatLoot
int i = 1;
while (phatLoot.findCollection(String.valueOf(i)) != null) {
i++;
}
//Add a new Collection
lootList.add(new LootCollection(String.valueOf(i)));
return true;
}
}
/**
* Registers the AddCollectionButton in the PhatLoot GUI
*/
public static void registerButton() {
//Register the Add Collection Button
ItemStack item = new ItemStack(Material.ENDER_CHEST);
ItemMeta info = Bukkit.getItemFactory().getItemMeta(item.getType());
info.setDisplayName("§2Add new collection...");
item.setItemMeta(info);
InventoryListener.registerButton(new AddCollectionButton(item));
}
/**
* Constructs a new LootCollection with the given name
*
* @param name The given name
*/
public LootCollection(String name) {
this.name = name;
lowerNumberOfLoots = PhatLootsConfig.defaultLowerNumberOfLoots;
upperNumberOfLoots = PhatLootsConfig.defaultUpperNumberOfLoots;
lootList = new LinkedList<>();
}
/**
* Constructs a new LootCollection with the given name and range
*
* @param name The given name
* @param lowerNumberOfLoots The lower bound of the range
* @param upperNumberOfLoots The upper bound of the range
*/
public LootCollection(String name, int lowerNumberOfLoots, int upperNumberOfLoots) {
this.name = name;
this.lowerNumberOfLoots = lowerNumberOfLoots;
this.upperNumberOfLoots = upperNumberOfLoots;
lootList = new LinkedList<>();
}
/**
* Constructs a new LootCollection from a Configuration Serialized phase
*
* @param map The map of data values
*/
public LootCollection(Map<String, Object> map) {
String currentLine = null; //The value that is about to be loaded (used for debugging)
try {
probability = (Double) map.get(currentLine = "Probability");
name = (String) map.get(currentLine = "Name");
lowerNumberOfLoots = (Integer) map.get(currentLine = "LowerNumberOfLoots");
upperNumberOfLoots = (Integer) map.get(currentLine = "UpperNumberOfLoots");
lootList = new LinkedList<>((List<Loot>) map.get(currentLine = "LootList"));
} catch (Exception ex) {
//Print debug messages
PhatLoots.logger.severe("Failed to load LootCollection line: " + currentLine);
PhatLoots.logger.severe("of PhatLoot: " + (PhatLoot.current == null ? "unknown" : PhatLoot.current));
PhatLoots.logger.severe("Last successfull load was...");
PhatLoots.logger.severe("PhatLoot: " + (PhatLoot.last == null ? "unknown" : PhatLoot.last));
PhatLoots.logger.severe("Loot: " + (Loot.last == null ? "unknown" : Loot.last.toString()));
}
}
/**
* Rolls for each Loot within the collection
*
* @param lootBundle The loot that has been rolled for
* @param lootingBonus The increased chance of getting rarer loots
*/
@Override
public void getLoot(LootBundle lootBundle, double lootingBonus) {
if (isRollForEach()) { //Roll for each Loot individually
for (Loot loot : lootList) {
if (loot.rollForLoot(lootingBonus)) {
if (loot.rollForLoot(lootingBonus)) {
loot.getLoot(lootBundle, lootingBonus);
}
}
}
} else { //Roll for all Loot collectively
//Roll for the amount of loots
int numberOfLoots = lowerNumberOfLoots == upperNumberOfLoots
? lowerNumberOfLoots
: PhatLootsUtil.rollForInt(lowerNumberOfLoots, upperNumberOfLoots);
//Make sure there are items that will be looted before entering the loop
if (!lootList.isEmpty()) {
//Sort the loot from lowest probability to highest
Collections.sort(lootList);
int numberLooted = 0;
LinkedList<Loot> removed = new LinkedList<>();
while (numberLooted < numberOfLoots) {
//Calculate the total probability to determine the maximum amount that can be rolled
double total = 0;
for (Loot loot : lootList) {
total += loot.probability;
}
//Roll a number between 0 and total and then subtract the looting bonus
//We subtract because a lower roll is better in this case
double roll = PhatLootsUtil.rollForDouble(total) - lootingBonus;
for (Loot loot : lootList) {
//Subtract the probability of each Loot from the roll until we drop below 0
roll -= loot.getProbability();
if (roll <= 0) {
//Give this loot
loot.getLoot(lootBundle, lootingBonus);
if (!allowDuplicates) {
removed.add(loot);
lootList.remove(loot);
}
break;
}
}
numberLooted++;
}
//Readd all removed Loot
lootList.addAll(removed);
}
}
}
/**
* Returns the list of loot in the collection
*
* @return The list of Loot in this Collection
*/
public LinkedList<Loot> getLootList() {
return lootList;
}
/**
* Returns the information of the LootCollection in the form of an ItemStack
*
* @return An ItemStack representation of the collection
*/
@Override
public ItemStack getInfoStack() {
//A LootCollection is represented by an Ender Chest
ItemStack infoStack = new ItemStack(Material.ENDER_CHEST);
//Set the display name to the name of the collection
ItemMeta info = Bukkit.getItemFactory().getItemMeta(infoStack.getType());
info.setDisplayName("§2" + name + " (Collection)");
//Add more specific details of the collection
List<String> details = new ArrayList<>();
details.add("§1Probability: §6" + probability);
if (isRollForEach()) {
details.add("§6Each loot is rolled for");
} else if (lowerNumberOfLoots == upperNumberOfLoots) {
details.add("§1Number of Loots: §6" + lowerNumberOfLoots);
} else {
details.add("§1Number of Loots: §6" + lowerNumberOfLoots + '-' + upperNumberOfLoots);
}
//Construct the ItemStack and return it
info.setLore(details);
infoStack.setItemMeta(info);
return infoStack;
}
/**
* @return false because this type of Loot has no toggleable settings
*/
@Override
public boolean onToggle(ClickType click) {
return false;
}
/**
* Modifies the amount associated with the Loot
*
* @param amount The amount to modify by (may be negative)
* @param both true if both lower and upper ranges should be modified, false for only the upper range
* @return true if the Loot InfoStack should be refreshed
*/
@Override
public boolean modifyAmount(int amount, boolean both) {
if (both) {
lowerNumberOfLoots += amount;
//Do not allow negative amounts
if (lowerNumberOfLoots < 0) {
lowerNumberOfLoots = 0;
}
}
upperNumberOfLoots += amount;
//Do not allow negative amounts
if (upperNumberOfLoots < lowerNumberOfLoots) {
upperNumberOfLoots = lowerNumberOfLoots;
}
return true;
}
/**
* Resets the amount of Loot to 1
*
* @return true if the Loot InfoStack should be refreshed
*/
@Override
public boolean resetAmount() {
lowerNumberOfLoots = 1;
upperNumberOfLoots = 1;
return true;
}
/**
* Returns true if the number of loots for this collection is not positive
*
* @return true if each loot should be rolled for separately
*/
public boolean isRollForEach() {
return upperNumberOfLoots <= 0;
}
/**
* Adds the given loot to this collection
*
* @param target the given Loot
* @return true if it was successfully added
*/
public boolean addLoot(Loot target) {
for (Loot loot : lootList) {
if (loot.equals(target)) {
return false;
}
}
lootList.add(target);
return true;
}
/**
* Removes the given loot from this collection
*
* @param target the given Loot
* @return false if it was not found
*/
public boolean removeLoot(Loot target) {
Iterator<Loot> itr = lootList.iterator();
while (itr.hasNext()) {
if (itr.next().equals(target)) {
itr.remove();
return true;
}
}
return false;
}
/**
* Returns the collection of the given name
*
* @param target The name of the collection to search for
* @return The LootCollection or null if it was not found
*/
public LootCollection findCollection(String target) {
//First check if this is the collection in question
if (name.equals(target)) {
return this;
}
//Scan each loot object
for (Loot loot : lootList) {
if (loot instanceof LootCollection) {
//Recursively look for the collection
LootCollection coll = ((LootCollection) loot).findCollection(target);
if (coll != null) {
return coll;
}
}
}
return null;
}
@Override
public String toString() {
return name + " (Collection)";
}
@Override
public boolean equals(Object object) {
if (!(object instanceof LootCollection)) {
return false;
}
LootCollection loot = (LootCollection) object;
return loot.name.equals(name);
}
@Override
public int hashCode() {
int hash = 5;
hash = 59 * hash + (this.name != null ? this.name.hashCode() : 0);
return hash;
}
@Override
public Map<String, Object> serialize() {
Map<String, Object> map = new TreeMap<>();
map.put("Probability", probability);
map.put("Name", name);
map.put("LowerNumberOfLoots", lowerNumberOfLoots);
map.put("UpperNumberOfLoots", upperNumberOfLoots);
map.put("LootList", lootList);
return map;
}
}