/
PokemonTab.java
1148 lines (1060 loc) · 54 KB
/
PokemonTab.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
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 me.corriekay.pokegoutil.windows;
import POGOProtos.Enums.PokemonFamilyIdOuterClass.PokemonFamilyId;
import POGOProtos.Enums.PokemonIdOuterClass.PokemonId;
import POGOProtos.Networking.Responses.NicknamePokemonResponseOuterClass.NicknamePokemonResponse;
import POGOProtos.Networking.Responses.ReleasePokemonResponseOuterClass;
import POGOProtos.Networking.Responses.SetFavoritePokemonResponseOuterClass;
import POGOProtos.Networking.Responses.UpgradePokemonResponseOuterClass;
import com.pokegoapi.api.PokemonGo;
import com.pokegoapi.api.map.pokemon.EvolutionResult;
import com.pokegoapi.api.player.PlayerProfile.Currency;
import com.pokegoapi.api.pokemon.Pokemon;
import com.pokegoapi.api.pokemon.PokemonMeta;
import com.pokegoapi.api.pokemon.PokemonMetaRegistry;
import me.corriekay.pokegoutil.utils.ConfigKey;
import me.corriekay.pokegoutil.utils.ConfigNew;
import me.corriekay.pokegoutil.utils.Utilities;
import me.corriekay.pokegoutil.utils.helpers.DateHelper;
import me.corriekay.pokegoutil.utils.helpers.JTableColumnPacker;
import me.corriekay.pokegoutil.utils.helpers.LDocumentListener;
import me.corriekay.pokegoutil.utils.pokemon.PokeHandler;
import me.corriekay.pokegoutil.utils.pokemon.PokemonCpUtils;
import me.corriekay.pokegoutil.utils.pokemon.PokemonUtils;
import me.corriekay.pokegoutil.utils.ui.GhostText;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.commons.lang3.mutable.MutableInt;
import org.apache.commons.lang3.text.WordUtils;
import javax.swing.*;
import javax.swing.RowSorter.SortKey;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.text.NumberFormat;
import java.util.*;
import java.util.List;
import java.util.function.BiConsumer;
@SuppressWarnings("serial")
public class PokemonTab extends JPanel {
private final PokemonGo go;
private final PokemonTable pt = new PokemonTable();
private final JTextField searchBar = new JTextField("");
private final JTextField ivTransfer = new JTextField("", 20);
private ConfigNew config = ConfigNew.getConfig();
public PokemonTab(PokemonGo go) {
setLayout(new BorderLayout());
this.go = go;
JPanel topPanel = new JPanel(new GridBagLayout());
JButton refreshPkmn, renameSelected, transferSelected, evolveSelected, powerUpSelected, toggleFavorite;
refreshPkmn = new JButton("Refresh List");
renameSelected = new JButton("Rename");
transferSelected = new JButton("Transfer");
evolveSelected = new JButton("Evolve");
powerUpSelected = new JButton("Power Up");
toggleFavorite = new JButton("Toggle Favorite");
pt.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent event) {
if (event.getValueIsAdjusting() == true) {
int selectedRows = pt.getSelectedRowCount();
if (selectedRows > 1) {
PokemonGoMainWindow.window.setTitle(selectedRows + " Pokémon selected");
} else {
PokemonGoMainWindow.window.refreshTitle();
}
}
}
});
GridBagConstraints gbc = new GridBagConstraints();
topPanel.add(refreshPkmn, gbc);
refreshPkmn.addActionListener(l -> new SwingWorker<Void, Void>() {
protected Void doInBackground() throws Exception {
refreshPkmn();
return null;
}
}.execute());
topPanel.add(renameSelected, gbc);
renameSelected.addActionListener(l -> new SwingWorker<Void, Void>() {
protected Void doInBackground() throws Exception {
renameSelected();
return null;
}
}.execute());
topPanel.add(transferSelected, gbc);
transferSelected.addActionListener(l -> new SwingWorker<Void, Void>() {
protected Void doInBackground() throws Exception {
transferSelected();
return null;
}
}.execute());
topPanel.add(evolveSelected, gbc);
evolveSelected.addActionListener(l -> new SwingWorker<Void, Void>() {
protected Void doInBackground() throws Exception {
evolveSelected();
return null;
}
}.execute());
topPanel.add(powerUpSelected, gbc);
powerUpSelected.addActionListener(l -> new SwingWorker<Void, Void>() {
protected Void doInBackground() throws Exception {
powerUpSelected();
return null;
}
}.execute());
topPanel.add(toggleFavorite, gbc);
toggleFavorite.addActionListener(l -> new SwingWorker<Void, Void>() {
protected Void doInBackground() throws Exception {
toggleFavorite();
return null;
}
}.execute());
ivTransfer.addKeyListener(
new KeyListener() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
new SwingWorker<Void, Void>() {
protected Void doInBackground() throws Exception {
selectLessThanIv();
return null;
}
}.execute();
}
}
@Override
public void keyTyped(KeyEvent e) {
//nothing here
}
@Override
public void keyReleased(KeyEvent e) {
//nothing here
}
}
);
topPanel.add(ivTransfer, gbc);
new GhostText(ivTransfer, "Pokemon IV");
JButton transferIv = new JButton("Select Pokemon < IV");
transferIv.addActionListener(l -> new SwingWorker<Void, Void>() {
protected Void doInBackground() throws Exception {
selectLessThanIv();
return null;
}
}.execute());
topPanel.add(transferIv, gbc);
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.gridwidth = 3;
gbc.fill = GridBagConstraints.HORIZONTAL;
topPanel.add(searchBar, gbc);
// pokemon name language drop down
String[] locales = {"en", "de", "fr", "ru", "zh_CN", "zh_HK"};
JComboBox<String> pokelang = new JComboBox<>(locales);
String locale = config.getString(ConfigKey.LANGUAGE);
pokelang.setSelectedItem(locale);
pokelang.addActionListener(e -> new SwingWorker<Void, Void>() {
protected Void doInBackground() throws Exception {
@SuppressWarnings("unchecked")
JComboBox<String> pokelang1 = (JComboBox<String>) e.getSource();
String lang = (String) pokelang1.getSelectedItem();
changeLanguage(lang);
return null;
}
}.execute());
topPanel.add(pokelang);
// Set font size if specified in config
Font font = pt.getFont();
int size = config.getInt(ConfigKey.FONT_SIZE, font.getSize());
if (size != font.getSize()) {
pt.setFont(font.deriveFont((float) size));
}
// Font size dropdown
String[] sizes = {"11", "12", "13", "14", "15", "16", "17", "18"};
JComboBox<String> fontSize = new JComboBox<>(sizes);
fontSize.setSelectedItem(String.valueOf(size));
fontSize.addActionListener(e -> new SwingWorker<Void, Void>() {
protected Void doInBackground() throws Exception {
@SuppressWarnings("unchecked")
JComboBox<String> source = (JComboBox<String>) e.getSource();
String size = source.getSelectedItem().toString();
pt.setFont(pt.getFont().deriveFont(Float.parseFloat(size)));
config.setInt(ConfigKey.FONT_SIZE, Integer.parseInt(size));
return null;
}
}.execute());
topPanel.add(fontSize);
LDocumentListener.addChangeListener(searchBar, e -> refreshList());
new GhostText(searchBar, "Search Pokémon...");
add(topPanel, BorderLayout.NORTH);
JScrollPane sp = new JScrollPane(pt);
sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
add(sp, BorderLayout.CENTER);
}
private void changeLanguage(String langCode) {
config.setString(ConfigKey.LANGUAGE, langCode);
refreshPkmn();
}
private void refreshPkmn() {
try {
go.getInventories().updateInventories(true);
} catch (Exception e) {
e.printStackTrace();
}
SwingUtilities.invokeLater(this::refreshList);
System.out.println("Done refreshing Pokémon list");
}
private void showFinishedText(String message, int size, MutableInt success, MutableInt skipped, MutableInt err) {
String finishText = message +
"\nPokémon total: " + size +
"\nSuccessful: " + success.getValue() +
(skipped.getValue() > 0 ? "\nSkipped: " + skipped.getValue() : "") +
(err.getValue() > 0 ? "\nErrors: " + err.getValue() : "");
if (config.getBool(ConfigKey.SHOW_BULK_POPUP)) {
JOptionPane.showMessageDialog(null, finishText);
} else {
System.out.println(finishText);
}
}
private void renameSelected() {
ArrayList<Pokemon> selection = getSelectedPokemon();
if (selection.size() == 0) return;
String renamePattern = inputOperation("Rename", selection);
MutableInt err = new MutableInt(), skipped = new MutableInt(), success = new MutableInt(), total = new MutableInt(1);
PokeHandler handler = new PokeHandler(selection);
BiConsumer<NicknamePokemonResponse.Result, Pokemon> perPokeCallback = (result, pokemon) -> {
System.out.println("Doing Rename " + total.getValue() + " of " + selection.size());
total.increment();
// We check if the Pokemon was skipped
boolean isSkipped = (pokemon.getNickname().equals(PokeHandler.generatePokemonNickname(renamePattern, pokemon))
&& result.getNumber() == NicknamePokemonResponse.Result.UNSET_VALUE);
if (isSkipped) {
System.out.println("Skipped renaming " + PokeHandler.getLocalPokeName(pokemon) + ", already named " + pokemon.getNickname());
skipped.increment();
return;
}
if (result.getNumber() == NicknamePokemonResponse.Result.SUCCESS_VALUE) {
success.increment();
System.out.println("Renaming " + PokeHandler.getLocalPokeName(pokemon) + " from \"" + pokemon.getNickname() + "\" to \"" + PokeHandler.generatePokemonNickname(renamePattern, pokemon) + "\", Result: Success!");
} else {
err.increment();
System.out.println("Renaming " + PokeHandler.getLocalPokeName(pokemon) + " failed! Code: " + result.toString() + "; Nick: " + PokeHandler.generatePokemonNickname(renamePattern, pokemon));
}
// If not last element and API was queried, sleep until the next one
if (!selection.get(selection.size() - 1).equals(pokemon)) {
int sleepMin = config.getInt(ConfigKey.DELAY_RENAME_MIN);
int sleepMax = config.getInt(ConfigKey.DELAY_RENAME_MAX);
Utilities.sleepRandom(sleepMin, sleepMax);
}
};
handler.bulkRenameWithPattern(renamePattern, perPokeCallback);
try {
go.getInventories().updateInventories(true);
} catch (Exception e) {
e.printStackTrace();
}
SwingUtilities.invokeLater(this::refreshList);
showFinishedText("Pokémon batch rename complete!", selection.size(), success, skipped, err);
}
private void transferSelected() {
ArrayList<Pokemon> selection = getSelectedPokemon();
if (selection.size() == 0) return;
if (confirmOperation("Transfer", selection)) {
MutableInt err = new MutableInt(), skipped = new MutableInt(), success = new MutableInt(), total = new MutableInt(1);
selection.forEach(poke -> {
System.out.println("Doing Transfer " + total.getValue() + " of " + selection.size());
total.increment();
if (poke.isFavorite()) {
System.out.println(PokeHandler.getLocalPokeName(poke) + " with " + poke.getCp() + " CP is favorite, skipping.");
skipped.increment();
return;
}
if (!poke.getDeployedFortId().isEmpty()) {
System.out.println(PokeHandler.getLocalPokeName(poke) + " with " + poke.getCp() + " CP is in gym, skipping.");
skipped.increment();
return;
}
try {
int candies = poke.getCandy();
ReleasePokemonResponseOuterClass.ReleasePokemonResponse.Result result = poke.transferPokemon();
if (result == ReleasePokemonResponseOuterClass.ReleasePokemonResponse.Result.SUCCESS) {
int newCandies = poke.getCandy();
System.out.println("Transferring " + PokeHandler.getLocalPokeName(poke) + ", Result: Success!");
System.out.println("Stat changes: (Candies : " + newCandies + "[+" + (newCandies - candies) + "])");
success.increment();
} else {
System.out.println(
"Error transferring " + PokeHandler.getLocalPokeName(poke) + ", result: " + result);
err.increment();
}
// If not last element, sleep until the next one
if (!selection.get(selection.size() - 1).equals(poke)) {
int sleepMin = config.getInt(ConfigKey.DELAY_TRANSFER_MIN);
int sleepMax = config.getInt(ConfigKey.DELAY_TRANSFER_MAX);
Utilities.sleepRandom(sleepMin, sleepMax);
}
} catch (Exception e) {
err.increment();
System.out.println("Error transferring " + PokeHandler.getLocalPokeName(poke) + "! " + Utilities.getRealExceptionMessage(e));
}
});
try {
go.getInventories().updateInventories(true);
} catch (Exception e) {
e.printStackTrace();
}
SwingUtilities.invokeLater(this::refreshList);
showFinishedText("Pokémon batch transfer complete!", selection.size(), success, skipped, err);
}
}
private void evolveSelected() {
ArrayList<Pokemon> selection = getSelectedPokemon();
if (selection.size() > 0) {
if (confirmOperation("Evolve", selection)) {
MutableInt err = new MutableInt(), skipped = new MutableInt(), success = new MutableInt(), total = new MutableInt(1);
selection.forEach(poke -> {
System.out.println("Doing Evolve " + total.getValue() + " of " + selection.size());
total.increment();
if (!poke.getDeployedFortId().isEmpty()) {
System.out.println(PokeHandler.getLocalPokeName(poke) + " with " + poke.getCp() + " CP is in gym, skipping.");
skipped.increment();
return;
}
try {
int candies = poke.getCandy();
int candiesToEvolve = poke.getCandiesToEvolve();
int cp = poke.getCp();
int hp = poke.getMaxStamina();
EvolutionResult er = poke.evolve();
if (er.isSuccessful()) {
Pokemon newPoke = er.getEvolvedPokemon();
int newCandies = newPoke.getCandy();
int newCp = newPoke.getCp();
int newHp = newPoke.getStamina();
System.out.println(
"Evolving " + PokeHandler.getLocalPokeName(poke) + ". Evolve result: Success!");
if (config.getBool(ConfigKey.TRANSFER_AFTER_EVOLVE)) {
ReleasePokemonResponseOuterClass.ReleasePokemonResponse.Result result = newPoke.transferPokemon();
System.out.println("Transferring " + StringUtils.capitalize(newPoke.getPokemonId().toString().toLowerCase()) + ", Result: " + result);
System.out.println("Stat changes: (Candies: " + newCandies + "[" + candies + "-" + candiesToEvolve + "]");
} else {
System.out.println("Stat changes: (Candies: " + newCandies + "[" + candies + "-" + candiesToEvolve + "], CP: " + newCp + "[+" + (newCp - cp) + "], HP: " + newHp + "[+" + (newHp - hp) + "])");
}
go.getInventories().updateInventories(true);
success.increment();
} else {
err.increment();
System.out.println("Error evolving " + PokeHandler.getLocalPokeName(poke) + ", result: " + er.toString());
}
// If not last element, sleep until the next one
if (!selection.get(selection.size() - 1).equals(poke)) {
int sleepMin = config.getInt(ConfigKey.DELAY_EVOLVE_MIN);
int sleepMax = config.getInt(ConfigKey.DELAY_EVOLVE_MAX);
Utilities.sleepRandom(sleepMin, sleepMax);
}
} catch (Exception e) {
err.increment();
System.out.println("Error evolving " + PokeHandler.getLocalPokeName(poke) + "! " + Utilities.getRealExceptionMessage(e));
}
});
try {
go.getInventories().updateInventories(true);
} catch (Exception e) {
e.printStackTrace();
}
SwingUtilities.invokeLater(this::refreshList);
showFinishedText("Pokémon batch evolve"
+ (config.getBool(ConfigKey.TRANSFER_AFTER_EVOLVE) ? "/transfer" : "")
+ " complete!", selection.size(), success, skipped, err);
}
}
}
private void powerUpSelected() {
ArrayList<Pokemon> selection = getSelectedPokemon();
if (selection.size() > 0) {
if (confirmOperation("PowerUp", selection)) {
MutableInt err = new MutableInt(), skipped = new MutableInt(), success = new MutableInt(), total = new MutableInt(1);
selection.forEach(poke -> {
try {
System.out.println("Doing Power Up " + total.getValue() + " of " + selection.size());
total.increment();
if (!poke.getDeployedFortId().isEmpty()) {
System.out.println(PokeHandler.getLocalPokeName(poke) + " with " + poke.getCp() + " CP is in gym, skipping.");
skipped.increment();
return;
}
int candies = poke.getCandy();
int cp = poke.getCp();
int hp = poke.getMaxStamina();
int stardustUsed = poke.getStardustCostsForPowerup();
int candiesToPowerUp = poke.getCandyCostsForPowerup();
UpgradePokemonResponseOuterClass.UpgradePokemonResponse.Result result = poke.powerUp();
go.getPlayerProfile().updateProfile();
if (result == UpgradePokemonResponseOuterClass.UpgradePokemonResponse.Result.SUCCESS) {
int newCandies = poke.getCandy();
int newCp = poke.getCp();
int newHp = poke.getMaxStamina();
System.out.println(
"Powering Up " + PokeHandler.getLocalPokeName(poke) + ", Result: Success!");
System.out.println("Stat changes: (Candies : " + newCandies + "[" + candies + "-" + candiesToPowerUp + "], CP: " + newCp + "[+" + (newCp - cp) + "], HP: " + newHp + "[+" + (newHp - hp) + "]) Stardust used " + stardustUsed + "[remaining: " + go.getPlayerProfile().getCurrency(Currency.STARDUST) + "]");
success.increment();
} else {
err.increment();
System.out.println(
"Error powering up " + PokeHandler.getLocalPokeName(poke) + ", result: " + result.toString());
}
// If not last element, sleep until the next one
if (!selection.get(selection.size() - 1).equals(poke)) {
int sleepMin = config.getInt(ConfigKey.DELAY_POWERUP_MIN);
int sleepMax = config.getInt(ConfigKey.DELAY_POWERUP_MAX);
Utilities.sleepRandom(sleepMin, sleepMax);
}
} catch (Exception e) {
err.increment();
System.out.println("Error powering up " + PokeHandler.getLocalPokeName(poke) + "! " + Utilities.getRealExceptionMessage(e));
}
});
try {
go.getInventories().updateInventories(true);
PokemonGoMainWindow.window.refreshTitle();
} catch (Exception e) {
e.printStackTrace();
}
SwingUtilities.invokeLater(this::refreshList);
showFinishedText("Pokémon batch powerup complete!", selection.size(), success, skipped, err);
}
}
}
//feature added by Ben Kauffman
private void toggleFavorite() {
ArrayList<Pokemon> selection = getSelectedPokemon();
if (selection.size() > 0) {
if (confirmOperation("Toggle Favorite", selection)) {
MutableInt err = new MutableInt(), skipped = new MutableInt(), success = new MutableInt(), total = new MutableInt(1);
selection.forEach(poke -> {
try {
System.out.println("Toggling favorite " + total.getValue() + " of " + selection.size());
total.increment();
SetFavoritePokemonResponseOuterClass.SetFavoritePokemonResponse.Result result = poke.setFavoritePokemon(!poke.isFavorite());
System.out.println("Attempting to set favorite for " + PokeHandler.getLocalPokeName(poke) + " to " + !poke.isFavorite() + "...");
go.getPlayerProfile().updateProfile();
if (result == SetFavoritePokemonResponseOuterClass.SetFavoritePokemonResponse.Result.SUCCESS) {
System.out.println(
"Favorite for " + PokeHandler.getLocalPokeName(poke) + " set to " + !poke.isFavorite() + ", Result: Success!");
success.increment();
} else {
err.increment();
System.out.println(
"Error toggling favorite for " + PokeHandler.getLocalPokeName(poke) + ", result: " + result.toString());
}
// If not last element, sleep until the next one
if (!selection.get(selection.size() - 1).equals(poke)) {
int sleepMin = config.getInt(ConfigKey.DELAY_FAVORITE_MIN);
int sleepMax = config.getInt(ConfigKey.DELAY_FAVORITE_MAX);
Utilities.sleepRandom(sleepMin, sleepMax);
}
} catch (Exception e) {
err.increment();
System.out.println("Error toggling favorite for " + PokeHandler.getLocalPokeName(poke) + "! " + Utilities.getRealExceptionMessage(e));
}
});
try {
PokemonGoMainWindow.window.refreshTitle();
} catch (Exception e) {
e.printStackTrace();
}
SwingUtilities.invokeLater(this::refreshPkmn);
showFinishedText("Pokémon batch \"toggle favorite\" complete!", selection.size(), success, skipped, err);
}
}
}
private void selectLessThanIv() {
if (!NumberUtils.isNumber(ivTransfer.getText())) {
System.out.println("Please select a valid IV value (0-100)");
return;
}
double ivLessThan = Double.parseDouble(ivTransfer.getText());
if (ivLessThan > 100 || ivLessThan < 0) {
System.out.println("Please select a valid IV value (0-100)");
return;
}
pt.clearSelection();
System.out.println("Selecting Pokemon with IV less than: " + ivTransfer.getText());
for (int i = 0; i < pt.getRowCount(); i++) {
double pIv = (double) pt.getValueAt(i, 3);
if (pIv < ivLessThan) {
pt.getSelectionModel().addSelectionInterval(i, i);
}
}
}
private String inputOperation(String operation, ArrayList<Pokemon> pokes) {
JPanel panel = _buildPanelForOperation(operation, pokes);
String message = "";
switch (operation) {
case "Rename":
message = "You want to rename " + pokes.size() + " Pokémon.\nYou can rename with normal text and patterns, or both combined. Patterns are going to be replaced with the Pokémons values.\nExisting patterns:\n";
for (PokeHandler.ReplacePattern pattern : PokeHandler.ReplacePattern.values()) {
message += "%" + pattern.name().toLowerCase() + "% -> " + pattern.toString() + "\n";
}
message += "\n";
}
String input = JOptionPane.showInputDialog(panel, message, operation, JOptionPane.PLAIN_MESSAGE);
return input;
}
private boolean confirmOperation(String operation, ArrayList<Pokemon> pokes) {
JPanel panel = _buildPanelForOperation(operation, pokes);
int response = JOptionPane.showConfirmDialog(null, panel,
"Please confirm " + operation + " of " + pokes.size() + " Pokémon", JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE);
return response == JOptionPane.OK_OPTION;
}
private JPanel _buildPanelForOperation(String operation, ArrayList<Pokemon> pokes) {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
JPanel innerPanel = new JPanel();
innerPanel.setLayout(new BoxLayout(innerPanel, BoxLayout.Y_AXIS));
innerPanel.setAlignmentX(CENTER_ALIGNMENT);
JScrollPane scroll = new JScrollPane(innerPanel);
scroll.setAlignmentX(CENTER_ALIGNMENT);
scroll.setMaximumSize(new Dimension(Integer.MAX_VALUE, 150));
panel.setMaximumSize(panel.getSize());
pokes.forEach(p -> {
String str = PokeHandler.getLocalPokeName(p) + " - CP: " + p.getCp() + ", IV: "
+ Utilities.percentage(p.getIvRatio()) + "%";
switch (operation) {
case "Evolve":
str += " Cost: " + p.getCandiesToEvolve();
str += p.getCandiesToEvolve() > 1 ? " Candies" : " Candy";
break;
case "PowerUp":
str += " Cost: " + p.getCandyCostsForPowerup();
str += p.getCandyCostsForPowerup() > 1 ? " Candies" : " Candy";
str += " " + p.getStardustCostsForPowerup() + " Stardust";
break;
case "Rename":
break;
case "Transfer":
break;
}
innerPanel.add(new JLabel(str));
});
panel.add(scroll);
return panel;
}
private ArrayList<Pokemon> getSelectedPokemon() {
ArrayList<Pokemon> pokes = new ArrayList<>();
PokemonTableModel model = (PokemonTableModel) pt.getModel();
for (int i : pt.getSelectedRows()) {
Pokemon poke = model.getPokemonByIndex(i);
if (poke != null) {
pokes.add(poke);
}
}
return pokes;
}
private void refreshList() {
List<Pokemon> pokes = new ArrayList<>();
String search = searchBar.getText().replaceAll(" ", "").replaceAll("_", "").replaceAll("snek", "ekans").toLowerCase();
String[] terms = search.split(";");
try {
go.getInventories().getPokebank().getPokemons().forEach(poke -> {
boolean useFamilyName = config.getBool(ConfigKey.INCLUDE_FAMILY);
String familyName = "";
if (useFamilyName) {
// Try translating family name
try {
PokemonId familyPokemonId = PokemonId.valueOf(poke.getPokemonFamily().toString().replaceAll("FAMILY_", ""));
familyName = PokeHandler.getLocalPokeName(familyPokemonId.getNumber());
} catch (IllegalArgumentException e) {
familyName = poke.getPokemonFamily().toString();
}
}
String searchme = PokeHandler.getLocalPokeName(poke)
+ ((useFamilyName) ? familyName : "")
+ poke.getNickname()
+ poke.getMeta().getType1() + poke.getMeta().getType2() + poke.getMove1() + poke.getMove2()
+ poke.getPokeball();
searchme = searchme.replaceAll("_FAST", "").replaceAll("FAMILY_", "").replaceAll("NONE", "").replaceAll("ITEM_", "").replaceAll("_", "").replaceAll(" ", "").toLowerCase();
for (String s : terms) {
if (searchme.contains(s)) {
pokes.add(poke);
// Break, so that a Pokémon isn't added twice even if it matches more than one criteria
break;
}
}
});
pt.constructNewTableModel(go, (search.equals("") || search.equals("searchpokémon...")
? go.getInventories().getPokebank().getPokemons() : pokes));
for (int i = 0; i < pt.getModel().getColumnCount(); i++) {
JTableColumnPacker.packColumn(pt, i, 4);
}
// Custom cell renderers
// Magic numbers pulled from max values of their respective columns in the moveset rankings spreadsheet calculations
// @see https://www.reddit.com/r/TheSilphRoad/comments/4vcobt/posthotfix_pokemon_go_full_moveset_rankings/
TableColumn duelAbilityCol = this.pt.getColumnModel().getColumn(24);
duelAbilityCol.setCellRenderer(new MoveSetRankingRenderer(PokemonUtils.DUEL_ABILITY_MAX));
TableColumn gymAttackCol = this.pt.getColumnModel().getColumn(25);
gymAttackCol.setCellRenderer(new MoveSetRankingRenderer(PokemonUtils.GYM_OFFENSE_MAX));
TableColumn gymDefenseCol = this.pt.getColumnModel().getColumn(26);
gymDefenseCol.setCellRenderer(new MoveSetRankingRenderer(PokemonUtils.GYM_DEFENSE_MAX));
TableColumn duelAbilityIVCol = this.pt.getColumnModel().getColumn(29);
duelAbilityIVCol.setCellRenderer(new MoveSetRankingRenderer(PokemonUtils.DUEL_ABILITY_IV_MAX));
TableColumn gymAttackIVCol = this.pt.getColumnModel().getColumn(30);
gymAttackIVCol.setCellRenderer(new MoveSetRankingRenderer(PokemonUtils.GYM_OFFENSE_IV_MAX));
TableColumn gymDefenseIVCol = this.pt.getColumnModel().getColumn(31);
gymDefenseIVCol.setCellRenderer(new MoveSetRankingRenderer(PokemonUtils.GYM_DEFENSE_IV_MAX));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Provide custom formatting for the moveset ranking columns while allowing sorting on original values
*/
private static class MoveSetRankingRenderer extends JLabel implements TableCellRenderer {
private final long scale;
public MoveSetRankingRenderer(long scale) {
this.scale = scale;
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int rowIndex, int vColIndex) {
setText(Utilities.percentageWithTwoCharacters(Double.parseDouble(value.toString()), this.scale));
setToolTipText(NumberFormat.getInstance().format(value));
setOpaque(true);
setDefaultSelectionColors(table, isSelected, this);
return this;
}
}
private static void setDefaultSelectionColors(JTable table, boolean isSelected, JLabel tcr) {
if (isSelected) {
tcr.setBackground(table.getSelectionBackground());
tcr.setForeground(table.getSelectionForeground());
} else {
tcr.setBackground(table.getBackground());
tcr.setForeground(table.getForeground());
}
}
private static class PokemonTable extends JTable {
/**
* data types:
* 0 String - Nickname
* 1 Integer - Pokemon Number
* 2 String - Type / Pokemon
* 3 Double - IV %
* 4 Double - Level
* 5 Integer - Attack
* 6 Integer - Defense
* 7 Integer - Stamina
* 8 String - Type 1
* 9 String - Type 2
* 10 String - Move 1
* 11 String - Move 2
* 12 Integer - CP
* 13 Integer - HP
* 14 Integer - Max CP (Current)
* 15 Integer - Max CP
* 16 Integer - Max Evolved CP (Current)
* 17 Integer - Max Evolved CP
* 18 Integer - Candies of type
* 19 String - Candies to Evolve
* 20 Integer - Star Dust to level
* 21 String - Pokeball Type
* 22 String(Date) - Caught at
* 23 Boolean - Favorite
* 24 Long - duelAbility
* 25 Double - gymOffense
* 26 Long - gymDefense
* 27 Double - Move 1 Rating
* 28 Double - Move 2 Rating
* 29 Long - duelAbility IV
* 30 Double - gymOffense IV
* 31 Long - gymDefense IV
*/
ConfigNew config = ConfigNew.getConfig();
int sortColIndex1, sortColIndex2;
SortOrder sortOrder1, sortOrder2;
private PokemonTable() {
setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
setAutoResizeMode(AUTO_RESIZE_OFF);
// Load sort configs
sortColIndex1 = config.getInt(ConfigKey.SORT_COLINDEX_1);
sortColIndex2 = config.getInt(ConfigKey.SORT_COLINDEX_2);
try {
sortOrder1 = SortOrder.valueOf(config.getString(ConfigKey.SORT_ORDER_1));
sortOrder2 = SortOrder.valueOf(config.getString(ConfigKey.SORT_ORDER_2));
} catch (IllegalArgumentException e) {
e.printStackTrace();
sortOrder1 = SortOrder.ASCENDING;
sortOrder2 = SortOrder.ASCENDING;
}
}
private void constructNewTableModel(PokemonGo go, List<Pokemon> pokes) {
PokemonTableModel ptm = new PokemonTableModel(go, pokes, this);
setModel(ptm);
TableRowSorter<TableModel> trs = new TableRowSorter<>(getModel());
Comparator<Integer> c = (i1, i2) -> Math.round(i1 - i2);
Comparator<Double> cDouble = (d1, d2) -> (int) (d1 - d2);
Comparator<String> cDate = (date1, date2) -> DateHelper.fromString(date1).compareTo(DateHelper.fromString(date2));
Comparator<String> cNullableInt = (s1, s2) -> {
if ("-".equals(s1))
s1 = "0";
if ("-".equals(s2))
s2 = "0";
return Integer.parseInt(s1) - Integer.parseInt(s2);
};
Comparator<Long> cLong = (l1, l2) -> l2.compareTo(l1);
trs.setComparator(0, c);
trs.setComparator(3, cDouble);
trs.setComparator(4, cDouble);
trs.setComparator(5, c);
trs.setComparator(6, c);
trs.setComparator(7, c);
trs.setComparator(12, c);
trs.setComparator(13, c);
trs.setComparator(14, c);
trs.setComparator(15, c);
trs.setComparator(16, c);
trs.setComparator(17, c);
trs.setComparator(18, c);
trs.setComparator(19, cNullableInt);
trs.setComparator(20, c);
trs.setComparator(22, cDate);
trs.setComparator(24, cLong);
trs.setComparator(25, cDouble);
trs.setComparator(26, cLong);
trs.setComparator(27, cDouble);
trs.setComparator(28, cDouble);
trs.setComparator(29, cLong);
trs.setComparator(30, cDouble);
trs.setComparator(31, cLong);
setRowSorter(trs);
List<SortKey> sortKeys = new ArrayList<>();
sortKeys.add(new SortKey(sortColIndex1, sortOrder1));
sortKeys.add(new SortKey(sortColIndex2, sortOrder2));
trs.setSortKeys(sortKeys);
// Add listener to save those sorting values
trs.addRowSorterListener(
e -> {
RowSorter sorter = e.getSource();
if (sorter != null) {
List<SortKey> keys = sorter.getSortKeys();
if (keys.size() > 0) {
System.out.println("DEBUG-> Size: " + keys.size());
for (SortKey key : keys) {
System.out.println("SortKey: { column: " + key.getColumn() + ", order: " + key.getSortOrder().toString() + "}");
}
SortKey prim = keys.get(0);
sortOrder1 = prim.getSortOrder();
config.setString(ConfigKey.SORT_ORDER_1, sortOrder1.toString());
sortColIndex1 = prim.getColumn();
config.setInt(ConfigKey.SORT_COLINDEX_1, sortColIndex1);
}
if (keys.size() > 1) {
SortKey sec = keys.get(1);
sortOrder2 = sec.getSortOrder();
config.setString(ConfigKey.SORT_ORDER_2, sortOrder2.toString());
sortColIndex2 = sec.getColumn();
config.setInt(ConfigKey.SORT_COLINDEX_2, sortColIndex2);
}
}
});
}
}
private static class PokemonTableModel extends AbstractTableModel {
PokemonTable pt;
private final ArrayList<Pokemon> pokeCol = new ArrayList<>();
private final ArrayList<Integer> numIdCol = new ArrayList<>();//0
private final ArrayList<String> nickCol = new ArrayList<>(),//1
speciesCol = new ArrayList<>();//2
private final ArrayList<Double> ivCol = new ArrayList<>(),//3
levelCol = new ArrayList<>();//4
private final ArrayList<Integer> atkCol = new ArrayList<>(),//5
defCol = new ArrayList<>(),//6
stamCol = new ArrayList<>();//7
private final ArrayList<String> type1Col = new ArrayList<>(),//8
type2Col = new ArrayList<>(),//9
move1Col = new ArrayList<>(),//10
move2Col = new ArrayList<>();//11
private final ArrayList<Integer> cpCol = new ArrayList<>(),//12
hpCol = new ArrayList<>(),//13
maxCpCurrentCol = new ArrayList<>(),//14
maxCpCol = new ArrayList<>(),//15
maxEvolvedCpCurrentCol = new ArrayList<>(),//16
maxEvolvedCpCol = new ArrayList<>(),//17
candiesCol = new ArrayList<>();//18
private final ArrayList<String> candies2EvlvCol = new ArrayList<>();//19
private final ArrayList<Integer> dustToLevelCol = new ArrayList<>();//20
private final ArrayList<String> pokeballCol = new ArrayList<>(),//21
caughtCol = new ArrayList<>(),//22
favCol = new ArrayList<>();//23
private final ArrayList<Long> duelAbilityCol = new ArrayList<>();//24
private final ArrayList<Double> gymOffenseCol = new ArrayList<>();//25
private final ArrayList<Long> gymDefenseCol = new ArrayList<>();//26
private final ArrayList<Double> move1RatingCol = new ArrayList<>(),//27
move2RatingCol = new ArrayList<>();//28
private final ArrayList<Long> duelAbilityIVCol = new ArrayList<>();//29
private final ArrayList<Double> gymOffenseIVCol = new ArrayList<>();//30
private final ArrayList<Long> gymDefenseIVCol = new ArrayList<>();//31
@Deprecated
private PokemonTableModel(PokemonGo go, List<Pokemon> pokes, PokemonTable pt) {
this.pt = pt;
MutableInt i = new MutableInt();
pokes.forEach(p -> {
pokeCol.add(i.getValue(), p);
numIdCol.add(i.getValue(), p.getMeta().getNumber());
nickCol.add(i.getValue(), p.getNickname());
speciesCol.add(i.getValue(),
PokeHandler.getLocalPokeName(p));
levelCol.add(i.getValue(), (double) p.getLevel());
ivCol.add(i.getValue(), Utilities.percentage(p.getIvRatio()));
cpCol.add(i.getValue(), p.getCp());
atkCol.add(i.getValue(), p.getIndividualAttack());
defCol.add(i.getValue(), p.getIndividualDefense());
stamCol.add(i.getValue(), p.getIndividualStamina());
type1Col.add(i.getValue(), StringUtils.capitalize(p.getMeta().getType1().toString().toLowerCase()));
type2Col.add(i.getValue(), StringUtils.capitalize(p.getMeta().getType2().toString().toLowerCase().replaceAll("none", "")));
Double dps1 = PokemonUtils.dpsForMove(p, true);
Double dps2 = PokemonUtils.dpsForMove(p, false);
move1Col.add(i.getValue(), WordUtils.capitalize(p.getMove1().toString().toLowerCase().replaceAll("_fast", "").replaceAll("_", " ")) + " (" + String.format("%.2f", dps1) + "dps)");
move2Col.add(i.getValue(), WordUtils.capitalize(p.getMove2().toString().toLowerCase().replaceAll("_", " ")) + " (" + String.format("%.2f", dps2) + "dps)");
hpCol.add(i.getValue(), p.getMaxStamina());
int trainerLevel = 1;
try {
trainerLevel = go.getPlayerProfile().getStats().getLevel();
} catch (Exception e1) {
e1.printStackTrace();
}
// Max CP calculation for current Pokemon
PokemonMeta pokemonMeta = PokemonMetaRegistry.getMeta(p.getPokemonId());
int maxCpCurrent = 0, maxCp = 0;
if (pokemonMeta == null) {
System.out.println("Error: Cannot find meta data for " + p.getPokemonId().name());
} else {
int attack = p.getIndividualAttack() + pokemonMeta.getBaseAttack();
int defense = p.getIndividualDefense() + pokemonMeta.getBaseDefense();
int stamina = p.getIndividualStamina() + pokemonMeta.getBaseStamina();
maxCpCurrent = PokemonCpUtils.getMaxCpForTrainerLevel(attack, defense, stamina, trainerLevel);
maxCp = PokemonCpUtils.getMaxCp(attack, defense, stamina);
maxCpCurrentCol.add(i.getValue(), maxCpCurrent);
maxCpCol.add(i.getValue(), maxCp);
}
// Max CP calculation for highest evolution of current Pokemon
PokemonFamilyId familyId = p.getPokemonFamily();
PokemonId highestFamilyId = PokemonMetaRegistry.getHightestForFamily(familyId);
// Eeveelutions exception handling
if (familyId.getNumber() == PokemonFamilyId.FAMILY_EEVEE.getNumber()) {
if (p.getPokemonId().getNumber() == PokemonId.EEVEE.getNumber()) {
PokemonMeta vap = PokemonMetaRegistry.getMeta(PokemonId.VAPOREON);
PokemonMeta fla = PokemonMetaRegistry.getMeta(PokemonId.FLAREON);
PokemonMeta jol = PokemonMetaRegistry.getMeta(PokemonId.JOLTEON);
if (vap != null && fla != null && jol != null) {
Comparator<PokemonMeta> cMeta = (m1, m2) -> {
int comb1 = PokemonCpUtils.getMaxCp(m1.getBaseAttack(), m1.getBaseDefense(), m1.getBaseStamina());
int comb2 = PokemonCpUtils.getMaxCp(m2.getBaseAttack(), m2.getBaseDefense(), m2.getBaseStamina());
return comb1 - comb2;
};
highestFamilyId = PokemonId.forNumber(Collections.max(Arrays.asList(vap, fla, jol), cMeta).getNumber());
}
} else {
// This is one of the eeveelutions, so PokemonMetaRegistry.getHightestForFamily() returns Eevee.
// We correct that here
highestFamilyId = p.getPokemonId();
}
}
PokemonMeta highestFamilyMeta = PokemonMetaRegistry.getMeta(highestFamilyId);
if (highestFamilyId == p.getPokemonId()) {
maxEvolvedCpCurrentCol.add(i.getValue(), maxCpCurrent);
maxEvolvedCpCol.add(i.getValue(), maxCp);
} else if (highestFamilyMeta == null) {
System.out.println("Error: Cannot find meta data for " + highestFamilyId.name());
} else {
int attack = highestFamilyMeta.getBaseAttack() + p.getIndividualAttack();
int defense = highestFamilyMeta.getBaseDefense() + p.getIndividualDefense();
int stamina = highestFamilyMeta.getBaseStamina() + p.getIndividualStamina();
maxEvolvedCpCurrentCol.add(i.getValue(), PokemonCpUtils.getMaxCpForTrainerLevel(attack, defense, stamina, trainerLevel));
maxEvolvedCpCol.add(i.getValue(), PokemonCpUtils.getMaxCp(attack, defense, stamina));
}
try {
candiesCol.add(i.getValue(), p.getCandy());
} catch (Exception e) {
e.printStackTrace();
}
if (p.getCandiesToEvolve() != 0)
candies2EvlvCol.add(i.getValue(), String.valueOf(p.getCandiesToEvolve()));
else
candies2EvlvCol.add(i.getValue(), "-");
dustToLevelCol.add(i.getValue(), p.getStardustCostsForPowerup());
pokeballCol.add(i.getValue(), WordUtils.capitalize(p.getPokeball().toString().toLowerCase().replaceAll("item_", "").replaceAll("_", " ")));
caughtCol.add(i.getValue(), DateHelper.toString(DateHelper.fromTimestamp(p.getCreationTimeMs())));
favCol.add(i.getValue(), (p.isFavorite()) ? "True" : "");
duelAbilityCol.add(i.getValue(), PokemonUtils.duelAbility(p, false));
gymOffenseCol.add(i.getValue(), PokemonUtils.gymOffense(p, false));
gymDefenseCol.add(i.getValue(), PokemonUtils.gymDefense(p, false));
duelAbilityIVCol.add(i.getValue(), PokemonUtils.duelAbility(p, true));
gymOffenseIVCol.add(i.getValue(), PokemonUtils.gymOffense(p, true));
gymDefenseIVCol.add(i.getValue(), PokemonUtils.gymDefense(p, true));
move1RatingCol.add(i.getValue(), PokemonUtils.moveRating(p, true));
move2RatingCol.add(i.getValue(), PokemonUtils.moveRating(p, false));
i.increment();
});
}
private Pokemon getPokemonByIndex(int i) {
try {
return pokeCol.get(pt.convertRowIndexToModel(i));
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Override
public String getColumnName(int columnIndex) {
switch (columnIndex) {
case 0:
return "Id";
case 1:
return "Nickname";