Resolved v3 API issue and added version check

This commit is contained in:
Stijn Bannink 2024-12-13 19:10:21 +01:00
parent 11a8884afb
commit de8a3ef5f7
Signed by: SBDeveloper
GPG key ID: B730712F2C3A9D7A
6 changed files with 188 additions and 15 deletions

View file

@ -1,13 +1,17 @@
package tech.sbdevelopment.vehiclesplusconverter.utils;
import com.google.common.io.Files;
import net.md_5.bungee.api.ChatColor;
import nl.sbdeveloper.vehiclesplus.storage.file.HJSONFile;
import org.bukkit.plugin.java.JavaPlugin;
import tech.sbdevelopment.vehiclesplusconverter.VehiclesPlusConverter;
import tech.sbdevelopment.vehiclesplusconverter.api.ConversionException;
import tech.sbdevelopment.vehiclesplusconverter.api.InvalidConversionException;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.logging.Level;
public class MainUtil {
@ -24,6 +28,31 @@ public class MainUtil {
return split[split.length - 1]; //Last position
}
public static String idToReadable(String id) {
if (id.contains("_")) {
// Split on _ and capitalize each part
String[] split = id.split("_");
StringBuilder builder = new StringBuilder();
for (String s : split) {
builder.append(s.substring(0, 1).toUpperCase()).append(s.substring(1));
builder.append(" ");
}
return builder.toString().trim();
} else {
// Capitalize the first letter
return id.substring(0, 1).toUpperCase() + id.substring(1);
}
}
public static void disablePlugin(JavaPlugin plugin) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException, IOException {
// Call protected .getFile() method, then rename that file to .jar.old and save
Method getFile = JavaPlugin.class.getDeclaredMethod("getFile");
getFile.setAccessible(true);
File file = (File) getFile.invoke(plugin);
File renamedFile = new File(file.getParent(), file.getName() + ".disabled");
Files.move(file, renamedFile);
}
public static String getTypeIdByClass(String baseVehicle, String type) throws ConversionException {
switch (type) {
case "BikeType":
@ -45,7 +74,10 @@ public class MainUtil {
public static void saveToVehiclesPlus(Object data, String subFolder, String fileName) {
File parentFolders = new File(nl.sbdeveloper.vehiclesplus.VehiclesPlus.getInstance().getDataFolder(), subFolder);
if (!parentFolders.exists() && !parentFolders.mkdirs()) return;
if (!parentFolders.exists() && !parentFolders.mkdirs()) {
VehiclesPlusConverter.getInstance().getLogger().log(Level.SEVERE, "Couldn't create the folder " + subFolder);
return;
}
HJSONFile jsonFile = new HJSONFile(nl.sbdeveloper.vehiclesplus.VehiclesPlus.getInstance(), subFolder + "/" + fileName);
try {

View file

@ -0,0 +1,98 @@
package tech.sbdevelopment.vehiclesplusconverter.utils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Version implements Comparable<Version> {
private static final Pattern VERSION_PATTERN = Pattern.compile(
"([0-9*]+)\\.([0-9*]+)(?:\\.([0-9*]+))?(?:\\.([0-9*]+))?(?:-SNAPSHOT)?"
);
private final Integer major;
private final Integer minor;
private final Integer patch;
private final Integer build;
public static Version of(String version) {
return new Version(version);
}
Version(String version) {
Matcher matcher = VERSION_PATTERN.matcher(version);
if (matcher.matches()) {
this.major = parsePart(matcher.group(1));
this.minor = parsePart(matcher.group(2));
this.patch = matcher.group(3) != null ? parsePart(matcher.group(3)) : null;
this.build = matcher.group(4) != null ? parsePart(matcher.group(4)) : null;
} else {
throw new IllegalArgumentException("Invalid version format: " + version);
}
}
private Integer parsePart(String part) {
if ("*".equals(part)) {
return null; // wildcard
} else {
return Integer.parseInt(part);
}
}
public boolean isNewerThan(Version other) {
return compareTo(other) > 0;
}
public boolean isOlderThan(Version other) {
return compareTo(other) < 0;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Version version = (Version) obj;
return this.compareTo(version) == 0;
}
@Override
public int compareTo(Version other) {
int result = comparePart(this.major, other.major);
if (result != 0) return result;
result = comparePart(this.minor, other.minor);
if (result != 0) return result;
result = comparePart(this.patch, other.patch);
if (result != 0) return result;
return comparePart(this.build, other.build);
}
private int comparePart(Integer part1, Integer part2) {
if (part1 == null && part2 == null) return 0;
if (part1 == null) return -1; // wildcard is less than any number
if (part2 == null) return 1; // any number is greater than wildcard
return Integer.compare(part1, part2);
}
@Override
public int hashCode() {
int result = (major != null ? major.hashCode() : 0);
result = 31 * result + (minor != null ? minor.hashCode() : 0);
result = 31 * result + (patch != null ? patch.hashCode() : 0);
result = 31 * result + (build != null ? build.hashCode() : 0);
return result;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(major != null ? major : "*").append('.')
.append(minor != null ? minor : "*");
if (patch != null || build != null) sb.append('.').append(patch != null ? patch : "*");
if (build != null) sb.append('.').append(build);
return sb.toString();
}
}