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(
|
2026-09-13 22:18:00 +03:00
|
|
|
instance -> {
|
|
|
|
|
var a = UUID_CODEC.fieldOf("a").forGetter(Link::a);
|
|
|
|
|
var endA = Codec.intRange(0, 1).fieldOf("end_a").forGetter(Link::endA);
|
|
|
|
|
var b = UUID_CODEC.fieldOf("b").forGetter(Link::b);
|
|
|
|
|
var endB = Codec.intRange(0, 1).fieldOf("end_b").forGetter(Link::endB);
|
|
|
|
|
var item = Codec.STRING.fieldOf("item").forGetter(Link::item);
|
|
|
|
|
var paid = Codec.BOOL.fieldOf("paid").forGetter(Link::paid);
|
|
|
|
|
var length =
|
|
|
|
|
Codec.doubleRange(1, 4)
|
|
|
|
|
.fieldOf("length")
|
|
|
|
|
.forGetter(Link::length);
|
|
|
|
|
var fields = instance.group(a, endA, b, endB, item, paid, length);
|
|
|
|
|
return fields.apply(instance, Link::new);
|
|
|
|
|
});
|
2026-09-13 21:55:44 +03:00
|
|
|
|
|
|
|
|
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<>();
|
2026-09-13 23:17:48 +03:00
|
|
|
private final Map<UUID, Set<UUID>> components = new HashMap<>();
|
2026-09-13 21:55:44 +03:00
|
|
|
|
|
|
|
|
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) {
|
2026-09-13 23:17:48 +03:00
|
|
|
for (Link link : links) {
|
|
|
|
|
if (link.touches(cart) && link.end(cart) == end) return link;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
2026-09-13 21:55:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void add(Link link) {
|
|
|
|
|
links.add(link);
|
2026-09-13 23:17:48 +03:00
|
|
|
components.clear();
|
2026-09-13 21:55:44 +03:00
|
|
|
setDirty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean remove(Link link) {
|
|
|
|
|
boolean removed = links.remove(link);
|
2026-09-13 23:17:48 +03:00
|
|
|
if (removed) {
|
|
|
|
|
components.clear();
|
|
|
|
|
setDirty();
|
|
|
|
|
}
|
2026-09-13 21:55:44 +03:00
|
|
|
return removed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Set<UUID> component(UUID root) {
|
2026-09-13 23:17:48 +03:00
|
|
|
var cached = components.get(root);
|
|
|
|
|
if (cached != null) return cached;
|
2026-09-13 21:55:44 +03:00
|
|
|
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 23:17:48 +03:00
|
|
|
var component = Set.copyOf(seen);
|
|
|
|
|
// Collision checks reuse topology until a chain changes; ordinary carts are not cached.
|
|
|
|
|
if (component.size() > 1) {
|
|
|
|
|
for (UUID cart : component) components.put(cart, component);
|
|
|
|
|
}
|
|
|
|
|
return component;
|
2026-09-13 09:38:31 +03:00
|
|
|
}
|
|
|
|
|
}
|