forked from mrkite/minutor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minutor.cpp
1220 lines (1053 loc) · 39.3 KB
/
minutor.cpp
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
/** Copyright (c) 2013, Sean Kasun */
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QAction>
#include <QtWidgets/QFileDialog>
#include <QtWidgets/QMessageBox>
#include <QtWidgets/QMenu>
#include <QtWidgets/QMenuBar>
#include <QtWidgets/QStatusBar>
#include <QtWidgets/QTreeWidget>
#include <QProgressDialog>
#include <QDir>
#include <QRegExp>
#include <QtCore/QJsonDocument>
#include <QtCore/QJsonArray>
#include <QtCore/QJsonObject>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply>
#include "minutor.h"
#include "mapview.h"
#include "labelledseparator.h"
#include "labelledslider.h"
#include "nbt/nbt.h"
#include "identifier/definitionmanager.h"
#include "identifier/entityidentifier.h"
#include "identifier/dimensionidentifier.h"
#include "settings.h"
#include "worldinfo.h"
#include "worldsave.h"
#include "overlay/properties.h"
#include "overlay/generatedstructure.h"
#include "overlay/village.h"
#include "jumpto.h"
#include "pngexport.h"
#include "chunkcache.h"
#include "search/searchchunksdialog.h"
#include "search/searchentityplugin.h"
#include "search/searchblockplugin.h"
#include "search/statisticdialog.h"
Minutor::Minutor()
{
m_ui.setupUi(this);
// central MapView wiget
mapview = new MapView;
connect(mapview, SIGNAL(hoverTextChanged(QString)),
statusBar(), SLOT(showMessage(QString)));
connect(mapview, SIGNAL(showProperties(QVariant)),
this, SLOT(showProperties(QVariant)));
ChunkCache const & cache = ChunkCache::Instance();
connect(&cache, &ChunkCache::structureFound,
this, &Minutor::addStructureFromChunk);
// Definition manager
dm = new DefinitionManager(this);
mapview->attach(dm);
connect(dm, SIGNAL(packsChanged()),
this, SLOT(updateDimensions()));
// world information
WorldInfo & wi = WorldInfo::Instance();
connect(&wi, SIGNAL(dimensionChanged(const DimensionInfo &)),
this, SLOT(viewDimension(const DimensionInfo &)));
// "Settings" dialog
dialogSettings = new Settings(this);
connect(dialogSettings, SIGNAL(settingsUpdated()),
this, SLOT(rescanWorlds()));
// "Jump To" dialog
dialogJumpTo = new JumpTo(this);
if (dialogSettings->autoUpdate) {
// get time of last update
QSettings settings;
QDateTime lastUpdateTime = settings.value("packupdate").toDateTime();
// auto-update only once a week
if (lastUpdateTime.addDays(7) < QDateTime::currentDateTime())
dm->autoUpdate();
}
setWindowTitle(qApp->applicationName());
// create dynamic part of menu
getWorldList();
createMenus();
// connect actions to handlers
createActions();
// configure status bar
createStatusBar();
// central layout
QBoxLayout *mainLayout;
if (dialogSettings->verticalDepth) {
mainLayout = new QHBoxLayout;
depth = new LabelledSlider(Qt::Vertical);
mainLayout->addWidget(mapview, 1);
mainLayout->addWidget(depth);
} else {
mainLayout = new QVBoxLayout;
depth = new LabelledSlider(Qt::Horizontal);
mainLayout->addWidget(depth);
mainLayout->addWidget(mapview, 1);
}
depth->setValue(255);
mainLayout->setSpacing(0);
mainLayout->setContentsMargins(0, 0, 0, 0);
connect(depth, SIGNAL(valueChanged(int)),
mapview, SLOT (setDepth(int)));
connect(mapview, SIGNAL(demandDepthChange(double)),
depth, SLOT (changeValue(double)));
connect(mapview, SIGNAL(demandDepthValue(double)),
depth, SLOT (setValue(double)));
connect(this, SIGNAL(worldLoaded(bool)),
mapview, SLOT (setEnabled(bool)));
connect(this, SIGNAL(worldLoaded(bool)),
depth, SLOT (setEnabled(bool)));
// player cache request/response to Mojang API
connect(&this->qnam, &QNetworkAccessManager::finished,
this, &Minutor::updatePlayerCache);
m_ui.centralwidget->setLayout(mainLayout);
layout()->setContentsMargins(0, 0, 0, 0);
propView = new Properties(this);
emit worldLoaded(false);
}
Minutor::~Minutor() {
// wait for sheduled tasks
QThreadPool::globalInstance()->waitForDone();
}
void Minutor::openWorld() {
QAction *action = qobject_cast<QAction*>(sender());
if (action)
loadWorld(action->data().toString());
}
void Minutor::open() {
QString dirName = QFileDialog::getExistingDirectory(this, tr("Open World"));
if (!dirName.isEmpty()) {
QDir path(dirName);
if (!path.exists("level.dat")) {
QMessageBox::warning(this,
tr("Couldn't open world"),
tr("%1 is not a valid Minecraft world")
.arg(dirName),
QMessageBox::Cancel);
return;
}
loadWorld(dirName);
}
}
void Minutor::reload() {
auto loc = *(mapview->getLocation());
loadWorld(currentWorld);
mapview->setLocation(loc.x, loc.y, loc.z, false, true);
}
void Minutor::save() {
int w_top, w_left, w_right, w_bottom;
WorldSave::findWorldBounds(mapview->getWorldPath(),
&w_top, &w_left, &w_bottom, &w_right);
PngExport pngoptions;
pngoptions.setBoundsFromChunks(w_top, w_left, w_bottom, w_right);
pngoptions.exec();
if (pngoptions.result() == QDialog::Rejected)
return;
// get filname to save to
QFileDialog fileDialog(this);
fileDialog.setDefaultSuffix("png");
QString filename = fileDialog.getSaveFileName(this, tr("Save world as PNG"),
QString(), "PNG Images (*.png)");
// check if filename was given
if (filename.isEmpty())
return;
// add .png suffix if not present
QFile file(filename);
QFileInfo fileinfo(file);
if (fileinfo.suffix().isEmpty()) {
filename.append(".png");
}
// save world to PNG image
savePNG(filename, false,
pngoptions.getRegionChecker(), pngoptions.getChunkChecker(),
pngoptions.getTop(), pngoptions.getLeft(),
pngoptions.getBottom(), pngoptions.getRight());
}
void Minutor::savePNG(QString filename, bool autoclose,
bool regionChecker, bool chunkChecker,
int w_top, int w_left, int w_bottom, int w_right) {
progressAutoclose = autoclose;
if (!filename.isEmpty()) {
WorldSave *ws = new WorldSave(filename, mapview,
regionChecker, chunkChecker,
w_top, w_left, w_bottom, w_right);
progress = new QProgressDialog();
progress->setCancelButton(NULL);
progress->setMaximum(100);
progress->show();
connect(ws, SIGNAL(progress(QString, double)),
this, SLOT(saveProgress(QString, double)));
connect(ws, SIGNAL(finished()),
this, SLOT(saveFinished()));
QThreadPool::globalInstance()->start(ws);
}
}
void Minutor::saveProgress(QString status, double value) {
progress->setValue(value*100);
progress->setLabelText(status);
}
void Minutor::saveFinished() {
progress->hide();
delete progress;
if (progressAutoclose)
this->close();
}
void Minutor::closeWorld() {
// clear "Jump to Player" menu
locations.clear();
for (int i = 0; i < playerActions.size(); i++) {
m_ui.menu_JumpPlayer->removeAction(playerActions[i]);
delete playerActions[i];
}
playerActions.clear();
m_ui.menu_JumpPlayer->setEnabled(false);
// clear "Dimensions" menu
WorldInfo::Instance().clearDimensionsMenu(m_ui.menu_Dimension);
// clear overlays
mapview->clearOverlayItems();
// clear other stuff
currentWorld = QDir();
emit worldLoaded(false);
// clear "Structures Overlays"
for (int i = 0; i < structureOverlayActions.size(); i++) {
m_ui.menu_Overlay->removeAction(structureOverlayActions[i]);
delete structureOverlayActions[i];
}
structureOverlayActions.clear();
overlayItemTypes.clear();
auto submenues = m_ui.menu_Overlay->findChildren<QMenu*>();
qDeleteAll(submenues);
}
void Minutor::jumpToLocation() {
QAction *action = qobject_cast<QAction*>(sender());
if (action) {
Location loc = locations[action->data().toInt()];
if ((loc.dimension == "minecraft:the_nether") && (m_ui.menu_Dimension[0].isEnabled())) {
// dimMenu[0] defaults to Overworld
loc.x *= 8;
loc.z *= 8;
}
mapview->setLocation(loc.x, loc.z);
}
}
void Minutor::jumpToXZ(int blockX, int blockZ) {
mapview->setLocation(blockX, blockZ);
}
void Minutor::setViewLighting(bool value) {
m_ui.action_Lighting->setChecked(value);
toggleFlags();
}
void Minutor::setViewMobspawning(bool value) {
m_ui.action_MobSpawning->setChecked(value);
toggleFlags();
}
void Minutor::setViewCavemode(bool value) {
m_ui.action_CaveMode->setChecked(value);
toggleFlags();
}
void Minutor::setViewDepthshading(bool value) {
m_ui.action_DepthShading->setChecked(value);
toggleFlags();
}
void Minutor::setViewBiomeColors(bool value) {
m_ui.action_BiomeColors->setChecked(value);
toggleFlags();
}
void Minutor::setViewSeaGroundMode(bool value) {
m_ui.action_SeaGround->setChecked(value);
toggleFlags();
}
void Minutor::setViewSingleLayer(bool value) {
m_ui.action_SingleLayer->setChecked(value);
toggleFlags();
}
void Minutor::setViewSlimeChunks(bool value) {
m_ui.action_SlimeChunks->setChecked(value);
toggleFlags();
}
void Minutor::setViewInhabitedTime(bool value) {
m_ui.action_InhabitedTime->setChecked(value);
toggleFlags();
}
void Minutor::setViewChunkLock(bool value) {
m_ui.action_ChunkLock->setChecked(value);
toggleFlags();
}
void Minutor::setDepth(int value) {
depth->setValue(value);
}
void Minutor::toggleFlags() {
int flags = 0;
if (m_ui.action_Lighting->isChecked()) flags |= MapView::flgLighting;
if (m_ui.action_MobSpawning->isChecked()) flags |= MapView::flgMobSpawn;
if (m_ui.action_CaveMode->isChecked()) flags |= MapView::flgCaveMode;
if (m_ui.action_DepthShading->isChecked()) flags |= MapView::flgDepthShading;
if (m_ui.action_BiomeColors->isChecked()) flags |= MapView::flgBiomeColors;
if (m_ui.action_SeaGround->isChecked()) flags |= MapView::flgSeaGround;
if (m_ui.action_SingleLayer->isChecked()) flags |= MapView::flgSingleLayer;
if (m_ui.action_SlimeChunks->isChecked()) flags |= MapView::flgSlimeChunks;
if (m_ui.action_InhabitedTime->isChecked()) flags |= MapView::flgInhabitedTime;
if (m_ui.action_ChunkLock->isChecked()) flags |= MapView::flgChunkLock;
mapview->setFlags(flags);
mapview->redraw();
}
void Minutor::toggleOverlays()
{
QSet<QString> overlayTypes;
for (auto action : structureOverlayActions) {
if (action->isChecked()) {
overlayTypes.insert(action->data().toMap()["type"].toString());
}
}
for (auto action : entityOverlayActions) {
if (action->isChecked()) {
overlayTypes.insert(action->data().toString());
}
}
mapview->setVisibleOverlayItemTypes(overlayTypes);
mapview->redraw();
}
void Minutor::toggleStructures(bool checked)
{
bool toggleEnabled = false;
for (QAction * action: m_ui.menu_Overlay->actions()) {
if (action == separatorStructureOverlay) {
// start after this separator
toggleEnabled = true;
} else if (action == separatorEntityOverlay) {
// stop before this separator
toggleEnabled = false;
} else if (action->menu() || action->isSeparator()) {
// stuff we want to ignore
} else { // action
if (toggleEnabled)
action->setChecked(checked);
}
}
toggleOverlays();
}
void Minutor::toggleEntities(bool checked)
{
bool toggleEnabled = false;
for (QAction * action: m_ui.menu_Overlay->actions()) {
if (action == separatorEntityOverlay) {
// start after this separator
toggleEnabled = true;
} else if (action->menu() || action->isSeparator() || (action == separatorStructureOverlay)) {
// stuff we want to ignore
} else { // action
if (toggleEnabled)
action->setChecked(checked);
}
}
toggleOverlays();
}
template<typename CheckBox, typename Iter>
void updateTristateCheckBox(CheckBox* cbox, Iter b_iter, Iter e_iter)
{
auto is_checked = [](auto action) { return action->isChecked(); };
if (std::any_of(b_iter, e_iter, is_checked)) {
if (std::all_of(b_iter, e_iter, is_checked)) {
cbox->setCheckState(Qt::Checked);
} else {
cbox->setCheckState(Qt::PartiallyChecked);
}
} else {
cbox->setCheckState(Qt::Unchecked);
}
}
void Minutor::updateToggleAllStructuresState()
{
auto actions = m_ui.menu_Overlay->actions();
auto b_iter = std::next(actions.begin(), actions.indexOf(separatorStructureOverlay) + 1);
auto e_iter = std::next(actions.begin(), actions.indexOf(separatorEntityOverlay));
updateTristateCheckBox(qobject_cast<LabeledSeparator*>(separatorStructureOverlay), b_iter, e_iter);
}
void Minutor::updateToggleAllEntitiesState()
{
auto actions = m_ui.menu_Overlay->actions();
auto b_iter = std::next(actions.begin(), actions.indexOf(separatorEntityOverlay) + 1);
updateTristateCheckBox(qobject_cast<LabeledSeparator*>(separatorEntityOverlay), b_iter, actions.end());
}
void Minutor::viewDimension(QString dim_string)
{
// when missing, add default namespace
if (!dim_string.contains(":"))
dim_string.insert(0,"minecraft:");
if (dim_string.startsWith("minecraft:")) {
// vanilla dimension
const DimensionInfo & dim = DimensionIdentifier::Instance().getDimensionInfo(dim_string);
if (dim.name != "Dummy Dimension") {
viewDimension(dim);
return;
}
}
// custom dimension
WorldInfo & wi(WorldInfo::Instance());
const QList<DimensionInfo> & dimensions = wi.getDimensions();
for (auto & dim: dimensions) {
if (dim.name == dim_string) {
viewDimension(dim);
return;
}
}
}
void Minutor::viewDimension(const DimensionInfo &dim) {
// update visability of Structure Overlays
for (QAction * action : qAsConst(structureOverlayActions)) {
QString dimension = action->data().toMap()["dimension"].toString();
if (dimension.isEmpty() ||
!dimension.compare(dim.name, Qt::CaseInsensitive)) {
action->setVisible(true);
} else {
action->setVisible(false);
}
}
// change depth slider or adjust default Y when dimension is activated
WorldInfo & wi(WorldInfo::Instance());
if (wi.getDataVersion() < 2800 ) {
// legacy versions before Cliffs & Caves (up to 1.17)
depth->setRange(0, 255);
dialogJumpTo->updateYrange(0, 255);
if (dim.id == "minecraft:overworld") {
depth->setValue(127); // cloud level
} else if (dim.id == "minecraft:the_nether") {
depth->setValue(95); // somewhere below Nether ceiling
} else {
depth->setValue(255); // top
}
} else {
// after Cliffs & Caves (1.18+)
depth->setRange(dim.minY, dim.maxY);
depth->setValue(dim.defaultY);
dialogJumpTo->updateYrange(dim.minY, dim.maxY);
}
// ensure selected action is checked
for (auto & action: m_ui.menu_Dimension->actions())
if (action->text() == dim.name) {
action->setChecked(true);
break;
}
// clear current map & update scale
QString path = QDir(currentWorld).absoluteFilePath(dim.path);
mapview->setDimension(path, dim.scale);
// reload structures for that dimension (old format from data directory)
loadStructures(path);
}
void Minutor::about() {
QMessageBox::about(this, tr("About %1").arg(qApp->applicationName()),
tr("<b>%1</b> v%2<br/>\n"
"© Copyright %3, %4")
.arg(qApp->applicationName())
.arg(qApp->applicationVersion())
.arg("2010 - 2024")
.arg(qApp->organizationName()));
}
void Minutor::updateDatapackActions()
{
m_ui.action_ChunkLock->setVisible(WorldInfo::Instance().isDatapackEnabled("chunklock"));
}
void Minutor::updateDimensions() {
WorldInfo::Instance().getDimensionsInWorld(currentWorld, m_ui.menu_Dimension, this);
}
void Minutor::createActions() {
// [File]
connect(m_ui.action_Open, SIGNAL(triggered()),
this, SLOT(open()));
connect(m_ui.action_Reload, SIGNAL(triggered()),
this, SLOT(reload()));
connect(this, SIGNAL(worldLoaded(bool)),
m_ui.action_Reload, SLOT(setEnabled(bool)));
connect(m_ui.action_SavePNG, SIGNAL(triggered()),
this, SLOT(save()));
connect(this, SIGNAL(worldLoaded(bool)),
m_ui.action_SavePNG, SLOT(setEnabled(bool)));
m_ui.action_Exit->setStatusTip(tr("Exit %1").arg(qApp->applicationName()));
connect(m_ui.action_Exit, SIGNAL(triggered()),
this, SLOT(close()));
// [View->Jump]
connect(m_ui.action_JumpSpawn, SIGNAL(triggered()),
this, SLOT(jumpToLocation()));
connect(this, SIGNAL(worldLoaded(bool)),
m_ui.action_JumpSpawn, SLOT(setEnabled(bool)));
connect(m_ui.action_JumpTo, SIGNAL(triggered()),
dialogJumpTo, SLOT(show()));
connect(this, SIGNAL(worldLoaded(bool)),
m_ui.action_JumpTo, SLOT(setEnabled(bool)));
// [View->Modes]
connect(m_ui.action_Lighting, SIGNAL(triggered()),
this, SLOT(toggleFlags()));
connect(m_ui.action_MobSpawning, SIGNAL(triggered()),
this, SLOT(toggleFlags()));
connect(m_ui.action_CaveMode, SIGNAL(triggered()),
this, SLOT(toggleFlags()));
connect(m_ui.action_DepthShading, SIGNAL(triggered()),
this, SLOT(toggleFlags()));
connect(m_ui.action_BiomeColors, SIGNAL(triggered()),
this, SLOT(toggleFlags()));
connect(m_ui.action_SeaGround, SIGNAL(triggered()),
this, SLOT(toggleFlags()));
connect(m_ui.action_SingleLayer, SIGNAL(triggered()),
this, SLOT(toggleFlags()));
connect(m_ui.action_SlimeChunks, SIGNAL(triggered()),
this, SLOT(toggleFlags()));
connect(m_ui.action_InhabitedTime, SIGNAL(triggered()),
this, SLOT(toggleFlags()));
connect(m_ui.action_ChunkLock, SIGNAL(triggered()),
this, SLOT(toggleFlags()));
// [View->Others]
// m_ui.action_Refresh->setStatusTip(tr("Reloads all chunks, "
// "but keeps the same position / dimension"));
connect(m_ui.action_Refresh, SIGNAL(triggered()),
mapview, SLOT(clearCache()));
// [Search]
connect(m_ui.action_SearchEntity, &QAction::triggered,
this, &Minutor::openSearchEntityDialog);
connect(this, &Minutor::worldLoaded,
m_ui.action_SearchEntity, &QAction::setEnabled);
connect(m_ui.action_SearchBlock, &QAction::triggered,
this, &Minutor::openSearchBlockDialog);
connect(this, &Minutor::worldLoaded,
m_ui.action_SearchBlock, &QAction::setEnabled);
connect(m_ui.action_StatisticBlock, &QAction::triggered,
this, &Minutor::openStatisticBlockDialog);
connect(this, &Minutor::worldLoaded,
m_ui.action_StatisticBlock, &QAction::setEnabled);
// [Help]
m_ui.action_About->setStatusTip(tr("About %1").arg(qApp->applicationName()));
connect(m_ui.action_About, SIGNAL(triggered()),
this, SLOT(about()));
m_ui.action_Settings->setStatusTip(tr("Change %1 Settings").arg(qApp->applicationName()));
connect(m_ui.action_Settings, SIGNAL(triggered()),
dialogSettings, SLOT(show()));
connect(m_ui.action_ManageDefinitions, SIGNAL(triggered()),
dm, SLOT(show()));
connect(dialogSettings, SIGNAL(checkForUpdates()),
dm, SLOT(checkForUpdates()));
}
// actionName will be modified, a "&" is added
QKeySequence Minutor::generateUniqueKeyboardShortcut(QString *actionName) {
// generate a unique keyboard shortcut
QKeySequence sequence;
// test all letters in given name
QString testName(*actionName);
for (int ampPos = testName.indexOf(":") + 1; ampPos < testName.length(); ++ampPos) {
QChar c = testName[ampPos];
sequence = QKeySequence(QString("Ctrl+")+c);
for (auto m : menuBar()->findChildren<QMenu*>()) {
for (auto a : m->actions()) {
if (a->shortcut() == sequence) {
sequence = QKeySequence();
break;
}
}
if (sequence.isEmpty())
break; // already eliminated this as a possbility
}
if (!sequence.isEmpty()) { // not eliminated, this one is ok
*actionName = testName.mid(0, ampPos) + "&" + testName.mid(ampPos);
break;
}
}
return sequence;
}
void Minutor::insertToggleAllAction(QMenu* menu)
{
Q_ASSERT(menu);
// todo: find good name
auto action = new LabeledSeparator(tr("toggle all"), menu);
menu->addAction(action);
connect(action, &QAction::triggered, menu, [=](bool checked) {
auto actions = menu->actions();
std::for_each(std::next(actions.begin()), actions.end(),
[=](auto action) { action->setChecked(checked); });
});
connect(action, &QAction::triggered, this, &Minutor::toggleOverlays);
}
void Minutor::updateToggleAllState(QMenu* menu)
{
Q_ASSERT(menu);
auto actions = menu->actions();
auto toggle_all_action = qobject_cast<LabeledSeparator*>(actions.constFirst());
Q_ASSERT(toggle_all_action);
updateTristateCheckBox(toggle_all_action, std::next(actions.begin()), actions.end());
}
void Minutor::createMenus() {
// [File]
m_ui.menu_OpenWorld->addActions(worldActions);
if (worldActions.size() == 0) // no worlds found
m_ui.menu_OpenWorld->setEnabled(false);
// [View]
// [Overlay]
separatorEntityOverlay = new LabeledSeparator("Entity Overlays", this);
separatorStructureOverlay = new LabeledSeparator("Structure Overlays", this);
m_ui.menu_Overlay->addAction(separatorStructureOverlay);
m_ui.menu_Overlay->addAction(separatorEntityOverlay);
connect(separatorStructureOverlay, &LabeledSeparator::triggered,
this, &Minutor::toggleStructures);
connect(separatorEntityOverlay, &LabeledSeparator::triggered,
this, &Minutor::toggleEntities);
EntityIdentifier &ei = EntityIdentifier::Instance();
for (auto it = ei.getCategoryList().constBegin();
it != ei.getCategoryList().constEnd(); it++) {
QString category = it->first;
QColor catcolor = it->second;
QString actionName = category;
QKeySequence sequence = generateUniqueKeyboardShortcut(&actionName);
QPixmap pixmap(16, 16);
QColor solidColor(catcolor);
solidColor.setAlpha(255);
pixmap.fill(solidColor);
// create new QAction
QAction* action = new QAction(pixmap, actionName, this);
action->setShortcut(sequence);
action->setStatusTip(tr("Toggle viewing of %1").arg(category));
action->setEnabled(true);
action->setData("Entity."+category);
action->setCheckable(true);
// put it into menu
entityOverlayActions.push_back(action);
m_ui.menu_Overlay->addAction(action); // add at bottom
// connect handler
connect(action, SIGNAL(triggered()),
this, SLOT(toggleOverlays()));
connect(action, SIGNAL(triggered()),
this, SLOT(updateToggleAllEntitiesState()));
}
// [Search]
// [Help]
}
void Minutor::createStatusBar() {
statusBar()->showMessage(tr("Ready"));
}
void Minutor::getWorldList() {
QDir mc(dialogSettings->mcpath);
if (mc.exists("saves")) {
mc.cd("saves");
}
// Create an action for each world found:
WorldInfo & wi(WorldInfo::Instance());
QDirIterator it(mc);
while (it.hasNext()) {
it.next();
if (!it.fileInfo().isDir() || !wi.parseWorldFolder(it.filePath())) {
continue;
}
auto * w = new QAction(this);
w->setText(wi.getLevelName());
w->setData(it.filePath());
connect(w, SIGNAL(triggered()),
this, SLOT(openWorld()));
worldActions.append(w);
}
wi.clear();
// Sort the actions by the world name:
std::sort(worldActions.begin(), worldActions.end(),
[](auto * act1, auto * act2) {
return (act1->text() < act2->text());
}
);
// Assign Ctrl+number shortcuts to the first 10 worlds:
int key = 1;
for (auto & act: worldActions)
{
act->setShortcut("Ctrl+" + QString::number(key));
key++;
if (key >= 10) {
break;
}
}
}
MapView *Minutor::getMapview() const
{
return mapview;
}
void Minutor::loadWorld(QDir path) {
// cleanup current state (just in case)
closeWorld();
currentWorld = path;
WorldInfo & wi(WorldInfo::Instance());
wi.parseWorldFolder(path);
wi.parseWorldInfo();
updateDatapackActions();
// add level name to window title
setWindowTitle(qApp->applicationName() + " - " + wi.getLevelName());
// Jump to: world spawn
m_ui.action_JumpSpawn->setData(locations.count());
locations.append(Location(wi.getSpawnX(), wi.getSpawnZ() ));
// Jump to: known players
if (path.cd("playerdata") || path.cd("players")) {
QDirIterator it(path.absolutePath(), {"*.dat"}, QDir::Files);
bool hasPlayers = false;
while (it.hasNext()) {
it.next();
if (it.fileInfo().isFile()) {
hasPlayers = true;
NBT player(it.filePath());
// player name from file name (old)
QString playerUUID = it.fileInfo().completeBaseName();
QString playerName = playerUUID;
QIcon playerIcon;
if (path.dirName() == "playerdata") {
// player name via UUID
QRegExp id("[0-9a-z]{8,8}\\-[0-9a-z]{4,4}\\-[0-9a-z]{4,4}"
"\\-[0-9a-z]{4,4}\\-[0-9a-z]{12,12}");
if (id.exactMatch(playerUUID)) {
QSettings settings;
// when present, remove old style cache
if (settings.contains("PlayerCache/"+playerUUID)) {
settings.remove("PlayerCache/"+playerUUID);
}
// check cache for player name
if (settings.contains("PlayerCache/"+playerUUID+"/name")) {
playerName = settings.value("PlayerCache/"+playerUUID+"/name", playerUUID).toString();
} else if (playerUUID[14]=='4') {
// only version 4 UUIDs can be resolved at Mojang API
// trigger HTTPS request to get player name
QString url = playerUUID;
url = "https://sessionserver.mojang.com/session/minecraft/profile/" + url.remove('-');
QNetworkRequest request;
request.setUrl(QUrl(url));
request.setRawHeader("User-Agent", "Minutor");
pendingNetworkAccess[this->qnam.get(request)] = playerUUID;
}
// check cache for player texture
if (settings.contains("PlayerCache/"+playerUUID+"/texture")) {
QString data64 = settings.value("PlayerCache/"+playerUUID+"/texture", playerUUID).toString();
QByteArray data = QByteArray::fromBase64(data64.toUtf8());
QImage head(reinterpret_cast<unsigned char *>(data.data()), 8, 8, QImage::Format_ARGB32);
playerIcon = QIcon(QPixmap::fromImage(head).scaled(16,16));
}
} else continue;
}
// current position of player
auto pos = player.at("Pos");
double posX = pos->at(0)->toDouble();
double posZ = pos->at(2)->toDouble();
// current dimension
QString dimension;
auto dim = player.at("Dimension");
if (dynamic_cast<const Tag_Int*>(dim)) {
// in very old versions this was an Tag_Int
switch (dim->toInt()) {
case -1: dimension = "minecraft:the_nether"; break;
case +1: dimension = "minecraft:the_end"; break;
default: dimension = "minecraft:overworld";
}
} if (dynamic_cast<const Tag_String*>(dim)) {
// now dimension is given as string
dimension = dim->toString();
}
QAction *p = new QAction(this);
p->setText(playerName);
p->setIcon(playerIcon);
p->setData(locations.count());
locations.append(Location(posX, posZ, dimension));
connect(p, SIGNAL(triggered()),
this, SLOT(jumpToLocation()));
playerActions.append(p);
// spawn location (bed)
if (player.has("SpawnX")) {
dimension = "minecraft:overworld";
if (player.has("SpawnDimension"))
dimension = player.at("SpawnDimension")->toString();
p = new QAction(this);
p->setText(playerName+"'s Bed");
p->setIcon(playerIcon);
p->setData(locations.count());
locations.append(Location(player.at("SpawnX")->toDouble(),
player.at("SpawnZ")->toDouble(),
dimension));
connect(p, SIGNAL(triggered()),
this, SLOT(jumpToLocation()));
playerActions.append(p);
}
}
}
m_ui.menu_JumpPlayer->addActions(playerActions);
m_ui.menu_JumpPlayer->setEnabled(hasPlayers);
path.cdUp();
}
// create Dimensions menu
WorldInfo::Instance().getDimensionsInWorld(path, m_ui.menu_Dimension, this);
// finalize
emit worldLoaded(true);
mapview->setLocation(locations.first().x, locations.first().z);
toggleFlags();
toggleOverlays();
}
void Minutor::updatePlayerCache(QNetworkReply * reply) {
auto response = reply->readAll();
if (response.length() == 0) {
reply->deleteLater();
return;
}
// we got a response
// response to initial profil query
if (reply->request().url().toString().contains("sessionserver.mojang.com")) {
QJsonDocument json = QJsonDocument::fromJson(response);
if (!json.isEmpty() && json.object().contains("name")) {
QString playerName = json.object().value("name").toString();
// reconstruct player UUID
QString playerUUID = pendingNetworkAccess[reply];
// store in player cache
QSettings settings;
settings.setValue("PlayerCache/"+playerUUID+"/name", playerName);
// get URL to skin texture
QString value = json.object().value("properties").toArray()[0].toObject().value("value").toString();
QJsonDocument vjson = QJsonDocument::fromJson(QByteArray::fromBase64(value.toUtf8()));
if (!vjson.isEmpty()) {
QString url = vjson.object()["textures"].toObject()["SKIN"].toObject()["url"].toString();
if (!url.isEmpty()) {
QNetworkRequest request;
request.setUrl(QUrl(url));
request.setRawHeader("User-Agent", "Minutor");
pendingNetworkAccess[this->qnam.get(request)] = playerUUID;
}
}
}
pendingNetworkAccess.remove(reply);
}
// response with skin texture
if (reply->request().url().toString().contains("textures.minecraft.net")) {
QImage skin;
skin.loadFromData(response);
QImage head(skin.copy(QRect(8,8,8,8)));
// reconstruct player UUID
QString playerUUID = pendingNetworkAccess[reply];
// store in player cache
QSettings settings;
QString data = QByteArray(reinterpret_cast<char*>(head.bits()), 4*8*8).toBase64();
settings.setValue("PlayerCache/"+playerUUID+"/texture", data);
pendingNetworkAccess.remove(reply);
}
reply->deleteLater();
}
void Minutor::rescanWorlds() {
worldActions.clear();
getWorldList();
m_ui.menu_OpenWorld->clear();
m_ui.menu_OpenWorld->addActions(worldActions);
m_ui.menu_OpenWorld->setEnabled(worldActions.count() != 0);
// we don't care about the auto-update toggle, since that only happens
// on startup anyway.
}
QMenu* Minutor::addOverlayItemMenu(QString path) {
// for vanilla the path is empty (minecraft:)
if ((path == "") || (path == "minecraft"))
return m_ui.menu_Overlay;
// split type name to get nested menu levels
QList<QString> pathlist = path.split('.');
// generate a nested menu structure to match the path
QMenu* menu = m_ui.menu_Overlay;
for (auto &p: pathlist) {
QMenu* submenu = menu->findChild<QMenu*>(p);
if (!submenu) {
// create new sub-menu
QMenu * submenu = new QMenu("&" + p, menu);
submenu->setObjectName(p);
// insert at alphabetical correct position
QAction * insertBeforeAction = nullptr;
for (QAction * action: menu->actions()) {
if (action == separatorEntityOverlay) {
// insert at least before Entities
insertBeforeAction = separatorEntityOverlay;
break;
} else if (action->menu()) {
// sub-menu means nested structures from a mod -> place before
if (action->text() > submenu->title()) {