Skip to content

Commit e8e4f38

Browse files
committed
General work
1 parent 28960aa commit e8e4f38

File tree

4 files changed

+100
-1
lines changed

4 files changed

+100
-1
lines changed

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ dependencies {
2828
testRuntimeOnly "org.apache.logging.log4j:log4j-core:2.12.1"
2929
testRuntimeOnly "org.apache.logging.log4j:log4j-slf4j18-impl:2.12.1"
3030

31+
testImplementation "com.github.TheRandomLabs:TRLUtils-IO:master-SNAPSHOT"
32+
3133
testImplementation "org.junit.jupiter:junit-jupiter-api:5.6.0-M1"
3234
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.6.0-M1"
3335
testImplementation "org.assertj:assertj-core:3.14.0"

src/main/java/com/therandomlabs/curseapi/minecraft/modpack/CurseModpack.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package com.therandomlabs.curseapi.minecraft.modpack;
22

3+
import java.nio.file.Path;
34
import java.util.Collection;
45
import java.util.List;
56

7+
import com.therandomlabs.curseapi.CurseException;
68
import com.therandomlabs.curseapi.file.MinimalCurseFile;
79
import com.therandomlabs.curseapi.minecraft.MCVersion;
10+
import com.therandomlabs.curseapi.util.MoshiUtils;
811

912
/**
1013
* Represents a CurseForge Minecraft modpack.
@@ -101,4 +104,41 @@ public interface CurseModpack {
101104
* @return this {@link CurseModpack}.
102105
*/
103106
CurseModpack files(Collection<? extends MinimalCurseFile> files);
107+
108+
/**
109+
* Returns this modpack as a JSON string.
110+
*
111+
* @return this modpack as a JSON string.
112+
*/
113+
String toJSON();
114+
115+
/**
116+
* Writes this modpack as a JSON string to the specified {@link Path}.
117+
*
118+
* @param path a {@link Path}.
119+
* @throws CurseException if an error occurs.
120+
*/
121+
void toJSON(Path path) throws CurseException;
122+
123+
/**
124+
* Creates a {@link CurseModpack} instance from a JSON string.
125+
*
126+
* @param json a JSON string.
127+
* @return a {@link CurseModpack} instance.
128+
* @throws CurseException if an error occurs.
129+
*/
130+
static CurseModpack fromJSON(String json) throws CurseException {
131+
return MoshiUtils.fromJSON(json, DefaultCurseModpack.class);
132+
}
133+
134+
/**
135+
* Parses the specified JSON string to create a {@link CurseModpack} instance.
136+
*
137+
* @param json a JSON string.
138+
* @return a {@link CurseModpack} instance.
139+
* @throws CurseException if an error occurs.
140+
*/
141+
static CurseModpack fromJSON(Path json) throws CurseException {
142+
return MoshiUtils.fromJSON(json, DefaultCurseModpack.class);
143+
}
104144
}

src/main/java/com/therandomlabs/curseapi/minecraft/modpack/DefaultCurseModpack.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
package com.therandomlabs.curseapi.minecraft.modpack;
22

3+
import java.nio.file.Path;
34
import java.util.ArrayList;
45
import java.util.Collection;
56
import java.util.Collections;
67
import java.util.List;
78

89
import com.google.common.base.Preconditions;
10+
import com.therandomlabs.curseapi.CurseAPI;
11+
import com.therandomlabs.curseapi.CurseException;
912
import com.therandomlabs.curseapi.file.MinimalCurseFile;
1013
import com.therandomlabs.curseapi.minecraft.MCVersion;
1114
import com.therandomlabs.curseapi.minecraft.MCVersions;
15+
import com.therandomlabs.curseapi.util.MoshiUtils;
1216

1317
@SuppressWarnings("squid:S1068")
1418
final class DefaultCurseModpack implements CurseModpack {
@@ -22,6 +26,14 @@ private static final class ModLoaderInfo {
2226
List<ModLoaderInfo> modLoaders = Collections.singletonList(new ModLoaderInfo());
2327
}
2428

29+
private static final class FileInfo extends MinimalCurseFile.Immutable {
30+
boolean required = true;
31+
32+
FileInfo() {
33+
super(CurseAPI.MIN_PROJECT_ID, CurseAPI.MIN_FILE_ID);
34+
}
35+
}
36+
2537
private MinecraftInfo minecraft = new MinecraftInfo();
2638
private String manifestType = "minecraftModpack";
2739
private int manifestVersion = 1;
@@ -30,7 +42,7 @@ private static final class ModLoaderInfo {
3042
private String author = "";
3143
//This files field is used for Moshi so that it knows that files should be converted t o
3244
//MinimalCurseFile.Immutables.
33-
private List<MinimalCurseFile.Immutable> files = new ArrayList<>();
45+
private List<FileInfo> files = new ArrayList<>();
3446
//This files field is used for everything else and uses the MinimalCurseFile type.
3547
private transient List<MinimalCurseFile> actualFiles;
3648

@@ -121,4 +133,14 @@ public CurseModpack files(Collection<? extends MinimalCurseFile> files) {
121133
this.actualFiles.addAll(files);
122134
return this;
123135
}
136+
137+
@Override
138+
public String toJSON() {
139+
return MoshiUtils.toJSON(this, DefaultCurseModpack.class);
140+
}
141+
142+
@Override
143+
public void toJSON(Path path) throws CurseException {
144+
MoshiUtils.toJSON(this, DefaultCurseModpack.class, path);
145+
}
124146
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.therandomlabs.curseapi.minecraft.modpack;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.nio.file.Path;
6+
import java.util.Optional;
7+
8+
import com.therandomlabs.curseapi.CurseAPI;
9+
import com.therandomlabs.curseapi.minecraft.MCVersions;
10+
import com.therandomlabs.utils.io.ZipFile;
11+
import org.junit.jupiter.api.Test;
12+
import org.junit.jupiter.api.io.TempDir;
13+
14+
public class CurseModpackTest {
15+
@Test
16+
public void atm4ShouldBeValid(@TempDir Path tempDirectory) throws Exception {
17+
final Optional<Path> optionalPath =
18+
CurseAPI.downloadFileToDirectory(316059, 2839369, tempDirectory);
19+
assertThat(optionalPath).isPresent();
20+
21+
final Path path = optionalPath.get();
22+
assertThat(path).isRegularFile();
23+
24+
try (ZipFile zipFile = new ZipFile(path)) {
25+
final CurseModpack modpack = CurseModpack.fromJSON(zipFile.getEntry("manifest.json"));
26+
assertThat(modpack.mcVersion()).isEqualTo(MCVersions.V1_14_4);
27+
assertThat(modpack.forgeVersion()).isEqualTo("1.14.4-28.1.104");
28+
assertThat(modpack.name()).isEqualTo("All the Mods 4");
29+
assertThat(modpack.version()).isEqualTo("0.2.5");
30+
assertThat(modpack.author()).isEqualTo("ATMTeam");
31+
assertThat(modpack.files()).isNotEmpty();
32+
assertThat(modpack.toJSON()).isNotNull();
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)