diff --git a/pom.xml b/pom.xml index 8469adf..b432e3d 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ tech.sbdevelopment VehiclesPlusConverter - 0.2.3 + 0.2.4 jar VehiclesPlusConverter @@ -80,9 +80,9 @@ nl.sbdeveloper VehiclesPlus-v3 - 3.0.1 + 3.0.3 system - ${project.basedir}/src/libs/VehiclesPlus-3.0.1.jar + ${project.basedir}/src/libs/VehiclesPlus-3.0.3.jar diff --git a/src/main/java/tech/sbdevelopment/vehiclesplusconverter/VehiclesPlusConverter.java b/src/main/java/tech/sbdevelopment/vehiclesplusconverter/VehiclesPlusConverter.java index 9602116..683a5c7 100644 --- a/src/main/java/tech/sbdevelopment/vehiclesplusconverter/VehiclesPlusConverter.java +++ b/src/main/java/tech/sbdevelopment/vehiclesplusconverter/VehiclesPlusConverter.java @@ -30,8 +30,8 @@ public final class VehiclesPlusConverter extends JavaPlugin { } Version versionNew = Version.of(Bukkit.getPluginManager().getPlugin("VehiclesPlus").getDescription().getVersion()); - if (versionNew.isOlderThan(Version.of("3.0.2"))) { - Bukkit.getLogger().severe("Your VehiclesPlus v3 plugin is too old! Please update to at least v3.0.2!"); + if (versionNew.isOlderThan(Version.of("3.0.3"))) { + Bukkit.getLogger().severe("Your VehiclesPlus v3 plugin is too old! Please update to at least v3.0.3!"); Bukkit.getPluginManager().disablePlugin(this); return; } diff --git a/src/main/java/tech/sbdevelopment/vehiclesplusconverter/handlers/Converter.java b/src/main/java/tech/sbdevelopment/vehiclesplusconverter/handlers/Converter.java index 3b099bf..ea814eb 100644 --- a/src/main/java/tech/sbdevelopment/vehiclesplusconverter/handlers/Converter.java +++ b/src/main/java/tech/sbdevelopment/vehiclesplusconverter/handlers/Converter.java @@ -25,12 +25,15 @@ import nl.sbdeveloper.vehiclesplus.utils.jackson.ColorList; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; import org.bukkit.command.CommandSender; +import org.bukkit.inventory.ItemStack; import org.bukkit.scheduler.BukkitRunnable; import tech.sbdevelopment.vehiclesplusconverter.VehiclesPlusConverter; import tech.sbdevelopment.vehiclesplusconverter.api.ConversionException; import java.io.File; +import java.util.Arrays; import java.util.Map; +import java.util.Optional; import java.util.UUID; import java.util.logging.Level; @@ -93,6 +96,7 @@ public class Converter { ); nl.sbdeveloper.vehiclesplus.api.VehiclesPlusAPI.getRimDesigns().put(entry.getKey(), rd); + rd.save(); VehiclesPlusConverter.getInstance().getLogger().info("Converted rim design: " + rd.getName()); } @@ -107,6 +111,7 @@ public class Converter { ); nl.sbdeveloper.vehiclesplus.api.VehiclesPlusAPI.getFuelTypes().put(entry.getKey(), ft); + ft.save(); VehiclesPlusConverter.getInstance().getLogger().info("Converted fuel type: " + ft.getName()); } @@ -198,20 +203,52 @@ public class Converter { )); } else if (part instanceof Wheel) { Wheel wheel = (Wheel) part; - vehicleModelBuilder = vehicleModelBuilder.part(new nl.sbdeveloper.vehiclesplus.api.vehicles.parts.impl.Wheel( + ItemStack wheelSkin = wheel.getSkinColored(); + + // Try to find an existing rim design with this skin + Optional matchingRimDesign = nl.sbdeveloper.vehiclesplus.api.VehiclesPlusAPI.getRimDesigns() + .values() + .stream() + .filter(rd -> compareItems(rd.getSkin(), wheelSkin)) + .findFirst(); + + if (matchingRimDesign.isEmpty()) { + // Create a new rim design since no matching one exists + String newRimDesignName = generateRimName(); + nl.sbdeveloper.vehiclesplus.api.vehicles.rims.RimDesign newRimDesign = new nl.sbdeveloper.vehiclesplus.api.vehicles.rims.RimDesign( + newRimDesignName, + wheelSkin, + HolderItemPosition.HEAD, + 1000 + ); + + // Add the new rim design to the rim designs map + nl.sbdeveloper.vehiclesplus.api.VehiclesPlusAPI.getRimDesigns().put(newRimDesignName, newRimDesign); + newRimDesign.save(); + VehiclesPlusConverter.getInstance().getLogger().info("Created new rim design '" + newRimDesignName + "' for wheel skin!"); + + // Create the wheel part with the new rim design + vehicleModelBuilder = vehicleModelBuilder.part(new nl.sbdeveloper.vehiclesplus.api.vehicles.parts.impl.Wheel( 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(), "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())), + newRimDesign, wheel.getColor(), wheel.getSteering(), wheel.getRotationOffset() - )); + )); + } else { + // Use the existing rim design that matches the skin + vehicleModelBuilder = vehicleModelBuilder.part(new nl.sbdeveloper.vehiclesplus.api.vehicles.parts.impl.Wheel( + wheel.getXOffset(), + wheel.getYOffset(), + wheel.getZOffset(), + matchingRimDesign.get(), + wheel.getColor(), + wheel.getSteering(), + wheel.getRotationOffset() + )); + } } } @@ -349,6 +386,9 @@ public class Converter { newVehicle.getStatics().setBroken(vehicle.getVehicleStats().getBroken()); newVehicle.getStatics().setCurrentHealth(vehicle.getVehicleStats().getHealth()); + // Set the trunk + newVehicle.getTrunkItems().addAll(Arrays.asList(vehicle.getVehicleTrunk().getContents())); + newVehicle.forceSave(); garage.addVehicle(newVehicle.getUuid()); } catch (Exception e) { diff --git a/src/main/java/tech/sbdevelopment/vehiclesplusconverter/utils/MainUtil.java b/src/main/java/tech/sbdevelopment/vehiclesplusconverter/utils/MainUtil.java index 921aea4..95b38dc 100644 --- a/src/main/java/tech/sbdevelopment/vehiclesplusconverter/utils/MainUtil.java +++ b/src/main/java/tech/sbdevelopment/vehiclesplusconverter/utils/MainUtil.java @@ -3,6 +3,7 @@ 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.inventory.ItemStack; import org.bukkit.plugin.java.JavaPlugin; import tech.sbdevelopment.vehiclesplusconverter.VehiclesPlusConverter; import tech.sbdevelopment.vehiclesplusconverter.api.ConversionException; @@ -86,4 +87,62 @@ public class MainUtil { VehiclesPlusConverter.getInstance().getLogger().log(Level.SEVERE, "Couldn't save to the file " + fileName, e); } } + + /** + * Compare two ItemStacks for equality based on material type and metadata + * + * @param item1 First ItemStack + * @param item2 Second ItemStack + * @return true if the items are considered equal + */ + public static boolean compareItems(ItemStack item1, ItemStack item2) { + if (item1 == null || item2 == null) return false; + if (!item1.getType().equals(item2.getType())) return false; + + org.bukkit.inventory.meta.ItemMeta meta1 = item1.getItemMeta(); + org.bukkit.inventory.meta.ItemMeta meta2 = item2.getItemMeta(); + + if (meta1 == null || meta2 == null) return meta1 == meta2; + + // Check CustomModelData if available (1.14+) + try { + return meta1.hasCustomModelData() && meta2.hasCustomModelData() + && meta1.getCustomModelData() == meta2.getCustomModelData(); + } catch (NoSuchMethodError ignored) { + // Pre 1.14, check Durability/Damage value instead + return item1.getDurability() == item2.getDurability(); + } + } + + private static final String[] RIM_PREFIXES = { + "sport", "racing", "luxury", "classic", "vintage", "modern", "elite", "premium", "pro", "ultra" + }; + + private static final String[] RIM_TYPES = { + "star", "spoke", "mesh", "alloy", "chrome", "forged", "cast", "split", "blade", "wave" + }; + + private static final String[] RIM_SUFFIXES = { + "design", "series", "line", "edition", "collection", "style", "plus", "max", "elite", "pro" + }; + + /** + * Generate a realistic rim design name + * + * @return A random rim design name + */ + public static String generateRimName() { + String prefix = RIM_PREFIXES[(int) (Math.random() * RIM_PREFIXES.length)]; + String type = RIM_TYPES[(int) (Math.random() * RIM_TYPES.length)]; + String suffix = RIM_SUFFIXES[(int) (Math.random() * RIM_SUFFIXES.length)]; + + // Add a random number between 100 and 999 + int number = 100 + (int) (Math.random() * 900); + + // Convert to PascalCase + return prefix.substring(0, 1).toUpperCase() + prefix.substring(1) + + type.substring(0, 1).toUpperCase() + type.substring(1) + + suffix.substring(0, 1).toUpperCase() + suffix.substring(1) + + number; + } }