Server side partition syncing and packets

This commit is contained in:
XevianLight
2026-01-17 13:48:58 -07:00
parent 79f3e2562e
commit 28639dae81
16 changed files with 666 additions and 11 deletions

View File

@@ -0,0 +1,62 @@
package net.xevianlight.aphelion.network;
import net.minecraft.server.level.ServerPlayer;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.neoforge.event.entity.player.PlayerEvent;
import net.neoforged.neoforge.event.tick.ServerTickEvent;
import net.neoforged.neoforge.network.PacketDistributor;
import net.xevianlight.aphelion.Aphelion;
import net.xevianlight.aphelion.core.space.SpacePartitionSavedData;
import net.xevianlight.aphelion.network.packet.PartitionData;
import net.xevianlight.aphelion.util.SpacePartitionHelper;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
@EventBusSubscriber(modid = Aphelion.MOD_ID)
public final class PartitionSync {
// send once right after join (safe: delayed to next server tick)
private static final Set<UUID> PENDING_JOIN_SEND = new HashSet<>();
@SubscribeEvent
public static void onLogin(PlayerEvent.PlayerLoggedInEvent e) {
if (e.getEntity() instanceof ServerPlayer sp) {
PENDING_JOIN_SEND.add(sp.getUUID());
}
}
private static final java.util.Map<UUID, PartitionData> LAST_SENT = new java.util.HashMap<>();
@SubscribeEvent
public static void onServerTick(ServerTickEvent.Post e) {
var server = e.getServer();
// Aphelion.LOGGER.info("WORKS!!!");
for (ServerPlayer sp : server.getPlayerList().getPlayers()) {
PartitionData now = computePartitionFor(sp); // your logic
PartitionData prev = LAST_SENT.get(sp.getUUID());
if (prev == null || !prev.equals(now)) {
PacketDistributor.sendToPlayer(sp, now);
LAST_SENT.put(sp.getUUID(), now);
}
}
}
private static PartitionData computePartitionFor(ServerPlayer sp) {
// Example: convert player position to partition coords
int px = (int)Math.floor(sp.getX() / SpacePartitionHelper.SIZE);
int pz = (int)Math.floor(sp.getZ() / SpacePartitionHelper.SIZE);
var orbit = SpacePartitionSavedData.get(sp.serverLevel()).getOrbitForPartition(px, pz);
String orbitId = (orbit != null) ? orbit.toString() : "aphelion:orbit/default";
return new PartitionData(orbitId);
}
}

View File

@@ -0,0 +1,15 @@
package net.xevianlight.aphelion.network;
import net.neoforged.neoforge.network.handling.IPayloadContext;
import net.xevianlight.aphelion.Aphelion;
import net.xevianlight.aphelion.client.PartitionClientState;
import net.xevianlight.aphelion.network.packet.PartitionData;
// Handle packets TO the client FROM the server
public class ServerPayloadHandler {
public static void handleDataOnMain(PartitionData data, IPayloadContext context) {
PartitionClientState.set(data);
Aphelion.LOGGER.info("Partition packet received! id={}", data.id());
}
}

View File

@@ -0,0 +1,24 @@
package net.xevianlight.aphelion.network.packet;
import io.netty.buffer.ByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;
import net.xevianlight.aphelion.Aphelion;
public record PartitionData (String id) implements CustomPacketPayload {
public static final Type<PartitionData> TYPE = new CustomPacketPayload.Type<>(ResourceLocation.fromNamespaceAndPath(Aphelion.MOD_ID, "partition_data"));
public static final StreamCodec<ByteBuf, PartitionData> STREAM_CODEC = StreamCodec.composite(
ByteBufCodecs.STRING_UTF8,
PartitionData::id,
PartitionData::new
);
@Override
public Type<? extends CustomPacketPayload> type() {
return TYPE;
}
}