Skip to content

Commit

Permalink
Fix version checks #145
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnuh committed Jul 26, 2023
1 parent 70b2248 commit 6ace659
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 31 deletions.
8 changes: 8 additions & 0 deletions api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,12 @@ dependencies {
api("com.codingforcookies:robert:1.2-SNAPSHOT") {
exclude(module = "spigot-api")
}
testImplementation("org.junit.jupiter:junit-jupiter-api:5.10.0")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.10.0")
}

tasks {
test {
useJUnitPlatform()
}
}
58 changes: 34 additions & 24 deletions api/src/main/java/com/dsh105/echopet/compat/api/util/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ public int[] getNumericVersion(String serverVersion){
serverVersion = serverVersion.substring(0, serverVersion.indexOf("R") - 1) + "-" + serverVersion.substring(serverVersion.indexOf("R"));
serverVersion = serverVersion.replace("_", ".");
}
int size = serverVersion.split("\\.").length;
Integer rev = null;
if(serverVersion.contains("-")){// Removes stuff like SNAPSHOT and R
String in = serverVersion.split("-")[1];
Expand All @@ -85,17 +84,22 @@ public int[] getNumericVersion(String serverVersion){
if(in.length() > 0) rev = ObjectParser.isInt(in);
serverVersion = serverVersion.split("-")[0];
}
int[] numericVersionParts = new int[size + (rev != null ? 1 : 0)];
serverVersion = serverVersion.replaceAll("\\D", "");
// Removes any non-numeric characters and replaces it with a period
// "+" is to turn groups of non-numeric characters into a single period
serverVersion = serverVersion.replaceAll("\\D+", ".");
// Split by period for numeric version comparison
String[] parts = serverVersion.split("\\.");

int size = serverVersion.split("\\.").length;
int[] numericVersionParts = new int[size + (rev != null ? 1 : 0)];
for(int i = 0; i < parts.length; i++){
try{
numericVersionParts[i] = ObjectParser.isInt(parts[i]);
}catch(NullPointerException ex){
}
}
if(rev != null) numericVersionParts[numericVersionParts.length - 1] = rev;
if(numericVersionParts.length <= 0) throw new IllegalArgumentException("Invalid version: " + serverVersion);
if(numericVersionParts.length == 0) throw new IllegalArgumentException("Invalid version: " + serverVersion);
return numericVersionParts;
}

Expand Down Expand Up @@ -137,10 +141,10 @@ public boolean isIdentical(String version){
}

/**
* Returns whether or not the given version is compatible with this version
* Returns whether the given version is compatible with this version
* <p>
* Makes a comparison to see if the given version is more recent (compatible) or identical
* to than this version. For example, if this version is 1.7-R2, a version of 1.7-R3 or 1.7-R4 will be considered
* to than this version. For example, if this version is 1.7-R2, a version of 1.7-R3, 1.8-R1, 2.0-R1 will be considered
* compatible, whereas 1.7-R1 will not
*
* @param version server version to make a comparison against e.g. 1.7-R2
Expand All @@ -151,11 +155,11 @@ public boolean isCompatible(String version){
}

/**
* Returns whether or not the given version is compatible with this version
* Returns whether this version supports the given version
* <p>
* Makes a comparison to see if the given version is more recent (compatible) or identical
* to than this version. For example, if this version is 1.7-R2, a version of 1.7-R3 or 1.7-R4 will be considered
* compatible, whereas 1.7-R1 will not
* Makes a comparison to see if the version given is earlier (supported) or identical
* to than this version. For example, if this version is 1.7-R3, a version of 1.7-R2 or 1.7-R1 will be considered
* supported, whereas 1.7-R4 will not
*
* @param version server version to make a comparison against e.g. 1.7-R2
* @return true if {@code latestAllowedVersion} is supported by this version
Expand All @@ -165,7 +169,7 @@ public boolean isSupported(String version){
}

/**
* Returns whether or not this version is identical to the given version
* Returns whether this version is identical to the given version
* <p>
* For example: 1.7-R1 matches 1.7-R1, but not 1.7-R2 or 1.7-R3
*
Expand All @@ -177,7 +181,7 @@ public boolean isIdentical(int[] version){
}

/**
* Returns whether or not the given version is compatible with this version
* Returns whether the given version is compatible with this version
* <p>
* Makes a comparison to see if the given version is more recent (compatible) or identical
* to than this version. For example, if this version is 1.7-R2, a version of 1.7-R3 or 1.7-R4 will be considered
Expand All @@ -191,7 +195,7 @@ public boolean isCompatible(int[] version){
}

/**
* Returns whether or not this version supports the given version
* Returns whether this version supports the given version
* <p>
* Makes a comparison to see if the version given is earlier (supported) or identical
* to than this version. For example, if this version is 1.7-R3, a version of 1.7-R2 or 1.7-R1 will be considered
Expand All @@ -205,7 +209,7 @@ public boolean isSupported(int[] version){
}

/**
* Returns whether or not this version is identical to the given version
* Returns whether this version is identical to the given version
* <p>
* For example: 1.7-R1 matches 1.7-R1, but not 1.7-R2 or 1.7-R3
*
Expand All @@ -222,27 +226,30 @@ public boolean isIdentical(Version version){
}

/**
* Returns whether or not the given version is compatible with this version
* Returns whether the given version is compatible with this version
* <p>
* Makes a comparison to see if the given version is more recent (compatible) or identical
* to than this version. For example, if this version is 1.7-R2, a version of 1.7-R3 or 1.7-R4 will be considered
* to than this version. For example, if this version is 1.7-R2, a version of 1.7-R3, 1.8-R1, 2.0-R1 will be considered
* compatible, whereas 1.7-R1 will not
*
* @param version server version to make a comparison against e.g. 1.7-R2
* @return true if the {@code minimumRequiredVersion} is compatible with this version
*/
public boolean isCompatible(Version version){
if(isIdentical(version)) return true;
boolean compatible = false;
for(int in = 0; in < numericVersion.length; in++){
if(version.getNumericVersion()[in] > numericVersion[in]) compatible = true;
else if(version.getNumericVersion()[in] < numericVersion[in]) return false;
// Prioritize the first number we see.
if(version.getNumericVersion()[in] > numericVersion[in]){
return true;
}else if(version.getNumericVersion()[in] < numericVersion[in]){
return false;
}
}
return compatible;
return false;
}

/**
* Returns whether or not this version supports the given version
* Returns whether this version supports the given version
* <p>
* Makes a comparison to see if the version given is earlier (supported) or identical
* to than this version. For example, if this version is 1.7-R3, a version of 1.7-R2 or 1.7-R1 will be considered
Expand All @@ -253,10 +260,13 @@ public boolean isCompatible(Version version){
*/
public boolean isSupported(Version version){
if(isIdentical(version)) return true;
boolean supported = false;
for(int in = 0; in < numericVersion.length; in++){
if(version.getNumericVersion()[in] < numericVersion[in]) supported = true;
if(version.getNumericVersion()[in] < numericVersion[in]){
return true;
}else if(version.getNumericVersion()[in] > numericVersion[in]){
return false;
}
}
return supported;
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* This file is part of EchoPet.
*
* EchoPet is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EchoPet is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EchoPet. If not, see <http://www.gnu.org/licenses/>.
*/

package com.dsh105.echopet.compat.api.util;

import org.junit.jupiter.api.Test;


import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

class VersionTest{

@Test
void isCompatible(){
Version version = new Version("1.19-R2");
// Confirm documentation is correct
assertTrue(version.isCompatible("1.19-R3"));
assertTrue(version.isCompatible("1.20-R1"));
assertTrue(version.isCompatible("2.0-R1"));
assertFalse(version.isCompatible("1.19-R1"));
// Extra tests
assertFalse(version.isCompatible("1.7-R2"));
}

@Test
void isSupported(){
Version version = new Version("1.7-R3");
// Confirm documentation is correct
assertTrue(version.isSupported("1.7-R2"));
assertTrue(version.isSupported("1.7-R1"));
assertFalse(version.isSupported("1.7-R4"));
// Extra tests
assertTrue(version.isSupported("1.6-R4"));
assertTrue(version.isSupported("1.0-R1"));
assertFalse(version.isSupported("2.0-R3"));
}
}
15 changes: 8 additions & 7 deletions nms/src/main/java/com/dsh105/echopet/nms/PetRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,14 @@ public class PetRegistry implements IPetRegistry{

public PetRegistry(){
for(PetType petType : PetType.values){
if(petType.isCompatible()){
try{
PetRegistrationEntry registrationEntry = PetRegistrationEntry.create(petType);
register(petType, registrationEntry);
}catch(PetRegistrationException e){
// not found = not compatible with this server version
}
if(!petType.isCompatible()){
continue;
}
try{
PetRegistrationEntry registrationEntry = PetRegistrationEntry.create(petType);
register(petType, registrationEntry);
}catch(PetRegistrationException e){
// not found = not compatible with this server version
}
}
}
Expand Down

0 comments on commit 6ace659

Please sign in to comment.