Skip to content

Commit e115af3

Browse files
author
Gregory
authored
Update to v0.1.1 (#2)
2 parents e510071 + e2a877b commit e115af3

File tree

11 files changed

+538
-14
lines changed

11 files changed

+538
-14
lines changed

build.gradle.kts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ plugins {
1010
val jvm = JavaVersion.VERSION_11
1111

1212
group = "me.gamercoder215.calcgames"
13-
version = "0.1.0"
13+
version = "0.1.1"
1414

1515
java {
1616
sourceCompatibility = jvm
@@ -31,9 +31,6 @@ dependencies {
3131
}
3232

3333
tasks {
34-
assemble {
35-
dependsOn("javadocJar", "sourcesJar")
36-
}
3734

3835
compileJava {
3936
options.encoding = "UTF-8"
@@ -79,6 +76,10 @@ tasks {
7976
}
8077
}
8178

79+
jar {
80+
dependsOn("javadocJar", "sourcesJar")
81+
}
82+
8283
register("sourcesJar", Jar::class) {
8384
dependsOn(classes)
8485

src/main/java/me/gamercoder215/calcgames/levelz/Block.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,26 @@ public Map<String, Object> getProperties() {
4343
return Map.copyOf(properties);
4444
}
4545

46+
/**
47+
* Creates a new Block with the given name and these properties.
48+
* @param name Block Name
49+
* @return New Block
50+
*/
51+
@NotNull
52+
public Block withName(@NotNull String name) {
53+
return new Block(name, properties);
54+
}
55+
56+
/**
57+
* Creates a new Block with this name and the given properties.
58+
* @param properties Block Properties
59+
* @return New Block
60+
*/
61+
@NotNull
62+
public Block withProperties(@NotNull Map<String, Object> properties) {
63+
return new Block(name, properties);
64+
}
65+
4666
@Override
4767
public boolean equals(Object o) {
4868
if (this == o) return true;

src/main/java/me/gamercoder215/calcgames/levelz/Level.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,7 @@ protected Level(@NotNull Map<String, String> headers) {
2929
this.headers.putAll(headers);
3030
}
3131

32-
/**
33-
* Gets an immutable copy of the raw headers for this level.
34-
* @return Level Headers
35-
*/
36-
@NotNull
37-
@Unmodifiable
38-
public final Map<String, String> getHeaders() {
39-
return Map.copyOf(headers);
40-
}
32+
// Implementation
4133

4234
/**
4335
* Gets the spawn point for this level.
@@ -62,6 +54,18 @@ public final Map<String, String> getHeaders() {
6254
@Unmodifiable
6355
public abstract Set<LevelObject> getBlocks();
6456

57+
// Default
58+
59+
/**
60+
* Gets an immutable copy of the raw headers for this level.
61+
* @return Level Headers
62+
*/
63+
@NotNull
64+
@Unmodifiable
65+
public final Map<String, String> getHeaders() {
66+
return Map.copyOf(headers);
67+
}
68+
6569
@Override
6670
public Level clone() {
6771
try {
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package me.gamercoder215.calcgames.levelz;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
import java.io.File;
6+
import java.io.IOException;
7+
import java.nio.file.Files;
8+
import java.nio.file.Path;
9+
import java.util.Map;
10+
11+
/**
12+
* Represents a class that exports a LevelZ Level to a file.
13+
*/
14+
public final class LevelExporter {
15+
16+
private final Level level;
17+
18+
/**
19+
* Whether to include headers in the export.
20+
*/
21+
public boolean includeHeaders = true;
22+
23+
/**
24+
* Whether to include data in the export.
25+
*/
26+
public boolean includeData = true;
27+
28+
/**
29+
* Whether to include a section separator in the export.
30+
*/
31+
public String fileExtension = "lvlz";
32+
33+
/**
34+
* The line separator to use in the export. Default is {@code \n}.
35+
*/
36+
public String lineSeparator = "\n";
37+
38+
private LevelExporter(Level level) {
39+
this.level = level;
40+
}
41+
42+
/**
43+
* Exports the Level to a file.
44+
* @param level Level to Export
45+
* @return Level Exporter
46+
*/
47+
@NotNull
48+
public static LevelExporter export(@NotNull Level level) {
49+
if (level == null) throw new IllegalArgumentException("Level cannot be null");
50+
51+
return new LevelExporter(level);
52+
}
53+
54+
/**
55+
* Exports the Level to a string.
56+
* @return Level String
57+
*/
58+
@NotNull
59+
public String writeToString() {
60+
StringBuilder builder = new StringBuilder();
61+
62+
if (includeHeaders) {
63+
for (Map.Entry<String, String> key : level.getHeaders().entrySet())
64+
builder.append("@").append(key.getKey()).append(" ").append(key.getValue()).append(lineSeparator);
65+
66+
builder.append("---").append(lineSeparator);
67+
}
68+
69+
if (includeData) {
70+
for (LevelObject block : level.getBlocks())
71+
builder.append(block.toString()).append(lineSeparator);
72+
}
73+
74+
builder.append("end");
75+
return builder.toString();
76+
}
77+
78+
/**
79+
* Writes to a file. This will create the file if it does not exist.
80+
* @param file File
81+
*/
82+
public void writeToFile(@NotNull File file) {
83+
if (file == null) throw new IllegalArgumentException("File cannot be null");
84+
85+
if (!file.exists())
86+
try {
87+
if (!file.createNewFile()) throw new IOException("Failed to create file");
88+
} catch (IOException e) {
89+
throw new RuntimeException(e);
90+
}
91+
92+
writeToPath(file.toPath());
93+
}
94+
95+
/**
96+
* Writes to a file path.
97+
* @param file File Path
98+
*/
99+
public void writeToPath(@NotNull Path file) {
100+
if (file == null) throw new IllegalArgumentException("Path cannot be null");
101+
102+
String data = writeToString();
103+
try {
104+
Files.write(file, data.getBytes());
105+
} catch (IOException e) {
106+
throw new RuntimeException(e);
107+
}
108+
}
109+
110+
111+
}

0 commit comments

Comments
 (0)