Skip to content

Commit

Permalink
change: improve display of flight plan load errors
Browse files Browse the repository at this point in the history
Signed-off-by: Octol1ttle <[email protected]>
  • Loading branch information
Octol1ttle committed Mar 23, 2024
1 parent 631185b commit cc749f6
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<FabricClientCommandSource> context) throws CommandSyntaxException {
ComputerHost host = HudRenderer.getHost();
String name = StringArgumentType.getString(context, "name");
List<Waypoint> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Waypoint> loaded;
private final LoadResultType type;

public FlightPlanLoadResult(@Nullable List<Waypoint> loaded, @NotNull LoadResultType type) {
this.loaded = loaded;
this.type = type;
}

public List<Waypoint> 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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -43,28 +43,33 @@ public static void save(List<Waypoint> plan, String name) {
}
}

public static @Nullable List<Waypoint> load(String name) {
public static @NotNull FlightPlanLoadResult load(String name) {
List<Waypoint> 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<ISerializableObject> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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")
);
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/assets/flightassistant/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/assets/flightassistant/lang/ru_ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "План полёта не может быть выполнен, начиная с посадочной точки назначения",
Expand All @@ -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",
Expand Down

0 comments on commit cc749f6

Please sign in to comment.