mirror of
https://github.com/XevianLight/Aphelion.git
synced 2026-05-11 10:00:54 +01:00
Rocket landing pad block. Added comments. Adjusted rocket values
This commit is contained in:
@@ -22,6 +22,8 @@ public final class KeyNetwork {
|
||||
|
||||
// consumeClick makes it fire once per press, not every tick held
|
||||
if (AphelionClient.ROCKET_LAUNCH_KEY.consumeClick() && mc.player.getVehicle() instanceof RocketEntity rocket) {
|
||||
|
||||
// Send a packet to the server telling it to try launching the rocket matching this id. The packet only contains the rocketId as an integer.
|
||||
PacketDistributor.sendToServer(new RocketLaunchPayload(rocket.getId()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,12 +3,13 @@ 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;
|
||||
import net.xevianlight.aphelion.network.packet.PartitionPayload;
|
||||
|
||||
// Handle packets TO the client FROM the server
|
||||
public class ServerPayloadHandler {
|
||||
public class PartitionPayloadHandler {
|
||||
|
||||
public static void handleDataOnMain(PartitionData data, IPayloadContext context) {
|
||||
public static void handleDataOnMain(PartitionPayload data, IPayloadContext context) {
|
||||
// Set our local partition state to the packet we just received.
|
||||
PartitionClientState.set(data);
|
||||
Aphelion.LOGGER.info("Partition packet received! id={}", data.id());
|
||||
}
|
||||
@@ -9,28 +9,18 @@ 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.network.packet.PartitionPayload;
|
||||
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<>();
|
||||
// Stora all packets we send to all players in a map so we can look it up later
|
||||
private static final java.util.Map<UUID, PartitionPayload> LAST_SENT = new java.util.HashMap<>();
|
||||
|
||||
@SubscribeEvent
|
||||
public static void onServerTick(ServerTickEvent.Post e) {
|
||||
@@ -39,24 +29,29 @@ public final class PartitionSync {
|
||||
// Aphelion.LOGGER.info("WORKS!!!");
|
||||
|
||||
for (ServerPlayer sp : server.getPlayerList().getPlayers()) {
|
||||
PartitionData now = computePartitionFor(sp); // your logic
|
||||
PartitionData prev = LAST_SENT.get(sp.getUUID());
|
||||
|
||||
// Prepare a new packet and compare it with the last one we sent the player
|
||||
PartitionPayload now = computePartitionFor(sp);
|
||||
PartitionPayload prev = LAST_SENT.get(sp.getUUID());
|
||||
|
||||
// If it is different, send them the new one
|
||||
if (prev == null || !prev.equals(now)) {
|
||||
PacketDistributor.sendToPlayer(sp, now);
|
||||
// Store this packet for later
|
||||
LAST_SENT.put(sp.getUUID(), now);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static PartitionData computePartitionFor(ServerPlayer sp) {
|
||||
// Example: convert player position to partition coords
|
||||
private static PartitionPayload computePartitionFor(ServerPlayer sp) {
|
||||
// convert player position to partition coords
|
||||
int px = (int)Math.floor(sp.getX() / SpacePartitionHelper.SIZE);
|
||||
int pz = (int)Math.floor(sp.getZ() / SpacePartitionHelper.SIZE);
|
||||
|
||||
// Get the orbit for the partition the player is in and create a packet for it
|
||||
var orbit = SpacePartitionSavedData.get(sp.serverLevel()).getOrbitForPartition(px, pz);
|
||||
String orbitId = (orbit != null) ? orbit.toString() : "aphelion:orbit/default";
|
||||
|
||||
return new PartitionData(orbitId);
|
||||
return new PartitionPayload(orbitId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,14 +7,14 @@ 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 record PartitionPayload(String id) implements CustomPacketPayload {
|
||||
public static final Type<PartitionPayload> TYPE = new CustomPacketPayload.Type<>(ResourceLocation.fromNamespaceAndPath(Aphelion.MOD_ID, "partition_data"));
|
||||
|
||||
public static final StreamCodec<ByteBuf, PartitionData> STREAM_CODEC = StreamCodec.composite(
|
||||
public static final StreamCodec<ByteBuf, PartitionPayload> STREAM_CODEC = StreamCodec.composite(
|
||||
ByteBufCodecs.STRING_UTF8,
|
||||
PartitionData::id,
|
||||
PartitionPayload::id,
|
||||
|
||||
PartitionData::new
|
||||
PartitionPayload::new
|
||||
);
|
||||
|
||||
@Override
|
||||
Reference in New Issue
Block a user