Refactoring of ModUtil members

This commit is contained in:
Frank 2021-08-18 14:29:15 +02:00
parent 71ad055ea5
commit 3bb8fec4b2
11 changed files with 221 additions and 193 deletions

View file

@ -496,63 +496,4 @@ public class DataFixerAPI {
Patch.getALL().add(patch.get());
}
/**
* Get mod version from string. String should be in format: %d.%d.%d
*
* @param version - {@link String} mod version.
* @return int mod version.
*/
public static int getModVersion(String version) {
if (version.isEmpty()) {
return 0;
}
try {
int res = 0;
final String semanticVersionPattern = "(\\d+)\\.(\\d+)\\.(\\d+)\\D*";
final Matcher matcher = Pattern.compile(semanticVersionPattern)
.matcher(version);
if (matcher.find()) {
if (matcher.groupCount() > 0) res = (Integer.parseInt(matcher.group(1)) & 0xFF) << 22;
if (matcher.groupCount() > 1) res |= (Integer.parseInt(matcher.group(2)) & 0xFF) << 14;
if (matcher.groupCount() > 2) res |= Integer.parseInt(matcher.group(3)) & 0x3FFF;
}
return res;
}
catch (Exception e) {
return 0;
}
}
/**
* Get mod version from integer. String will be in format %d.%d.%d
*
* @param version - mod version in integer form.
* @return {@link String} mod version.
*/
public static String getModVersion(int version) {
int a = (version >> 22) & 0xFF;
int b = (version >> 14) & 0xFF;
int c = version & 0x3FFF;
return String.format(Locale.ROOT, "%d.%d.%d", a, b, c);
}
/**
* {@code true} if the version v1 is larger than v2
* @param v1 A Version string
* @param v2 Another Version string
* @return v1 &gt; v2
*/
public static boolean isLargerVersion(String v1, String v2){
return getModVersion(v1) > getModVersion(v2);
}
/**
* {@code true} if the version v1 is larger or equal v2
* @param v1 A Version string
* @param v2 Another Version string
* @return v1 &ge; v2
*/
public static boolean isLargerOrEqualVersion(String v1, String v2){
return getModVersion(v1) >= getModVersion(v2);
}
}

View file

@ -7,6 +7,7 @@ import net.minecraft.nbt.Tag;
import org.jetbrains.annotations.NotNull;
import ru.bclib.BCLib;
import ru.bclib.api.WorldDataAPI;
import ru.bclib.util.ModUtil;
import java.io.File;
import java.io.IOException;
@ -133,7 +134,7 @@ public class MigrationProfile {
}
public int currentPatchLevel(@NotNull String modID) {
return DataFixerAPI.getModVersion(currentPatchVersion(modID));
return ModUtil.convertModVersion(currentPatchVersion(modID));
}
public boolean hasAnyFixes() {

View file

@ -2,6 +2,7 @@ package ru.bclib.api.datafixer;
import net.minecraft.nbt.CompoundTag;
import org.jetbrains.annotations.NotNull;
import ru.bclib.util.ModUtil;
import java.util.ArrayList;
import java.util.HashMap;
@ -90,7 +91,7 @@ public abstract class Patch {
}
this.version = version;
this.level = DataFixerAPI.getModVersion(version);
this.level = ModUtil.convertModVersion(version);
if (!ALL.stream()
.filter(p -> p.modID
.equals(modID))