Skip to content

Commit

Permalink
Implemented hologram rewriting
Browse files Browse the repository at this point in the history
Fixed #97
Probably fixed #458
Probably fixed #457
  • Loading branch information
FlorianMichael committed Oct 26, 2023
1 parent ebed6dd commit b0e7a20
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,15 @@ public void transform(Direction direction, State state, PacketWrapper packetWrap
@Override
public void init(UserConnection userConnection) {
userConnection.put(new InventoryTracker(userConnection));
userConnection.put(new EntityTracker1_7_6_10(userConnection));
userConnection.put(new EntityTracker1_7_6_10(userConnection, metadataRewriter));
userConnection.put(new PlayerSessionStorage(userConnection));
userConnection.put(new GameProfileStorage(userConnection));
userConnection.put(new Scoreboard(userConnection));
userConnection.put(new CompressionStatusTracker(userConnection));
userConnection.put(new WorldBorderEmulator(userConnection));

if (!userConnection.has(ClientWorld.class)) {
userConnection.put(new ClientWorld(userConnection));
userConnection.put(new ClientWorld());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,35 @@ public void updateLocation(boolean remount) {
if (entityIds == null) return;

if (currentState == State.ZOMBIE) {
updateZombieLocation();
teleportEntity(entityId, locX, locY, locZ, yaw, pitch);

final PacketWrapper entityHeadLook = PacketWrapper.create(ClientboundPackets1_7_2_5.ENTITY_HEAD_LOOK, user);

entityHeadLook.write(Type.INT, entityId);
entityHeadLook.write(Type.BYTE, (byte) ((headYaw / 360f) * 256));

PacketUtil.sendPacket(entityHeadLook, Protocol1_7_6_10To1_8.class, true, true);
} else if (currentState == State.HOLOGRAM) {
updateHologramLocation(remount);
if (remount) {
PacketWrapper detach = PacketWrapper.create(ClientboundPackets1_7_2_5.ATTACH_ENTITY, null, user);
detach.write(Type.INT, entityIds[1]);
detach.write(Type.INT, -1);
detach.write(Type.BOOLEAN, false);
PacketUtil.sendPacket(detach, Protocol1_7_6_10To1_8.class, true, true);
}

// Don't ask me where this offset is coming from
teleportEntity(entityIds[0], locX, (locY + (marker ? 54.85 : small ? 56 : 57) - 0.16), locZ, 0, 0); // Skull

if (remount) {
teleportEntity(entityIds[1], locX, locY + 56.75, locZ, 0, 0); // Horse

PacketWrapper attach = PacketWrapper.create(ClientboundPackets1_7_2_5.ATTACH_ENTITY, null, user);
attach.write(Type.INT, entityIds[1]);
attach.write(Type.INT, entityIds[0]);
attach.write(Type.BOOLEAN, false);
PacketUtil.sendPacket(attach, Protocol1_7_6_10To1_8.class, true, true);
}
}
}

Expand All @@ -142,15 +168,6 @@ protected void teleportEntity(final int entityId, final double x, final double y
PacketUtil.sendPacket(entityTeleport, Protocol1_7_6_10To1_8.class, true, true);
}

protected void updateHeadYaw(final int entityId, final float headYaw) {
final PacketWrapper entityHeadLook = PacketWrapper.create(ClientboundPackets1_7_2_5.ENTITY_HEAD_LOOK, user);

entityHeadLook.write(Type.INT, entityId);
entityHeadLook.write(Type.BYTE, (byte) ((headYaw / 360f) * 256));

PacketUtil.sendPacket(entityHeadLook, Protocol1_7_6_10To1_8.class, true, true);
}

protected void spawnEntity(final int entityId, final int type, final double locX, final double locY, final double locZ) {
final PacketWrapper spawnMob = PacketWrapper.create(ClientboundPackets1_7_2_5.SPAWN_MOB, null, user);

Expand All @@ -170,34 +187,6 @@ protected void spawnEntity(final int entityId, final int type, final double locX
PacketUtil.sendPacket(spawnMob, Protocol1_7_6_10To1_8.class, true, true);
}

private void updateZombieLocation() {
teleportEntity(entityId, locX, locY, locZ, yaw, pitch);
updateHeadYaw(entityId, headYaw);
}

private void updateHologramLocation(boolean remount) {
if (remount) {
PacketWrapper detach = PacketWrapper.create(ClientboundPackets1_7_2_5.ATTACH_ENTITY, null, user);
detach.write(Type.INT, entityIds[1]);
detach.write(Type.INT, -1);
detach.write(Type.BOOLEAN, false);
PacketUtil.sendPacket(detach, Protocol1_7_6_10To1_8.class, true, true);
}

// Don't ask me where this offset is coming from
teleportEntity(entityIds[0], locX, (locY + (marker ? 54.85 : small ? 56 : 57)), locZ, 0, 0); // Skull

if (remount) {
teleportEntity(entityIds[1], locX, locY + 56.75, locZ, 0, 0); // Horse

PacketWrapper attach = PacketWrapper.create(ClientboundPackets1_7_2_5.ATTACH_ENTITY, null, user);
attach.write(Type.INT, entityIds[1]);
attach.write(Type.INT, entityIds[0]);
attach.write(Type.BOOLEAN, false);
PacketUtil.sendPacket(attach, Protocol1_7_6_10To1_8.class, true, true);
}
}

public void updateMetadata() {
if (entityIds == null) return;

Expand Down Expand Up @@ -232,9 +221,9 @@ private void writeHologramMeta(PacketWrapper metadataPacket) {
metadataPacket.write(Type.INT, entityIds[1]);

List<Metadata> metadataList = new ArrayList<>();
metadataList.add(new Metadata(12, MetaType1_7_6_10.Int, -1700000));
metadataList.add(new Metadata(10, MetaType1_7_6_10.String, name));
metadataList.add(new Metadata(11, MetaType1_7_6_10.Byte, (byte) 1));
metadataList.add(new Metadata(MetaIndex1_7_6_10To1_8.ENTITY_AGEABLE_AGE.getIndex(), MetaType1_7_6_10.Int, -1700000));
metadataList.add(new Metadata(MetaIndex1_7_6_10To1_8.ENTITY_LIVING_NAME_TAG.getIndex(), MetaType1_7_6_10.String, name));
metadataList.add(new Metadata(MetaIndex1_7_6_10To1_8.ENTITY_LIVING_NAME_TAG_VISIBILITY.getIndex(), MetaType1_7_6_10.Byte, (byte) 1));

metadataPacket.write(Types1_7_6_10.METADATA_LIST, metadataList);
}
Expand All @@ -243,54 +232,39 @@ public void sendSpawnPacket() {
if (entityIds != null) deleteEntity();

if (currentState == State.ZOMBIE) {
spawnZombie();
spawnEntity(entityId, 54, locX, locY, locZ);

entityIds = new int[]{entityId};
} else if (currentState == State.HOLOGRAM) {
spawnHologram();
int[] entityIds = {entityId, additionalEntityId()};

PacketWrapper spawnSkull = PacketWrapper.create(ClientboundPackets1_7_2_5.SPAWN_ENTITY, null, user);
spawnSkull.write(Type.VAR_INT, entityIds[0]);
spawnSkull.write(Type.BYTE, (byte) 66);
spawnSkull.write(Type.INT, (int) (locX * 32.0));
spawnSkull.write(Type.INT, (int) (locY * 32.0));
spawnSkull.write(Type.INT, (int) (locZ * 32.0));
spawnSkull.write(Type.BYTE, (byte) 0);
spawnSkull.write(Type.BYTE, (byte) 0);
spawnSkull.write(Type.INT, 0);
PacketUtil.sendPacket(spawnSkull, Protocol1_7_6_10To1_8.class, true, true);

spawnEntity(entityIds[1], 100, locX, locY, locZ); // Horse

this.entityIds = entityIds;
}

updateMetadata();
updateLocation(true);
}

private void spawnZombie() {
spawnEntity(entityId, 54, locX, locY, locZ);

entityIds = new int[]{entityId};
}

private void spawnHologram() {
int[] entityIds = {entityId, additionalEntityId()};

PacketWrapper spawnSkull = PacketWrapper.create(ClientboundPackets1_7_2_5.SPAWN_ENTITY, null, user);
spawnSkull.write(Type.VAR_INT, entityIds[0]);
spawnSkull.write(Type.BYTE, (byte) 66);
spawnSkull.write(Type.INT, (int) (locX * 32.0));
spawnSkull.write(Type.INT, (int) (locY * 32.0));
spawnSkull.write(Type.INT, (int) (locZ * 32.0));
spawnSkull.write(Type.BYTE, (byte) 0);
spawnSkull.write(Type.BYTE, (byte) 0);
spawnSkull.write(Type.INT, 0);
PacketUtil.sendPacket(spawnSkull, Protocol1_7_6_10To1_8.class, true, true);

spawnEntity(entityIds[1], 100, locX, locY, locZ); // Horse

this.entityIds = entityIds;
}

private int additionalEntityId() {
return Integer.MAX_VALUE - 16000 - entityId;
}

public AABB getBoundingBox() {
double w = this.small ? 0.25 : 0.5;
double h = this.small ? 0.9875 : 1.975;
Vector3d min = new Vector3d(this.locX - w / 2, this.locY, this.locZ - w / 2);
Vector3d max = new Vector3d(this.locX + w / 2, this.locY + h, this.locZ + w / 2);
return new AABB(min, max);
}

public void deleteEntity() {
if (entityIds == null) return;

PacketWrapper despawn = PacketWrapper.create(ClientboundPackets1_7_2_5.DESTROY_ENTITIES, null, user);
despawn.write(Type.BYTE, (byte) entityIds.length);
for (int id : entityIds) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@
package com.viaversion.viarewind.protocol.protocol1_7_6_10to1_8.packets;

import com.viaversion.viarewind.protocol.protocol1_7_6_10to1_8.Protocol1_7_6_10To1_8;
import com.viaversion.viarewind.protocol.protocol1_7_6_10to1_8.model.VirtualHologramEntity;
import com.viaversion.viarewind.protocol.protocol1_7_6_10to1_8.storage.EntityTracker1_7_6_10;
import com.viaversion.viarewind.protocol.protocol1_7_6_10to1_8.storage.GameProfileStorage;
import com.viaversion.viarewind.protocol.protocol1_7_6_10to1_8.storage.PlayerSessionStorage;
import com.viaversion.viarewind.protocol.protocol1_7_6_10to1_8.types.Types1_7_6_10;
import com.viaversion.viarewind.api.minecraft.EntityModel;
import com.viaversion.viaversion.api.minecraft.Position;
import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_10;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.minecraft.metadata.Metadata;
Expand Down Expand Up @@ -55,7 +54,7 @@ public void register() {

handler(wrapper -> {
final short slot = wrapper.get(Type.SHORT, 0);
final UUID uuid = wrapper.user().get(EntityTracker1_7_6_10.class).getPlayerUUID(wrapper.get(Type.INT, 0));
final UUID uuid = wrapper.user().get(EntityTracker1_7_6_10.class).getPlayerUUID(wrapper.get(Type.INT, 0));
if (uuid == null) return;

final Item item = wrapper.get(Types1_7_6_10.COMPRESSED_NBT_ITEM, 0);
Expand Down Expand Up @@ -129,6 +128,20 @@ public void register() {
map(Type.BYTE); // y
map(Type.BYTE); // z
read(Type.BOOLEAN); // on ground

handler(wrapper -> {
final EntityTracker1_7_6_10 tracker = wrapper.user().get(EntityTracker1_7_6_10.class);

final VirtualHologramEntity hologram = tracker.getVirtualHologramMap().get(wrapper.get(Type.INT, 0));
if (hologram != null) {
wrapper.cancel();
final int x = wrapper.get(Type.BYTE, 0);
final int y = wrapper.get(Type.BYTE, 1);
final int z = wrapper.get(Type.BYTE, 2);

hologram.handleOriginalMovementPacket(x / 32.0, y / 32.0, z / 32.0);
}
});
}
});

Expand All @@ -139,6 +152,19 @@ public void register() {
map(Type.BYTE); // yaw
map(Type.BYTE); // pitch
read(Type.BOOLEAN); // on ground

handler(wrapper -> {
final EntityTracker1_7_6_10 tracker = wrapper.user().get(EntityTracker1_7_6_10.class);

final VirtualHologramEntity hologram = tracker.getVirtualHologramMap().get(wrapper.get(Type.INT, 0));
if (hologram != null) {
wrapper.cancel();
final int yaw = wrapper.get(Type.BYTE, 0);
final int pitch = wrapper.get(Type.BYTE, 1);

hologram.setYawPitch(yaw * 360f / 256, pitch * 360f / 256);
}
});
}
});

Expand All @@ -152,6 +178,24 @@ public void register() {
map(Type.BYTE); // yaw
map(Type.BYTE); // pitch
read(Type.BOOLEAN); // on ground

handler(wrapper -> {
final EntityTracker1_7_6_10 tracker = wrapper.user().get(EntityTracker1_7_6_10.class);

final VirtualHologramEntity hologram = tracker.getVirtualHologramMap().get(wrapper.get(Type.INT, 0));
if (hologram != null) {
wrapper.cancel();
final int x = wrapper.get(Type.BYTE, 0);
final int y = wrapper.get(Type.BYTE, 1);
final int z = wrapper.get(Type.BYTE, 2);

final int yaw = wrapper.get(Type.BYTE, 3);
final int pitch = wrapper.get(Type.BYTE, 4);

hologram.handleOriginalMovementPacket(x / 32.0, y / 32.0, z / 32.0);
hologram.setYawPitch(yaw * 360f / 256, pitch * 360f / 256);
}
});
}
});

Expand All @@ -166,14 +210,30 @@ public void register() {
map(Type.BYTE); // pitch
read(Type.BOOLEAN); // on ground
handler(wrapper -> {
int entityId = wrapper.get(Type.INT, 0);
EntityTracker1_7_6_10 tracker = wrapper.user().get(EntityTracker1_7_6_10.class);
EntityTypes1_10.EntityType type = tracker.getEntityMap().get(entityId);
final EntityTracker1_7_6_10 tracker = wrapper.user().get(EntityTracker1_7_6_10.class);

final int entityId = wrapper.get(Type.INT, 0);
final EntityTypes1_10.EntityType type = tracker.getEntityMap().get(entityId);

if (type == EntityTypes1_10.EntityType.MINECART_ABSTRACT) { // TODO | Realign all entities?
int y = wrapper.get(Type.INT, 2);
y += 12;
wrapper.set(Type.INT, 2, y);
}

final VirtualHologramEntity hologram = tracker.getVirtualHologramMap().get(entityId);
if (hologram != null) {
wrapper.cancel();
final int x = wrapper.get(Type.INT, 1);
final int y = wrapper.get(Type.INT, 2);
final int z = wrapper.get(Type.INT, 3);

final int yaw = wrapper.get(Type.BYTE, 0);
final int pitch = wrapper.get(Type.BYTE, 1);

hologram.updateReplacementPosition(x / 32.0, y / 32.0, z / 32.0);
hologram.setYawPitch(yaw * 360f / 256, pitch * 360f / 256);
}
});
}
});
Expand All @@ -183,6 +243,18 @@ public void register() {
public void register() {
map(Type.VAR_INT, Type.INT); // entity id
map(Type.BYTE); // head yaw

handler(wrapper -> {
final EntityTracker1_7_6_10 tracker = wrapper.user().get(EntityTracker1_7_6_10.class);

final VirtualHologramEntity hologram = tracker.getVirtualHologramMap().get(wrapper.get(Type.INT, 0));
if (hologram != null) {
wrapper.cancel();
final int yaw = wrapper.get(Type.BYTE, 0);

hologram.setHeadYaw(yaw * 360f / 256);
}
});
}
});

Expand Down Expand Up @@ -218,6 +290,8 @@ public void register() {
final EntityTracker1_7_6_10 tracker = wrapper.user().get(EntityTracker1_7_6_10.class);
if (tracker.getEntityReplacementMap().containsKey(entityId)) {
tracker.updateMetadata(entityId, metadataList);
wrapper.cancel();
return;
}
if (tracker.getEntityMap().containsKey(entityId)) {
protocol.getMetadataRewriter().transform(tracker.getEntityMap().get(entityId), metadataList);
Expand Down
Loading

0 comments on commit b0e7a20

Please sign in to comment.