-
-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathConfigValues.java
More file actions
1042 lines (897 loc) · 45.4 KB
/
ConfigValues.java
File metadata and controls
1042 lines (897 loc) · 45.4 KB
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package codes.biscuit.skyblockaddons.config;
import codes.biscuit.skyblockaddons.SkyblockAddons;
import codes.biscuit.skyblockaddons.core.Feature;
import codes.biscuit.skyblockaddons.core.Language;
import codes.biscuit.skyblockaddons.core.chroma.ManualChromaManager;
import codes.biscuit.skyblockaddons.features.discordrpc.DiscordStatus;
import codes.biscuit.skyblockaddons.features.enchants.EnchantListLayout;
import codes.biscuit.skyblockaddons.features.enchants.EnchantManager;
import codes.biscuit.skyblockaddons.utils.*;
import codes.biscuit.skyblockaddons.utils.objects.FloatPair;
import com.google.gson.*;
import lombok.Getter;
import lombok.Setter;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.ScaledResolution;
import net.minecraft.crash.CrashReport;
import net.minecraft.util.MathHelper;
import net.minecraft.util.ReportedException;
import org.apache.commons.lang3.mutable.Mutable;
import org.apache.commons.lang3.mutable.MutableFloat;
import org.apache.commons.lang3.mutable.MutableInt;
import org.apache.commons.lang3.mutable.MutableObject;
import org.apache.commons.lang3.text.WordUtils;
import org.apache.logging.log4j.Logger;
import java.awt.geom.Point2D;
import java.beans.Introspector;
import java.io.*;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.locks.ReentrantLock;
public class ConfigValues {
private static final int CONFIG_VERSION = 9;
private final static float DEFAULT_GUI_SCALE = normalizeValueNoStep(1);
private final static float GUI_SCALE_MINIMUM = 0.5F;
private final static float GUI_SCALE_MAXIMUM = 5;
private static final ReentrantLock SAVE_LOCK = new ReentrantLock();
private static final SkyblockAddons main = SkyblockAddons.getInstance();
private static final Logger logger = SkyblockAddons.getLogger();
private final Map<Feature, FloatPair> defaultCoordinates = new EnumMap<>(Feature.class);
private final Map<Feature, EnumUtils.AnchorPoint> defaultAnchorPoints = new EnumMap<>(Feature.class);
private final Map<Feature, Float> defaultGuiScales = new EnumMap<>(Feature.class);
private final Map<Feature, FloatPair> defaultBarSizes = new EnumMap<>(Feature.class);
private final File settingsConfigFile;
private JsonObject loadedConfig = new JsonObject();
@Getter
@Setter
private JsonObject languageConfig = new JsonObject();
@Getter
private final Set<Feature> disabledFeatures = EnumSet.noneOf(Feature.class);
private final Map<Feature, Integer> colors = new HashMap<>();
private Map<Feature, Float> guiScales = new EnumMap<>(Feature.class);
private final Map<Feature, FloatPair> barSizes = new EnumMap<>(Feature.class);
private final MutableInt warningSeconds = new MutableInt(4);
private final Map<Feature, FloatPair> coordinates = new EnumMap<>(Feature.class);
private Map<Feature, EnumUtils.AnchorPoint> anchorPoints = new EnumMap<>(Feature.class);
private final MutableObject<Language> language = new MutableObject<>(Language.ENGLISH);
private final MutableObject<EnumUtils.BackpackStyle> backpackStyle = new MutableObject<>(EnumUtils.BackpackStyle.GUI);
private final MutableObject<EnumUtils.PowerOrbDisplayStyle> powerOrbDisplayStyle = new MutableObject<>(EnumUtils.PowerOrbDisplayStyle.COMPACT);
private final MutableObject<EnumUtils.TextStyle> textStyle = new MutableObject<>(EnumUtils.TextStyle.STYLE_ONE);
private final Map<String, Set<Integer>> profileLockedSlots = new HashMap<>();
@Getter
private final Set<Feature> chromaFeatures = EnumSet.noneOf(Feature.class);
@Deprecated
private final MutableFloat oldChromaSpeed = new MutableFloat(0.19354838F); // 2.0
private final MutableObject<EnumUtils.ChromaMode> chromaMode = new MutableObject<>(EnumUtils.ChromaMode.FADE);
private final MutableFloat chromaFadeWidth = new MutableFloat(0.22580644F); // 10° Hue
private final MutableObject<DiscordStatus> discordDetails = new MutableObject<>(DiscordStatus.LOCATION);
private final MutableObject<DiscordStatus> discordStatus = new MutableObject<>(DiscordStatus.AUTO_STATUS);
private final MutableObject<DiscordStatus> discordAutoDefault = new MutableObject<>(DiscordStatus.NONE);
@Getter
private final List<String> discordCustomStatuses = new ArrayList<>();
@Getter
private final MutableFloat mapZoom = new MutableFloat(0.18478261F); // 1.3
@Getter
private final MutableFloat healingCircleOpacity = new MutableFloat(0.4);
@Setter
@Getter
private MutableFloat chromaSize = new MutableFloat(30);
@Getter
private final MutableFloat chromaSpeed = new MutableFloat(6);
@Getter
private final MutableFloat chromaSaturation = new MutableFloat(0.75F);
@Getter
private final MutableFloat chromaBrightness = new MutableFloat(0.9F);
private final MutableObject<EnchantListLayout> enchantLayout = new MutableObject<>(EnchantListLayout.NORMAL);
public ConfigValues(File settingsConfigFile) {
this.settingsConfigFile = settingsConfigFile;
}
public void loadValues() {
try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream("default.json");
InputStreamReader inputStreamReader = new InputStreamReader(Objects.requireNonNull(inputStream),
StandardCharsets.UTF_8)) {
JsonObject defaultValues = SkyblockAddons.getGson().fromJson(inputStreamReader, JsonObject.class);
deserializeFeatureFloatCoordsMapFromID(defaultValues, defaultCoordinates, "coordinates");
deserializeEnumEnumMapFromIDS(defaultValues, defaultAnchorPoints, "anchorPoints", Feature.class, EnumUtils.AnchorPoint.class);
deserializeEnumNumberMapFromID(defaultValues, defaultGuiScales, "guiScales", Feature.class, float.class);
deserializeFeatureIntCoordsMapFromID(defaultValues, defaultBarSizes, "barSizes");
} catch (Exception ex) {
CrashReport crashReport = CrashReport.makeCrashReport(ex, "Reading default settings file");
throw new ReportedException(crashReport);
}
if (settingsConfigFile.exists()) {
try (FileReader reader = new FileReader(settingsConfigFile)) {
JsonElement fileElement = new JsonParser().parse(reader);
if (fileElement == null || fileElement.isJsonNull()) {
throw new JsonParseException("File is null!");
}
loadedConfig = fileElement.getAsJsonObject();
} catch (JsonParseException | IllegalStateException | IOException ex) {
logger.error("There was an error loading the config. Resetting all settings to default.");
logger.catching(ex);
addDefaultsAndSave();
return;
}
int configVersion;
if (loadedConfig.has("configVersion")) {
configVersion = loadedConfig.get("configVersion").getAsInt();
} else {
configVersion = ConfigValues.CONFIG_VERSION;
}
deserializeFeatureSetFromID(disabledFeatures, "disabledFeatures");
deserializeStringIntSetMap(profileLockedSlots, "profileLockedSlots");
deserializeNumber(warningSeconds, "warningSeconds", int.class);
try {
if (loadedConfig.has("language")) {
String languageKey = loadedConfig.get("language").getAsString();
Language configLanguage = Language.getFromPath(languageKey);
if (configLanguage != null) {
setLanguage(configLanguage); // TODO Will this crash?
// language.setValue(configLanguage);
}
}
} catch (Exception ex) {
logger.error("Failed to deserialize path: language");
logger.catching(ex);
}
deserializeEnumValueFromOrdinal(backpackStyle, "backpackStyle");
deserializeEnumValueFromOrdinal(powerOrbDisplayStyle, "powerOrbStyle");
deserializeEnumEnumMapFromIDS(anchorPoints, "anchorPoints", Feature.class, EnumUtils.AnchorPoint.class);
deserializeEnumNumberMapFromID(guiScales, "guiScales", Feature.class, float.class);
try {
for (Feature feature : Feature.getGuiFeatures()) { // TODO Legacy format from 1.3.4, remove in the future.
String property = Introspector.decapitalize(WordUtils.capitalizeFully(feature.toString().replace("_", " "))).replace(" ", "");
String x = property+"X";
String y = property+"Y";
if (loadedConfig.has(x)) {
coordinates.put(feature, new FloatPair(loadedConfig.get(x).getAsFloat(), loadedConfig.get(y).getAsFloat()));
}
}
} catch (Exception ex) {
logger.error("Failed to deserialize path: coordinates (legacy)");
logger.catching(ex);
}
if (loadedConfig.has("coordinates")) {
deserializeFeatureFloatCoordsMapFromID(coordinates, "coordinates");
} else {
deserializeFeatureFloatCoordsMapFromID(coordinates, "guiPositions"); // TODO Legacy format from 1.4.2/1.5-betas, remove in the future.
}
deserializeFeatureIntCoordsMapFromID(barSizes, "barSizes");
if (loadedConfig.has("featureColors")) { // TODO Legacy format from 1.3.4, remove in the future.
try {
for (Map.Entry<String, JsonElement> element : loadedConfig.getAsJsonObject("featureColors").entrySet()) {
Feature feature = Feature.fromId(Integer.parseInt(element.getKey()));
if (feature != null) {
ColorCode colorCode = ColorCode.values()[element.getValue().getAsInt()];
if (colorCode.isColor() && colorCode != ColorCode.RED) { // Red is default, no need to set it.
colors.put(feature, colorCode.getColor());
}
}
}
} catch (Exception ex) {
logger.error("Failed to deserialize path: featureColors");
logger.catching(ex);
}
} else {
deserializeEnumNumberMapFromID(colors, "colors", Feature.class, int.class);
}
deserializeEnumValueFromOrdinal(textStyle, "textStyle");
deserializeFeatureSetFromID(chromaFeatures, "chromaFeatures");
if (configVersion <= 8) {
deserializeNumber(oldChromaSpeed, "chromaSpeed", float.class);
chromaSpeed.setValue(MathUtils.denormalizeSliderValue(oldChromaSpeed.floatValue(), 0.1F, 10, 0.5F));
} else {
deserializeNumber(chromaSpeed, "chromaSpeed", float.class);
}
deserializeNumber(chromaSize, "chromaSize", float.class);
deserializeEnumValueFromOrdinal(chromaMode, "chromaMode");
deserializeNumber(chromaFadeWidth, "chromaFadeWidth", float.class);
deserializeEnumValueFromOrdinal(discordStatus, "discordStatus");
deserializeEnumValueFromOrdinal(discordDetails, "discordDetails");
deserializeEnumValueFromOrdinal(discordAutoDefault, "discordAutoDefault");
deserializeStringCollection(discordCustomStatuses, "discordCustomStatuses");
deserializeEnumValueFromOrdinal(enchantLayout, "enchantLayout");
deserializeNumber(mapZoom, "mapZoom", float.class);
deserializeNumber(chromaSaturation, "chromaSaturation", float.class);
deserializeNumber(chromaBrightness, "chromaBrightness", float.class);
if (configVersion <= 5) {
disabledFeatures.add(Feature.REPLACE_ROMAN_NUMERALS_WITH_NUMBERS);
} else if (configVersion <= 6) {
putDefaultBarSizes();
for (Map.Entry<Feature, FloatPair> entry : coordinates.entrySet()) {
if (getAnchorPoint(entry.getKey()) == EnumUtils.AnchorPoint.BOTTOM_MIDDLE) {
FloatPair coords = entry.getValue();
coords.setX(coords.getX()-91);
coords.setY(coords.getY()-39);
}
}
} else if (configVersion <= 7) {
for (Map.Entry<Feature, FloatPair> entry : coordinates.entrySet()) {
Feature feature = entry.getKey();
FloatPair coords = entry.getValue();
if (feature == Feature.MAGMA_BOSS_TIMER || feature == Feature.DARK_AUCTION_TIMER || feature == Feature.FARM_EVENT_TIMER ||feature == Feature.ZEALOT_COUNTER || feature == Feature.SKILL_DISPLAY
|| feature == Feature.SHOW_TOTAL_ZEALOT_COUNT || feature == Feature.SHOW_SUMMONING_EYE_COUNT || feature == Feature.SHOW_AVERAGE_ZEALOTS_PER_EYE ||
feature == Feature.BIRCH_PARK_RAINMAKER_TIMER || feature == Feature.COMBAT_TIMER_DISPLAY || feature == Feature.ENDSTONE_PROTECTOR_DISPLAY) {
coords.setY(coords.getY() + 2/2F);
coords.setX(coords.getX() - 18/2F);
coords.setY(coords.getY() - 9/2F);
if (feature == Feature.COMBAT_TIMER_DISPLAY) {
coords.setY(coords.getY() + 15/2F);
}
}
if (feature.getGuiFeatureData() != null && feature.getGuiFeatureData().getDrawType() == EnumUtils.DrawType.BAR) {
coords.setY(coords.getY() + 1);
}
}
}
int lastFeatureID;
if (loadedConfig.has("lastFeatureID")) {
lastFeatureID = loadedConfig.get("lastFeatureID").getAsInt();
} else {
// This system was added after this feature.
lastFeatureID = Feature.SKYBLOCK_ADDONS_BUTTON_IN_PAUSE_MENU.getId();
}
// This will go through every feature, and if they are new features that didn't exist before
// that should be disabled by default, and their coordinates are default, this will disable those features.
for (Feature feature : Feature.values()) {
if (feature.getId() > lastFeatureID && feature.isDefaultDisabled() && featureCoordinatesAreDefault(feature)) {
this.getDisabledFeatures().add(feature);
}
}
} else {
addDefaultsAndSave();
}
}
private void addDefaultsAndSave() {
Minecraft mc = Minecraft.getMinecraft();
if (mc != null) {
if (mc.getLanguageManager() != null && mc.getLanguageManager().getCurrentLanguage().getLanguageCode() != null) {
String minecraftLanguage = Minecraft.getMinecraft().getLanguageManager().getCurrentLanguage().getLanguageCode().toLowerCase(Locale.US);
Language configLanguage = Language.getFromPath(minecraftLanguage);
if (configLanguage != null) { // Check if we have the exact locale they are using for Minecraft
language.setValue(configLanguage);
} else { // Check if we at least have the same language (different locale)
String languageCode = minecraftLanguage.split("_")[0];
for (Language loopLanguage : Language.values()) {
String loopLanguageCode = loopLanguage.getPath().split("_")[0];
if (loopLanguageCode.equals(languageCode)) {
language.setValue(loopLanguage);
break;
}
}
}
}
}
for (Feature feature : Feature.values()) {
ColorCode color = feature.getDefaultColor();
if (color != null) {
colors.put(feature, color.getColor());
}
if (feature.isDefaultDisabled()) {
disabledFeatures.add(feature);
}
}
setAllCoordinatesToDefault();
putDefaultBarSizes();
saveConfig();
}
@SuppressWarnings("ResultOfMethodCallIgnored")
public void saveConfig() {
EnchantManager.markCacheDirty();
SkyblockAddons.runAsync(() -> {
if (!SAVE_LOCK.tryLock()) {
return;
}
logger.info("Saving config");
try {
settingsConfigFile.createNewFile();
JsonObject saveConfig = new JsonObject();
JsonArray jsonArray = new JsonArray();
for (Feature element : disabledFeatures) {
jsonArray.add(new GsonBuilder().create().toJsonTree(element.getId()));
}
saveConfig.add("disabledFeatures", jsonArray);
JsonObject profileSlotsObject = new JsonObject();
for (Map.Entry<String, Set<Integer>> entry : profileLockedSlots.entrySet()) {
JsonArray lockedSlots = new JsonArray();
for (int slot : entry.getValue()) {
lockedSlots.add(new GsonBuilder().create().toJsonTree(slot));
}
profileSlotsObject.add(entry.getKey(), lockedSlots);
}
saveConfig.add("profileLockedSlots", profileSlotsObject);
JsonObject anchorObject = new JsonObject();
for (Feature feature : Feature.getGuiFeatures()) {
anchorObject.addProperty(String.valueOf(feature.getId()), getAnchorPoint(feature).getId());
}
saveConfig.add("anchorPoints", anchorObject);
JsonObject scalesObject = new JsonObject();
for (Feature feature : guiScales.keySet()) {
scalesObject.addProperty(String.valueOf(feature.getId()), guiScales.get(feature));
}
saveConfig.add("guiScales", scalesObject);
JsonObject colorsObject = new JsonObject();
for (Feature feature : colors.keySet()) {
int featureColor = colors.get(feature);
if (featureColor != ColorCode.RED.getColor()) { // Red is default, no need to save it!
colorsObject.addProperty(String.valueOf(feature.getId()), colors.get(feature));
}
}
saveConfig.add("colors", colorsObject);
// Old gui coordinates, for backwards compatibility...
JsonObject coordinatesObject = new JsonObject();
for (Feature feature : coordinates.keySet()) {
JsonArray coordinatesArray = new JsonArray();
coordinatesArray.add(new GsonBuilder().create().toJsonTree(Math.round(coordinates.get(feature).getX())));
coordinatesArray.add(new GsonBuilder().create().toJsonTree(Math.round(coordinates.get(feature).getY())));
coordinatesObject.add(String.valueOf(feature.getId()), coordinatesArray);
}
saveConfig.add("guiPositions", coordinatesObject);
// New gui coordinates
coordinatesObject = new JsonObject();
for (Feature feature : coordinates.keySet()) {
JsonArray coordinatesArray = new JsonArray();
coordinatesArray.add(new GsonBuilder().create().toJsonTree(coordinates.get(feature).getX()));
coordinatesArray.add(new GsonBuilder().create().toJsonTree(coordinates.get(feature).getY()));
coordinatesObject.add(String.valueOf(feature.getId()), coordinatesArray);
}
saveConfig.add("coordinates", coordinatesObject);
JsonObject barSizesObject = new JsonObject();
for (Feature feature : barSizes.keySet()) {
JsonArray sizesArray = new JsonArray();
sizesArray.add(new GsonBuilder().create().toJsonTree(barSizes.get(feature).getX()));
sizesArray.add(new GsonBuilder().create().toJsonTree(barSizes.get(feature).getY()));
barSizesObject.add(String.valueOf(feature.getId()), sizesArray);
}
saveConfig.add("barSizes", barSizesObject);
saveConfig.addProperty("warningSeconds", warningSeconds);
saveConfig.addProperty("textStyle", textStyle.getValue().ordinal());
saveConfig.addProperty("language", language.getValue().getPath());
saveConfig.addProperty("backpackStyle", backpackStyle.getValue().ordinal());
saveConfig.addProperty("powerOrbStyle", powerOrbDisplayStyle.getValue().ordinal());
JsonArray chromaFeaturesArray = new JsonArray();
for (Feature feature : chromaFeatures) {
chromaFeaturesArray.add(new GsonBuilder().create().toJsonTree(feature.getId()));
}
saveConfig.add("chromaFeatures", chromaFeaturesArray);
saveConfig.addProperty("chromaSpeed", chromaSpeed);
saveConfig.addProperty("chromaMode", chromaMode.getValue().ordinal());
saveConfig.addProperty("chromaSize", chromaSize);
saveConfig.addProperty("discordStatus", discordStatus.getValue().ordinal());
saveConfig.addProperty("discordDetails", discordDetails.getValue().ordinal());
saveConfig.addProperty("discordAutoDefault", discordAutoDefault.getValue().ordinal());
saveConfig.addProperty("enchantLayout", enchantLayout.getValue().ordinal());
JsonArray discordCustomStatusesArray = new JsonArray();
for (String string : discordCustomStatuses) {
discordCustomStatusesArray.add(new GsonBuilder().create().toJsonTree(string));
}
saveConfig.add("discordCustomStatuses", discordCustomStatusesArray);
saveConfig.addProperty("mapZoom", mapZoom);
saveConfig.addProperty("chromaSaturation", chromaSaturation);
saveConfig.addProperty("chromaBrightness", chromaBrightness);
saveConfig.addProperty("configVersion", CONFIG_VERSION);
int largestFeatureID = 0;
for (Feature feature : Feature.values()) {
if (feature.getId() > largestFeatureID) largestFeatureID = feature.getId();
}
saveConfig.addProperty("lastFeatureID", largestFeatureID);
try (FileWriter writer = new FileWriter(settingsConfigFile)) {
SkyblockAddons.getGson().toJson(saveConfig, writer);
}
} catch (Exception ex) {
logger.error("An error occurred while attempting to save the config!");
logger.catching(ex);
}
SAVE_LOCK.unlock();
logger.info("Config saved");
});
}
private void deserializeFeatureSetFromID(Collection<Feature> collection, String path) {
try {
if (loadedConfig.has(path)) {
for (JsonElement element : loadedConfig.getAsJsonArray(path)) {
Feature feature = Feature.fromId(element.getAsInt());
if (feature != null) {
collection.add(feature);
}
}
}
} catch (Exception ex) {
logger.error("Failed to deserialize path: "+ path);
logger.catching(ex);
}
}
private void deserializeStringCollection(Collection<String> collection, String path) {
try {
if (loadedConfig.has(path)) {
for (JsonElement element : loadedConfig.getAsJsonArray(path)) {
String string = element.getAsString();
if (string != null) {
collection.add(string);
}
}
}
} catch (Exception ex) {
logger.error("Failed to deserialize path: "+ path);
logger.catching(ex);
}
}
private void deserializeStringIntSetMap(Map<String, Set<Integer>> map, String path) {
try {
if (loadedConfig.has(path)) {
JsonObject profileSlotsObject = loadedConfig.getAsJsonObject(path);
for (Map.Entry<String, JsonElement> entry : profileSlotsObject.entrySet()) {
Set<Integer> slots = new HashSet<>();
for (JsonElement element : entry.getValue().getAsJsonArray()) {
slots.add(element.getAsInt());
}
map.put(entry.getKey(), slots);
}
}
} catch (Exception ex) {
logger.error("Failed to deserialize path: "+ path);
logger.catching(ex);
}
}
private <E extends Enum<?>, F extends Enum<?>> void deserializeEnumEnumMapFromIDS(Map<E, F> map, String path, Class<E> keyClass, Class<F> valueClass) {
deserializeEnumEnumMapFromIDS(loadedConfig, map, path, keyClass, valueClass);
}
@SuppressWarnings("unchecked")
private <E extends Enum<?>, F extends Enum<?>> void deserializeEnumEnumMapFromIDS(JsonObject jsonObject, Map<E, F> map, String path, Class<E> keyClass, Class<F> valueClass) {
try {
if (jsonObject.has(path)) {
for (Map.Entry<String, JsonElement> element : jsonObject.getAsJsonObject(path).entrySet()) {
Method fromId = keyClass.getDeclaredMethod("fromId", int.class);
E key = (E)fromId.invoke(null, Integer.parseInt(element.getKey()));
fromId = valueClass.getDeclaredMethod("fromId", int.class);
F value = (F)fromId.invoke(null, element.getValue().getAsInt());
if (key != null && value != null) {
map.put(key, value);
}
}
}
} catch (Exception ex) {
logger.error("Failed to deserialize path: "+ path);
logger.catching(ex);
}
}
private <E extends Enum<?>, N extends Number> void deserializeEnumNumberMapFromID(Map<E, N> map, String path, Class<E> keyClass, Class<N> numberClass) {
deserializeEnumNumberMapFromID(loadedConfig, map, path, keyClass, numberClass);
}
@SuppressWarnings("unchecked")
private <E extends Enum<?>, N extends Number> void deserializeEnumNumberMapFromID(JsonObject jsonObject, Map<E, N> map, String path, Class<E> keyClass, Class<N> numberClass) {
try {
if (jsonObject.has(path)) {
for (Map.Entry<String, JsonElement> element : jsonObject.getAsJsonObject(path).entrySet()) {
Method fromId = keyClass.getDeclaredMethod("fromId", int.class);
E key = (E)fromId.invoke(null, Integer.parseInt(element.getKey()));
if (key != null) {
map.put(key, (N)getNumber(element.getValue(), numberClass));
}
}
}
} catch (Exception ex) {
logger.error("Failed to deserialize path: "+ path);
logger.catching(ex);
}
}
private <N extends Number> void deserializeNumber(Mutable<Number> number, String path, Class<N> numberClass) {
try {
if (loadedConfig.has(path)) {
number.setValue(getNumber(loadedConfig.get(path), numberClass));
}
} catch (Exception ex) {
logger.error("Failed to deserialize path: "+ path);
logger.catching(ex);
}
}
private Number getNumber(JsonElement jsonElement, Class<? extends Number> numberClass) {
if (numberClass == byte.class) { return jsonElement.getAsByte();
} else if (numberClass == short.class) { return jsonElement.getAsShort();
} else if (numberClass == int.class) { return jsonElement.getAsInt();
} else if (numberClass == long.class) { return jsonElement.getAsLong();
} else if (numberClass == float.class) { return jsonElement.getAsFloat();
} else if (numberClass == double.class) { return jsonElement.getAsDouble(); }
return null;
}
@SuppressWarnings("unchecked")
private <E extends Enum<?>> void deserializeEnumValueFromOrdinal(MutableObject<E> value, String path) {
try {
Class<? extends Enum<?>> enumClass = value.getValue().getDeclaringClass();
Method method = enumClass.getDeclaredMethod("values");
Object valuesObject = method.invoke(null);
E[] values = (E[])valuesObject;
if (loadedConfig.has(path)) {
int ordinal = loadedConfig.get(path).getAsInt();
if (values.length > ordinal) {
E enumValue = values[ordinal];
if (enumValue != null) {
value.setValue(values[ordinal]);
}
}
}
} catch (Exception ex) {
logger.error("Failed to deserialize path: "+ path);
logger.catching(ex);
}
}
private void deserializeFeatureFloatCoordsMapFromID(Map<Feature, FloatPair> map, String path) {
deserializeFeatureFloatCoordsMapFromID(loadedConfig, map, path);
}
private void deserializeFeatureFloatCoordsMapFromID(JsonObject jsonObject, Map<Feature, FloatPair> map, String path) {
try {
if (jsonObject.has(path)) {
for (Map.Entry<String, JsonElement> element : jsonObject.getAsJsonObject(path).entrySet()) {
Feature feature = Feature.fromId(Integer.parseInt(element.getKey()));
if (feature != null) {
JsonArray coords = element.getValue().getAsJsonArray();
map.put(feature, new FloatPair(coords.get(0).getAsFloat(), coords.get(1).getAsFloat()));
}
}
}
} catch (Exception ex) {
logger.error("Failed to deserialize path: "+ path);
logger.catching(ex);
}
}
private void deserializeFeatureIntCoordsMapFromID(Map<Feature, FloatPair> map, String path) {
deserializeFeatureIntCoordsMapFromID(loadedConfig, map, path);
}
private void deserializeFeatureIntCoordsMapFromID(JsonObject jsonObject, Map<Feature, FloatPair> map, String path) {
try {
if (jsonObject.has(path)) {
for (Map.Entry<String, JsonElement> element : jsonObject.getAsJsonObject(path).entrySet()) {
Feature feature = Feature.fromId(Integer.parseInt(element.getKey()));
if (feature != null) {
JsonArray coords = element.getValue().getAsJsonArray();
map.put(feature, new FloatPair(coords.get(0).getAsFloat(), coords.get(1).getAsFloat()));
}
}
}
} catch (Exception ex) {
logger.error("Failed to deserialize path: "+ path);
logger.catching(ex);
}
}
public void setAllCoordinatesToDefault() {
coordinates.clear();
for (Map.Entry<Feature, FloatPair> entry : defaultCoordinates.entrySet()) {
coordinates.put(entry.getKey(), entry.getValue().cloneCoords());
}
anchorPoints = new HashMap<>(defaultAnchorPoints);
guiScales = new HashMap<>(defaultGuiScales);
}
private void putDefaultCoordinates(Feature feature) {
FloatPair coords = defaultCoordinates.get(feature);
if (coords != null) {
coordinates.put(feature, coords);
}
}
public void putDefaultBarSizes() {
barSizes.clear();
for (Map.Entry<Feature, FloatPair> entry : defaultBarSizes.entrySet()) {
barSizes.put(entry.getKey(), entry.getValue().cloneCoords());
}
}
public static float normalizeValueNoStep(float value) {
return MathHelper.clamp_float((snapNearDefaultValue(value) - ConfigValues.GUI_SCALE_MINIMUM) /
(ConfigValues.GUI_SCALE_MAXIMUM - ConfigValues.GUI_SCALE_MINIMUM), 0.0F, 1.0F);
}
/** These two are taken from GuiOptionSlider. */
public static float denormalizeScale(float value) {
return snapNearDefaultValue(ConfigValues.GUI_SCALE_MINIMUM + (ConfigValues.GUI_SCALE_MAXIMUM - ConfigValues.GUI_SCALE_MINIMUM) *
MathHelper.clamp_float(value, 0.0F, 1.0F));
}
public static float snapNearDefaultValue(float value) {
if (value != 1 && value > 1-0.05 && value < 1+0.05) {
return 1;
}
return value;
}
/**
* Checks the received {@code OnlineData} to determine if the given feature should be disabled.
* This method checks the list of features to be disabled for all versions first and then checks the list of features that
* should be disabled for this specific version.
*
* @param feature The feature to check
* @return {@code true} if the feature should be disabled, {@code false} otherwise
*/
public boolean isRemoteDisabled(Feature feature) {
if (feature == null) return false;
HashMap<String, List<Integer>> disabledFeatures = main.getOnlineData().getDisabledFeatures();
if (disabledFeatures.containsKey("all")) {
if (disabledFeatures.get("all") != null) {
if (disabledFeatures.get("all").contains(feature.getId())) {
return true;
}
} else {
logger.error("\"all\" key in disabled features map has value of null. Please fix online data.");
}
}
/*
Check for disabled features for this mod version. Pre-release versions will follow the disabled features
list for their release version. For example, the version {@code 1.6.0-beta.10} will adhere to the list
for version {@code 1.6.0}
*/
String version = SkyblockAddons.VERSION;
if (version.contains("-")) {
version = version.split("-")[0];
}
if (disabledFeatures.containsKey(version)) {
if (disabledFeatures.get(version) != null) {
return disabledFeatures.get(version).contains(feature.getId());
} else {
logger.error("\"" + version + "\" key in disabled features map has value of null. Please fix online data.");
}
}
return false;
}
/**
* @param feature The feature to check.
* @return Whether the feature is disabled.
*/
public boolean isDisabled(Feature feature) {
return disabledFeatures.contains(feature) || isRemoteDisabled(feature);
}
/**
* @param feature The feature to check.
* @return Whether the feature is enabled.
*/
public boolean isEnabled(Feature feature) {
return !isDisabled(feature);
}
// TODO Don't force alpha in the future...
public int getColor(Feature feature) {
return this.getColor(feature, 255);
}
public int getColor(Feature feature, int alpha) {
// If the minimum alpha value is being limited let's make sure we are a little higher than that
// if (GlStateManager.alphaState.alphaTest && GlStateManager.alphaState.func == GL11.GL_GREATER && alpha / 255F <= GlStateManager.alphaState.ref) {
// alpha = ColorUtils.getAlphaIntFromFloat( GlStateManager.alphaState.ref + 0.001F);
// }
if (chromaFeatures.contains(feature)) {
return ManualChromaManager.getChromaColor(0, 0, alpha);
}
if (colors.containsKey(feature)) {
return ColorUtils.setColorAlpha(colors.get(feature), alpha);
}
ColorCode defaultColor = feature.getDefaultColor();
return ColorUtils.setColorAlpha(defaultColor != null ? defaultColor.getColor() : ColorCode.RED.getColor(), alpha);
}
/**
* Return skyblock color compatible with new shaders. Can bind the color (white) unconditionally
* @param feature the feature
* @return the color
*/
public SkyblockColor getSkyblockColor(Feature feature) {
SkyblockColor color = ColorUtils.getDummySkyblockColor(getColor(feature), chromaFeatures.contains(feature));
// If chroma is enabled, and we are using shaders, set color to white
if (color.drawMulticolorUsingShader()) {
color.setColor(0xFFFFFFFF);
}
return color;
}
public ColorCode getRestrictedColor(Feature feature) {
Integer featureColor = colors.get(feature);
if (featureColor != null) {
for (ColorCode colorCode : ColorCode.values()) {
if (!colorCode.isColor()) {
continue;
}
if (colorCode.getColor() == featureColor) {
return colorCode;
}
}
}
return feature.getDefaultColor();
}
private boolean featureCoordinatesAreDefault(Feature feature) {
if (!defaultCoordinates.containsKey(feature)) {
return true;
}
if (!coordinates.containsKey(feature)) {
return true;
}
return coordinates.get(feature).equals(defaultCoordinates.get(feature));
}
public void setColor(Feature feature, int color) {
colors.put(feature, color);
}
public float getActualX(Feature feature) {
int maxX = new ScaledResolution(Minecraft.getMinecraft()).getScaledWidth();
return getAnchorPoint(feature).getX(maxX) + getRelativeCoords(feature).getX();
}
public float getActualY(Feature feature) {
int maxY = new ScaledResolution(Minecraft.getMinecraft()).getScaledHeight();
return getAnchorPoint(feature).getY(maxY) + getRelativeCoords(feature).getY();
}
public FloatPair getSizes(Feature feature) {
return barSizes.getOrDefault(feature, defaultBarSizes.containsKey(feature) ? defaultBarSizes.get(feature).cloneCoords() : new FloatPair(1, 1));
}
public float getSizesX(Feature feature) {
return Math.min(Math.max(getSizes(feature).getX(), .25F), 1);
}
public float getSizesY(Feature feature) {
return Math.min(Math.max(getSizes(feature).getY(), .25F), 1);
}
public void setScaleX(Feature feature, float x) {
FloatPair coords = getSizes(feature);
coords.setX(x);
}
public void setScaleY(Feature feature, float y) {
FloatPair coords = getSizes(feature);
coords.setY(y);
}
public FloatPair getRelativeCoords(Feature feature) {
if (coordinates.containsKey(feature)) {
return coordinates.get(feature);
} else {
putDefaultCoordinates(feature);
if (coordinates.containsKey(feature)) {
return coordinates.get(feature);
} else {
return new FloatPair(0,0);
}
}
}
public void setCoords(Feature feature, float x, float y) {
if (coordinates.containsKey(feature)) {
coordinates.get(feature).setX(x);
coordinates.get(feature).setY(y);
} else {
coordinates.put(feature, new FloatPair(x, y));
}
}
public EnumUtils.AnchorPoint getClosestAnchorPoint(float x, float y) {
ScaledResolution sr = new ScaledResolution(Minecraft.getMinecraft());
int maxX = sr.getScaledWidth();
int maxY = sr.getScaledHeight();
double shortestDistance = -1;
EnumUtils.AnchorPoint closestAnchorPoint = EnumUtils.AnchorPoint.BOTTOM_MIDDLE; // default
for (EnumUtils.AnchorPoint point : EnumUtils.AnchorPoint.values()) {
double distance = Point2D.distance(x, y, point.getX(maxX), point.getY(maxY));
if (shortestDistance == -1 || distance < shortestDistance) {
closestAnchorPoint = point;
shortestDistance = distance;
}
}
return closestAnchorPoint;
}
public void setClosestAnchorPoint(Feature feature) {
float x1 = getActualX(feature);
float y1 = getActualY(feature);
ScaledResolution sr = new ScaledResolution(Minecraft.getMinecraft());
int maxX = sr.getScaledWidth();
int maxY = sr.getScaledHeight();
double shortestDistance = -1;
EnumUtils.AnchorPoint closestAnchorPoint = EnumUtils.AnchorPoint.BOTTOM_MIDDLE; // default
for (EnumUtils.AnchorPoint point : EnumUtils.AnchorPoint.values()) {
double distance = Point2D.distance(x1, y1, point.getX(maxX), point.getY(maxY));
if (shortestDistance == -1 || distance < shortestDistance) {
closestAnchorPoint = point;
shortestDistance = distance;
}
}
if (this.getAnchorPoint(feature) == closestAnchorPoint) {
return;
}
float targetX = getActualX(feature);
float targetY = getActualY(feature);
float x = targetX-closestAnchorPoint.getX(maxX);
float y = targetY-closestAnchorPoint.getY(maxY);
anchorPoints.put(feature, closestAnchorPoint);
setCoords(feature, x, y);
}
public EnumUtils.AnchorPoint getAnchorPoint(Feature feature) {
return anchorPoints.getOrDefault(feature, defaultAnchorPoints.getOrDefault(feature, EnumUtils.AnchorPoint.BOTTOM_MIDDLE));
}
public Set<Integer> getLockedSlots() {
String profile = main.getUtils().getProfileName();
if (!profileLockedSlots.containsKey(profile)) {
profileLockedSlots.put(profile, new HashSet<>());
}
return profileLockedSlots.get(profile);
}
public void setGuiScale(Feature feature, float scale) {
guiScales.put(feature, scale);
}
public float getGuiScale(Feature feature) {
return getGuiScale(feature, true);
}
public float getGuiScale(Feature feature, boolean denormalized) {
float value = ConfigValues.DEFAULT_GUI_SCALE;
if (guiScales.containsKey(feature)) {
value = guiScales.get(feature);
}
if (denormalized) {
value = denormalizeScale(value);
}
return value;
}
public void setChroma(Feature feature, boolean enabled) {
if (enabled) {
chromaFeatures.add(feature);
} else {
chromaFeatures.remove(feature);
}
}
public int getWarningSeconds() {
return warningSeconds.getValue();
}
public void setWarningSeconds(int warningSeconds) {
this.warningSeconds.setValue(warningSeconds);
}
public Language getLanguage() {
return language.getValue();
}
public void setLanguage(Language language) {
this.language.setValue(language);
}
public EnumUtils.BackpackStyle getBackpackStyle() {
return backpackStyle.getValue();
}
public void setBackpackStyle(EnumUtils.BackpackStyle backpackStyle) {
this.backpackStyle.setValue(backpackStyle);
}
public EnumUtils.PowerOrbDisplayStyle getPowerOrbDisplayStyle() {
return powerOrbDisplayStyle.getValue();
}
public void setPowerOrbDisplayStyle(EnumUtils.PowerOrbDisplayStyle powerOrbDisplayStyle) {
this.powerOrbDisplayStyle.setValue(powerOrbDisplayStyle);
}
public EnumUtils.TextStyle getTextStyle() {
return textStyle.getValue();
}
public void setTextStyle(EnumUtils.TextStyle textStyle) {
this.textStyle.setValue(textStyle);
}
public EnumUtils.ChromaMode getChromaMode() {
return chromaMode.getValue();
}
public void setChromaMode(EnumUtils.ChromaMode chromaMode) {
this.chromaMode.setValue(chromaMode);
}
public void setChromaFadeWidth(float chromaFadeWidth) {
this.chromaFadeWidth.setValue(chromaFadeWidth);
}
public float getChromaFadeWidth() {
return chromaFadeWidth.getValue();
}
public void setDiscordDetails(DiscordStatus discordDetails) {
this.discordDetails.setValue(discordDetails);
}
public void setDiscordStatus(DiscordStatus discordStatus) {
this.discordStatus.setValue(discordStatus);