forked from mrkite/minutor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminutor.cpp
763 lines (662 loc) · 23.4 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
/** 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 "./minutor.h"
#include "./mapview.h"
#include "./labelledslider.h"
#include "./nbt.h"
#include "./json.h"
#include "./definitionmanager.h"
#include "./entityidentifier.h"
#include "./settings.h"
#include "./dimensionidentifier.h"
#include "./worldsave.h"
#include "./properties.h"
#include "./generatedstructure.h"
#include "./village.h"
#include "./jumpto.h"
#include "./pngexport.h"
Minutor::Minutor() {
mapview = new MapView;
connect(mapview, SIGNAL(hoverTextChanged(QString)),
statusBar(), SLOT(showMessage(QString)));
connect(mapview, SIGNAL(showProperties(QVariant)),
this, SLOT(showProperties(QVariant)));
connect(mapview, SIGNAL(addOverlayItemType(QString, QColor)),
this, SLOT(addOverlayItemType(QString, QColor)));
dm = new DefinitionManager(this);
mapview->attach(dm);
connect(dm, SIGNAL(packsChanged()),
this, SLOT(updateDimensions()));
DimensionIdentifier *dimensions = &DimensionIdentifier::Instance();
connect(dimensions, SIGNAL(dimensionChanged(const DimensionInfo &)),
this, SLOT(viewDimension(const DimensionInfo &)));
settings = new Settings(this);
connect(settings, SIGNAL(settingsUpdated()),
this, SLOT(rescanWorlds()));
jumpTo = new JumpTo(this);
if (settings->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();
}
createActions();
createMenus();
createStatusBar();
QBoxLayout *mainLayout;
if (settings->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(int)),
depth, SLOT(changeValue(int)));
connect(mapview, SIGNAL(demandDepthValue(int)),
depth, SLOT(setValue(int)));
connect(this, SIGNAL(worldLoaded(bool)),
mapview, SLOT(setEnabled(bool)));
connect(this, SIGNAL(worldLoaded(bool)),
depth, SLOT(setEnabled(bool)));
QWidget *central = new QWidget;
central->setLayout(mainLayout);
setCentralWidget(central);
layout()->setContentsMargins(0, 0, 0, 0);
setWindowTitle(qApp->applicationName());
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::findBounds(mapview->getWorldPath(),
&w_top, &w_left, &w_bottom, &w_right);
PngExport pngoptions;
pngoptions.setBounds(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 menu
locations.clear();
for (int i = 0; i < players.size(); i++) {
jumpMenu->removeAction(players[i]);
delete players[i];
}
players.clear();
jumpMenu->setEnabled(false);
// clear dimensions menu
DimensionIdentifier::Instance().removeDimensions(dimMenu);
// clear overlays
mapview->clearOverlayItems();
// clear other stuff
currentWorld = QString();
emit worldLoaded(false);
}
void Minutor::jumpToLocation() {
QAction *action = qobject_cast<QAction*>(sender());
if (action) {
Location loc = locations[action->data().toInt()];
mapview->setLocation(loc.x, loc.z);
}
}
void Minutor::jumpToXZ(int blockX, int blockZ) {
mapview->setLocation(blockX, blockZ);
}
void Minutor::setViewLighting(bool value) {
lightingAct->setChecked(value);
toggleFlags();
}
void Minutor::setViewMobspawning(bool value) {
mobSpawnAct->setChecked(value);
toggleFlags();
}
void Minutor::setViewCavemode(bool value) {
caveModeAct->setChecked(value);
toggleFlags();
}
void Minutor::setViewDepthshading(bool value) {
depthShadingAct->setChecked(value);
toggleFlags();
}
void Minutor::setViewBiomeColors(bool value) {
biomeColorsAct->setChecked(value);
toggleFlags();
}
void Minutor::setDepth(int value) {
depth->setValue(value);
}
void Minutor::toggleFlags() {
int flags = 0;
if (lightingAct->isChecked()) flags |= MapView::flgLighting;
if (mobSpawnAct->isChecked()) flags |= MapView::flgMobSpawn;
if (caveModeAct->isChecked()) flags |= MapView::flgCaveMode;
if (depthShadingAct->isChecked()) flags |= MapView::flgDepthShading;
if (biomeColorsAct->isChecked()) flags |= MapView::flgBiomeColors;
mapview->setFlags(flags);
QSet<QString> overlayTypes;
for (auto action : structureActions) {
if (action->isChecked()) {
overlayTypes.insert(action->data().toMap()["type"].toString());
}
}
for (auto action : entityActions) {
if (action->isChecked()) {
overlayTypes.insert(action->data().toString());
}
}
mapview->setVisibleOverlayItemTypes(overlayTypes);
mapview->redraw();
}
void Minutor::viewDimension(const DimensionInfo &dim) {
for (auto action : structureActions) {
QString dimension = action->data().toMap()["dimension"].toString();
if (dimension.isEmpty() ||
!dimension.compare(dim.name, Qt::CaseInsensitive)) {
action->setVisible(true);
} else {
action->setVisible(false);
}
}
mapview->setDimension(dim.path, dim.scale);
}
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(2019)
.arg(qApp->organizationName()));
}
void Minutor::updateDimensions() {
DimensionIdentifier::Instance().getDimensions(currentWorld, dimMenu, this);
}
void Minutor::createActions() {
getWorldList();
// [File]
openAct = new QAction(tr("&Open..."), this);
openAct->setShortcut(tr("Ctrl+O"));
openAct->setStatusTip(tr("Open a world"));
connect(openAct, SIGNAL(triggered()),
this, SLOT(open()));
reloadAct = new QAction(tr("&Reload"), this);
reloadAct->setShortcut(tr("F5"));
reloadAct->setStatusTip(tr("Reload current world"));
connect(reloadAct, SIGNAL(triggered()),
this, SLOT(reload()));
connect(this, SIGNAL(worldLoaded(bool)),
reloadAct, SLOT(setEnabled(bool)));
saveAct = new QAction(tr("&Save PNG..."), this);
saveAct->setShortcut(tr("Ctrl+S"));
saveAct->setStatusTip(tr("Save as PNG"));
connect(saveAct, SIGNAL(triggered()),
this, SLOT(save()));
connect(this, SIGNAL(worldLoaded(bool)),
saveAct, SLOT(setEnabled(bool)));
exitAct = new QAction(tr("E&xit"), this);
exitAct->setShortcut(tr("Ctrl+Q"));
exitAct->setStatusTip(tr("Exit %1").arg(qApp->applicationName()));
connect(exitAct, SIGNAL(triggered()),
this, SLOT(close()));
// [View]
jumpSpawnAct = new QAction(tr("Jump to &Spawn"), this);
jumpSpawnAct->setShortcut(tr("F1"));
jumpSpawnAct->setStatusTip(tr("Jump to world spawn"));
connect(jumpSpawnAct, SIGNAL(triggered()),
this, SLOT(jumpToLocation()));
connect(this, SIGNAL(worldLoaded(bool)),
jumpSpawnAct, SLOT(setEnabled(bool)));
lightingAct = new QAction(tr("&Lighting"), this);
lightingAct->setCheckable(true);
lightingAct->setShortcut(tr("Ctrl+L"));
lightingAct->setStatusTip(tr("Toggle lighting on/off"));
connect(lightingAct, SIGNAL(triggered()),
this, SLOT(toggleFlags()));
depthShadingAct = new QAction(tr("&Depth shading"), this);
depthShadingAct->setCheckable(true);
depthShadingAct->setShortcut(tr("Ctrl+D"));
depthShadingAct->setStatusTip(tr("Toggle shading based on relative depth"));
connect(depthShadingAct, SIGNAL(triggered()),
this, SLOT(toggleFlags()));
mobSpawnAct = new QAction(tr("&Mob spawning"), this);
mobSpawnAct->setCheckable(true);
mobSpawnAct->setShortcut(tr("Ctrl+M"));
mobSpawnAct->setStatusTip(tr("Toggle show mob spawning on/off"));
connect(mobSpawnAct, SIGNAL(triggered()),
this, SLOT(toggleFlags()));
biomeColorsAct = new QAction(tr("&Biome Colors"), this);
biomeColorsAct->setCheckable(true);
biomeColorsAct->setShortcut(tr("Ctrl+B"));
biomeColorsAct->setStatusTip(tr("Toggle draw biome colors or block colors"));
connect(biomeColorsAct, SIGNAL(triggered()),
this, SLOT(toggleFlags()));
caveModeAct = new QAction(tr("&Cave Mode"), this);
caveModeAct->setCheckable(true);
caveModeAct->setShortcut(tr("Ctrl+C"));
caveModeAct->setStatusTip(tr("Toggle cave mode on/off"));
connect(caveModeAct, SIGNAL(triggered()),
this, SLOT(toggleFlags()));
jumpToAct = new QAction(tr("&Jump To"), this);
jumpToAct->setShortcut(tr("Ctrl+G"));
jumpToAct->setStatusTip(tr("Jump to a location"));
connect(jumpToAct, SIGNAL(triggered()),
jumpTo, SLOT(show()));
connect(this, SIGNAL(worldLoaded(bool)),
jumpToAct, SLOT(setEnabled(bool)));
// [View->Entity Overlay]
manageDefsAct = new QAction(tr("Manage &Definitions..."), this);
manageDefsAct->setStatusTip(tr("Manage block and biome definitions"));
connect(manageDefsAct, SIGNAL(triggered()),
dm, SLOT(show()));
refreshAct = new QAction(tr("Refresh"), this);
refreshAct->setShortcut(tr("F2"));
refreshAct->setStatusTip(tr("Reloads all chunks, "
"but keeps the same position / dimension"));
connect(refreshAct, SIGNAL(triggered()),
mapview, SLOT(clearCache()));
// [Help]
aboutAct = new QAction(tr("&About"), this);
aboutAct->setStatusTip(tr("About %1").arg(qApp->applicationName()));
connect(aboutAct, SIGNAL(triggered()),
this, SLOT(about()));
settingsAct = new QAction(tr("Settings..."), this);
settingsAct->setStatusTip(tr("Change %1 Settings")
.arg(qApp->applicationName()));
connect(settingsAct, SIGNAL(triggered()),
settings, SLOT(show()));
updatesAct = new QAction(tr("Check for updates..."), this);
updatesAct->setStatusTip(tr("Check for updated packs"));
connect(updatesAct, SIGNAL(triggered()),
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=0; 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::populateEntityOverlayMenu() {
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);
entityActions.push_back(new QAction(pixmap, actionName, this));
entityActions.last()->setShortcut(sequence);
entityActions.last()->setStatusTip(tr("Toggle viewing of %1")
.arg(category));
entityActions.last()->setEnabled(true);
entityActions.last()->setData("Entity."+category);
entityActions.last()->setCheckable(true);
entityOverlayMenu->addAction(entityActions.last());
connect(entityActions.last(), SIGNAL(triggered()),
this, SLOT(toggleFlags()));
}
}
void Minutor::createMenus() {
// [File]
fileMenu = menuBar()->addMenu(tr("&File"));
worldMenu = fileMenu->addMenu(tr("&Open World"));
worldMenu->addActions(worlds);
if (worlds.size() == 0) // no worlds found
worldMenu->setEnabled(false);
fileMenu->addAction(openAct);
fileMenu->addAction(reloadAct);
fileMenu->addSeparator();
fileMenu->addAction(saveAct);
fileMenu->addSeparator();
fileMenu->addAction(exitAct);
// [View]
viewMenu = menuBar()->addMenu(tr("&View"));
viewMenu->addAction(jumpSpawnAct);
viewMenu->addAction(jumpToAct);
jumpMenu = viewMenu->addMenu(tr("&Jump to Player"));
jumpMenu->setEnabled(false);
dimMenu = viewMenu->addMenu(tr("&Dimension"));
dimMenu->setEnabled(false);
viewMenu->addSeparator();
viewMenu->addAction(lightingAct);
viewMenu->addAction(mobSpawnAct);
viewMenu->addAction(caveModeAct);
viewMenu->addAction(depthShadingAct);
viewMenu->addAction(biomeColorsAct);
// [View->Overlay]
structureOverlayMenu = viewMenu->addMenu(tr("&Structure Overlay"));
entityOverlayMenu = viewMenu->addMenu(tr("&Entity Overlay"));
populateEntityOverlayMenu();
viewMenu->addSeparator();
viewMenu->addAction(refreshAct);
viewMenu->addSeparator();
viewMenu->addAction(manageDefsAct);
menuBar()->addSeparator();
// [Help]
helpMenu = menuBar()->addMenu(tr("&Help"));
helpMenu->addAction(aboutAct);
helpMenu->addSeparator();
helpMenu->addAction(settingsAct);
helpMenu->addAction(updatesAct);
}
void Minutor::createStatusBar() {
statusBar()->showMessage(tr("Ready"));
}
QString Minutor::getWorldName(QDir path) {
if (!path.exists("level.dat")) // no level.dat? no world
return QString();
NBT level(path.filePath("level.dat"));
return level.at("Data")->at("LevelName")->toString();
}
void Minutor::getWorldList() {
QDir mc(settings->mcpath);
if (!mc.cd("saves"))
return;
QDirIterator it(mc);
int key = 1;
while (it.hasNext()) {
it.next();
if (it.fileInfo().isDir()) {
QString name = getWorldName(it.filePath());
if (!name.isNull()) {
QAction *w = new QAction(this);
w->setText(name);
w->setData(it.filePath());
if (key < 10) {
w->setShortcut("Ctrl+"+QString::number(key));
key++;
}
connect(w, SIGNAL(triggered()),
this, SLOT(openWorld()));
worlds.append(w);
}
}
}
}
MapView *Minutor::getMapview() const
{
return mapview;
}
void Minutor::loadWorld(QDir path) {
// cleanup current state (just in case)
closeWorld();
currentWorld = path;
NBT level(path.filePath("level.dat"));
auto data = level.at("Data");
// add level name to window title
setWindowTitle(qApp->applicationName() + " - " +
data->at("LevelName")->toString());
// save world spawn
jumpSpawnAct->setData(locations.count());
locations.append(Location(data->at("SpawnX")->toDouble(),
data->at("SpawnZ")->toDouble()));
// show saved players
if (path.cd("playerdata") || path.cd("players")) {
QDirIterator it(path);
bool hasPlayers = false;
while (it.hasNext()) {
it.next();
if (it.fileInfo().isFile()) {
hasPlayers = true;
NBT player(it.filePath());
auto pos = player.at("Pos");
double posX = pos->at(0)->toDouble();
double posZ = pos->at(2)->toDouble();
auto dim = player.at("Dimension");
if (dim && (dim->toInt() == -1)) {
posX *= 8;
posZ *= 8;
}
QString playerName = it.fileInfo().completeBaseName();
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(playerName)) {
playerName = QString("Player %1").arg(players.length());
}
QAction *p = new QAction(this);
p->setText(playerName);
p->setData(locations.count());
locations.append(Location(posX, posZ));
connect(p, SIGNAL(triggered()),
this, SLOT(jumpToLocation()));
players.append(p);
if (player.has("SpawnX")) { // player has a bed
p = new QAction(this);
p->setText(playerName+"'s Bed");
p->setData(locations.count());
locations.append(Location(player.at("SpawnX")->toDouble(),
player.at("SpawnZ")->toDouble()));
connect(p, SIGNAL(triggered()),
this, SLOT(jumpToLocation()));
players.append(p);
}
}
}
jumpMenu->addActions(players);
jumpMenu->setEnabled(hasPlayers);
path.cdUp();
}
if (path.cd("data")) {
loadStructures(path);
path.cdUp();
}
// show dimensions
DimensionIdentifier::Instance().getDimensions(path, dimMenu, this);
emit worldLoaded(true);
mapview->setLocation(locations.first().x, locations.first().z);
toggleFlags();
}
void Minutor::rescanWorlds() {
worlds.clear();
getWorldList();
worldMenu->clear();
worldMenu->addActions(worlds);
worldMenu->setEnabled(worlds.count() != 0);
// we don't care about the auto-update toggle, since that only happens
// on startup anyway.
}
void Minutor::addOverlayItemType(QString type, QColor color,
QString dimension) {
if (!overlayItemTypes.contains(type)) {
overlayItemTypes.insert(type);
QList<QString> path = type.split('.');
QList<QString>::const_iterator pathIt, nextIt, endPathIt = path.end();
nextIt = path.begin();
nextIt++; // skip first part
pathIt = nextIt++;
QMenu* cur = structureOverlayMenu;
// generate a nested menu structure to match the path
while (nextIt != endPathIt) {
QList<QMenu*> results =
cur->findChildren<QMenu*>(*pathIt, Qt::FindDirectChildrenOnly);
if (results.empty()) {
cur = cur->addMenu("&" + *pathIt);
cur->setObjectName(*pathIt);
} else {
cur = results.front();
}
pathIt = ++nextIt;
}
// generate a unique keyboard shortcut
QString actionName = path.last();
QKeySequence sequence = generateUniqueKeyboardShortcut(&actionName);
QPixmap pixmap(16, 16);
QColor solidColor(color);
solidColor.setAlpha(255);
pixmap.fill(solidColor);
QMap<QString, QVariant> entityData;
entityData["type"] = type;
entityData["dimension"] = dimension;
structureActions.push_back(new QAction(pixmap, actionName, this));
structureActions.last()->setShortcut(sequence);
structureActions.last()->setStatusTip(tr("Toggle viewing of %1")
.arg(type));
structureActions.last()->setEnabled(true);
structureActions.last()->setData(entityData);
structureActions.last()->setCheckable(true);
cur->addAction(structureActions.last());
connect(structureActions.last(), SIGNAL(triggered()),
this, SLOT(toggleFlags()));
}
}
void Minutor::addOverlayItem(QSharedPointer<OverlayItem> item) {
// create menu entries (if necessary)
addOverlayItemType(item->type(), item->color(), item->dimension());
// const OverlayItem::Point& p = item->midpoint();
// overlayItems[item->type()].insertMulti(QPair<int, int>(p.x, p.z), item);
mapview->addOverlayItem(item);
}
void Minutor::showProperties(QVariant props) {
if (!props.isNull()) {
propView->DisplayProperties(props);
propView->show();
}
}
void Minutor::loadStructures(const QDir &dataPath) {
// attempt to parse all of the files in the data directory, looking for
// generated structures
for (auto &fileName : dataPath.entryList(QStringList() << "*.dat")) {
NBT file(dataPath.filePath(fileName));
auto data = file.at("data");
auto items = GeneratedStructure::tryParseDatFile(data);
for (auto &item : items) {
addOverlayItem(item);
}
if (items.isEmpty()) {
// try parsing it as a villages.dat file
int underidx = fileName.lastIndexOf('_');
int dotidx = fileName.lastIndexOf('.');
QString dimension = "overworld";
if (underidx > 0) {
dimension = fileName.mid(underidx + 1, dotidx - underidx - 1);
}
items = Village::tryParseDatFile(data, dimension);
for (auto &item : items) {
addOverlayItem(item);
}
}
}
}