Skip to content
This repository was archived by the owner on Jan 16, 2023. It is now read-only.

Commit b9f51b9

Browse files
committed
1.0.1 & Finished UpdateChecker
- Bumped version to 1.0.1 - Moved UpdateChecker::hasNewVersion into VersionUtilities - Fix a bug that would result in lower version being detected as a new version in hasNewVersion - Added a unit test for VersionUtilities - Removed uuidToName inside PlayerUtilities - Code cleanup
1 parent 3691d57 commit b9f51b9

File tree

8 files changed

+116
-89
lines changed

8 files changed

+116
-89
lines changed

build.gradle

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ apply plugin: 'com.github.johnrengelman.shadow'
2828
apply plugin: 'net.minecraftforge.gradle.forge'
2929
apply plugin: 'org.spongepowered.mixin'
3030

31-
version = '1.0.0'
31+
version = '1.0.1'
3232
group = 'de.timmi6790.mcmod'
3333
archivesBaseName = 'RandomMineplexMod'
3434

@@ -48,6 +48,15 @@ dependencies {
4848
annotationProcessor 'org.projectlombok:lombok:1.18.12'
4949

5050
implementation 'com.github.Timmi6790:Commons:4e06ba7769'
51+
52+
testImplementation 'org.junit.jupiter:junit-jupiter:5.7.0'
53+
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
54+
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
55+
testImplementation 'org.junit.jupiter:junit-jupiter-params:5.7.0'
56+
testImplementation 'org.assertj:assertj-core:3.17.2'
57+
testImplementation 'org.mockito:mockito-core:3.5.13'
58+
testImplementation 'org.mockito:mockito-inline:3.5.13'
59+
testImplementation 'org.mockito:mockito-junit-jupiter:3.5.13'
5160
}
5261

5362
minecraft {
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package de.timmi6790.mpmod;
22

3+
import lombok.experimental.UtilityClass;
4+
5+
@UtilityClass
36
public class Reference {
47
public static final String MODID = "RMM";
58
public static final String NAME = "RandomMineplexMod";
6-
public static final String VERSION = "1.0.0";
9+
public static final String VERSION = "1.0.1";
710
public static final String VERSION_URL = "https://gist.githubusercontent.com/Timmi6790/2ead90ef6f97baeb689d076094e88c09/raw/RandomMineplexMod-Version";
811
public static final String DOWNLOAD_URL = "https://github.com/Timmi6790/RandomMineplexMod/releases";
912
}

src/main/java/de/timmi6790/mpmod/command/AbstractCommandGroup.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ public AbstractCommandGroup(final String commandName) {
2424
super(commandName);
2525
}
2626

27+
public String[][] getTabCompleteOptions() {
28+
return this.tabCompleteOptions.clone();
29+
}
30+
31+
public void setTabCompleteOptions(final String[][] tabCompleteOptions) {
32+
this.tabCompleteOptions = tabCompleteOptions.clone();
33+
}
34+
2735
protected Optional<AbstractCommand> getSubCommand(final String name) {
2836
for (final AbstractCommand command : this.subCommands) {
2937
if (command.getCommandName().equalsIgnoreCase(name)) {

src/main/java/de/timmi6790/mpmod/listeners/UpdateChecker.java

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import de.timmi6790.mpmod.McMod;
44
import de.timmi6790.mpmod.utilities.EventUtilities;
55
import de.timmi6790.mpmod.utilities.MessageBuilder;
6+
import de.timmi6790.mpmod.utilities.VersionUtilities;
67
import lombok.AllArgsConstructor;
78
import net.minecraft.event.ClickEvent;
89
import net.minecraft.util.EnumChatFormatting;
@@ -13,52 +14,31 @@
1314
import java.io.IOException;
1415
import java.io.InputStreamReader;
1516
import java.net.URL;
16-
import java.net.URLConnection;
1717
import java.nio.charset.StandardCharsets;
18-
import java.util.StringTokenizer;
1918
import java.util.concurrent.Executors;
2019

2120
@AllArgsConstructor
2221
public class UpdateChecker {
2322
private final McMod mod;
2423

25-
private boolean hasNewVersion(final String currentVersion, final String otherVersion) {
26-
final StringTokenizer currentVersionToken = new StringTokenizer(currentVersion, ".");
27-
final StringTokenizer otherVersionToken = new StringTokenizer(otherVersion, ".");
28-
29-
while (currentVersionToken.hasMoreTokens() || otherVersionToken.hasMoreElements()) {
30-
String currentPart = "0";
31-
if (currentVersionToken.hasMoreTokens()) {
32-
currentPart = currentVersionToken.nextToken();
33-
}
34-
35-
String otherPart = "0";
36-
if (otherVersionToken.hasMoreTokens()) {
37-
otherPart = otherVersionToken.nextToken();
38-
}
39-
40-
if (otherPart.compareTo(currentPart) > 0) {
41-
return true;
42-
}
43-
}
44-
45-
return false;
46-
}
47-
4824
@SubscribeEvent
4925
public void onServerJoin(final FMLNetworkEvent.ClientConnectedToServerEvent event) {
5026
// Only run this once
5127
EventUtilities.unRegisterEvents(this);
5228

29+
// Only check if we have both a version url and also a download url
30+
if (this.mod.getDownloadUrl().isEmpty() || this.mod.getVersionUrl().isEmpty()) {
31+
return;
32+
}
33+
5334
Executors.newSingleThreadExecutor().submit(() -> {
54-
try {
55-
final URLConnection connection = new URL(this.mod.getVersionUrl()).openConnection();
56-
final String version = new BufferedReader(new InputStreamReader(
57-
connection.getInputStream(),
58-
StandardCharsets.UTF_8
59-
)).readLine();
35+
try (final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
36+
new URL(this.mod.getVersionUrl()).openConnection().getInputStream(),
37+
StandardCharsets.UTF_8
38+
))) {
39+
final String version = bufferedReader.readLine();
6040

61-
if (!this.mod.getDownloadUrl().isEmpty() && this.hasNewVersion(this.mod.getVersion(), version)) {
41+
if (VersionUtilities.hasNewVersion(this.mod.getVersion(), version)) {
6242
new MessageBuilder(this.mod.getModName(), EnumChatFormatting.YELLOW)
6343
.addMessage("\n\nA new version is available!", EnumChatFormatting.GRAY)
6444
.addMessage("\nCurrent version: ", EnumChatFormatting.GRAY)

src/main/java/de/timmi6790/mpmod/utilities/DataUtilities.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,41 @@
1111

1212
@UtilityClass
1313
public class DataUtilities {
14-
private final Pattern INTEGER_PATTERN = Pattern.compile("^-?\\d+$");
15-
private final Pattern FLOAT_PATTERN = Pattern.compile("[+-]?(\\d+|\\d+\\.\\d+|\\.\\d+|\\d+\\.)([eE]\\d+)?");
16-
private final Pattern DOUBLE_PATTERN = Pattern.compile("[\\x00-\\x20]*[+-]?(((((\\p{Digit}+)(\\.)?((\\p{Digit}+)?)([eE][+-]?(\\p{Digit}+))?)|(\\.((\\p{Digit}+))([eE][+-]?(\\p{Digit}+))?)|(((0[xX](\\p{XDigit}+)(\\.)?)|(0[xX](\\p{XDigit}+)?(\\.)(\\p{XDigit}+)))[pP][+-]?(\\p{Digit}+)))[fFdD]?))[\\x00-\\x20]*");
1714
private final Pattern BOOLEAN_PATTERN = Pattern.compile("true|false", Pattern.CASE_INSENSITIVE);
1815

1916
public boolean isInt(final Object value) {
20-
return INTEGER_PATTERN.matcher(String.valueOf(value)).matches();
17+
try {
18+
Integer.parseInt(String.valueOf(value));
19+
return true;
20+
} catch (final NumberFormatException ignore) {
21+
return false;
22+
}
2123
}
2224

2325
public boolean isDouble(final Object value) {
24-
return DOUBLE_PATTERN.matcher(String.valueOf(value)).matches();
26+
try {
27+
Double.parseDouble(String.valueOf(value));
28+
return true;
29+
} catch (final NumberFormatException ignore) {
30+
return false;
31+
}
2532
}
2633

2734
public boolean isFloat(final Object value) {
28-
return FLOAT_PATTERN.matcher(String.valueOf(value)).matches();
35+
try {
36+
Float.parseFloat(String.valueOf(value));
37+
return true;
38+
} catch (final NumberFormatException ignore) {
39+
return false;
40+
}
2941
}
3042

3143
public boolean isBoolean(final Object value) {
3244
return BOOLEAN_PATTERN.matcher(String.valueOf(value)).matches();
3345
}
3446

3547
public boolean hasEmptyArg(final String[] args, final int position) {
36-
return (args.length == position + 1 && args[position].length() == 0);
48+
return args.length == position + 1 && args[position].length() == 0;
3749
}
3850

3951
public List<String> getStartWithIgnoreCase(final Collection<String> options, final String start) {

src/main/java/de/timmi6790/mpmod/utilities/PlayerUtilities.java

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,14 @@
11
package de.timmi6790.mpmod.utilities;
22

3-
import com.github.benmanes.caffeine.cache.AsyncLoadingCache;
4-
import com.github.benmanes.caffeine.cache.Caffeine;
5-
import com.google.gson.JsonElement;
6-
import com.google.gson.JsonParser;
7-
import lombok.SneakyThrows;
83
import lombok.experimental.UtilityClass;
94
import net.minecraft.client.Minecraft;
105
import net.minecraft.client.network.NetworkPlayerInfo;
116

12-
import java.io.InputStreamReader;
13-
import java.net.URL;
14-
import java.net.URLConnection;
15-
import java.nio.charset.StandardCharsets;
167
import java.util.HashSet;
17-
import java.util.Optional;
188
import java.util.Set;
19-
import java.util.UUID;
20-
import java.util.concurrent.CompletableFuture;
21-
import java.util.concurrent.ExecutionException;
22-
import java.util.concurrent.TimeUnit;
23-
import java.util.concurrent.TimeoutException;
249

2510
@UtilityClass
2611
public class PlayerUtilities {
27-
private final AsyncLoadingCache<UUID, Optional<String>> playerNameCache = Caffeine.newBuilder()
28-
.maximumSize(1000)
29-
.expireAfterWrite(20, TimeUnit.MINUTES)
30-
.buildAsync(uuid -> {
31-
try {
32-
final URL url = new URL("https://sessionserver.mojang.com/session/minecraft/profile/" + PlayerUtilities.uuidShorter(uuid));
33-
final URLConnection con = url.openConnection();
34-
35-
final JsonElement element = new JsonParser().parse(new InputStreamReader(
36-
con.getInputStream(),
37-
StandardCharsets.UTF_8
38-
));
39-
40-
return Optional.of(element.getAsJsonObject().get("name").getAsString());
41-
} catch (final Exception ignore) {
42-
return Optional.empty();
43-
}
44-
});
45-
46-
@SneakyThrows
47-
public Optional<String> uuidToName(final UUID uuid) {
48-
final CompletableFuture<Optional<String>> nameFuture = playerNameCache.get(uuid);
49-
try {
50-
return nameFuture.get(20, TimeUnit.SECONDS);
51-
} catch (final ExecutionException | TimeoutException e) {
52-
return Optional.empty();
53-
}
54-
}
55-
56-
private String uuidShorter(final UUID uuid) {
57-
return uuid.toString().replace("-", "");
58-
}
59-
6012
public Set<String> getAllPlayersInLobby() {
6113
final Set<String> playersInLobby = new HashSet<>();
6214
// Nethandler Players
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package de.timmi6790.mpmod.utilities;
2+
3+
import lombok.NonNull;
4+
import lombok.experimental.UtilityClass;
5+
6+
import java.util.StringTokenizer;
7+
8+
@UtilityClass
9+
public class VersionUtilities {
10+
private String getTokenOrDefault(final StringTokenizer tokenizer, final String defaultValue) {
11+
if (tokenizer.hasMoreTokens()) {
12+
return tokenizer.nextToken();
13+
}
14+
15+
return defaultValue;
16+
}
17+
18+
public boolean hasNewVersion(@NonNull final String currentVersion, @NonNull final String newVersion) {
19+
final StringTokenizer currentVersionToken = new StringTokenizer(currentVersion, ".");
20+
final StringTokenizer newVersionToken = new StringTokenizer(newVersion, ".");
21+
22+
while (currentVersionToken.hasMoreTokens() || newVersionToken.hasMoreElements()) {
23+
final String currentPart = getTokenOrDefault(currentVersionToken, "0");
24+
final String newPart = getTokenOrDefault(newVersionToken, "0");
25+
26+
final int compareValue = newPart.compareTo(currentPart);
27+
if (compareValue > 0) {
28+
return true;
29+
} else if (compareValue < 0) {
30+
return false;
31+
}
32+
}
33+
34+
// Equal versions
35+
return false;
36+
}
37+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package de.timmi6790.mpmod.utilities;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.junit.jupiter.params.ParameterizedTest;
5+
import org.junit.jupiter.params.provider.ValueSource;
6+
7+
import static org.assertj.core.api.Assertions.assertThat;
8+
9+
class VersionUtilitiesTest {
10+
@ParameterizedTest
11+
@ValueSource(strings = {"2", "200000", "1.1.1", "1.0.0.1", "10.0.0", "1.0.1", "1.0.a"})
12+
void hasNewVersionHigherVersion(final String higherVersion) {
13+
assertThat(VersionUtilities.hasNewVersion("1.0.0", higherVersion)).isTrue();
14+
}
15+
16+
@Test
17+
void hasNewVersionEqual() {
18+
assertThat(VersionUtilities.hasNewVersion("1.0.0.", "1.0.0")).isFalse();
19+
}
20+
21+
@ParameterizedTest
22+
@ValueSource(strings = {"0", "0.9.9", "0.0.0.0.0.0.0.1"})
23+
void hasNewVersionLowerVersion(final String lowerVersion) {
24+
assertThat(VersionUtilities.hasNewVersion("1.0.0", lowerVersion)).isFalse();
25+
}
26+
}

0 commit comments

Comments
 (0)