Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import net.tfminecraft.vehicleframework.loaders.ConfigLoader;
import net.tfminecraft.vehicleframework.loaders.FuelLoader;
import net.tfminecraft.vehicleframework.loaders.TrainsLoader;
import net.tfminecraft.vehicleframework.loaders.PlayerSkinLoader;
import net.tfminecraft.vehicleframework.loaders.VehicleLoader;
import net.tfminecraft.vehicleframework.loaders.WeaponTemplateLoader;
import net.tfminecraft.vehicleframework.loaders.ArmorTemplateLoader;
Expand Down Expand Up @@ -242,6 +243,7 @@ public void loadConfigs() {
File folder = new File(getDataFolder(), "vehicles");
VFLogger.info("Loading vehicles...");
vehicleLoader.reload(folder);
loadPlayerSkins();

folder = new File(getDataFolder(), "ammunition");
VFLogger.info("Loading ammunition...");
Expand Down Expand Up @@ -326,6 +328,15 @@ public static VehicleFramework getInstance() {
return plugin;
}

/** Re-reads player-skins.yml into the loaded vehicles; spawned vehicles stay loaded. */
public PlayerSkinLoader.Result loadPlayerSkins() {
PlayerSkinLoader.Result result = PlayerSkinLoader.apply(
new File(getDataFolder(), PlayerSkinLoader.FILE_NAME), VehicleLoader.get());
VFLogger.info("Loaded " + result.loaded + " player vehicle skin(s)"
+ (result.skipped > 0 ? ", skipped " + result.skipped : ""));
return result;
}

public void reload() {
PersistenceLog.append("RELOAD_BEGIN loaded=" + vehicleManager.get().size());
vehicleManager.unloadAll();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private static String encodePayload(ActiveVehicle v) {
vehicleData.put("id", v.getId());
vehicleData.put("name", v.getName());
vehicleData.put("yaw", (double) v.getEntity().getLocation().getYaw());
vehicleData.put("skin", v.getSkinHandler().getCurrentSkin().getId());
vehicleData.put("skin", v.getSkinHandler().getSavedSkinId());
vehicleData.put("owner", v.getOwnerData().getOwner());
vehicleData.put("whitelisted", v.getOwnerData().isWhiteListed());
vehicleData.put("ticketsEnabled", v.getOwnerData().isTicketsEnabled());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package net.tfminecraft.vehicleframework.loaders;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Pattern;

import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;

import net.tfminecraft.vehicleframework.VFLogger;
import net.tfminecraft.vehicleframework.vehicles.Vehicle;
import net.tfminecraft.vehicleframework.vehicles.handlers.skins.VehicleSkin;

/**
* Loads website vehicle skins from {@code player-skins.yml} into the vehicle registry.
*
* <p>ArmourShop writes the file when it applies an approved upload. Each entry adds one skin to
* an existing vehicle type; the vehicle's own YAML is never touched. Spawned vehicles share their
* type's skin map, so a reload here reaches them without unloading anything.
*
* <pre>
* vskin_drumleaf_sunrise:
* vehicle: karve
* name: "Sunrise"
* model: vskin_drumleaf_sunrise
* owner: 3f2a7c1e-... # the uploader's account UUID
* </pre>
*/
public final class PlayerSkinLoader {

public static final String FILE_NAME = "player-skins.yml";
private static final Pattern ID = Pattern.compile("[a-z0-9_]+");

private PlayerSkinLoader() {}

/** Result counts of one load. */
public static final class Result {
public final int loaded;
public final int skipped;
public final boolean fileError;

public Result(int loaded, int skipped, boolean fileError) {
this.loaded = loaded;
this.skipped = skipped;
this.fileError = fileError;
}
}

/**
* Replaces every player skin in {@code vehicles} with the entries in {@code file}. A file
* that cannot be read keeps the skins already loaded.
*/
public static Result apply(File file, Map<String, Vehicle> vehicles) {
YamlConfiguration config = new YamlConfiguration();
if (file != null && file.exists() && !file.isFile()) {
VFLogger.log(file.getName() + " is not a file; keeping the loaded player skins");
return new Result(0, 0, true);
}
if (file != null && file.isFile()) {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
try {
config.load(file);
} catch (IOException | InvalidConfigurationException e) {
VFLogger.log("Failed to read " + file.getName() + "; keeping the loaded player skins: " + e.getMessage());
return new Result(0, 0, true);
}
}
for (Vehicle vehicle : vehicles.values()) {
vehicle.getSkinHandler().removePlayerSkins();
}
int loaded = 0;
int skipped = 0;
for (String key : config.getKeys(false)) {
String problem = add(key, config.getConfigurationSection(key), vehicles);
if (problem == null) {
loaded++;
} else {
skipped++;
VFLogger.log("Skipping player skin '" + key + "': " + problem);
}
}
return new Result(loaded, skipped, false);
}

private static String add(String key, ConfigurationSection section, Map<String, Vehicle> vehicles) {
if (section == null) return "not a section";
if (!ID.matcher(key).matches()) return "id must be lowercase letters, digits and _";
String vehicleId = section.getString("vehicle");
Vehicle vehicle = vehicleId == null ? null : vehicles.get(vehicleId);
if (vehicle == null) return "unknown vehicle '" + vehicleId + "'";
String model = section.getString("model");
if (model == null || model.isBlank()) return "no model";
UUID owner;
try {
owner = UUID.fromString(section.getString("owner", "").trim());
} catch (IllegalArgumentException e) {
return "owner must be the uploader's UUID (player skins are owner-only)";
}
VehicleSkin skin = new VehicleSkin(key, section.getString("name"), model.trim(), owner);
if (!vehicle.getSkinHandler().addPlayerSkin(skin)) {
return "vehicle '" + vehicleId + "' already has a skin with this id";
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import net.tfminecraft.vehicleframework.ownership.OwnershipActions;
import net.tfminecraft.vehicleframework.ownership.OwnershipMode;
import net.tfminecraft.vehicleframework.VFLogger;
import net.tfminecraft.vehicleframework.loaders.PlayerSkinLoader;
import net.tfminecraft.vehicleframework.VehicleFramework;
import net.tfminecraft.vehicleframework.vehicles.ActiveVehicle;
import net.tfminecraft.vehicleframework.weapons.ammunition.Ammunition;
Expand Down Expand Up @@ -173,6 +174,14 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
QuarantineCommand.list(sender);
return true;
}
if(args[0].equalsIgnoreCase("skins") && args.length == 2 && args[1].equalsIgnoreCase("reload")) {
// Player skins only: unlike /vf reload this never unloads spawned vehicles.
PlayerSkinLoader.Result result = VehicleFramework.getInstance().loadPlayerSkins();
sender.sendMessage(result.fileError
? "§cCould not read " + PlayerSkinLoader.FILE_NAME + "; kept the loaded player skins."
: "§aLoaded " + result.loaded + " player skin(s)" + (result.skipped > 0 ? ", skipped " + result.skipped + " (see log)" : "") + ".");
return true;
}
if(args[0].equalsIgnoreCase("reload") && args.length == 1) {
Player p = null;
if(sender instanceof Player) p = (Player) sender;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,15 +344,29 @@ private ItemStack getWeaponItem(ActiveWeapon w) {
public void skinSelection(Inventory i, Player p, ActiveVehicle v, boolean open) {
if(open) {
i = VehicleFramework.plugin.getServer().createInventory(new VFInventoryHolder(v.getUUID(), VFGUI.SKIN_SELECTION), 27, "§7Select Skin");
} else {
// The list depends on the viewer and the current skin, so a refresh can be shorter.
i.clear();
}
int x = 0;
String currentId = v.getSkinHandler().getCurrentSkin().getId();
List<VehicleSkin> shown = new ArrayList<>();
VehicleSkin currentSkin = null;
for(VehicleSkin skin : v.getSkinHandler().getSkins().values()) {
if (!SkinHandler.isModelAvailable(skin) && !skin.getId().equalsIgnoreCase(currentId)) {
boolean current = skin.getId().equalsIgnoreCase(currentId);
if (!current && (!SkinHandler.isModelAvailable(skin) || !skin.mayUse(p))) {
continue;
}
i.setItem(x, getSkinItem(skin, currentId, v.getIcon()));
x++;
if (current) currentSkin = skin;
shown.add(skin);
}
if (shown.size() > i.getSize()) {
// Too many to show: keep the current skin in the last slot if it would be cut off.
boolean cut = currentSkin != null && shown.indexOf(currentSkin) >= i.getSize();
shown = new ArrayList<>(shown.subList(0, cut ? i.getSize() - 1 : i.getSize()));
if (cut) shown.add(currentSkin);
}
for(int x = 0; x < shown.size(); x++) {
i.setItem(x, getSkinItem(shown.get(x), currentId, v.getIcon()));
}
//i.setItem(26, createDismountButton());
int slotn = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
import net.tfminecraft.vehicleframework.vehicles.component.Harness;
import net.tfminecraft.vehicleframework.vehicles.handlers.container.Container;
import net.tfminecraft.vehicleframework.vehicles.handlers.SkinHandler;
import net.tfminecraft.vehicleframework.vehicles.handlers.skins.VehicleSkin;
import net.tfminecraft.vehicleframework.vehicles.handlers.VehicleTicketInteract;
import net.tfminecraft.vehicleframework.vehicles.handlers.VehicleTicketItems;
import net.tfminecraft.vehicleframework.vehicles.handlers.VehicleTicketRules;
Expand All @@ -111,6 +112,7 @@ public class VehicleManager implements Listener{
public static final String TICKET_PASSENGER_ONLY = "§cA ticket only lets you ride in a passenger seat.";
public static final String NOT_ALLOWED_ABOARD = "§cThe owner has not allowed you aboard this vehicle.";
public static final String NOT_ALLOWED_PAINT = "§cThe owner has not allowed you to paint this vehicle.";
public static final String NOT_YOUR_SKIN = "§cYou do not own this skin.";
public static final String NOT_ALLOWED_TOW = "§cThe owner has not allowed you to tow this vehicle.";
public static final String NOT_ALLOWED_TOWING = "§cThe owner has not allowed you to hitch anything to this vehicle.";
public static final String NOT_ALLOWED_UNHITCH = "§cThe owner has not allowed you to unhitch this vehicle.";
Expand Down Expand Up @@ -1295,10 +1297,15 @@ public void skinSelect(InventoryClickEvent e) {
p.sendMessage("§cAlready using this skin");
return;
}
if (skinId == null || !SkinHandler.isModelAvailable(v.getSkinHandler().getSkins().get(skinId))) {
VehicleSkin chosen = skinId == null ? null : v.getSkinHandler().getSkins().get(skinId);
if (chosen == null || !SkinHandler.isModelAvailable(chosen)) {
p.sendMessage("§cThat skin is not available.");
return;
}
if (!chosen.mayUse(p)) {
p.sendMessage(NOT_YOUR_SKIN);
return;
}
if (!v.changeSkin(skinId)) {
p.sendMessage("§cCould not change skin.");
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public ActiveVehicle spawn(Location loc, Vehicle v, VehicleManager manager, Inco
Entity e = null;
try {
String skinId = i == null ? v.getSkinHandler().getCurrentSkin().getId() : i.getSkin();
VehicleSkin skin = v.getSkinHandler().getSkins().get(skinId);
VehicleSkin skin = v.getSkinHandler().resolveSpawnSkin(skinId);
if (skin == null || !SkinHandler.isModelAvailable(skin)) {
throw new IllegalArgumentException("Missing vehicle skin/model: " + skinId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;

Expand All @@ -19,8 +21,17 @@
public class SkinHandler {
private ActiveVehicle v;
private VehicleSkin currentSkin;
/** The skin a vehicle falls back to when its saved skin is gone (the vehicle's model key). */
private String defaultSkinId;
/**
* The skin id saved with the vehicle. It differs from the current skin while a missing skin
* is shown as the default, so a skin that is only briefly unavailable (a ModelEngine reload,
* an unreadable player-skins.yml) comes back on the next load instead of being overwritten.
*/
private String savedSkinId;

private HashMap<String, VehicleSkin> skins = new HashMap<>();
// Insertion order: config skins first, player skins after them. Spawned vehicles share this map.
private HashMap<String, VehicleSkin> skins = new LinkedHashMap<>();

public static boolean isModelAvailable(String modelId) {
if (modelId == null || modelId.isBlank()) {
Expand All @@ -44,13 +55,62 @@ public SkinHandler(String model, ConfigurationSection config) {
}
skins.put(key, skin);
}
defaultSkinId = model;
savedSkinId = model;
currentSkin = skins.get(model);
}

public SkinHandler(ActiveVehicle vehicle, String skin, SkinHandler another) {
v = vehicle;
skins = another.getSkins();
currentSkin = skins.get(skin);
defaultSkinId = another.defaultSkinId;
// The spawner already resolved (and logged) the same fallback.
VehicleSkin resolved = another.resolve(skin, false);
currentSkin = resolved != null ? resolved : skins.get(skin);
savedSkinId = skin != null ? skin : defaultSkinId;
}

/**
* The skin a saved vehicle spawns with. A skin id that is no longer listed, or a player skin
* whose model is gone, falls back to the default skin so a revoked upload never strands the
* vehicle. A config skin with a missing model is returned as is (the spawner reports it).
*/
public VehicleSkin resolveSpawnSkin(String requested) {
return resolve(requested, true);
}

private VehicleSkin resolve(String requested, boolean log) {
VehicleSkin skin = requested == null ? null : skins.get(requested);
if (skin != null && (!skin.isPlayerSkin() || isModelAvailable(skin))) {
return skin;
}
VehicleSkin fallback = defaultSkinId == null ? null : skins.get(defaultSkinId);
if (log && fallback != null && requested != null && !requested.equals(defaultSkinId)) {
VFLogger.log("Vehicle skin '" + requested + "' is unavailable; using '" + defaultSkinId + "' instead");
}
return fallback != null ? fallback : skin;
}

/** Adds a website skin; refuses an id the vehicle's own config already uses. */
public boolean addPlayerSkin(VehicleSkin skin) {
if (skin == null || !skin.isPlayerSkin()) return false;
VehicleSkin existing = skins.get(skin.getId());
if (existing != null && !existing.isPlayerSkin()) return false;
skins.put(skin.getId(), skin);
return true;
}

/** Removes every website skin, keeping the vehicle's own config skins. */
public int removePlayerSkins() {
int removed = 0;
Iterator<VehicleSkin> it = skins.values().iterator();
while (it.hasNext()) {
if (it.next().isPlayerSkin()) {
it.remove();
removed++;
}
}
return removed;
}

public boolean canChangeSkin(String id, boolean override) {
Expand All @@ -72,9 +132,16 @@ public String changeSkin(String id) {
VehicleSkin skin = skins.get(id);
if (!isModelAvailable(skin)) return null;
currentSkin = skin;
savedSkinId = skin.getId();
return currentSkin.getModel();
}

/** The skin id to save: the painted skin, even while a missing one is shown as the default. */
public String getSavedSkinId() {
if (savedSkinId != null) return savedSkinId;
return currentSkin == null ? null : currentSkin.getId();
}

public VehicleSkin getCurrentSkin() {
return currentSkin;
}
Expand Down
Loading