Skip to content

Commit

Permalink
🐛 Fix clients older than 1.20.2 not having correct positioning of pas…
Browse files Browse the repository at this point in the history
…senger
  • Loading branch information
Matt-MX committed Nov 17, 2024
1 parent 844752a commit 562696b
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ repositories {
maven("https://repo.extendedclip.com/content/repositories/placeholderapi/")
maven("https://repo.codemc.io/repository/maven-releases/")
maven("https://maven.evokegames.gg/snapshots")
maven("https://repo.viaversion.com")
}

dependencies {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ kotlin.code.style=official
# Project configuration
group_name = com.mattmx
id = nametags
version = 1.3
version = 1.4

plugin_name = NameTags
plugin_main_class_name = NameTags
Expand Down
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ runPaper = "2.2.4"
packetEvents = "2.6.0"
entityLib = "2.4.11-SNAPSHOT"
tab = "4.1.8"
viaversion = "5.0.0"

[libraries]

Expand All @@ -23,6 +24,7 @@ ktgui = { module = "com.mattmx:ktgui", version.ref = "ktgui" }
packet-events = { module = "com.github.retrooper:packetevents-spigot", version.ref = "packetEvents" }
entity-lib = { module = "me.tofaa.entitylib:spigot", version.ref = "entityLib" }
tab-api = { module = "com.github.NEZNAMY:TAB-API", version.ref = "tab" }
via-version = { module = "com.viaversion:viaversion-api", version.ref = "viaversion" }

[plugins]

Expand Down
43 changes: 43 additions & 0 deletions src/main/java/com/mattmx/nametags/OutgoingPacketListener.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
package com.mattmx.nametags;

import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.event.PacketListenerAbstract;
import com.github.retrooper.packetevents.event.PacketSendEvent;
import com.github.retrooper.packetevents.manager.server.ServerVersion;
import com.github.retrooper.packetevents.manager.server.VersionComparison;
import com.github.retrooper.packetevents.protocol.entity.data.EntityData;
import com.github.retrooper.packetevents.protocol.entity.data.EntityDataTypes;
import com.github.retrooper.packetevents.protocol.packettype.PacketType;
import com.github.retrooper.packetevents.protocol.player.ClientVersion;
import com.github.retrooper.packetevents.protocol.potion.PotionTypes;
import com.github.retrooper.packetevents.util.Vector3f;
import com.github.retrooper.packetevents.wrapper.play.server.*;
import com.mattmx.nametags.entity.NameTagEntity;
import me.tofaa.entitylib.meta.display.TextDisplayMeta;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;

public class OutgoingPacketListener extends PacketListenerAbstract {
private static final Vector3f PRE_1_20_2_TRANSLATION = new Vector3f(0f, 0.4f, 0f);
private final @NotNull NameTags plugin;

public OutgoingPacketListener(@NotNull NameTags plugin) {
Expand Down Expand Up @@ -40,6 +49,40 @@ public void onPacketSend(@NotNull PacketSendEvent event) {
event.getUser().sendPacket(nameTagEntity.getPassengersPacket());
});
}
case PacketType.Play.Server.ENTITY_METADATA -> {
if (event.getUser().getClientVersion().isNewerThanOrEquals(ClientVersion.V_1_20_2)) {
return;
}

WrapperPlayServerEntityMetadata packet = new WrapperPlayServerEntityMetadata(event);

NameTagEntity nameTagEntity = plugin.getEntityManager().getNameTagEntityByTagEntityId(packet.getEntityId());

if (nameTagEntity == null) return;

byte index = PacketEvents.getAPI()
.getServerManager()
.getVersion()
.is(VersionComparison.OLDER_THAN, ServerVersion.V_1_20_2)
? (byte) 10
: (byte) 11;

packet.getEntityMetadata()
.stream()
.filter((meta) -> meta.getIndex() == index)
.findFirst()
.ifPresentOrElse((data) -> {
Vector3f vec = (Vector3f) data.getValue();
data.setValue(vec.add(PRE_1_20_2_TRANSLATION));
}, () -> packet.getEntityMetadata().add(new EntityData(
index,
EntityDataTypes.VECTOR3F,
PRE_1_20_2_TRANSLATION
))
);

event.markForReEncode(true);
}
case PacketType.Play.Server.DESTROY_ENTITIES -> {
WrapperPlayServerDestroyEntities packet = new WrapperPlayServerDestroyEntities(event);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ public class NameTagEntityManager {
.orElse(null);
}

public @Nullable NameTagEntity getNameTagEntityByTagEntityId(int entityId) {
return entityMap.values()
.stream()
.filter((e) -> e.getPassenger().getEntityId() == entityId)
.findFirst()
.orElse(null);
}

public @NotNull Collection<NameTagEntity> getAllEntities() {
return this.entityMap.values();
}
Expand Down

0 comments on commit 562696b

Please sign in to comment.