Skip to content

Commit 45aceca

Browse files
committed
Add Comments Support
1 parent cc24634 commit 45aceca

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

src/main/java/xyz/calcugames/levelz/Keywords.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,13 @@ public interface Keywords {
2525
*/
2626
String DEFAULT_COORDINATE_3D = "[0, 0, 0]";
2727

28+
/**
29+
* The beginning of a comment.
30+
*/
31+
char COMMENT_CHAR = '#';
32+
33+
/**
34+
* The beginning of a comment.
35+
*/
36+
String COMMENT = "#";
2837
}

src/main/java/xyz/calcugames/levelz/parser/InternalParser.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,15 +256,19 @@ static Level parse(String[] file, Random seed) {
256256

257257
Set<LevelObject> blocks = new HashSet<>();
258258
for (String line : split[1]) {
259+
if (line.startsWith(Keywords.COMMENT)) continue;
259260
if (line.equalsIgnoreCase(Keywords.END)) break;
260261

262+
int ci = line.indexOf(Keywords.COMMENT_CHAR);
263+
String line0 = line.substring(0, ci == -1 ? line.length() : ci).trim();
264+
261265
if (is2D) {
262-
Map<Block, Coordinate2D[]> blocks2D = read2DLine(line, seed);
266+
Map<Block, Coordinate2D[]> blocks2D = read2DLine(line0, seed);
263267
blocks2D.forEach((k, v) -> {
264268
for (Coordinate2D c : v) blocks.add(new LevelObject(k, c));
265269
});
266270
} else {
267-
Map<Block, Coordinate3D[]> blocks3D = read3DLine(line, seed);
271+
Map<Block, Coordinate3D[]> blocks3D = read3DLine(line0, seed);
268272
blocks3D.forEach((k, v) -> {
269273
for (Coordinate3D c : v) blocks.add(new LevelObject(k, c));
270274
});

src/test/java/xyz/calcugames/levelz/parser/TestLevelParser.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@ public void testParse() {
3737
Level3D l3 = (Level3D) p3.parse();
3838

3939
Assertions.assertEquals(l3.getSpawn(), new Coordinate3D(0, 0, 0));
40+
41+
InputStream f4 = TestLevelParser.class.getResourceAsStream("/examples/2D/volcano/4.lvlz");
42+
LevelParser p4 = builder.input(f4).build();
43+
Level2D l4 = (Level2D) p4.parse();
44+
45+
Assertions.assertEquals(l4.getScroll(), Scroll.NONE);
46+
Assertions.assertEquals(l4.getSpawn(), new Coordinate2D(5, 1));
47+
48+
InputStream f5 = TestLevelParser.class.getResourceAsStream("/examples/3D/grasslands/3.lvlz");
49+
LevelParser p5 = builder.input(f5).build();
50+
Level3D l5 = (Level3D) p5.parse();
51+
52+
Assertions.assertEquals(l5.getSpawn(), new Coordinate3D(0, 10, 0));
4053
}
4154

4255
}

0 commit comments

Comments
 (0)