Resolved v3 API issue and added version check
This commit is contained in:
parent
11a8884afb
commit
de8a3ef5f7
6 changed files with 188 additions and 15 deletions
10
pom.xml
10
pom.xml
|
@ -67,22 +67,22 @@
|
|||
<dependency>
|
||||
<groupId>org.spigotmc</groupId>
|
||||
<artifactId>spigot-api</artifactId>
|
||||
<version>1.21-R0.1-SNAPSHOT</version>
|
||||
<version>1.21.1-R0.1-SNAPSHOT</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>nl.sbdeveloper</groupId>
|
||||
<artifactId>VehiclesPlus-v2</artifactId>
|
||||
<version>2.4.6.2</version>
|
||||
<version>2.4.7.2</version>
|
||||
<scope>system</scope>
|
||||
<systemPath>${project.basedir}/src/libs/VehiclesPlus-2.4.6.2.jar</systemPath>
|
||||
<systemPath>${project.basedir}/src/libs/VehiclesPlus-2.4.7.2.jar</systemPath>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>nl.sbdeveloper</groupId>
|
||||
<artifactId>VehiclesPlus-v3</artifactId>
|
||||
<version>3.0.0-SNAPSHOT</version>
|
||||
<version>3.0.1</version>
|
||||
<scope>system</scope>
|
||||
<systemPath>${project.basedir}/src/libs/VehiclesPlus-3.0.0-SNAPSHOT-Clean.jar</systemPath>
|
||||
<systemPath>${project.basedir}/src/libs/VehiclesPlus-3.0.1.jar</systemPath>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
|
@ -3,6 +3,7 @@ package tech.sbdevelopment.vehiclesplusconverter;
|
|||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import tech.sbdevelopment.vehiclesplusconverter.cmd.ConverterCMD;
|
||||
import tech.sbdevelopment.vehiclesplusconverter.utils.Version;
|
||||
|
||||
public final class VehiclesPlusConverter extends JavaPlugin {
|
||||
private static VehiclesPlusConverter instance;
|
||||
|
@ -21,6 +22,20 @@ public final class VehiclesPlusConverter extends JavaPlugin {
|
|||
return;
|
||||
}
|
||||
|
||||
Version versionOld = Version.of(Bukkit.getPluginManager().getPlugin("VehiclesPlusPro").getDescription().getVersion());
|
||||
if (versionOld.isOlderThan(Version.of("2.4.7.1"))) {
|
||||
Bukkit.getLogger().severe("Your VehiclesPlus v2 plugin is too old! Please update to at least v2.4.7.1!");
|
||||
Bukkit.getPluginManager().disablePlugin(this);
|
||||
return;
|
||||
}
|
||||
|
||||
Version versionNew = Version.of(Bukkit.getPluginManager().getPlugin("VehiclesPlus").getDescription().getVersion());
|
||||
if (versionNew.isOlderThan(Version.of("3.0.1"))) {
|
||||
Bukkit.getLogger().severe("Your VehiclesPlus v3 plugin is too old! Please update to at least v3.0.1!");
|
||||
Bukkit.getPluginManager().disablePlugin(this);
|
||||
return;
|
||||
}
|
||||
|
||||
getCommand("vpconvert").setExecutor(new ConverterCMD());
|
||||
}
|
||||
|
||||
|
|
|
@ -21,7 +21,7 @@ public class ConverterCMD implements CommandExecutor {
|
|||
Converter.convert(sender);
|
||||
} else {
|
||||
sender.sendMessage(__("&7[&3&lVehiclesPlusConverter&7] &fPlease use &b/vpconvert confirm &fto start the conversion!"));
|
||||
sender.sendMessage(__("&7[&3&lVehiclesPlusConverter&7] &4&lPLEASE NOTE: &cExisting v3 vehicles may be overwritten! &c&lCreate a backup before confirming."));
|
||||
sender.sendMessage(__("&7[&3&lVehiclesPlusConverter&7] &4&lPLEASE NOTE: &cExisting v3 vehicles may be overwritten! &c&lCreate a backup before confirming!!!"));
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
|
|
@ -68,6 +68,15 @@ public class Converter {
|
|||
@Override
|
||||
public void run() {
|
||||
sender.sendMessage(__("&7[&3&lVehiclesPlusConverter&7] &f" + (counter == 15 ? "Conversion finished! " : "") + "&bRebooting in &3" + counter + " &bseconds..."));
|
||||
if (counter == 1) {
|
||||
sender.sendMessage(__("&7[&3&lVehiclesPlusConverter&7] &4&lPLEASE NOTE: &cVehiclesPlus v2 and the converter are disabled from now on!"));
|
||||
try {
|
||||
disablePlugin(VehiclesPlus.getInstance());
|
||||
disablePlugin(VehiclesPlusConverter.getInstance());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
counter--;
|
||||
}
|
||||
}.runTaskTimer(VehiclesPlusConverter.getInstance(), 20L, 20L);
|
||||
|
@ -78,10 +87,13 @@ public class Converter {
|
|||
nl.sbdeveloper.vehiclesplus.api.vehicles.rims.RimDesign rd = new nl.sbdeveloper.vehiclesplus.api.vehicles.rims.RimDesign(
|
||||
entry.getValue().getName(),
|
||||
entry.getValue().getSkin(),
|
||||
HolderItemPosition.HEAD,
|
||||
entry.getValue().getPrice()
|
||||
);
|
||||
|
||||
nl.sbdeveloper.vehiclesplus.api.VehiclesPlusAPI.getRimDesigns().put(entry.getKey(), rd);
|
||||
|
||||
VehiclesPlusConverter.getInstance().getLogger().info("Converted rim design: " + rd.getName());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -94,15 +106,19 @@ public class Converter {
|
|||
);
|
||||
|
||||
nl.sbdeveloper.vehiclesplus.api.VehiclesPlusAPI.getFuelTypes().put(entry.getKey(), ft);
|
||||
|
||||
VehiclesPlusConverter.getInstance().getLogger().info("Converted fuel type: " + ft.getName());
|
||||
}
|
||||
}
|
||||
|
||||
private static void convertVehicleModels() {
|
||||
VehiclesPlusConverter.getInstance().getLogger().info("Converting vehicle models: " + VehiclesPlusAPI.getVehicleManager().getBaseVehicleMap().values().stream().map(BaseVehicle::getName).reduce((s1, s2) -> s1 + ", " + s2).orElse(""));
|
||||
|
||||
for (BaseVehicle baseVehicle : VehiclesPlusAPI.getVehicleManager().getBaseVehicleMap().values()) {
|
||||
try {
|
||||
VehicleModel.Builder vehicleModelBuilder = VehicleModel.builder()
|
||||
.id(baseVehicle.getName())
|
||||
.displayName(baseVehicle.getName())
|
||||
.displayName(idToReadable(baseVehicle.getName()))
|
||||
.typeId(getTypeIdByClass(baseVehicle.getName(), getClassByFullName(baseVehicle.getVehicleType())))
|
||||
.availableColors(new ColorList(baseVehicle.getBaseColorList()));
|
||||
|
||||
|
@ -177,7 +193,6 @@ public class Converter {
|
|||
wheel.getZOffset(),
|
||||
nl.sbdeveloper.vehiclesplus.api.VehiclesPlusAPI.getRimDesign(VehiclesPlus.getVehicleManager().getRimDesignHashMap().values().stream().findFirst().orElseThrow(() -> new ConversionException("No RimDesign found while loading Wheel in file", baseVehicle.getName())).getName()).orElseThrow(() -> new ConversionException("No matching RimDesign found while loading Wheel in file", baseVehicle.getName())),
|
||||
wheel.getColor(),
|
||||
HolderItemPosition.HEAD,
|
||||
wheel.getSteering(),
|
||||
wheel.getRotationOffset()
|
||||
));
|
||||
|
@ -222,7 +237,7 @@ public class Converter {
|
|||
baseVehicle.getExhaustSettings().getXOffset(),
|
||||
baseVehicle.getExhaustSettings().getYOffset(),
|
||||
baseVehicle.getExhaustSettings().getZOffset(),
|
||||
baseVehicle.getExhaustSettings().getParticle().get()
|
||||
baseVehicle.getExhaustSettings().getParticleName().get()
|
||||
))
|
||||
.exitWhileMoving(baseVehicle.getCanExitWhileMoving())
|
||||
.price(baseVehicle.getPrice())
|
||||
|
@ -238,17 +253,27 @@ public class Converter {
|
|||
baseVehicle.getHitbox().getHeight()
|
||||
))
|
||||
.realisticSteering(baseVehicle.getSteeringType())
|
||||
.permissions(Permissions.builder()
|
||||
.permissions(baseVehicle.getPermissions() != null ?
|
||||
Permissions.builder()
|
||||
.buy(baseVehicle.getPermissions().getBuyPermission())
|
||||
.ride(baseVehicle.getPermissions().getRidePermission())
|
||||
.sitWithoutRidePermission(baseVehicle.getPermissions().getEnterWithoutRidePermission())
|
||||
.adjust("vp.adjust." + baseVehicle.getName())
|
||||
.spawn("vp.spawn." + baseVehicle.getName())
|
||||
.build()
|
||||
: Permissions.builder()
|
||||
.buy("vp.buy." + baseVehicle.getName())
|
||||
.ride("vp.ride." + baseVehicle.getName())
|
||||
.sitWithoutRidePermission(true)
|
||||
.adjust("vp.adjust." + baseVehicle.getName())
|
||||
.spawn("vp.spawn." + baseVehicle.getName())
|
||||
.build())
|
||||
.sounds(defaultSounds)
|
||||
.gearbox(new Gearbox(true, 10))
|
||||
.build();
|
||||
|
||||
VehiclesPlusConverter.getInstance().getLogger().info("Converted vehicle model: " + model.getId());
|
||||
|
||||
saveToVehiclesPlus(model, "vehicles/" + model.getTypeId(), model.getId());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
|
@ -261,11 +286,12 @@ public class Converter {
|
|||
UUID ownerUUID = set.getKey();
|
||||
String ownerName = Bukkit.getOfflinePlayer(ownerUUID).getName();
|
||||
if (ownerName == null) {
|
||||
Bukkit.getLogger().severe("Could not convert vehicles for player with UUID " + ownerUUID + ", the player name is unknown!");
|
||||
VehiclesPlusConverter.getInstance().getLogger().severe("Could not convert vehicles for player with UUID " + ownerUUID + ", the player name is unknown!");
|
||||
continue;
|
||||
}
|
||||
|
||||
final Garage garage = nl.sbdeveloper.vehiclesplus.api.VehiclesPlusAPI.getGarage(ownerName).orElseGet(() -> new Garage(ownerName, ownerUUID));
|
||||
VehiclesPlusConverter.getInstance().getLogger().info("Converting vehicles for player with UUID " + ownerUUID + " (" + ownerName + ")...");
|
||||
|
||||
for (StorageVehicle vehicle : set.getValue()) {
|
||||
try {
|
||||
|
@ -281,6 +307,8 @@ public class Converter {
|
|||
e.printStackTrace();
|
||||
Bukkit.getLogger().severe("Could not convert vehicles for player with UUID " + ownerUUID + ", could not save the vehicle!");
|
||||
}
|
||||
|
||||
VehiclesPlusConverter.getInstance().getLogger().info("Converted vehicle: " + vehicle.getUuid());
|
||||
}
|
||||
|
||||
try {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue