Skip to content

Commit e2fcbaf

Browse files
authored
Improve hologram emulation in 1.8->1.7 (#667)
* Fix metadata flags not saving cross packets syncing * Fix marker zombie no gravity offsets * Improve interaction by rewriting "extra" entity ids of holograms to normal hologram id
1 parent 9ae4614 commit e2fcbaf

3 files changed

Lines changed: 51 additions & 4 deletions

File tree

common/src/main/java/com/viaversion/viarewind/protocol/v1_8to1_7_6_10/data/VirtualHologramEntity.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.viaversion.viarewind.protocol.v1_7_6_10to1_7_2_5.packet.ClientboundPackets1_7_2_5;
2525
import com.viaversion.viarewind.protocol.v1_8to1_7_6_10.Protocol1_8To1_7_6_10;
2626
import com.viaversion.viarewind.protocol.v1_8to1_7_6_10.rewriter.EntityPacketRewriter1_8;
27+
import com.viaversion.viarewind.protocol.v1_8to1_7_6_10.storage.EntityTracker1_8;
2728
import com.viaversion.viaversion.api.connection.UserConnection;
2829
import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_8;
2930
import com.viaversion.viaversion.api.minecraft.entitydata.EntityData;
@@ -46,6 +47,8 @@ public class VirtualHologramEntity {
4647
private String name = null;
4748
private float yaw, pitch;
4849
private float headYaw;
50+
private byte flags = 0;
51+
private byte armorStandFlags = 0;
4952
private boolean small = false;
5053
private boolean marker = false;
5154
private boolean sneaking = false;
@@ -99,8 +102,6 @@ public void syncState(final EntityPacketRewriter1_8 entityRewriter, final List<E
99102
}
100103

101104
// Filter armor stand data to calculate emulation
102-
byte flags = 0;
103-
byte armorStandFlags = 0;
104105
for (EntityData entityData : entityDataTracker) {
105106
if (entityData.id() == 0 && entityData.dataType() == EntityDataTypes1_8.BYTE) {
106107
flags = ((Number) entityData.getValue()).byteValue();
@@ -167,6 +168,18 @@ private double getOffset() {
167168
} else {
168169
return baseOffset + (0.9875 * 2);
169170
}
171+
} else if (currentState == State.ZOMBIE_NO_GRAVITY) {
172+
// 1.7 ride stack offset sum: Squid (0.95 * 0.75) + Zombie (1.8 * 0.75) = 2.0625
173+
// By starting at -2.0625, we zero out the 1.7 passenger height so we can apply 1.8's offsets.
174+
double baseOffset = -2.0625;
175+
176+
if (marker) {
177+
// 1.8 Marker ArmorStand height is 0.0 -> mounted offset 0.0
178+
return baseOffset;
179+
} else {
180+
// 1.8 Normal ArmorStand height is 1.975 -> mounted offset 1.975 * 0.75 = 1.48125
181+
return baseOffset + 1.48125;
182+
}
170183
} else {
171184
return -0.4;
172185
}
@@ -291,6 +304,13 @@ public void sendSpawnPacket(final EntityPacketRewriter1_8 entityRewriter) {
291304
this.entityIds = entityIds;
292305
}
293306

307+
for (int extraId : entityIds) {
308+
if (extraId != entityId) {
309+
((EntityTracker1_8) user.getEntityTracker(Protocol1_8To1_7_6_10.class)).setExtraHologramId(entityId, extraId);
310+
}
311+
}
312+
313+
294314
sendEntityDataUpdate(entityRewriter);
295315
if (entityIds == null) {
296316
return;
@@ -326,6 +346,13 @@ public void deleteEntity() {
326346
if (entityIds == null) {
327347
return;
328348
}
349+
350+
for (int extraId : entityIds) {
351+
if (extraId != entityId) {
352+
((EntityTracker1_8) user.getEntityTracker(Protocol1_8To1_7_6_10.class)).removeExtraHologramId(extraId);
353+
}
354+
}
355+
329356
final PacketWrapper despawn = PacketWrapper.create(ClientboundPackets1_7_2_5.REMOVE_ENTITIES, user);
330357
despawn.write(Types.BYTE, (byte) entityIds.length);
331358
for (int id : entityIds) {

common/src/main/java/com/viaversion/viarewind/protocol/v1_8to1_7_6_10/rewriter/PlayerPacketRewriter1_8.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -532,13 +532,15 @@ public void register() {
532532
if (mode != 0) {
533533
return;
534534
}
535-
final int entityId = wrapper.get(Types.VAR_INT, 0);
536535
final EntityTracker1_8 tracker = wrapper.user().getEntityTracker(Protocol1_8To1_7_6_10.class);
536+
final int entityId = tracker.getHologramIdWithExtra(wrapper.get(Types.VAR_INT, 0));
537537
final PlayerSessionStorage position = wrapper.user().get(PlayerSessionStorage.class);
538538

539-
if (!tracker.getHolograms().containsKey(entityId)) {
539+
if (entityId == -1) {
540540
return;
541541
}
542+
543+
wrapper.set(Types.VAR_INT, 0, entityId);
542544
final AABB boundingBox = tracker.getHolograms().get(entityId).getBoundingBox();
543545

544546
Vector3d pos = new Vector3d(position.getPosX(), position.getPosY() + 1.8, position.getPosZ());

common/src/main/java/com/viaversion/viarewind/protocol/v1_8to1_7_6_10/storage/EntityTracker1_8.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
public class EntityTracker1_8 extends EntityTrackerBase {
4747

4848
private final Int2ObjectMap<VirtualHologramEntity> holograms = new Int2ObjectArrayMap<>();
49+
private final Int2IntMap extraHologramIds = new Int2IntArrayMap();
4950
private final Int2IntMap vehicles = new Int2IntArrayMap();
5051
private final Int2ObjectMap<UUID> entityIdToUUID = new Int2ObjectArrayMap<>();
5152
private final Object2IntMap<UUID> entityUUIDToId = new Object2IntOpenHashMap<>();
@@ -88,6 +89,8 @@ public void removeEntity(int entityId) {
8889
@Override
8990
public void clearEntities() {
9091
super.clearEntities();
92+
holograms.clear();
93+
extraHologramIds.clear();
9194
vehicles.clear();
9295
}
9396

@@ -189,6 +192,21 @@ public Int2ObjectMap<VirtualHologramEntity> getHolograms() {
189192
return holograms;
190193
}
191194

195+
public void setExtraHologramId(final int entityId, final int extraId) {
196+
extraHologramIds.put(extraId, entityId);
197+
}
198+
199+
public void removeExtraHologramId(int extraId) {
200+
extraHologramIds.remove(extraId);
201+
}
202+
203+
public int getHologramIdWithExtra(final int id) {
204+
if (holograms.containsKey(id)) {
205+
return id;
206+
}
207+
return extraHologramIds.getOrDefault(id, -1);
208+
}
209+
192210
public boolean isSpectator() {
193211
return clientEntityGameMode == 3;
194212
}

0 commit comments

Comments
 (0)