Skip to content

Commit

Permalink
Refactor MinecraftVersion to include a range of minor patches which a…
Browse files Browse the repository at this point in the history
… MinecraftVersion targets
  • Loading branch information
md5sha256 committed Aug 11, 2024
1 parent 8d49c16 commit 8489904
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public enum MinecraftVersion {
* This constant represents Minecraft (Java Edition) Version 1.20
* ("The Trails & Tales Update")
*/
MINECRAFT_1_20(20, "1.20.x"),
MINECRAFT_1_20(20, 0, 4,"1.20.x"),

/**
* This constant represents Minecraft (Java Edition) Version 1.20.5
Expand All @@ -71,6 +71,7 @@ public enum MinecraftVersion {
private final boolean virtual;
private final int majorVersion;
private final int minorVersion;
private final int maxMinorVersion;

/**
* This constructs a new {@link MinecraftVersion} with the given name.
Expand All @@ -86,6 +87,7 @@ public enum MinecraftVersion {
this.name = name;
this.majorVersion = majorVersion;
this.minorVersion = -1;
this.maxMinorVersion = -1;
this.virtual = false;
}

Expand All @@ -105,6 +107,29 @@ public enum MinecraftVersion {
this.name = name;
this.majorVersion = majorVersion;
this.minorVersion = minor;
this.maxMinorVersion = -1;
this.virtual = false;
}

/**
* This constructs a new {@link MinecraftVersion} with the given name.
* This constructor forces the {@link MinecraftVersion} to be real.
* It must be a real version of Minecraft.
*
* @param majorVersion
* The major (minor in semver, major in MC land) version of minecraft as an {@link Integer}
* @param minor
* The minor (patch in semver, minor in MC land) version of minecraft as an {@link Integer}
* @param maxMinorVersion
* The maximum minor (patch) version of minecraft this version represents
* @param name
* The display name of this {@link MinecraftVersion}
*/
MinecraftVersion(int majorVersion, int minor, int maxMinorVersion, @Nonnull String name) {
this.name = name;
this.majorVersion = majorVersion;
this.minorVersion = minor;
this.maxMinorVersion = maxMinorVersion;
this.virtual = false;
}

Expand All @@ -122,6 +147,7 @@ public enum MinecraftVersion {
this.name = name;
this.majorVersion = 0;
this.minorVersion = -1;
this.maxMinorVersion = -1;
this.virtual = virtual;
}

Expand Down Expand Up @@ -185,7 +211,8 @@ public boolean isMinecraftVersion(int minecraftVersion) {
public boolean isMinecraftVersion(int minecraftVersion, int patchVersion) {
return !isVirtual()
&& this.majorVersion == minecraftVersion
&& (this.minorVersion == -1 || this.minorVersion >= patchVersion);
&& (this.minorVersion == -1 || this.minorVersion <= patchVersion)
&& (this.maxMinorVersion == -1 || patchVersion <= this.maxMinorVersion);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ void testMatches() {
Assertions.assertFalse(MinecraftVersion.MINECRAFT_1_20_5.isMinecraftVersion(20, 4));
}

@Test
@DisplayName("Test if Minecraft versions match minor versions")
void testMatchesMinor() {
Assertions.assertTrue(MinecraftVersion.MINECRAFT_1_16.isMinecraftVersion(16, 1));
Assertions.assertTrue(MinecraftVersion.MINECRAFT_1_16.isMinecraftVersion(16, 2));

Assertions.assertTrue(MinecraftVersion.MINECRAFT_1_20.isMinecraftVersion(20, 4));
Assertions.assertTrue(MinecraftVersion.MINECRAFT_1_20_5.isMinecraftVersion(20, 6));

Assertions.assertFalse(MinecraftVersion.MINECRAFT_1_20.isMinecraftVersion(20, 5));
Assertions.assertFalse(MinecraftVersion.MINECRAFT_1_16.isMinecraftVersion(17, 1));
Assertions.assertFalse(MinecraftVersion.MINECRAFT_1_20_5.isMinecraftVersion(20, 4));
}

@Test
@DisplayName("Test if Minecraft versions are ordered correctly (#atLeast)")
void testAtLeast() {
Expand Down

0 comments on commit 8489904

Please sign in to comment.