diff --git a/src/main/java/tech/sbdevelopment/vehiclesplusconverter/api/ConversionException.java b/src/main/java/tech/sbdevelopment/vehiclesplusconverter/api/ConversionException.java index 92a46c7..592df09 100644 --- a/src/main/java/tech/sbdevelopment/vehiclesplusconverter/api/ConversionException.java +++ b/src/main/java/tech/sbdevelopment/vehiclesplusconverter/api/ConversionException.java @@ -1,9 +1,14 @@ package tech.sbdevelopment.vehiclesplusconverter.api; import java.io.IOException; +import java.util.Set; public class ConversionException extends IOException { - public ConversionException(String before, String filename) { - super(before + " " + filename + ".yml"); + public ConversionException(String explanation, String filename) { + super(explanation + " " + filename + ".yml!"); + } + + public ConversionException(String explanation, String filename, String expected, Set available) { + super(explanation + " " + filename + ".yml! Expected " + expected + ", available: " + String.join(", ", available)); } } diff --git a/src/main/java/tech/sbdevelopment/vehiclesplusconverter/handlers/Converter.java b/src/main/java/tech/sbdevelopment/vehiclesplusconverter/handlers/Converter.java index b059df6..3b099bf 100644 --- a/src/main/java/tech/sbdevelopment/vehiclesplusconverter/handlers/Converter.java +++ b/src/main/java/tech/sbdevelopment/vehiclesplusconverter/handlers/Converter.java @@ -23,14 +23,16 @@ import nl.sbdeveloper.vehiclesplus.api.vehicles.settings.impl.*; import nl.sbdeveloper.vehiclesplus.storage.db.exceptions.DataStorageException; import nl.sbdeveloper.vehiclesplus.utils.jackson.ColorList; import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; import org.bukkit.command.CommandSender; import org.bukkit.scheduler.BukkitRunnable; import tech.sbdevelopment.vehiclesplusconverter.VehiclesPlusConverter; import tech.sbdevelopment.vehiclesplusconverter.api.ConversionException; -import java.util.List; +import java.io.File; import java.util.Map; import java.util.UUID; +import java.util.logging.Level; import static tech.sbdevelopment.vehiclesplusconverter.utils.MainUtil.*; @@ -73,8 +75,7 @@ public class Converter { try { disablePlugin(VehiclesPlus.getInstance()); disablePlugin(VehiclesPlusConverter.getInstance()); - } catch (Exception e) { - e.printStackTrace(); + } catch (Exception ignored) { } } counter--; @@ -115,6 +116,11 @@ public class Converter { 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()) { + if (baseVehicle.getName().startsWith("Example")) { + VehiclesPlusConverter.getInstance().getLogger().info("Skipping example vehicle model: " + baseVehicle.getName()); + continue; + } + try { VehicleModel.Builder vehicleModelBuilder = VehicleModel.builder() .id(baseVehicle.getName()) @@ -137,7 +143,12 @@ public class Converter { turretSeat.getXOffset(), turretSeat.getYOffset(), turretSeat.getZOffset(), - baseVehicle.getPartList().stream().filter(Turret.class::isInstance).findFirst().orElseThrow(() -> new ConversionException("No Turret found while loading TurretSeat in file", baseVehicle.getName())).getUID() + baseVehicle.getPartList() + .stream() + .filter(Turret.class::isInstance) + .findFirst() + .orElseThrow(() -> new ConversionException("No Turret found while loading TurretSeat in file", baseVehicle.getName())) + .getUID() )); } else if (part instanceof Seat) { Seat seat = (Seat) part; @@ -191,7 +202,12 @@ public class Converter { wheel.getXOffset(), wheel.getYOffset(), wheel.getZOffset(), - nl.sbdeveloper.vehiclesplus.api.VehiclesPlusAPI.getRimDesign(VehiclesPlus.getVehicleManager().getRimDesignHashMap().values().stream().map(str -> str.getName().toLowerCase()).findFirst().orElseThrow(() -> new ConversionException("No RimDesign found while loading Wheel in file", baseVehicle.getName()))).orElseThrow(() -> new ConversionException("No matching RimDesign found while loading Wheel in file", baseVehicle.getName())), + nl.sbdeveloper.vehiclesplus.api.VehiclesPlusAPI.getRimDesign(VehiclesPlus.getVehicleManager().getRimDesignHashMap().values() + .stream() + .map(str -> str.getName().toLowerCase()) + .findFirst() + .orElseThrow(() -> new ConversionException("No RimDesign found while loading Wheel in file", baseVehicle.getName(), "N/A", VehiclesPlus.getVehicleManager().getRimDesignHashMap().keySet()))) + .orElseThrow(() -> new ConversionException("No matching RimDesign found while loading Wheel in file", baseVehicle.getName(), "N/A", VehiclesPlus.getVehicleManager().getRimDesignHashMap().keySet())), wheel.getColor(), wheel.getSteering(), wheel.getRotationOffset() @@ -275,6 +291,7 @@ public class Converter { VehiclesPlusConverter.getInstance().getLogger().info("Converted vehicle model: " + model.getId()); saveToVehiclesPlus(model, "vehicles/" + model.getTypeId(), model.getId()); + nl.sbdeveloper.vehiclesplus.api.VehiclesPlusAPI.getVehicleModels().put(model.getId(), model); } catch (Exception e) { VehiclesPlusConverter.getInstance().getLogger().log(java.util.logging.Level.SEVERE, "Could not convert vehicle model: " + baseVehicle.getName(), e); } @@ -282,49 +299,74 @@ public class Converter { } private static void convertVehicles() { - for (Map.Entry> set : VehiclesPlusAPI.getVehicleManager().getPlayerVehicleHashMap().entrySet()) { - UUID ownerUUID = set.getKey(); - String ownerName = Bukkit.getOfflinePlayer(ownerUUID).getName(); - if (ownerName == null) { - VehiclesPlusConverter.getInstance().getLogger().severe("Could not convert vehicles for player with UUID " + ownerUUID + ", the player name is unknown!"); - continue; - } + File baseDir = new File("plugins/VehiclesPlusPro/data"); + if (!baseDir.exists()) { + VehiclesPlusConverter.getInstance().getLogger().severe("Could not find the VehiclesPlusPro data folder!"); + return; + } - 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 (File playerDir : baseDir.listFiles()) { + if (playerDir.isDirectory()) { + String ownerUUID = playerDir.getName(); - for (StorageVehicle vehicle : set.getValue()) { - try { - nl.sbdeveloper.vehiclesplus.api.vehicles.impl.StorageVehicle newVehicle = new nl.sbdeveloper.vehiclesplus.api.vehicles.impl.StorageVehicle( - UUID.randomUUID(), - nl.sbdeveloper.vehiclesplus.api.VehiclesPlusAPI.getVehicleModels().values().stream().filter(v -> v.getId().equalsIgnoreCase(vehicle.getBaseVehicle())).findFirst().orElseThrow(() -> new ConversionException("No VehicleModel found for", vehicle.getUuid())), - false - ); - - // Set the vehicle's stats - newVehicle.getStatics().forceSetMaxSpeed(vehicle.getVehicleStats().getSpeed()); - newVehicle.getStatics().forceSetTurningRadius(vehicle.getVehicleStats().getSteering()); - newVehicle.getStatics().forceSetFuelTank(vehicle.getVehicleStats().getFuelTank()); - newVehicle.getStatics().setCurrentFuel(vehicle.getVehicleStats().getCurrentFuel()); - newVehicle.getStatics().forceSetAcceleration(vehicle.getVehicleStats().getAcceleration()); - newVehicle.getStatics().setBroken(vehicle.getVehicleStats().getBroken()); - newVehicle.getStatics().setCurrentHealth(vehicle.getVehicleStats().getHealth()); - - newVehicle.forceSave(); - garage.addVehicle(newVehicle.getUuid()); - } catch (Exception e) { - e.printStackTrace(); - Bukkit.getLogger().severe("Could not convert vehicles for player with UUID " + ownerUUID + ", could not save the vehicle!"); + OfflinePlayer player = Bukkit.getOfflinePlayer(UUID.fromString(ownerUUID)); + if (player.getName() == null) { + VehiclesPlusConverter.getInstance().getLogger().severe("Could not find player with UUID: " + ownerUUID + ", skipping..."); + continue; } - VehiclesPlusConverter.getInstance().getLogger().info("Converted vehicle: " + vehicle.getUuid()); - } + VehiclesPlusConverter.getInstance().getLogger().info("Converting vehicles for player with UUID " + ownerUUID + "..."); + final Garage garage = nl.sbdeveloper.vehiclesplus.api.VehiclesPlusAPI.getPersonalGarage(player); - try { - garage.forceSave(); - } catch (DataStorageException e) { - e.printStackTrace(); - Bukkit.getLogger().severe("Could not convert vehicles for player with UUID " + ownerUUID + ", could not save the garage!"); + int i = 0; + for (File vehicleFile : playerDir.listFiles()) { + if (vehicleFile.getName().contains(".yml")) { + StorageVehicle vehicle; + try { + vehicle = StorageVehicle.loadFromStorage(vehicleFile.getAbsolutePath()); + } catch (Exception e) { + Bukkit.getLogger().log(Level.SEVERE, "Could not convert vehicles for player with UUID " + ownerUUID + ", could not load the vehicle!", e); + continue; + } + if (vehicle == null) { + Bukkit.getLogger().warning("Failed to load vehicle for player with UUID " + ownerUUID + " from file: " + vehicleFile.getName() + ", skipping..."); + continue; + } + + try { + nl.sbdeveloper.vehiclesplus.api.vehicles.impl.StorageVehicle newVehicle = new nl.sbdeveloper.vehiclesplus.api.vehicles.impl.StorageVehicle( + UUID.randomUUID(), + nl.sbdeveloper.vehiclesplus.api.VehiclesPlusAPI.getVehicleModels().values().stream().filter(v -> v.getId().equalsIgnoreCase(vehicle.getBaseVehicle())).findFirst().orElseThrow(() -> new ConversionException("No VehicleModel found for", vehicle.getUuid(), vehicle.getBaseVehicle(), nl.sbdeveloper.vehiclesplus.api.VehiclesPlusAPI.getVehicleModels().keySet())), + false + ); + + // Set the vehicle's stats + newVehicle.getStatics().forceSetMaxSpeed(vehicle.getVehicleStats().getSpeed()); + newVehicle.getStatics().forceSetTurningRadius(vehicle.getVehicleStats().getSteering()); + newVehicle.getStatics().forceSetFuelTank(vehicle.getVehicleStats().getFuelTank()); + newVehicle.getStatics().setCurrentFuel(vehicle.getVehicleStats().getCurrentFuel()); + newVehicle.getStatics().forceSetAcceleration(vehicle.getVehicleStats().getAcceleration()); + newVehicle.getStatics().setBroken(vehicle.getVehicleStats().getBroken()); + newVehicle.getStatics().setCurrentHealth(vehicle.getVehicleStats().getHealth()); + + newVehicle.forceSave(); + garage.addVehicle(newVehicle.getUuid()); + } catch (Exception e) { + Bukkit.getLogger().log(Level.SEVERE, "Could not convert vehicles for player with UUID " + ownerUUID + ", could not save the vehicle!", e); + } + + VehiclesPlusConverter.getInstance().getLogger().info("Converted vehicle: " + vehicle.getUuid()); + i++; + } + } + + try { + garage.forceSave(); + } catch (DataStorageException e) { + Bukkit.getLogger().log(Level.SEVERE, "Could not convert vehicles for player with UUID " + ownerUUID + ", could not save the garage!", e); + } + + VehiclesPlusConverter.getInstance().getLogger().info("Converted " + i + " vehicles for player with UUID " + ownerUUID); } } }