Fixed stored vehicle conversion
This commit is contained in:
parent
7ffb83c805
commit
6e4603553b
2 changed files with 92 additions and 45 deletions
|
@ -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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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,49 +299,74 @@ 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!");
|
}
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
final Garage garage = nl.sbdeveloper.vehiclesplus.api.VehiclesPlusAPI.getGarage(ownerName).orElseGet(() -> new Garage(ownerName, ownerUUID));
|
for (File playerDir : baseDir.listFiles()) {
|
||||||
VehiclesPlusConverter.getInstance().getLogger().info("Converting vehicles for player with UUID " + ownerUUID + " (" + ownerName + ")...");
|
if (playerDir.isDirectory()) {
|
||||||
|
String ownerUUID = playerDir.getName();
|
||||||
|
|
||||||
for (StorageVehicle vehicle : set.getValue()) {
|
OfflinePlayer player = Bukkit.getOfflinePlayer(UUID.fromString(ownerUUID));
|
||||||
try {
|
if (player.getName() == null) {
|
||||||
nl.sbdeveloper.vehiclesplus.api.vehicles.impl.StorageVehicle newVehicle = new nl.sbdeveloper.vehiclesplus.api.vehicles.impl.StorageVehicle(
|
VehiclesPlusConverter.getInstance().getLogger().severe("Could not find player with UUID: " + ownerUUID + ", skipping...");
|
||||||
UUID.randomUUID(),
|
continue;
|
||||||
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!");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
int i = 0;
|
||||||
garage.forceSave();
|
for (File vehicleFile : playerDir.listFiles()) {
|
||||||
} catch (DataStorageException e) {
|
if (vehicleFile.getName().contains(".yml")) {
|
||||||
e.printStackTrace();
|
StorageVehicle vehicle;
|
||||||
Bukkit.getLogger().severe("Could not convert vehicles for player with UUID " + ownerUUID + ", could not save the garage!");
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue