Use a blacklist for the server version check.

The whitelisting approach works well for the legacy builds, because the
version landscape doesn't change with those. However, the main build on
the master branch will not run on a server version other than 1.13, so
come 1.14, MobArena will be broken.

This commit fixes this problem by changing to a blacklisting approach
for the main build on the master branch. Checking for versions that the
build *can't* run on brings back MobArena's old resilience to version
changes on the Minecraft side of things.
This commit is contained in:
Andreas Troelsen
2019-04-16 12:38:19 +02:00
parent 93af93d5a0
commit 4f548e08a6
3 changed files with 88 additions and 13 deletions
@@ -2,35 +2,26 @@ package com.garbagemule.MobArena;
import org.bukkit.Server;
import java.util.Arrays;
import java.util.StringJoiner;
class ServerVersionCheck {
private static final String[] EXACTS = {"1.13"};
private static final String[] PREFIXES = {"1.13."};
private static final String[] EXACTS = {"1.8", "1.9", "1.10", "1.11", "1.12"};
private static final String[] PREFIXES = {"1.8.", "1.9.", "1.10.", "1.11.", "1.12."};
static void check(Server server) {
String version = getMinecraftVersion(server);
for (String exact : EXACTS) {
if (version.equals(exact)) {
return;
fail(version);
}
}
for (String prefix : PREFIXES) {
if (version.startsWith(prefix)) {
return;
fail(version);
}
}
throw new IllegalStateException(new StringJoiner(" ")
.add("Incompatible server version!")
.add("This build only works on " + Arrays.toString(EXACTS) + ",")
.add("but this server is running " + version + ".")
.add("Perhaps you downloaded the wrong build?")
.toString()
);
}
private static String getMinecraftVersion(Server server) {
@@ -41,4 +32,13 @@ class ServerVersionCheck {
return version.substring(start, end);
}
private static void fail(String version) {
throw new IllegalStateException(new StringJoiner(" ")
.add("Incompatible server version!")
.add("This build does not work on " + version + ".")
.add("Perhaps you downloaded the wrong build?")
.toString()
);
}
}
@@ -0,0 +1,74 @@
package com.garbagemule.MobArena;
import org.bukkit.Server;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@SuppressWarnings("WeakerAccess")
@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class ServerVersionCheckTest {
public Server server;
@Rule
public ExpectedException exception = ExpectedException.none();
@Before
public void setup() {
server = mock(Server.class);
}
@Test
public void oneFourteen() {
win("git-Spigot-cafebae-dedbeef (MC: 1.14)");
}
@Test
public void oneThirteen() {
win("git-Spigot-f09662d-be557e6 (MC: 1.13.2)");
}
private void win(String version) {
when(server.getVersion()).thenReturn(version);
ServerVersionCheck.check(server);
}
@Test
public void oneTwelve() {
fail("git-Spigot-79a30d7-acbc348 (MC: 1.12.2)");
}
@Test
public void oneEleven() {
fail("git-Spigot-3fb9445-6e3cec8 (MC: 1.11.2)");
}
@Test
public void oneTen() {
fail("git-Spigot-de459a2-51263e9 (MC: 1.10.2)");
}
@Test
public void oneNine() {
fail("git-Spigot-c6871e2-0cd0397 (MC: 1.9.4)");
}
@Test
public void oneEight() {
fail("git-Spigot-21fe707-e1ebe52 (MC: 1.8.8)");
}
private void fail(String version) {
when(server.getVersion()).thenReturn(version);
exception.expect(IllegalStateException.class);
ServerVersionCheck.check(server);
}
}