2026-09-13 09:38:31 +03:00
|
|
|
package online.astradev.content.train;
|
|
|
|
|
|
|
|
|
|
import com.mojang.serialization.Codec;
|
|
|
|
|
import com.mojang.serialization.codecs.RecordCodecBuilder;
|
2026-09-13 21:55:44 +03:00
|
|
|
|
2026-09-13 09:38:31 +03:00
|
|
|
import net.minecraft.resources.Identifier;
|
|
|
|
|
import net.minecraft.server.level.ServerLevel;
|
|
|
|
|
import net.minecraft.world.level.saveddata.*;
|
|
|
|
|
|
2026-09-13 21:55:44 +03:00
|
|
|
import java.util.*;
|
|
|
|
|
|
2026-09-13 09:38:31 +03:00
|
|
|
/** One authoritative record per chain, including unloaded carts. No duplicated refunds. */
|
|
|
|
|
public final class TrainData extends SavedData {
|
2026-09-13 21:55:44 +03:00
|
|
|
public record Link(
|
|
|
|
|
UUID a, int endA, UUID b, int endB, String item, boolean paid, double length) {
|
|
|
|
|
private static final Codec<UUID> UUID_CODEC =
|
|
|
|
|
Codec.STRING.xmap(UUID::fromString, UUID::toString);
|
|
|
|
|
static final Codec<Link> CODEC =
|
|
|
|
|
RecordCodecBuilder.create(
|
|
|
|
|
i ->
|
|
|
|
|
i.group(
|
|
|
|
|
UUID_CODEC.fieldOf("a").forGetter(Link::a),
|
|
|
|
|
Codec.intRange(0, 1)
|
|
|
|
|
.fieldOf("end_a")
|
|
|
|
|
.forGetter(Link::endA),
|
|
|
|
|
UUID_CODEC.fieldOf("b").forGetter(Link::b),
|
|
|
|
|
Codec.intRange(0, 1)
|
|
|
|
|
.fieldOf("end_b")
|
|
|
|
|
.forGetter(Link::endB),
|
|
|
|
|
Codec.STRING.fieldOf("item").forGetter(Link::item),
|
|
|
|
|
Codec.BOOL.fieldOf("paid").forGetter(Link::paid),
|
|
|
|
|
Codec.doubleRange(1, 4)
|
|
|
|
|
.fieldOf("length")
|
|
|
|
|
.forGetter(Link::length))
|
|
|
|
|
.apply(i, Link::new));
|
|
|
|
|
|
|
|
|
|
public boolean touches(UUID id) {
|
|
|
|
|
return a.equals(id) || b.equals(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public UUID other(UUID id) {
|
|
|
|
|
return a.equals(id) ? b : a;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int end(UUID id) {
|
|
|
|
|
return a.equals(id) ? endA : endB;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static final Codec<TrainData> CODEC =
|
|
|
|
|
Link.CODEC
|
|
|
|
|
.listOf()
|
|
|
|
|
.xmap(TrainData::new, d -> List.copyOf(d.links))
|
|
|
|
|
.fieldOf("links")
|
|
|
|
|
.codec();
|
|
|
|
|
private static final SavedDataType<TrainData> TYPE =
|
|
|
|
|
new SavedDataType<>(
|
|
|
|
|
Identifier.fromNamespaceAndPath("astracontent", "trains"),
|
|
|
|
|
TrainData::new,
|
|
|
|
|
CODEC);
|
|
|
|
|
private final List<Link> links = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
public TrainData() {}
|
|
|
|
|
|
|
|
|
|
private TrainData(List<Link> links) {
|
|
|
|
|
this.links.addAll(links);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static TrainData get(ServerLevel level) {
|
|
|
|
|
return level.getDataStorage().computeIfAbsent(TYPE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<Link> links() {
|
|
|
|
|
return List.copyOf(links);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Link at(UUID cart, int end) {
|
|
|
|
|
return links.stream()
|
|
|
|
|
.filter(l -> l.touches(cart) && l.end(cart) == end)
|
|
|
|
|
.findFirst()
|
|
|
|
|
.orElse(null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void add(Link link) {
|
|
|
|
|
links.add(link);
|
|
|
|
|
setDirty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean remove(Link link) {
|
|
|
|
|
boolean removed = links.remove(link);
|
|
|
|
|
if (removed) setDirty();
|
|
|
|
|
return removed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Set<UUID> component(UUID root) {
|
|
|
|
|
var seen = new HashSet<UUID>();
|
|
|
|
|
var pending = new ArrayDeque<UUID>();
|
|
|
|
|
pending.add(root);
|
|
|
|
|
while (!pending.isEmpty()) {
|
|
|
|
|
var id = pending.removeFirst();
|
|
|
|
|
if (!seen.add(id)) continue;
|
|
|
|
|
for (var link : links) if (link.touches(id)) pending.add(link.other(id));
|
|
|
|
|
}
|
2026-09-13 09:38:31 +03:00
|
|
|
return seen;
|
|
|
|
|
}
|
|
|
|
|
}
|