Fixed stored vehicle conversion

This commit is contained in:
Stijn Bannink 2024-12-30 21:53:41 +01:00
parent 7ffb83c805
commit 6e4603553b
Signed by: SBDeveloper
GPG key ID: B730712F2C3A9D7A
2 changed files with 92 additions and 45 deletions

View file

@ -1,9 +1,14 @@
package tech.sbdevelopment.vehiclesplusconverter.api; package tech.sbdevelopment.vehiclesplusconverter.api;
import java.io.IOException; import java.io.IOException;
import java.util.Set;
public class ConversionException extends IOException { public class ConversionException extends IOException {
public ConversionException(String before, String filename) { public ConversionException(String explanation, String filename) {
super(before + " " + filename + ".yml"); super(explanation + " " + filename + ".yml!");
}
public ConversionException(String explanation, String filename, String expected, Set<String> available) {
super(explanation + " " + filename + ".yml! Expected " + expected + ", available: " + String.join(", ", available));
} }
} }

View file

@ -23,14 +23,16 @@ import nl.sbdeveloper.vehiclesplus.api.vehicles.settings.impl.*;
import nl.sbdeveloper.vehiclesplus.storage.db.exceptions.DataStorageException; import nl.sbdeveloper.vehiclesplus.storage.db.exceptions.DataStorageException;
import nl.sbdeveloper.vehiclesplus.utils.jackson.ColorList; import nl.sbdeveloper.vehiclesplus.utils.jackson.ColorList;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
import tech.sbdevelopment.vehiclesplusconverter.VehiclesPlusConverter; import tech.sbdevelopment.vehiclesplusconverter.VehiclesPlusConverter;
import tech.sbdevelopment.vehiclesplusconverter.api.ConversionException; import tech.sbdevelopment.vehiclesplusconverter.api.ConversionException;
import java.util.List; import java.io.File;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
import java.util.logging.Level;
import static tech.sbdevelopment.vehiclesplusconverter.utils.MainUtil.*; import static tech.sbdevelopment.vehiclesplusconverter.utils.MainUtil.*;
@ -73,8 +75,7 @@ public class Converter {
try { try {
disablePlugin(VehiclesPlus.getInstance()); disablePlugin(VehiclesPlus.getInstance());
disablePlugin(VehiclesPlusConverter.getInstance()); disablePlugin(VehiclesPlusConverter.getInstance());
} catch (Exception e) { } catch (Exception ignored) {
e.printStackTrace();
} }
} }
counter--; 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("")); 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()) { for (BaseVehicle baseVehicle : VehiclesPlusAPI.getVehicleManager().getBaseVehicleMap().values()) {
if (baseVehicle.getName().startsWith("Example")) {
VehiclesPlusConverter.getInstance().getLogger().info("Skipping example vehicle model: " + baseVehicle.getName());
continue;
}
try { try {
VehicleModel.Builder vehicleModelBuilder = VehicleModel.builder() VehicleModel.Builder vehicleModelBuilder = VehicleModel.builder()
.id(baseVehicle.getName()) .id(baseVehicle.getName())
@ -137,7 +143,12 @@ public class Converter {
turretSeat.getXOffset(), turretSeat.getXOffset(),
turretSeat.getYOffset(), turretSeat.getYOffset(),
turretSeat.getZOffset(), 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) { } else if (part instanceof Seat) {
Seat seat = (Seat) part; Seat seat = (Seat) part;
@ -191,7 +202,12 @@ public class Converter {
wheel.getXOffset(), wheel.getXOffset(),
wheel.getYOffset(), wheel.getYOffset(),
wheel.getZOffset(), 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.getColor(),
wheel.getSteering(), wheel.getSteering(),
wheel.getRotationOffset() wheel.getRotationOffset()
@ -275,6 +291,7 @@ public class Converter {
VehiclesPlusConverter.getInstance().getLogger().info("Converted vehicle model: " + model.getId()); VehiclesPlusConverter.getInstance().getLogger().info("Converted vehicle model: " + model.getId());
saveToVehiclesPlus(model, "vehicles/" + model.getTypeId(), model.getId()); saveToVehiclesPlus(model, "vehicles/" + model.getTypeId(), model.getId());
nl.sbdeveloper.vehiclesplus.api.VehiclesPlusAPI.getVehicleModels().put(model.getId(), model);
} catch (Exception e) { } catch (Exception e) {
VehiclesPlusConverter.getInstance().getLogger().log(java.util.logging.Level.SEVERE, "Could not convert vehicle model: " + baseVehicle.getName(), e); VehiclesPlusConverter.getInstance().getLogger().log(java.util.logging.Level.SEVERE, "Could not convert vehicle model: " + baseVehicle.getName(), e);
} }
@ -282,22 +299,44 @@ public class Converter {
} }
private static void convertVehicles() { private static void convertVehicles() {
for (Map.Entry<UUID, List<StorageVehicle>> set : VehiclesPlusAPI.getVehicleManager().getPlayerVehicleHashMap().entrySet()) { File baseDir = new File("plugins/VehiclesPlusPro/data");
UUID ownerUUID = set.getKey(); if (!baseDir.exists()) {
String ownerName = Bukkit.getOfflinePlayer(ownerUUID).getName(); VehiclesPlusConverter.getInstance().getLogger().severe("Could not find the VehiclesPlusPro data folder!");
if (ownerName == null) { return;
VehiclesPlusConverter.getInstance().getLogger().severe("Could not convert vehicles for player with UUID " + ownerUUID + ", the player name is unknown!"); }
for (File playerDir : baseDir.listFiles()) {
if (playerDir.isDirectory()) {
String ownerUUID = playerDir.getName();
OfflinePlayer player = Bukkit.getOfflinePlayer(UUID.fromString(ownerUUID));
if (player.getName() == null) {
VehiclesPlusConverter.getInstance().getLogger().severe("Could not find player with UUID: " + ownerUUID + ", skipping...");
continue; 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 + "...");
VehiclesPlusConverter.getInstance().getLogger().info("Converting vehicles for player with UUID " + ownerUUID + " (" + ownerName + ")..."); final Garage garage = nl.sbdeveloper.vehiclesplus.api.VehiclesPlusAPI.getPersonalGarage(player);
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;
}
for (StorageVehicle vehicle : set.getValue()) {
try { try {
nl.sbdeveloper.vehiclesplus.api.vehicles.impl.StorageVehicle newVehicle = new nl.sbdeveloper.vehiclesplus.api.vehicles.impl.StorageVehicle( nl.sbdeveloper.vehiclesplus.api.vehicles.impl.StorageVehicle newVehicle = new nl.sbdeveloper.vehiclesplus.api.vehicles.impl.StorageVehicle(
UUID.randomUUID(), 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())), 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 false
); );
@ -313,18 +352,21 @@ public class Converter {
newVehicle.forceSave(); newVehicle.forceSave();
garage.addVehicle(newVehicle.getUuid()); garage.addVehicle(newVehicle.getUuid());
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); Bukkit.getLogger().log(Level.SEVERE, "Could not convert vehicles for player with UUID " + ownerUUID + ", could not save the vehicle!", e);
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()); VehiclesPlusConverter.getInstance().getLogger().info("Converted vehicle: " + vehicle.getUuid());
i++;
}
} }
try { try {
garage.forceSave(); garage.forceSave();
} catch (DataStorageException e) { } catch (DataStorageException e) {
e.printStackTrace(); Bukkit.getLogger().log(Level.SEVERE, "Could not convert vehicles for player with UUID " + ownerUUID + ", could not save the garage!", e);
Bukkit.getLogger().severe("Could not convert vehicles for player with UUID " + ownerUUID + ", could not save the garage!"); }
VehiclesPlusConverter.getInstance().getLogger().info("Converted " + i + " vehicles for player with UUID " + ownerUUID);
} }
} }
} }