Skip to content

Commit

Permalink
Split NBT logic into appropriate classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Octol1ttle committed Mar 8, 2024
1 parent 8d966b7 commit 6f19448
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import ru.octol1ttle.flightassistant.HudRenderer;
import ru.octol1ttle.flightassistant.computers.ComputerHost;
import ru.octol1ttle.flightassistant.computers.navigation.Waypoint;
import ru.octol1ttle.flightassistant.config.FlightPlanNbt;
import ru.octol1ttle.flightassistant.computers.navigation.FlightPlanNbt;

public class LoadFlightPlanCommand {
public static final SimpleCommandExceptionType NO_SUCH_PLAN = new SimpleCommandExceptionType(Text.translatable("commands.flightassistant.no_such_plan"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import net.minecraft.text.Text;
import ru.octol1ttle.flightassistant.HudRenderer;
import ru.octol1ttle.flightassistant.computers.ComputerHost;
import ru.octol1ttle.flightassistant.config.FlightPlanNbt;
import ru.octol1ttle.flightassistant.computers.navigation.FlightPlanNbt;

public class SaveFlightPlanCommand {
public static int execute(CommandContext<FabricClientCommandSource> context) throws CommandSyntaxException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package ru.octol1ttle.flightassistant.computers.navigation;

import dev.isxander.yacl3.platform.YACLPlatform;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.nbt.InvalidNbtException;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.nbt.NbtIo;
import net.minecraft.nbt.NbtList;
import org.jetbrains.annotations.Nullable;
import ru.octol1ttle.flightassistant.FlightAssistant;

public class FlightPlanNbt {
private static final Path PLAN_PATH = YACLPlatform.getConfigDir().resolve("%s/plans/".formatted(FlightAssistant.MODID));

public static void write(List<Waypoint> plan, String name) {
NbtCompound planNbt = new NbtCompound();

NbtList listNbt = new NbtList();
for (Waypoint waypoint : plan) {
listNbt.add(waypoint.writeToNbt(new NbtCompound()));
}

planNbt.put("Waypoints", listNbt);

Path path = PLAN_PATH.resolve("%s.dat".formatted(name));
try {
Files.createDirectories(PLAN_PATH);
NbtIo.write(planNbt, path);
} catch (IOException e) {
FlightAssistant.LOGGER.error("IO error detected during flight plan serialization to: %s".formatted(path.toAbsolutePath().toString()), e);
}
}

public static @Nullable List<Waypoint> read(String name) {
List<Waypoint> loaded = new ArrayList<>();

Path path = PLAN_PATH.resolve("%s.dat".formatted(name));
try {
NbtCompound compound = NbtIo.read(path);
if (compound == null) {
return null;
}

NbtList list = compound.getList("Waypoints", NbtElement.COMPOUND_TYPE);
for (NbtElement waypointElement : list) {
if (waypointElement.getNbtType() != NbtCompound.TYPE) {
throw new InvalidNbtException("List item is not NbtCompound");
}

NbtCompound waypointNbt = (NbtCompound) waypointElement;
loaded.add(Waypoint.readFromNbt(waypointNbt));
}
} catch (InvalidNbtException e) {
FlightAssistant.LOGGER.error("Invalid NBT detected during flight plan deserialization from: %s".formatted(path.toAbsolutePath().toString()));
} catch (IOException e) {
FlightAssistant.LOGGER.error("IO error detected during flight plan deserialization from: %s".formatted(path.toAbsolutePath().toString()));
}

return loaded;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package ru.octol1ttle.flightassistant.computers.navigation;

import net.minecraft.nbt.InvalidNbtException;
import net.minecraft.nbt.NbtCompound;

public record LandingMinimums(AltitudeType type, int altitude) {
public enum AltitudeType {
ABSOLUTE("Absolute"),
Expand All @@ -9,5 +12,22 @@ public enum AltitudeType {
AltitudeType(String nbtName) {
this.nbtName = nbtName;
}

static AltitudeType fromNbtName(String nbtName) throws InvalidNbtException {
for (AltitudeType type : AltitudeType.values()) {
if (type.nbtName.equals(nbtName)) {
return type;
}
}

throw new InvalidNbtException("Unknown altitude type: %s".formatted(nbtName));
}
}

public static LandingMinimums readFromNbt(NbtCompound compound) {
return new LandingMinimums(
AltitudeType.fromNbtName(compound.getString("AltitudeType")),
compound.getInt("Altitude")
);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package ru.octol1ttle.flightassistant.computers.navigation;

import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.text.Text;
import org.jetbrains.annotations.Nullable;
import org.joml.Vector2d;
import ru.octol1ttle.flightassistant.HudComponent;

public class LandingWaypoint extends Waypoint {
public final LandingMinimums minimums;
public final @Nullable LandingMinimums minimums;

public LandingWaypoint(Vector2d targetPosition, @Nullable LandingMinimums minimums) {
super(targetPosition, null, null);
Expand Down Expand Up @@ -38,4 +40,31 @@ public Text formatMinimums() {
public void setTargetAltitude(Integer targetAltitude) {
this.targetAltitude = targetAltitude;
}

@Override
public NbtCompound writeToNbt(NbtCompound compound) {
compound.putBoolean("IsLanding", true);

compound.putDouble("TargetX", targetPosition().x);
compound.putDouble("TargetZ", targetPosition().y);
if (minimums != null) {
NbtCompound minimumsNbt = new NbtCompound();
minimumsNbt.putString("AltitudeType", minimums.type().nbtName);
minimumsNbt.putInt("Altitude", minimums.altitude());

compound.put("Minimums", minimumsNbt);
}

return compound;
}

public static LandingWaypoint readFromNbt(NbtCompound compound) {
Vector2d targetPosition = new Vector2d(compound.getDouble("TargetX"), compound.getDouble("TargetZ"));
LandingMinimums minimums = null;
if (compound.contains("Minimums", NbtElement.COMPOUND_TYPE)) {
minimums = LandingMinimums.readFromNbt(compound.getCompound("Minimums"));
}

return new LandingWaypoint(targetPosition, minimums);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package ru.octol1ttle.flightassistant.computers.navigation;

import net.minecraft.nbt.InvalidNbtException;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import org.jetbrains.annotations.Nullable;
import org.joml.Vector2d;

Expand All @@ -25,4 +28,37 @@ public Integer targetAltitude() {
public Integer targetSpeed() {
return targetSpeed;
}

public NbtCompound writeToNbt(NbtCompound compound) {
compound.putBoolean("IsLanding", false);

compound.putDouble("TargetX", targetPosition().x);
compound.putDouble("TargetZ", targetPosition().y);
if (targetAltitude() != null) {
compound.putInt("TargetAltitude", targetAltitude());
}
if (targetSpeed() != null) {
compound.putInt("TargetSpeed", targetSpeed());
}

return compound;
}

public static Waypoint readFromNbt(NbtCompound compound) throws InvalidNbtException {
if (compound.getBoolean("IsLanding")) {
return LandingWaypoint.readFromNbt(compound);
}

Vector2d targetPosition = new Vector2d(compound.getDouble("TargetX"), compound.getDouble("TargetZ"));
Integer targetAltitude = null;
Integer targetSpeed = null;
if (compound.contains("TargetAltitude", NbtElement.INT_TYPE)) {
targetAltitude = compound.getInt("TargetAltitude");
}
if (compound.contains("TargetSpeed", NbtElement.INT_TYPE)) {
targetSpeed = compound.getInt("TargetSpeed");
}

return new Waypoint(targetPosition, targetAltitude, targetSpeed);
}
}
113 changes: 0 additions & 113 deletions src/main/java/ru/octol1ttle/flightassistant/config/FlightPlanNbt.java

This file was deleted.

0 comments on commit 6f19448

Please sign in to comment.