From cc749f66fbaf4858380a6c77fe0aeb04a9251bb5 Mon Sep 17 00:00:00 2001 From: Octol1ttle Date: Sat, 23 Mar 2024 11:46:06 +0500 Subject: [PATCH] change: improve display of flight plan load errors Signed-off-by: Octol1ttle --- .../commands/plan/LoadFlightPlanCommand.java | 16 +++---- .../computers/navigation/LandingMinimums.java | 10 ++--- .../serialization/FlightPlanLoadResult.java | 42 +++++++++++++++++++ .../serialization/FlightPlanSerializer.java | 27 +++++++----- .../serialization/WaypointSerializer.java | 4 +- .../assets/flightassistant/lang/en_us.json | 4 +- .../assets/flightassistant/lang/ru_ru.json | 4 +- 7 files changed, 79 insertions(+), 28 deletions(-) create mode 100644 src/main/java/ru/octol1ttle/flightassistant/serialization/FlightPlanLoadResult.java diff --git a/src/main/java/ru/octol1ttle/flightassistant/commands/plan/LoadFlightPlanCommand.java b/src/main/java/ru/octol1ttle/flightassistant/commands/plan/LoadFlightPlanCommand.java index c5c7d206..48002047 100644 --- a/src/main/java/ru/octol1ttle/flightassistant/commands/plan/LoadFlightPlanCommand.java +++ b/src/main/java/ru/octol1ttle/flightassistant/commands/plan/LoadFlightPlanCommand.java @@ -3,27 +3,27 @@ import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.exceptions.CommandSyntaxException; -import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; -import java.util.List; import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; import net.minecraft.text.Text; import ru.octol1ttle.flightassistant.HudRenderer; import ru.octol1ttle.flightassistant.computers.ComputerHost; -import ru.octol1ttle.flightassistant.computers.navigation.Waypoint; +import ru.octol1ttle.flightassistant.serialization.FlightPlanLoadResult; import ru.octol1ttle.flightassistant.serialization.FlightPlanSerializer; public class LoadFlightPlanCommand { - public static final SimpleCommandExceptionType NO_SUCH_PLAN = new SimpleCommandExceptionType(Text.translatable("commands.flightassistant.no_such_plan")); public static int execute(CommandContext context) throws CommandSyntaxException { ComputerHost host = HudRenderer.getHost(); String name = StringArgumentType.getString(context, "name"); - List loaded = FlightPlanSerializer.load(name); - if (loaded == null) { - throw NO_SUCH_PLAN.create(); + FlightPlanLoadResult result = FlightPlanSerializer.load(name); + if (result.getType() != FlightPlanLoadResult.LoadResultType.SUCCESS) { + context.getSource().sendError(result.getType().getText()); + + return -1; } + host.plan.clear(); - host.plan.addAll(loaded); + host.plan.addAll(result.getWaypoints()); context.getSource().sendFeedback(Text.translatable("commands.flightassistant.flight_plan_loaded", host.plan.size(), name)); return 0; } diff --git a/src/main/java/ru/octol1ttle/flightassistant/computers/navigation/LandingMinimums.java b/src/main/java/ru/octol1ttle/flightassistant/computers/navigation/LandingMinimums.java index 303787f4..1272413f 100644 --- a/src/main/java/ru/octol1ttle/flightassistant/computers/navigation/LandingMinimums.java +++ b/src/main/java/ru/octol1ttle/flightassistant/computers/navigation/LandingMinimums.java @@ -5,14 +5,14 @@ public enum AltitudeType { ABSOLUTE("Absolute"), ABOVE_GROUND("AboveGround"); - public final String serialisedName; - AltitudeType(String serialisedName) { - this.serialisedName = serialisedName; + public final String serializedName; + AltitudeType(String serializedName) { + this.serializedName = serializedName; } - public static AltitudeType fromSerialisedName(String nbtName) { + public static AltitudeType fromSerializedName(String nbtName) { for (AltitudeType type : AltitudeType.values()) { - if (type.serialisedName.equals(nbtName)) { + if (type.serializedName.equals(nbtName)) { return type; } } diff --git a/src/main/java/ru/octol1ttle/flightassistant/serialization/FlightPlanLoadResult.java b/src/main/java/ru/octol1ttle/flightassistant/serialization/FlightPlanLoadResult.java new file mode 100644 index 00000000..dce943ed --- /dev/null +++ b/src/main/java/ru/octol1ttle/flightassistant/serialization/FlightPlanLoadResult.java @@ -0,0 +1,42 @@ +package ru.octol1ttle.flightassistant.serialization; + +import java.util.List; +import net.minecraft.text.Text; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import ru.octol1ttle.flightassistant.computers.navigation.Waypoint; + +public class FlightPlanLoadResult { + private final @Nullable List loaded; + private final LoadResultType type; + + public FlightPlanLoadResult(@Nullable List loaded, @NotNull LoadResultType type) { + this.loaded = loaded; + this.type = type; + } + + public List getWaypoints() { + return loaded; + } + + public LoadResultType getType() { + return type; + } + + public enum LoadResultType { + SUCCESS(Text.translatable("commands.flightassistant.flight_plan_loaded")), + NOT_FOUND(Text.translatable("commands.flightassistant.flight_plan_not_found")), + NBT_NOT_SUPPORTED(Text.translatable("commands.flightassistant.flight_plan_not_supported")), + ERROR(Text.translatable("commands.flightassistant.flight_plan_load_error")); + + private final Text text; + + LoadResultType(Text text) { + this.text = text; + } + + public Text getText() { + return text; + } + } +} diff --git a/src/main/java/ru/octol1ttle/flightassistant/serialization/FlightPlanSerializer.java b/src/main/java/ru/octol1ttle/flightassistant/serialization/FlightPlanSerializer.java index 00573348..db868da1 100644 --- a/src/main/java/ru/octol1ttle/flightassistant/serialization/FlightPlanSerializer.java +++ b/src/main/java/ru/octol1ttle/flightassistant/serialization/FlightPlanSerializer.java @@ -7,7 +7,7 @@ import java.util.ArrayList; import java.util.List; import net.minecraft.MinecraftVersion; -import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.NotNull; import ru.octol1ttle.flightassistant.FlightAssistant; import ru.octol1ttle.flightassistant.MinecraftProtocolVersions; import ru.octol1ttle.flightassistant.computers.navigation.Waypoint; @@ -43,28 +43,33 @@ public static void save(List plan, String name) { } } - public static @Nullable List load(String name) { + public static @NotNull FlightPlanLoadResult load(String name) { List loaded = new ArrayList<>(); try { ISerializableObject object = JSON_FACTORY.createSerializer().read(PLAN_PATH, name); - if (object == null && MinecraftVersion.CURRENT.getProtocolVersion() >= MinecraftProtocolVersions.R20_3) { - object = NBT_FACTORY.createSerializer().read(PLAN_PATH, name); + + if (object == null) { + if (MinecraftVersion.CURRENT.getProtocolVersion() >= MinecraftProtocolVersions.R20_3) { + object = NBT_FACTORY.createSerializer().read(PLAN_PATH, name); + } else if (Files.exists(PLAN_PATH.resolve("%s.dat".formatted(name)))) { + return new FlightPlanLoadResult(null, FlightPlanLoadResult.LoadResultType.NBT_NOT_SUPPORTED); + } } if (object == null) { - return null; + return new FlightPlanLoadResult(null, FlightPlanLoadResult.LoadResultType.NOT_FOUND); } + ISerializableList list = object.getList("Waypoints"); for (ISerializableObject waypointObject : list) { loaded.add(WaypointSerializer.load(waypointObject)); } - } catch (IllegalStateException e) { - FlightAssistant.LOGGER.error("Invalid data detected during flight plan deserialization with name: %s".formatted(name), e); - } catch (IOException e) { - FlightAssistant.LOGGER.error("IO error detected during flight plan deserialization with name: %s".formatted(name), e); - } - return loaded; + return new FlightPlanLoadResult(loaded, FlightPlanLoadResult.LoadResultType.SUCCESS); + } catch (Exception e) { + FlightAssistant.LOGGER.error("Error deserializing flight plan with name: %s".formatted(name), e); + return new FlightPlanLoadResult(null, FlightPlanLoadResult.LoadResultType.ERROR); + } } } diff --git a/src/main/java/ru/octol1ttle/flightassistant/serialization/WaypointSerializer.java b/src/main/java/ru/octol1ttle/flightassistant/serialization/WaypointSerializer.java index 67b462ed..25814fc0 100644 --- a/src/main/java/ru/octol1ttle/flightassistant/serialization/WaypointSerializer.java +++ b/src/main/java/ru/octol1ttle/flightassistant/serialization/WaypointSerializer.java @@ -30,7 +30,7 @@ private static ISerializableObject saveLanding(LandingWaypoint waypoint, ISerial object.putDouble("TargetZ", waypoint.targetPosition().y); if (waypoint.minimums != null) { ISerializableObject minimums = FlightPlanSerializer.WRITE_FACTORY.createObject(); - minimums.putString("AltitudeType", waypoint.minimums.type().serialisedName); + minimums.putString("AltitudeType", waypoint.minimums.type().serializedName); minimums.putInt("Altitude", waypoint.minimums.altitude()); object.put("Minimums", minimums); @@ -69,7 +69,7 @@ private static LandingWaypoint readLanding(ISerializableObject object) { private static LandingMinimums readMinimums(ISerializableObject object) { return new LandingMinimums( - LandingMinimums.AltitudeType.fromSerialisedName(object.getString("AltitudeType")), + LandingMinimums.AltitudeType.fromSerializedName(object.getString("AltitudeType")), object.getInt("Altitude") ); } diff --git a/src/main/resources/assets/flightassistant/lang/en_us.json b/src/main/resources/assets/flightassistant/lang/en_us.json index 64b42a03..43185392 100644 --- a/src/main/resources/assets/flightassistant/lang/en_us.json +++ b/src/main/resources/assets/flightassistant/lang/en_us.json @@ -86,7 +86,6 @@ "config.flightassistant.computers.chunk_state.protection": "Unloaded chunk protection mode", "commands.flightassistant.no_such_waypoint": "There is no waypoint at that index", - "commands.flightassistant.no_such_plan": "There is no plan with that name", "commands.flightassistant.nothing_to_clear": "There are no waypoints to clear", "commands.flightassistant.first_wp_cannot_be_landing": "The first waypoint cannot be a landing waypoint", "commands.flightassistant.cannot_execute_from_landing": "The flight plan cannot be executed starting from a landing waypoint", @@ -98,6 +97,9 @@ "commands.flightassistant.flight_plan_executed": "Executed flight plan starting from waypoint at index %s (%s waypoints in total)", "commands.flightassistant.flight_plan_saved": "Saved flight plan with %s waypoints as \"%s\"", "commands.flightassistant.flight_plan_loaded": "Loaded flight plan with %s waypoints from \"%s\"", + "commands.flightassistant.flight_plan_not_found": "There is no plan with that name", + "commands.flightassistant.flight_plan_not_supported": "This plan cannot be loaded in the current version of Minecraft", + "commands.flightassistant.flight_plan_load_error": "There was an error loading this plan", "commands.flightassistant.flight_plan_empty": "The flight plan is empty!", "commands.flightassistant.total_waypoints": "There are %s waypoints in total", "commands.flightassistant.waypoint_info": "Index %s: | X: %s | Z: %s | Altitude: %s | Speed %s", diff --git a/src/main/resources/assets/flightassistant/lang/ru_ru.json b/src/main/resources/assets/flightassistant/lang/ru_ru.json index 21f58af0..b00cef54 100644 --- a/src/main/resources/assets/flightassistant/lang/ru_ru.json +++ b/src/main/resources/assets/flightassistant/lang/ru_ru.json @@ -87,7 +87,6 @@ "config.flightassistant.computers.chunk_state.protection": "Защита при незагружённом чанке", "commands.flightassistant.no_such_waypoint": "На указанной позиции нет точки назначения", - "commands.flightassistant.no_such_plan": "Не найден план полёта с таким именем", "commands.flightassistant.nothing_to_clear": "Нет точек назначения для очищения", "commands.flightassistant.first_wp_cannot_be_landing": "Первая точка назначения не может быть посадочной", "commands.flightassistant.cannot_execute_from_landing": "План полёта не может быть выполнен, начиная с посадочной точки назначения", @@ -99,6 +98,9 @@ "commands.flightassistant.flight_plan_executed": "Выполнен план полёта, начиная с точки назначения на позиции %s (всего %s точек назначения)", "commands.flightassistant.flight_plan_saved": "Сохранён план полёта с %s точками назначения как \"%s\"", "commands.flightassistant.flight_plan_loaded": "Загружен план полёта с %s точками назначения из \"%s\"", + "commands.flightassistant.flight_plan_not_found": "Не найден план полёта с таким именем", + "commands.flightassistant.flight_plan_not_supported": "Этот план полёта не может быть загружен на текущей версии Minecraft", + "commands.flightassistant.flight_plan_load_error": "Произошла ошибка при загрузке этого плана полёта", "commands.flightassistant.flight_plan_empty": "План полёта пуст!", "commands.flightassistant.total_waypoints": "Всего %s точек назначения", "commands.flightassistant.waypoint_info": "Позиция: %s | X: %s | Z: %s | Высота: %s | Скорость %s",