Skip to content

Commit 5b2aab7

Browse files
committed
Fix entity spawn packet and tile item data
1 parent 6aab695 commit 5b2aab7

File tree

3 files changed

+50
-24
lines changed

3 files changed

+50
-24
lines changed

src/main/java/com/brandon3055/brandonscore/blocks/BlockBCore.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@
4343
* This is the base block class form all blocks.
4444
*/
4545
public class BlockBCore extends Block implements IBCoreBlock {
46-
//Cant register my own data without breaking server side compat, and cant use BLOCK_ENTITY_DATA, because vanilla would try to handle that, so ENTITY_DATA! Probably fine... As long as not random mods step in and do random shit to any item that happens to have ENTITY_DATA...
47-
public static final DataComponentType<CustomData> BC_TILE_DATA_TAG = DataComponents.ENTITY_DATA;
46+
public static final DataComponentType<CustomData> BC_TILE_DATA_TAG = DataComponents.CUSTOM_DATA;
4847
// public static final String BC_TILE_DATA_TAG = "bc_tile_data";
4948
public static final String BC_MANAGED_DATA_FLAG = "bc_managed_data"; //Seemed like as good a place as any to put this.
5049

src/main/java/com/brandon3055/brandonscore/network/BCoreNetwork.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import net.minecraft.network.protocol.Packet;
2121
import net.minecraft.resources.ResourceLocation;
2222
import net.minecraft.server.MinecraftServer;
23+
import net.minecraft.server.level.ServerEntity;
2324
import net.minecraft.server.level.ServerLevel;
2425
import net.minecraft.server.level.ServerPlayer;
2526
import net.minecraft.sounds.SoundEvent;
@@ -149,22 +150,25 @@ public static void sendParticle(Level level, ParticleOptions particleData, Vecto
149150
* @param entity The entity being spawned.
150151
* @return A packet to return in {@link Entity#getAddEntityPacket()}
151152
*/
152-
public static Packet<?> getEntitySpawnPacket(Entity entity, int data) {
153+
public static Packet<?> getEntitySpawnPacket(Entity entity, ServerEntity serverEntity, int ownerId) {
153154
PacketCustom packet = new PacketCustom(CHANNEL_NAME, C_SPAWN_ENTITY, entity.registryAccess());
154-
packet.writeRegistryId(BuiltInRegistries.ENTITY_TYPE, entity.getType());
155155
packet.writeInt(entity.getId());
156156
packet.writeUUID(entity.getUUID());
157-
packet.writeDouble(entity.getX());
158-
packet.writeDouble(entity.getY());
159-
packet.writeDouble(entity.getZ());
160-
packet.writeByte((byte) Mth.floor(entity.getXRot() * 256.0F / 360.0F));
161-
packet.writeByte((byte) Mth.floor(entity.getYRot() * 256.0F / 360.0F));
157+
158+
packet.writeDouble(serverEntity.getPositionBase().x());
159+
packet.writeDouble(serverEntity.getPositionBase().y());
160+
packet.writeDouble(serverEntity.getPositionBase().z());
161+
packet.writeByte((byte) Mth.floor(serverEntity.getLastSentXRot() * 256.0F / 360.0F));
162+
packet.writeByte((byte) Mth.floor(serverEntity.getLastSentYRot() * 256.0F / 360.0F));
162163
packet.writeByte((byte) (entity.getYHeadRot() * 256.0F / 360.0F));
164+
165+
packet.writeRegistryId(BuiltInRegistries.ENTITY_TYPE, entity.getType());
166+
packet.writeVarInt(ownerId);
167+
163168
Vec3 velocity = entity.getDeltaMovement();
164169
packet.writeFloat((float) velocity.x);
165170
packet.writeFloat((float) velocity.y);
166171
packet.writeFloat((float) velocity.z);
167-
packet.writeVarInt(data);
168172
return packet.toClientPacket();
169173
}
170174

src/main/java/com/brandon3055/brandonscore/network/ClientPacketHandler.java

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
import net.minecraft.core.BlockPos;
1414
import net.minecraft.core.particles.ParticleOptions;
1515
import net.minecraft.core.particles.ParticleTypes;
16+
import net.minecraft.core.registries.BuiltInRegistries;
1617
import net.minecraft.network.chat.MessageSignature;
1718
import net.minecraft.sounds.SoundEvent;
1819
import net.minecraft.sounds.SoundSource;
20+
import net.minecraft.util.Mth;
1921
import net.minecraft.world.entity.Entity;
2022
import net.minecraft.world.entity.EntityType;
2123
import net.minecraft.world.entity.projectile.Projectile;
@@ -86,43 +88,64 @@ private static void handlePlaySound(PacketCustom packet, Minecraft mc) {
8688
mc.level.playLocalSound(pos, sound, category, volume, pitch, distanceDelay);
8789
}
8890

91+
// PacketCustom packet = new PacketCustom(CHANNEL_NAME, C_SPAWN_ENTITY, entity.registryAccess());
92+
// packet.writeInt(entity.getId());
93+
// packet.writeUUID(entity.getUUID());
94+
//
95+
// packet.writeDouble(serverEntity.getPositionBase().x());
96+
// packet.writeDouble(serverEntity.getPositionBase().y());
97+
// packet.writeDouble(serverEntity.getPositionBase().z());
98+
// packet.writeByte((byte) Mth.floor(serverEntity.getLastSentXRot() * 256.0F / 360.0F));
99+
// packet.writeByte((byte) Mth.floor(serverEntity.getLastSentYRot() * 256.0F / 360.0F));
100+
// packet.writeByte((byte) (entity.getYHeadRot() * 256.0F / 360.0F));
101+
//
102+
// packet.writeRegistryId(BuiltInRegistries.ENTITY_TYPE, entity.getType());
103+
// packet.writeVarInt(ownerId);
104+
//
105+
// Vec3 velocity = entity.getDeltaMovement();
106+
// packet.writeFloat((float) velocity.x);
107+
// packet.writeFloat((float) velocity.y);
108+
// packet.writeFloat((float) velocity.z);
109+
89110
private static void handleEntitySpawn(PacketCustom packet, Minecraft mc) {
90111
if (mc.level == null) {
91112
return;
92113
}
93-
94-
EntityType<?> type = packet.readRegistryId();//ForgeRegistries.ENTITY_TYPES.byId(packet.readVarInt());
95114
int entityID = packet.readInt();
96115
UUID uuid = packet.readUUID();
116+
97117
double posX = packet.readDouble();
98118
double posY = packet.readDouble();
99119
double posZ = packet.readDouble();
100-
byte yaw = packet.readByte();
101-
byte pitch = packet.readByte();
102-
byte headYaw = packet.readByte();
120+
byte xRot = packet.readByte();
121+
byte yRot = packet.readByte();
122+
byte headYRot = packet.readByte();
123+
124+
EntityType<?> type = packet.readRegistryId();//ForgeRegistries.ENTITY_TYPES.byId(packet.readVarInt());
125+
int ownerId = packet.readVarInt();
103126
Vec3 velocity = new Vec3(packet.readFloat(), packet.readFloat(), packet.readFloat());
104-
int data = packet.readVarInt();
127+
105128
Entity entity = type.create(mc.level);
106129
if (entity == null) {
107130
return;
108131
}
109132

110-
//THis is a hack, but meh. Should work.
111-
if (entity instanceof Projectile projectile && data != 0) {
112-
Entity e = mc.level.getEntity(data);
133+
//This is a hack, but meh. Should work.
134+
if (entity instanceof Projectile projectile && ownerId != 0) {
135+
Entity e = mc.level.getEntity(ownerId);
113136
if (e != null) {
114137
projectile.setOwner(entity);
115138
}
116139
}
117140

118-
entity.setPosRaw(posX, posY, posZ);
119-
entity.absMoveTo(posX, posY, posZ, (pitch * 360) / 256.0F, (yaw * 360) / 256.0F);
120-
entity.setYHeadRot((headYaw * 360) / 256.0F);
121-
entity.setYBodyRot((headYaw * 360) / 256.0F);
141+
entity.setDeltaMovement(velocity);
142+
entity.syncPacketPositionCodec(posX, posY, posZ);
143+
entity.moveTo(posX, posY, posZ);
144+
entity.setXRot(xRot);
145+
entity.setYRot(yRot);
122146
entity.setId(entityID);
123147
entity.setUUID(uuid);
124148
mc.level.addEntity(entity);
125-
entity.lerpMotion(velocity.x, velocity.y, velocity.z);
126149
}
127150

128151
private static void handleEntityVelocity(PacketCustom packet, Minecraft mc) {

0 commit comments

Comments
 (0)