Skip to content

Commit 571abb9

Browse files
authored
Merge pull request #4 from UhoMazino/mw-export
Added MapWriter export feature.
2 parents 538eb8e + 7c29d7e commit 571abb9

File tree

2 files changed

+101
-31
lines changed

2 files changed

+101
-31
lines changed

src/org/prank/Main.java

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
package org.prank;
22

3+
import javax.swing.*;
4+
5+
import java.io.PrintWriter;
6+
import java.io.StringWriter;
7+
38
import static org.prank.ConfigReader.readWorldGenConfig;
49

510
public class Main {
611
public static void main(String[] args) {
7-
readWorldGenConfig();
8-
new MainFrame().setVisible(true);
12+
try {
13+
readWorldGenConfig();
14+
new MainFrame();
15+
} catch (Exception e) {
16+
StringWriter stringWriter = new StringWriter();
17+
e.printStackTrace(new PrintWriter(stringWriter));
18+
JOptionPane.showMessageDialog(null, stringWriter.toString(), "Some shit happened", JOptionPane.ERROR_MESSAGE);
19+
e.printStackTrace();
20+
}
921
}
10-
}
22+
}

src/org/prank/MainFrame.java

+86-28
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
import javax.swing.*;
44
import java.awt.*;
55
import java.awt.event.ActionEvent;
6-
import java.awt.event.ActionListener;
76
import java.awt.event.WindowAdapter;
87
import java.awt.event.WindowEvent;
98
import java.io.File;
109
import java.io.FileWriter;
11-
import java.lang.reflect.Array;
12-
import java.util.*;
1310
import java.util.List;
11+
import java.util.*;
1412

1513
@SuppressWarnings({"WeakerAccess", "SameParameterValue"})
1614
public class MainFrame extends JFrame {
@@ -32,7 +30,7 @@ public void windowClosing(WindowEvent we) {
3230
}
3331
});
3432

35-
setSize(640, 480);
33+
setSize(660, 480);
3634
setResizable(false);
3735
setLayout(null);
3836

@@ -42,12 +40,13 @@ public void windowClosing(WindowEvent we) {
4240
setTextAreas();
4341
setComboBoxes();
4442

45-
setVisible(true);
46-
4743
tfSeed.setText("");
4844
tfSize.setText("10");
4945
tfOffsetX.setText("0");
5046
tfOffsetZ.setText("0");
47+
cbDim.setSelectedItem("overworld");
48+
49+
setVisible(true);
5150
}
5251

5352
public void generate(long wSeed, int chX, int chZ, String dim) {
@@ -145,24 +144,21 @@ private long getSeed() {
145144
return seed;
146145
}
147146

148-
private void export() {
147+
private void exportJM() {
149148
if (result.isEmpty())
150149
calculate();
151150
File waypoints = new File("waypoints");
152-
if (!waypoints.exists()) {
153-
//noinspection ResultOfMethodCallIgnored
154-
waypoints.mkdir();
155-
}
156-
result.forEach(this::writeWayPoint);
151+
if (!waypoints.exists()) waypoints.mkdir();
152+
result.forEach(this::writeJMWayPoint);
157153
}
158154

159-
private void writeWayPoint(Coord coord, String name) {
155+
private void writeJMWayPoint(Coord coord, String name) {
160156
// Translate chunk coords to block coords
161157
int x = coord.x * 16 + 8;
162158
int y = coord.y;
163159
int z = coord.z * 16 + 8;
164160

165-
String upName = name.substring(0, 1).toUpperCase() + name.substring(1, name.length());
161+
String upName = name.substring(0, 1).toUpperCase() + name.substring(1);
166162
String wpName = upName + "_" + x + "," + y + "," + z;
167163
String fileName = "waypoints/" + wpName + "." + getCurrentDimID() + ".json";
168164

@@ -172,21 +168,22 @@ private void writeWayPoint(Coord coord, String name) {
172168
z *= 8;
173169
}
174170

175-
int hash = name.hashCode();
171+
Color color = getColor(name);
176172
StringBuilder sb = new StringBuilder("{")
177173
.append("\"id\": \"").append(wpName).append("\", ")
178174
.append("\"name\": \"").append(upName).append("\", ")
179175
.append("\"icon\": \"waypoint-normal.png\", ")
180176
.append("\"x\": ").append(x).append(", ")
181177
.append("\"y\": ").append(y).append(", ")
182178
.append("\"z\": ").append(z).append(", ")
183-
.append("\"r\": ").append((hash) & 0xff).append(", ")
184-
.append("\"g\": ").append((hash >> 8) & 0xff).append(", ")
185-
.append("\"b\": ").append((hash >> 16) & 0xff).append(", ")
179+
.append("\"r\": ").append(color.red).append(", ")
180+
.append("\"g\": ").append(color.green).append(", ")
181+
.append("\"b\": ").append(color.blue).append(", ")
186182
.append("\"enable\": true, ")
187183
.append("\"type\": \"Normal\", ")
188184
.append("\"origin\": \"JourneyMap\", ")
189185
.append("\"dimensions\": [").append(getCurrentDimID()).append("]}");
186+
190187
try (FileWriter file = new FileWriter(fileName)) {
191188
file.write(sb.toString());
192189
file.flush();
@@ -195,8 +192,50 @@ private void writeWayPoint(Coord coord, String name) {
195192
}
196193
}
197194

195+
private void exportMW() {
196+
if (result.isEmpty())
197+
calculate();
198+
199+
StringBuilder fileBuilder = new StringBuilder()
200+
.append("# Configuration file\n\n")
201+
.append("markers {\n");
202+
203+
int id = 0;
204+
for (Coord key : result.keySet())
205+
fileBuilder.append(this.writeMwWayPoint(id++, key, result.get(key)));
206+
207+
fileBuilder
208+
.append("\tI:markerCount=").append(result.size()).append("\n")
209+
.append("\tS:visibleGroup=all\n}\n");
210+
// .append("world {\n\tI:dimensionList <\n\t\t").append(getCurrentDimID()).append("\n\t>\n}");
211+
212+
try (FileWriter file = new FileWriter("mapwriter.cfg", false)) {
213+
file.write(fileBuilder.toString());
214+
} catch (Exception e) {
215+
e.printStackTrace();
216+
}
217+
}
218+
219+
private String writeMwWayPoint(int id, Coord coord, String name) {
220+
// Translate chunk coords to block coords
221+
int x = coord.x * 16 + 8, y = coord.y, z = coord.z * 16 + 8;
222+
String upName = name.substring(0, 1).toUpperCase() + name.substring(1);
223+
if (getCurrentDimID() == -1) {
224+
x *= 8;
225+
z *= 8;
226+
}
227+
228+
Color color = getColor(name);
229+
return "\tS:marker" + id + "=" + upName + ":" +
230+
x + ":" + y + ":" + z + ":" + getCurrentDimID() + ":" +
231+
color.hex + ":" + upName + "\n";
232+
}
233+
198234
private int getCurrentDimID() {
199-
String dim = String.valueOf(cbDim.getSelectedItem()).toLowerCase();
235+
return getDimID(String.valueOf(cbDim.getSelectedItem()).toLowerCase());
236+
}
237+
238+
private int getDimID(String dim) {
200239
if (Dimensions.knownDimensions.containsKey(dim))
201240
return Dimensions.knownDimensions.get(dim);
202241
JOptionPane.showMessageDialog(this, "Ask somebody to add the dimension to code.", "Unknown dimension: " + dim, JOptionPane.WARNING_MESSAGE);
@@ -206,7 +245,8 @@ private int getCurrentDimID() {
206245

207246
private void setButtons() {
208247
setButton("Calculate", 10, 40, 95, 25, this::calculate);
209-
setButton("JM Export", 110, 40, 95, 25, this::export);
248+
setButton("JM Export", 110, 40, 95, 25, this::exportJM);
249+
setButton("MW Export", 210, 40, 105, 25, this::exportMW);
210250
JButton btnRefresh = setButton("↻", 300, 10, 25, 25, this::refresh);
211251
btnRefresh.setMargin(new Insets(0, 0, 0, 0));
212252
}
@@ -227,8 +267,8 @@ private JButton setButton(String name, int x, int y, int w, int h, Runnable acti
227267
private void setInputs() {
228268
tfSeed = setTextField(100, 10, 200, 25);
229269
tfSize = setTextField(380, 10, 50, 25);
230-
tfOffsetX = setTextField(300, 40, 50, 25);
231-
tfOffsetZ = setTextField(380, 40, 50, 25);
270+
tfOffsetX = setTextField(390, 40, 50, 25);
271+
tfOffsetZ = setTextField(470, 40, 50, 25);
232272
}
233273

234274
private JTextField setTextField(int x, int y, int w, int h) {
@@ -241,9 +281,9 @@ private JTextField setTextField(int x, int y, int w, int h) {
241281
private void setLabels() {
242282
setLabel("World seed:", 10, 10, 70, 25);
243283
setLabel("Size:", 340, 10, 50, 25);
244-
setLabel("Offset", 230, 40, 50, 25);
245-
setLabel("X:", 280, 40, 50, 25);
246-
setLabel("Z:", 360, 40, 50, 25);
284+
setLabel("Offset", 320, 40, 50, 25);
285+
setLabel("X:", 365, 40, 50, 25);
286+
setLabel("Z:", 450, 40, 50, 25);
247287
}
248288

249289
private void setLabel(String text, int x, int y, int w, int h) {
@@ -253,7 +293,7 @@ private void setLabel(String text, int x, int y, int w, int h) {
253293
}
254294

255295
private void setTextAreas() {
256-
taOutput = setTextArea(10, 70, 615, 375);
296+
taOutput = setTextArea(10, 70, 635, 375);
257297
taOutput.setFont(new Font("monospaced", Font.PLAIN, 12));
258298
}
259299

@@ -271,8 +311,7 @@ private void setComboBoxes() {
271311
Arrays.sort(dims);
272312
cbDim = setComboBox(440, 10, 100, 25, dims);
273313
cbDim.addActionListener(this::refreshOres);
274-
cbOre = setComboBox(440, 40, 100, 25);
275-
cbDim.setSelectedItem("overworld");
314+
cbOre = setComboBox(550, 10, 100, 25);
276315
}
277316

278317
@SafeVarargs
@@ -298,4 +337,23 @@ private void refreshOres(ActionEvent e) {
298337

299338
if (filteredOres.contains(oreName)) cbOre.setSelectedItem(oreName);
300339
}
340+
341+
private Color getColor(String name) {
342+
return new Color(name.hashCode());
343+
}
344+
345+
private static class Color {
346+
public int red;
347+
public int green;
348+
public int blue;
349+
350+
public String hex;
351+
352+
public Color(int hash) {
353+
red = (hash) & 0xff;
354+
green = (hash >> 8) & 0xff;
355+
blue = (hash >> 16) & 0xff;
356+
hex = Integer.toHexString(hash & 0xffffff);
357+
}
358+
}
301359
}

0 commit comments

Comments
 (0)