Skip to content

Commit 1739654

Browse files
committed
General work
1 parent 026882e commit 1739654

File tree

8 files changed

+148
-30
lines changed

8 files changed

+148
-30
lines changed

build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ tasks.withType(SpotBugsTask) {
7676
}
7777
}
7878

79+
tasks.withType(JavaCompile) {
80+
options.encoding = "UTF-8"
81+
options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
82+
}
83+
7984
task sourcesJar(type: Jar, dependsOn: classes) {
8085
archiveClassifier.set("sources")
8186
from sourceSets.main.allSource

config/pmd.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@
100100
<rule ref="category/java/errorprone.xml/CloneMethodMustImplementCloneable"/>
101101
<rule ref="category/java/errorprone.xml/CloneMethodReturnTypeMustMatchClassName"/>
102102
<rule ref="category/java/errorprone.xml/CloseResource"/>
103-
<rule ref="category/java/errorprone.xml/CompareObjectsWithEquals"/>
104103
<rule ref="category/java/errorprone.xml/DoNotCallGarbageCollectionExplicitly"/>
105104
<rule ref="category/java/errorprone.xml/DoNotExtendJavaLangThrowable"/>
106105
<rule ref="category/java/errorprone.xml/DontUseFloatTypeForLoopIndices"/>

src/main/java/com/therandomlabs/curseapi/minecraft/CurseAPIMinecraft.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88
public final class CurseAPIMinecraft {
99
/**
10-
* The ID of Minecraft on CurseForge.
10+
* Minecraft's ID on CurseForge.
1111
*/
1212
public static final int MINECRAFT_ID = 432;
1313

src/main/java/com/therandomlabs/curseapi/minecraft/ForgeSVCMinecraftProvider.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import java.util.SortedSet;
55
import java.util.TreeSet;
66

7-
import com.google.common.collect.ImmutableSortedSet;
87
import com.therandomlabs.curseapi.CurseAPIProvider;
98
import com.therandomlabs.curseapi.CurseException;
109
import com.therandomlabs.curseapi.forgesvc.ForgeSVCProvider;
@@ -36,15 +35,19 @@ private ForgeSVCMinecraftProvider() {}
3635
*/
3736
@Override
3837
public SortedSet<? extends CurseGameVersion<?>> gameVersions(int id) {
39-
if (failedToRetrieveVersions && versions.isEmpty()) {
40-
//This initializes the MCVersion constants, which create local MCVersion instances
41-
//if failedToRetrieveVersions is true.
42-
MCVersions.getAll();
43-
}
44-
38+
MCVersions.initialize();
4539
return id == CurseAPIMinecraft.MINECRAFT_ID ? new TreeSet<>(versions) : null;
4640
}
4741

42+
/**
43+
* {@inheritDoc}
44+
*/
45+
@Override
46+
public CurseGameVersion<?> gameVersion(int gameID, String versionString) {
47+
MCVersions.initialize();
48+
return gameID == CurseAPIMinecraft.MINECRAFT_ID ? MCVersions.get(versionString) : null;
49+
}
50+
4851
@SuppressWarnings("PMD.ForLoopCanBeForeach")
4952
private static SortedSet<MCVersion> getVersions() {
5053
final Logger logger = LoggerFactory.getLogger(ForgeSVCMinecraftProvider.class);
@@ -55,12 +58,14 @@ private static SortedSet<MCVersion> getVersions() {
5558

5659
for (int i = 0; i < versions.size(); i++) {
5760
final MCVersion version = versions.get(i);
58-
version.setIndex(versions.size() - i - 1);
59-
//Initialize MCVersionGroup instance.
61+
//Initialize MCVersion#versionGroup so that the MCVersion adds itself to the
62+
//version group.
6063
version.versionGroup();
64+
//We multiply the index by 2 so that snapshots defined in MCVersions can fit in.
65+
version.setSortIndex((versions.size() - i - 1) * 2);
6166
}
6267

63-
return ImmutableSortedSet.copyOf(versions);
68+
return new TreeSet<>(versions);
6469
} catch (CurseException ex) {
6570
logger.error(
6671
"Failed to retrieve Minecraft versions; a local copy will be used instead", ex

src/main/java/com/therandomlabs/curseapi/minecraft/MCVersion.java

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.Set;
77
import java.util.TreeSet;
88

9+
import com.google.common.primitives.Ints;
910
import com.therandomlabs.curseapi.game.CurseGameVersion;
1011
import com.therandomlabs.curseapi.game.CurseGameVersionGroup;
1112

@@ -38,8 +39,14 @@ public Set<MCVersion> versions() {
3839
return new TreeSet<>(versions);
3940
}
4041

41-
static MCVersionGroup get(MCVersion version) {
42+
static CurseGameVersionGroup<MCVersion> get(MCVersion version) {
4243
final String[] versionElements = version.versionString().split("\\.");
44+
45+
//Modloaders don't have version groups.
46+
if (Ints.tryParse(versionElements[0]) == null) {
47+
return CurseGameVersionGroup.none(CurseAPIMinecraft.MINECRAFT_ID);
48+
}
49+
4350
final String versionString = versionElements[0] + "." + versionElements[1];
4451
final MCVersionGroup versionGroup =
4552
versionGroups.computeIfAbsent(versionString, MCVersionGroup::new);
@@ -48,13 +55,13 @@ static MCVersionGroup get(MCVersion version) {
4855
}
4956
}
5057

51-
private int index;
58+
private int sortIndex;
5259
private String versionString;
5360

54-
private transient MCVersionGroup versionGroup;
61+
private transient CurseGameVersionGroup<MCVersion> versionGroup;
5562

56-
MCVersion(int index, String versionString) {
57-
this.index = index;
63+
MCVersion(int sortIndex, String versionString) {
64+
this.sortIndex = sortIndex;
5865
this.versionString = versionString;
5966
}
6067

@@ -91,11 +98,16 @@ public String versionString() {
9198
*/
9299
@Override
93100
public int compareTo(MCVersion version) {
94-
return Integer.compare(index, version.index);
101+
return Integer.compare(sortIndex, version.sortIndex);
102+
}
103+
104+
//This method is called by MCVersions.
105+
int getSortIndex() {
106+
return sortIndex;
95107
}
96108

97-
//This method is called by ForgeSVCMinecraftProvider.
98-
void setIndex(int index) {
99-
this.index = index;
109+
//This method is called by ForgeSVCMinecraftProvider and MCVersions.
110+
void setSortIndex(int sortIndex) {
111+
this.sortIndex = sortIndex;
100112
}
101113
}

src/main/java/com/therandomlabs/curseapi/minecraft/MCVersionGroups.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,10 @@ public final class MCVersionGroups {
8282
*/
8383
public static final CurseGameVersionGroup<MCVersion> V1_14 = MCVersions.V1_14.versionGroup();
8484

85+
/**
86+
* Minecraft 1.15.
87+
*/
88+
public static final CurseGameVersionGroup<MCVersion> V1_15 = MCVersions.V1_15.versionGroup();
89+
8590
private MCVersionGroups() {}
8691
}

src/main/java/com/therandomlabs/curseapi/minecraft/MCVersions.java

Lines changed: 100 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
* by CurseForge.
1010
*/
1111
public final class MCVersions {
12+
/**
13+
* Represents an unknown version of Minecraft.
14+
*/
15+
public static final MCVersion UNKNOWN = new MCVersion(-1, "Unknown");
16+
1217
/**
1318
* Minecraft 1.0.
1419
*/
@@ -149,6 +154,11 @@ public final class MCVersions {
149154
*/
150155
public static final MCVersion V1_7_10 = initialize("1.7.10");
151156

157+
/**
158+
* Minecraft 1.8 snapshots.
159+
*/
160+
public static final MCVersion V1_8_SNAPSHOT = create("1.8", "1.8-Snapshot");
161+
152162
/**
153163
* Minecraft 1.8.
154164
*/
@@ -199,6 +209,11 @@ public final class MCVersions {
199209
*/
200210
public static final MCVersion V1_8_9 = initialize("1.8.9");
201211

212+
/**
213+
* Minecraft 1.9 snapshots.
214+
*/
215+
public static final MCVersion V1_9_SNAPSHOT = create("1.9", "1.9-Snapshot");
216+
202217
/**
203218
* Minecraft 1.9.
204219
*/
@@ -224,6 +239,11 @@ public final class MCVersions {
224239
*/
225240
public static final MCVersion V1_9_4 = initialize("1.9.4");
226241

242+
/**
243+
* Minecraft 1.10 snapshots.
244+
*/
245+
public static final MCVersion V1_10_SNAPSHOT = create("1.10", "1.10-Snapshot");
246+
227247
/**
228248
* Minecraft 1.10.
229249
*/
@@ -239,6 +259,11 @@ public final class MCVersions {
239259
*/
240260
public static final MCVersion V1_10_2 = initialize("1.10.2");
241261

262+
/**
263+
* Minecraft 1.11 snapshots.
264+
*/
265+
public static final MCVersion V1_11_SNAPSHOT = create("1.11", "1.11-Snapshot");
266+
242267
/**
243268
* Minecraft 1.11.
244269
*/
@@ -254,6 +279,11 @@ public final class MCVersions {
254279
*/
255280
public static final MCVersion V1_11_2 = initialize("1.11.2");
256281

282+
/**
283+
* Minecraft 1.12 snapshots.
284+
*/
285+
public static final MCVersion V1_12_SNAPSHOT = create("1.12", "1.12-Snapshot");
286+
257287
/**
258288
* Minecraft 1.12.
259289
*/
@@ -269,6 +299,11 @@ public final class MCVersions {
269299
*/
270300
public static final MCVersion V1_12_2 = initialize("1.12.2");
271301

302+
/**
303+
* Minecraft 1.13 snapshots.
304+
*/
305+
public static final MCVersion V1_13_SNAPSHOT = create("1.13", "1.13-Snapshot");
306+
272307
/**
273308
* Minecraft 1.13.
274309
*/
@@ -284,6 +319,11 @@ public final class MCVersions {
284319
*/
285320
public static final MCVersion V1_13_2 = initialize("1.13.2");
286321

322+
/**
323+
* Minecraft 1.14 snapshots.
324+
*/
325+
public static final MCVersion V1_14_SNAPSHOT = create("1.14", "1.14-Snapshot");
326+
287327
/**
288328
* Minecraft 1.14.
289329
*/
@@ -310,9 +350,34 @@ public final class MCVersions {
310350
public static final MCVersion V1_14_4 = initialize("1.14.4");
311351

312352
/**
313-
* Represents an unknown version of Minecraft.
353+
* Minecraft 1.15 snapshots.
314354
*/
315-
public static final MCVersion UNKNOWN = new MCVersion(-1, "Unknown");
355+
public static final MCVersion V1_15_SNAPSHOT = create("1.15", "1.15-Snapshot");
356+
357+
/**
358+
* Minecraft 1.15.
359+
*/
360+
public static final MCVersion V1_15 = initialize("1.15");
361+
362+
/**
363+
* Minecraft 1.15.1.
364+
*/
365+
public static final MCVersion V1_15_1 = initialize("1.15.1");
366+
367+
/**
368+
* The Fabric modloader.
369+
*/
370+
public static final MCVersion FABRIC = initialize("Fabric");
371+
372+
/**
373+
* The Minecraft Forge modloader.
374+
*/
375+
public static final MCVersion FORGE = initialize("Forge");
376+
377+
/**
378+
* The Rift modloader.
379+
*/
380+
public static final MCVersion RIFT = initialize("Rift");
316381

317382
private MCVersions() {}
318383

@@ -355,14 +420,13 @@ public static MCVersion get(String versionString) {
355420
return UNKNOWN;
356421
}
357422

423+
static void initialize() {
424+
//This is called by ForgeSVCMinecraftProvider.
425+
}
426+
358427
private static MCVersion initialize(String versionString) {
359428
if (ForgeSVCMinecraftProvider.failedToRetrieveVersions) {
360-
final MCVersion version =
361-
new MCVersion(ForgeSVCMinecraftProvider.versions.size(), versionString);
362-
ForgeSVCMinecraftProvider.versions.add(version);
363-
//Initialize MCVersionGroup instance.
364-
version.versionGroup();
365-
return version;
429+
return create(-1, versionString);
366430
}
367431

368432
for (MCVersion version : ForgeSVCMinecraftProvider.versions) {
@@ -371,6 +435,33 @@ private static MCVersion initialize(String versionString) {
371435
}
372436
}
373437

374-
throw new IllegalArgumentException("Invalid version string: " + versionString);
438+
//The Minecraft versions API often takes some time to acknowledge newer versions of
439+
//Minecraft.
440+
return create(-1, versionString);
441+
}
442+
443+
//Snapshots and modloaders are also not provided in the Minecraft versions API.
444+
private static MCVersion create(int index, String versionString) {
445+
if (index == -1) {
446+
index = ForgeSVCMinecraftProvider.versions.size() * 2;
447+
}
448+
449+
final MCVersion version = new MCVersion(index, versionString);
450+
//Initialize MCVersion#versionGroup so that the MCVersion adds itself to the version group.
451+
version.versionGroup();
452+
ForgeSVCMinecraftProvider.versions.add(version);
453+
return version;
454+
}
455+
456+
//This method is used to create snapshot versions.
457+
private static MCVersion create(String nextVersionString, String versionString) {
458+
if (ForgeSVCMinecraftProvider.failedToRetrieveVersions) {
459+
return create(-1, versionString);
460+
}
461+
462+
final MCVersion nextVersion = get(nextVersionString);
463+
//Since sort indexes are normally separated by 2,
464+
//there should be a gap that this version can fit in.
465+
return create(nextVersion == UNKNOWN ? -1 : nextVersion.getSortIndex() - 1, versionString);
375466
}
376467
}

src/test/java/com/therandomlabs/curseapi/minecraft/MCVersionsTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public void mcVersionsShouldNotBeEmpty() throws CurseException {
2222
@Test
2323
public void mcVersionComparisonsShouldBeCorrect() {
2424
assertThat(MCVersions.V1_14_4.newerThan(MCVersions.V1_14_3)).isTrue();
25+
assertThat(MCVersions.V1_8_SNAPSHOT.olderThan(MCVersions.V1_8)).isTrue();
2526
assertThat(MCVersions.V1_0.olderThan(MCVersions.V1_2_1)).isTrue();
2627
assertThat(MCVersions.V1_1.olderThan(MCVersions.V1_0)).isFalse();
2728
}

0 commit comments

Comments
 (0)