Skip to content

Commit

Permalink
fix rocket synchronization
Browse files Browse the repository at this point in the history
  • Loading branch information
screret committed Aug 26, 2024
1 parent 626707d commit 7b8a9a8
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package argent_matter.gcyr.client.renderer.entity;

import argent_matter.gcyr.common.data.GCYRNetworking;
import argent_matter.gcyr.common.entity.RocketEntity;
import argent_matter.gcyr.common.networking.c2s.PacketRequestRocketBlocks;
import argent_matter.gcyr.util.PosWithState;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.MethodsReturnNonnullByDefault;
Expand Down Expand Up @@ -35,12 +33,6 @@ public ResourceLocation getTextureLocation(RocketEntity entity) {

@Override
public void render(RocketEntity entity, float entityYaw, float partialTick, PoseStack poseStack, MultiBufferSource buffer, int packedLight) {
if (entity.getBlocks().isEmpty()) {
GCYRNetworking.NETWORK.sendToServer(new PacketRequestRocketBlocks(entity.getId()));
super.render(entity, entityYaw, partialTick, poseStack, buffer, packedLight);
return;
}

poseStack.pushPose();

// render blocks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
public class GCYREntities {
public static EntityEntry<RocketEntity> ROCKET = REGISTRATE.entity("rocket", RocketEntity::new, MobCategory.MISC)
.renderer(() -> RocketEntityRenderer::new)
.properties(b -> b.fireImmune().clientTrackingRange(8).updateInterval(3).setShouldReceiveVelocityUpdates(true).sized(1, 1))
.properties(b -> b.fireImmune()
.clientTrackingRange(8)
.updateInterval(3)
.setShouldReceiveVelocityUpdates(true)
.sized(1, 1))
.register();

public static void init() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public static void init() {
NETWORK.registerC2S(PacketRequestPlanetData.class);
NETWORK.registerC2S(PacketSendSelectedDimension.class);
NETWORK.registerC2S(PacketCreateSpaceStation.class);
NETWORK.registerC2S(PacketRequestRocketBlocks.class);

NETWORK.registerS2C(PacketReturnPlanetData.class);
NETWORK.registerS2C(PacketSyncDysonSphereStatus.class);
Expand Down
58 changes: 52 additions & 6 deletions src/main/java/argent_matter/gcyr/common/entity/RocketEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import it.unimi.dsi.fastutil.objects.Object2BooleanOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import lombok.Getter;
import net.minecraft.ChatFormatting;
import net.minecraft.MethodsReturnNonnullByDefault;
import net.minecraft.core.BlockPos;
Expand All @@ -60,10 +61,10 @@
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.NbtUtils;
import net.minecraft.nbt.Tag;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener;
import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.network.syncher.EntityDataSerializers;
import net.minecraft.network.syncher.SynchedEntityData;
Expand All @@ -73,6 +74,7 @@
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundSource;
import net.minecraft.tags.FluidTags;
import net.minecraft.util.Mth;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.damagesource.DamageSource;
Expand All @@ -90,14 +92,16 @@
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.entity.IEntityAdditionalSpawnData;
import net.minecraftforge.network.NetworkHooks;

import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.*;

@MethodsReturnNonnullByDefault
@ParametersAreNonnullByDefault
public class RocketEntity extends Entity implements HasCustomInventoryScreen, IUIHolder, PlayerRideable /*, IManaged, IAutoPersistEntity*/ {
public class RocketEntity extends Entity implements HasCustomInventoryScreen, IUIHolder, PlayerRideable, IEntityAdditionalSpawnData /*, IManaged, IAutoPersistEntity*/ {

public static final Object2BooleanMap<Fluid> FUEL_CACHE = new Object2BooleanOpenHashMap<>();

Expand Down Expand Up @@ -133,6 +137,10 @@ public class RocketEntity extends Entity implements HasCustomInventoryScreen, IU

private final Set<BlockPos> thrusterPositions = new HashSet<>();

@Getter
private ThreadLocal<Boolean> hasRequestedBlockSync = ThreadLocal.withInitial(() -> false);


public RocketEntity(EntityType<?> entityType, Level level) {
super(entityType, level);
this.configSlot = new ItemStackTransfer(1);
Expand Down Expand Up @@ -236,7 +244,7 @@ public InteractionResult interact(Player player, InteractionHand hand) {
}

player.startRiding(this);
return InteractionResult.CONSUME;
return result;
}

return result;
Expand Down Expand Up @@ -466,7 +474,28 @@ public void setDeltaMovement(Vec3 vec) {
public void fall() {
if (this.isNoGravity()) return;
Vec3 delta = this.getDeltaMovement();
this.setDeltaMovement(delta.add(0, -LivingEntity.DEFAULT_BASE_GRAVITY, 0));

// drag
float xRot = this.getXRot() * (float) (Math.PI / 180.0);
Vec3 lookAngle = this.getLookAngle();
double hyp = Math.sqrt(lookAngle.x * lookAngle.x + lookAngle.z * lookAngle.z);
double magnitude = lookAngle.length();
double drag = Math.cos(xRot);
drag = drag * drag * Math.min(1.0, magnitude / 0.4);
delta = delta.add(0, LivingEntity.DEFAULT_BASE_GRAVITY * (-1.0 + drag * 0.75), 0);

if (delta.y < 0.0 && hyp > 0.0) {
double d6 = delta.y * -0.1 * drag;
delta = delta.add(0.0, d6, 0.0);
}

if (xRot < 0.0F && hyp > 0.0) {
double d10 = magnitude * (double)(-Mth.sin(xRot)) * 0.04;
delta = delta.add(0.0, d10 * 3.2, 0.0);
}

this.setDeltaMovement(delta.multiply(0.0, 0.98, 0.0));

// braking
if (getControllingPassenger() != null && ((LivingEntityAccessor)getControllingPassenger()).isJumping() && consumeFuel()) {
this.setDeltaMovement(delta.x, Math.min(delta.y + 0.05, -0.05), delta.z);
Expand Down Expand Up @@ -838,7 +867,7 @@ public void addBlock(PosWithState state) {

blocks.add(state);

this.entityData.set(POSITIONED_STATES, blocks, true);
this.setBlocks(blocks);
BlockPos pos = state.pos();
BlockPos size = this.entityData.get(SIZE);
this.entityData.set(SIZE, new BlockPos(
Expand Down Expand Up @@ -883,6 +912,10 @@ public List<PosWithState> getBlocks() {
return this.entityData.get(POSITIONED_STATES);
}

public void setBlocks(List<PosWithState> blocks) {
this.entityData.set(POSITIONED_STATES, blocks, true);
}

public void addSeatPos(BlockPos pos) {
List<BlockPos> seats = this.entityData.get(SEAT_POSITIONS);
seats.add(pos);
Expand Down Expand Up @@ -998,13 +1031,26 @@ protected void addAdditionalSaveData(CompoundTag compound) {
public void onSyncedDataUpdated(EntityDataAccessor<?> key) {
if (POSITIONED_STATES.equals(key) || SIZE.equals(key)) {
this.setBoundingBox(makeBoundingBox());
hasRequestedBlockSync.set(false);
}
super.onSyncedDataUpdated(key);
}

@Override
public Packet<ClientGamePacketListener> getAddEntityPacket() {
return new ClientboundAddEntityPacket(this);
return NetworkHooks.getEntitySpawningPacket(this);
}

@Override
public void writeSpawnData(FriendlyByteBuf buf) {
GCYREntityDataSerializers.POSITIONED_BLOCK_STATE_LIST.write(buf, getBlocks());
GCYREntityDataSerializers.BLOCK_POS_LIST.write(buf, getSeatPositions());
}

@Override
public void readSpawnData(FriendlyByteBuf buf) {
setBlocks(GCYREntityDataSerializers.POSITIONED_BLOCK_STATE_LIST.read(buf));
this.entityData.set(SEAT_POSITIONS, GCYREntityDataSerializers.BLOCK_POS_LIST.read(buf));
}

public static void setEntityRotation(Entity vehicle, float rotation) {
Expand Down

This file was deleted.

0 comments on commit 7b8a9a8

Please sign in to comment.