added gravity

This commit is contained in:
TechnoDraconic
2026-02-05 00:44:15 -08:00
parent 3b5d10f414
commit fa41966de4
9 changed files with 186 additions and 2 deletions

View File

@@ -0,0 +1,42 @@
package net.xevianlight.aphelion.network;
import net.minecraft.world.entity.player.Player;
import net.xevianlight.aphelion.core.saveddata.types.GravityData;
import net.xevianlight.aphelion.systems.GravityService;
import net.xevianlight.aphelion.systems.OxygenService;
/// Read-only player state object; updated by a server packet every so often
public record ClientPlayerState(boolean oxygen, float gravity, float temperature) {
// Default player state
private static ClientPlayerState localData = new ClientPlayerState(true, GravityData.DEFAULT_GRAVITY * 0.5f, 50f);
public static void updateState(ClientPlayerState newData) {
onStateUpdate(localData, newData);
localData = newData;
}
public static ClientPlayerState getServerStateOf(Player player) {
return new ClientPlayerState(OxygenService.hasOxygen(player), GravityService.getGravityAccel(player), 50f);
}
/// For things like playing SFX, VFX, etc. etc.
public static void onStateUpdate(ClientPlayerState oldData, ClientPlayerState newData) {
// TODO: add sfx
if (!oldData.oxygen() && newData.oxygen()) {
// On oxygen gained
}
if (oldData.oxygen() && !newData.oxygen()) {
// On oxygen removed
}
if (newData.gravity() - 0.25f > oldData.gravity()) {
// On gravity increased by > 0.25
}
if (oldData.gravity() - 0.25f > newData.gravity()) {
// On gravity decreased by > 0.25
}
}
public static ClientPlayerState getLocalData() {
return localData;
}
}

View File

@@ -0,0 +1,11 @@
package net.xevianlight.aphelion.network;
import net.neoforged.neoforge.network.handling.IPayloadContext;
import net.xevianlight.aphelion.network.packet.ClientPlayerStateUpdatePacket;
public class ClientPlayerStateUpdateHandler {
public static void handleDataOnMain(ClientPlayerStateUpdatePacket packet, IPayloadContext context) {
context.enqueueWork(() -> ClientPlayerState.updateState(new ClientPlayerState(packet.oxygen(), packet.gravity(), packet.temp())));
}
}

View File

@@ -2,12 +2,16 @@ package net.xevianlight.aphelion.network;
import net.minecraft.client.Minecraft;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.neoforge.client.event.ClientTickEvent;
import net.neoforged.neoforge.event.tick.ServerTickEvent;
import net.neoforged.neoforge.network.PacketDistributor;
import net.xevianlight.aphelion.Aphelion;
import net.xevianlight.aphelion.entites.vehicles.RocketEntity;
import net.xevianlight.aphelion.network.packet.ClientPlayerStateUpdatePacket;
import net.xevianlight.aphelion.network.packet.RocketLaunchPayload;
import net.xevianlight.aphelion.client.AphelionClient;
@@ -27,4 +31,17 @@ public final class KeyNetwork {
PacketDistributor.sendToServer(new RocketLaunchPayload(rocket.getId()));
}
}
@SubscribeEvent
public static void onServerTick(ServerTickEvent.Post event) {
int FREQ = 4;
for (ServerPlayer p : event.getServer().getPlayerList().getPlayers()) {
if (p.tickCount % FREQ == 0) {
ClientPlayerState state = ClientPlayerState.getServerStateOf(p);
PacketDistributor.sendToPlayer(p, new ClientPlayerStateUpdatePacket(state.oxygen(), state.gravity(), state.temperature()));
}
}
}
}

View File

@@ -0,0 +1,29 @@
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 ClientPlayerStateUpdatePacket(boolean oxygen, float gravity, float temp) implements CustomPacketPayload {
public static final CustomPacketPayload.Type<ClientPlayerStateUpdatePacket> TYPE = new CustomPacketPayload.Type<>(ResourceLocation.fromNamespaceAndPath(Aphelion.MOD_ID, "player_state_update"));
public static final StreamCodec<ByteBuf, ClientPlayerStateUpdatePacket> STREAM_CODEC = StreamCodec.composite(
ByteBufCodecs.BOOL,
ClientPlayerStateUpdatePacket::oxygen,
ByteBufCodecs.FLOAT,
ClientPlayerStateUpdatePacket::gravity,
ByteBufCodecs.FLOAT,
ClientPlayerStateUpdatePacket::temp,
ClientPlayerStateUpdatePacket::new
);
@Override
public CustomPacketPayload.Type<? extends CustomPacketPayload> type() {
return TYPE;
}
}