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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
|
package fyresmodjam.worldgen;
import java.awt.Color;
import java.util.HashMap;
import cpw.mods.fml.common.FMLCommonHandler;
import fyresmodjam.ModjamMod;
import fyresmodjam.misc.ConfigData;
import net.minecraft.entity.boss.EntityDragon;
import net.minecraft.entity.boss.EntityWither;
import net.minecraft.entity.monster.EntityGhast;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.potion.Potion;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.World;
import net.minecraft.world.WorldSavedData;
import net.minecraft.world.storage.MapStorage;
public class FyresWorldData extends WorldSavedData {
/*
* @formatter:off
*/
public static String[] validDisadvantages = { "Tougher Mobs", "Weak", "Explosive Traps", "Increased Mob Spawn",
// "Neverending Rain",
// "Neverending Night",
"Permadeath", "Trigger-happy", "Diabolic", "Organized Foes" };
public static String[] disadvantageDescriptions = { "Hostile enemies takes 25% less damage", "-25% melee damage",
"Traps also trigger explosions when set off", "+33% hostile mob spawn rate",
// "Constantly rains",
// "Constant night",
"Items dropped upon death are permanently lost", "Mobs have a chance to explode on death",
"All mobs have a chance to recieve a random blessing",
"Mobs spawn with armor and weapons, increasing with level" };
public static String[] validTasks = { "Kill", "Burn" };
/*
* @formatter:on
*/
public static String key = "FyresWorldData";
public int[] potionValues = null;
public int[] potionDurations = null;
public int[][] mushroomColors = null;
public String currentDisadvantage = null;
public String currentTask = null;
public int currentTaskID = -1;
public int currentTaskAmount = 0;
public int progress = 0;
public int tasksCompleted = 0;
public int rewardLevels = -1;
public boolean enderDragonKilled = false;
// TODO pull these into a class instead of using parallel arrays
@SuppressWarnings("rawtypes")
public static Class[] validMobs = { EntityDragon.class, EntityGhast.class, EntityWither.class };
public static String[] validMobNames = { "Ender Dragon", "Ghast", "Wither" };
public static int[][] mobNumbers = { new int[] { 1, 1 }, new int[] { 5, 15 }, new int[] { 1, 1 } };
public HashMap<String, String> blessingByPlayer = new HashMap<String, String>();
public HashMap<String, int[]> potionKnowledgeByPlayer = new HashMap<String, int[]>();
public HashMap<String, NBTTagCompound> killStatsByPlayer = new HashMap<String, NBTTagCompound>();
public HashMap<String, NBTTagCompound> weaponStatsByPlayer = new HashMap<String, NBTTagCompound>();
public HashMap<String, NBTTagCompound> craftingStatsByPlayer = new HashMap<String, NBTTagCompound>();
public static ItemStack[] validItems = { new ItemStack(Blocks.diamond_block), new ItemStack(Blocks.gold_block),
new ItemStack(Blocks.emerald_block), new ItemStack(Blocks.lapis_block), new ItemStack(Items.diamond),
new ItemStack(Items.emerald), new ItemStack(Items.gold_ingot), new ItemStack(Items.nether_star),
new ItemStack(Items.ghast_tear) };
public FyresWorldData() {
super(key);
}
public FyresWorldData(String key) {
super(key);
}
public static FyresWorldData forWorld(World world) {
MapStorage storage = world.perWorldStorage;
FyresWorldData result = (FyresWorldData) storage.loadData(FyresWorldData.class, key);
if (result == null) {
result = new FyresWorldData();
storage.setData(key, result);
result.checkWorldData();
}
return result;
}
@Override
public void readFromNBT(NBTTagCompound nbttagcompound) {
if (nbttagcompound.hasKey("values")) {
potionValues = nbttagcompound.getIntArray("values");
}
if (nbttagcompound.hasKey("durations")) {
potionDurations = nbttagcompound.getIntArray("durations");
}
if (nbttagcompound.hasKey("currentDisadvantage")) {
currentDisadvantage = nbttagcompound.getString("currentDisadvantage");
}
if (nbttagcompound.hasKey("currentTask")) {
currentTask = nbttagcompound.getString("currentTask");
}
if (nbttagcompound.hasKey("currentTaskID")) {
currentTaskID = nbttagcompound.getInteger("currentTaskID");
}
if (nbttagcompound.hasKey("currentTaskAmount")) {
currentTaskAmount = nbttagcompound.getInteger("currentTaskAmount");
}
if (nbttagcompound.hasKey("progress")) {
progress = nbttagcompound.getInteger("progress");
}
if (nbttagcompound.hasKey("tasksCompleted")) {
tasksCompleted = nbttagcompound.getInteger("tasksCompleted");
}
if (nbttagcompound.hasKey("enderDragonKilled")) {
enderDragonKilled = nbttagcompound.getBoolean("enderDragonKilled");
}
if (nbttagcompound.hasKey("rewardLevels")) {
rewardLevels = nbttagcompound.getInteger("rewardLevels");
}
mushroomColors = new int[13][];
for (int i = 0; i < 13; i++) {
if (nbttagcompound.hasKey("mushroomColors_" + (i + 1))) {
mushroomColors[i] = nbttagcompound.getIntArray("mushroomColors_" + (i + 1));
}
}
if (nbttagcompound.hasKey("TempPlayerStats")) {
NBTTagCompound tempStats = nbttagcompound.getCompoundTag("TempPlayerStats");
for (Object o : tempStats.func_150296_c()) {
if (o == null || !(o instanceof NBTTagCompound)) {
continue;
}
NBTTagCompound player = (NBTTagCompound) o;
blessingByPlayer.put(player.getString("Name"), player.getString("Blessing"));
potionKnowledgeByPlayer.put(player.getString("Name"), player.getIntArray("PotionKnowledge"));
if (player.hasKey("KillStats")) {
killStatsByPlayer.put(player.getString("Name"), player.getCompoundTag("KillStats"));
}
if (player.hasKey("WeaponStats")) {
weaponStatsByPlayer.put(player.getString("Name"), player.getCompoundTag("WeaponStats"));
}
if (player.hasKey("CraftingStats")) {
craftingStatsByPlayer.put(player.getString("Name"), player.getCompoundTag("CraftingStats"));
}
}
}
checkWorldData();
}
@Override
public void writeToNBT(NBTTagCompound nbttagcompound) {
checkWorldData();
nbttagcompound.setIntArray("values", potionValues);
nbttagcompound.setIntArray("durations", potionDurations);
nbttagcompound.setString("currentDisadvantage", currentDisadvantage);
nbttagcompound.setString("currentTask", currentTask);
nbttagcompound.setInteger("currentTaskID", currentTaskID);
nbttagcompound.setInteger("currentTaskAmount", currentTaskAmount);
nbttagcompound.setInteger("progress", progress);
nbttagcompound.setInteger("tasksCompleted", tasksCompleted);
nbttagcompound.setBoolean("enderDragonKilled", enderDragonKilled);
nbttagcompound.setInteger("rewardLevels", rewardLevels);
for (int i = 0; i < 13; i++) {
nbttagcompound.setIntArray("mushroomColors_" + (i + 1), mushroomColors[i]);
}
fillBlessingsByPlayer(nbttagcompound);
}
private void fillBlessingsByPlayer(NBTTagCompound nbttagcompound) {
if (!blessingByPlayer.isEmpty()) {
NBTTagCompound tempPlayerStats = new NBTTagCompound();
for (String s : blessingByPlayer.keySet()) {
if (s == null) {
continue;
}
NBTTagCompound player = new NBTTagCompound();
player.setString("Name", s);
player.setString("Blessing", blessingByPlayer.get(s));
player.setIntArray("PotionKnowledge", potionKnowledgeByPlayer.get(s));
if (killStatsByPlayer.containsKey(s)) {
player.setTag("KillStats", killStatsByPlayer.get(s));
}
if (weaponStatsByPlayer.containsKey(s)) {
player.setTag("WeaponStats", weaponStatsByPlayer.get(s));
}
if (craftingStatsByPlayer.containsKey(s)) {
player.setTag("CraftingStats", craftingStatsByPlayer.get(s));
}
tempPlayerStats.setTag(s, player);
}
nbttagcompound.setTag("TempPlayerStats", tempPlayerStats);
}
}
private void checkWorldData() {
checkPotionValues();
if (mushroomColors == null) {
mushroomColors = new int[13][2];
for (int i = 0; i < 13; i++) {
mushroomColors[i][0] = randomRGBColor();
mushroomColors[i][1] = randomRGBColor();
}
}
checkDisadvantage();
checkTasks();
if (rewardLevels == -1) {
rewardLevels = getRewardLevels();
}
}
private void checkTasks() {
if (currentTask == null) {
giveNewTask();
} else {
boolean changeTask = true;
// Change invalid tasks
for (String s : validTasks) {
if (s.equals(currentTask)) {
changeTask = false;
break;
}
}
boolean alreadyKilledDragon = currentTask != null && currentTask.equals("Kill") && currentTaskID == 0
&& enderDragonKilled;
if (changeTask || alreadyKilledDragon) {
giveNewTask();
} else {
if (currentTask.equals("Kill")) {
// Make sure mob type is valid
currentTaskID %= validMobs.length;
}
}
}
}
private void checkDisadvantage() {
boolean changeDisadvantage = true;
if (currentDisadvantage != null) {
boolean valid = validateDisadvantage();
changeDisadvantage = !valid && !currentDisadvantage.equals("None");
}
if (changeDisadvantage) {
currentDisadvantage = chooseDisadvantage();
MinecraftServer server = FMLCommonHandler.instance().getMinecraftServerInstance();
boolean permadeathOnHardcore = server.isHardcore() && currentDisadvantage.equals("Permadeath");
while (server != null && permadeathOnHardcore) {
currentDisadvantage = chooseDisadvantage();
}
}
}
private void checkPotionValues() {
if (potionValues == null) {
potionValues = new int[] { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
for (int i = 0; i < 13 - 1; i++) {
int randPotionID = getRandomPotionID();
boolean stop = false;
while (potionIDIsValid(randPotionID) || !stop) {
stop = true;
randPotionID = getRandomPotionID();
for (int i3 = 0; i3 < 13 - 1; i3++) {
if (potionValues[i3] == randPotionID) {
stop = false;
break;
}
}
}
potionValues[i] = randPotionID;
}
} else {
for (int i = 0; i < 13 - 1; i++) {
if (Potion.potionTypes[potionValues[i]] == null) {
int randPotionID = getRandomPotionID();
while (potionIDIsValid(randPotionID)) {
randPotionID = getRandomPotionID();
}
potionValues[i] = randPotionID;
}
}
}
if (potionDurations == null) {
potionDurations = new int[13 - 1];
}
for (int i = 0; i < 13 - 1; i++) {
if (potionDurations[i] != 0) {
continue;
}
potionDurations[i] = getPotionDurations();
}
}
private int getPotionDurations() {
return 5 + ModjamMod.r.nextInt(26);
}
private boolean potionIDIsValid(int randPotionID) {
return Potion.potionTypes[randPotionID] == null;
}
private int getRandomPotionID() {
return ModjamMod.r.nextInt(Potion.potionTypes.length);
}
private int randomRGBColor() {
return Color.HSBtoRGB(ModjamMod.r.nextFloat(), ModjamMod.r.nextFloat(), ModjamMod.r.nextFloat());
}
private boolean validateDisadvantage() {
boolean valid = false;
for (String s : validDisadvantages) {
if (s.equals(currentDisadvantage)) {
valid = true;
break;
}
}
return valid;
}
private String chooseDisadvantage() {
return validDisadvantages[ModjamMod.r.nextInt(validDisadvantages.length)];
}
public void giveNewTask() {
progress = 0;
currentTask = getTaskType();
if (currentTask.equals("Kill")) {
currentTaskID = getTaskMob(enderDragonKilled);
int baseMobNumber = mobNumbers[currentTaskID][0];
currentTaskAmount = baseMobNumber + getMobNumberAdjuster();
} else if (currentTask.equals("Burn")) {
currentTaskID = getTaskItem();
setTaskItemCount();
}
rewardLevels = getRewardLevels();
markDirty();
}
private int getRewardLevels() {
return 5 + ModjamMod.r.nextInt(6);
}
private int setTaskItemCount() {
boolean isNetherStar = validItems[currentTaskID].getItem() == Items.nether_star;
boolean isBlock = validItems[currentTaskID].getItem() instanceof ItemBlock;
int taskAmount;
if (isNetherStar) {
taskAmount = 1;
} else {
taskAmount = 5 + ModjamMod.r.nextInt(28);
}
if (isBlock) {
return taskAmount / 4;
} else {
return taskAmount;
}
}
private int getTaskItem() {
return ModjamMod.r.nextInt(validItems.length);
}
private int getMobNumberAdjuster() {
return ModjamMod.r.nextInt(mobNumbers[currentTaskID][1]);
}
private String getTaskType() {
return validTasks[ModjamMod.r.nextInt(validTasks.length)];
}
private int getTaskMob(boolean dragonKilled) {
if (!dragonKilled) {
return ModjamMod.r.nextInt(validMobs.length);
} else {
return 1 + ModjamMod.r.nextInt(validMobs.length - 1);
}
}
public String getDisadvantage() {
return ConfigData.disableDisadvantages ? "None" : currentDisadvantage;
}
}
|