为载具和枪械ID读取添加兜底

This commit is contained in:
Light_Quanta 2025-05-24 02:06:57 +08:00
parent db692df419
commit bca8ba3fce
No known key found for this signature in database
GPG key ID: 11A39A1B8C890959
2 changed files with 25 additions and 6 deletions

View file

@ -1,7 +1,6 @@
package com.atsuishio.superbwarfare.data.vehicle; package com.atsuishio.superbwarfare.data.vehicle;
import com.atsuishio.superbwarfare.Mod; import com.atsuishio.superbwarfare.Mod;
import com.atsuishio.superbwarfare.network.message.receive.GunsDataMessage;
import com.atsuishio.superbwarfare.network.message.receive.VehiclesDataMessage; import com.atsuishio.superbwarfare.network.message.receive.VehiclesDataMessage;
import com.google.gson.Gson; import com.google.gson.Gson;
import net.minecraft.server.level.ServerPlayer; import net.minecraft.server.level.ServerPlayer;
@ -33,8 +32,17 @@ public class VehicleDataTool {
Gson gson = new Gson(); Gson gson = new Gson();
var data = gson.fromJson(new InputStreamReader(attribute.open()), DefaultVehicleData.class); var data = gson.fromJson(new InputStreamReader(attribute.open()), DefaultVehicleData.class);
if (!vehicleData.containsKey(data.id)) { String id;
vehicleData.put(data.id, data); if (!data.id.isEmpty()) {
id = data.id;
} else {
var path = entry.getKey().getPath();
id = Mod.MODID + ":" + path.substring(VEHICLE_DATA_FOLDER.length() + 1, path.length() - VEHICLE_DATA_FOLDER.length() - 1);
Mod.LOGGER.warn("Vehicle ID for {} is empty, try using {} as id", id, path);
}
if (!vehicleData.containsKey(id)) {
vehicleData.put(id, data);
} }
} catch (Exception e) { } catch (Exception e) {
Mod.LOGGER.error(e.getMessage()); Mod.LOGGER.error(e.getMessage());

View file

@ -25,6 +25,8 @@ public class GunsTool {
public static HashMap<String, DefaultGunData> gunsData = new HashMap<>(); public static HashMap<String, DefaultGunData> gunsData = new HashMap<>();
public static final String GUN_DATA_FOLDER = "guns";
/** /**
* 初始化数据从data中读取数据json文件 * 初始化数据从data中读取数据json文件
*/ */
@ -32,15 +34,24 @@ public class GunsTool {
gunsData.clear(); gunsData.clear();
GunData.dataCache.invalidateAll(); GunData.dataCache.invalidateAll();
for (var entry : manager.listResources("guns", file -> file.getPath().endsWith(".json")).entrySet()) { for (var entry : manager.listResources(GUN_DATA_FOLDER, file -> file.getPath().endsWith(".json")).entrySet()) {
var attribute = entry.getValue(); var attribute = entry.getValue();
try { try {
Gson gson = new Gson(); Gson gson = new Gson();
var data = gson.fromJson(new InputStreamReader(attribute.open()), DefaultGunData.class); var data = gson.fromJson(new InputStreamReader(attribute.open()), DefaultGunData.class);
if (!gunsData.containsKey(data.id)) { String id;
gunsData.put(data.id, data); if (!data.id.trim().isEmpty()) {
id = data.id;
} else {
var path = entry.getKey().getPath();
id = Mod.MODID + ":" + path.substring(GUN_DATA_FOLDER.length() + 1, path.length() - GUN_DATA_FOLDER.length() - 1);
Mod.LOGGER.warn("Gun ID for {} is empty, try using {} as id", path, id);
}
if (!gunsData.containsKey(id)) {
gunsData.put(id, data);
} }
} catch (Exception e) { } catch (Exception e) {
Mod.LOGGER.error(e.getMessage()); Mod.LOGGER.error(e.getMessage());