Skip to content

Commit

Permalink
🔁 Refactor to not use streams on netty for efficiency
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt-MX committed Feb 4, 2025
1 parent 07c878b commit 372c768
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 36 deletions.
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.5.1
version = 1.5.2

plugin_name = NameTags
plugin_main_class_name = NameTags
Expand Down
86 changes: 51 additions & 35 deletions src/main/java/com/mattmx/nametags/OutgoingPacketListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,51 +74,60 @@ public void onPacketSend(@NotNull PacketSendEvent event) {
? PRE_1_20_2_TRANSLATION_INDEX
: POST_1_20_2_TRANSLATION_INDEX;

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_OFFSET));
}, () -> packet.getEntityMetadata().add(new EntityData(
index,
EntityDataTypes.VECTOR3F,
PRE_1_20_2_TRANSLATION_OFFSET
))
);
boolean found = false;
for (final EntityData entry : packet.getEntityMetadata()) {
if (entry.getIndex() != index) {
continue;
}

found = true;
Vector3f vec = (Vector3f) entry.getValue();
entry.setValue(vec.add(PRE_1_20_2_TRANSLATION_OFFSET));
break;
}

if (!found) {
packet.getEntityMetadata().add(new EntityData(
index,
EntityDataTypes.VECTOR3F,
PRE_1_20_2_TRANSLATION_OFFSET
));
}

event.markForReEncode(true);
}

// Apply relational placeholders to the text of an outgoing display entity
if (nameTagEntity.getBukkitEntity() instanceof Player from) {
packet.getEntityMetadata()
.stream()
.filter((meta) -> meta.getIndex() == TEXT_DISPLAY_TEXT_INDEX && meta.getValue() instanceof Component)
.findFirst()
.ifPresent((data) -> {
final Component originalText = (Component) data.getValue();
final Player to = event.getPlayer();
for (final EntityData entry : packet.getEntityMetadata()) {
if (entry.getIndex() != TEXT_DISPLAY_TEXT_INDEX || !(entry.getType() instanceof Component)) {
continue;
}

final Component originalText = (Component) entry.getValue();
final Player to = event.getPlayer();

// TODO(Matt): Replace use of legacy serializer
String legacy = LegacyComponentSerializer
.legacyAmpersand()
.serialize(originalText);

// TODO(Matt): Replace use of legacy serializer
String legacy = LegacyComponentSerializer
.legacyAmpersand()
.serialize(originalText);
// If it doesn't have any placeholders in then stop
if (!legacy.contains("%rel_")) break;

// If it doesn't have any placeholders in then stop
if (!legacy.contains("%rel_")) return;
final Component appliedText = LegacyComponentSerializer
.legacyAmpersand()
.deserialize(PapiHook.setRelationalPlaceholders(from, to, legacy));

final Component appliedText = LegacyComponentSerializer
.legacyAmpersand()
.deserialize(PapiHook.setRelationalPlaceholders(from, to, legacy));
if (!originalText.equals(appliedText)) {

if (!originalText.equals(appliedText)) {
entry.setValue(appliedText);

data.setValue(appliedText);
event.markForReEncode(true);
}

event.markForReEncode(true);
}
});
break;
}
}
}
case PacketType.Play.Server.DESTROY_ENTITIES -> {
Expand Down Expand Up @@ -164,7 +173,14 @@ public void onPacketSend(@NotNull PacketSendEvent event) {
if (nameTagEntity == null) return;

// If the packet doesn't already contain our entity
if (Arrays.stream(packet.getPassengers()).noneMatch((i) -> nameTagEntity.getPassenger().getEntityId() == i)) {
boolean containsNameTagPassenger = false;
for (final int passengerId : packet.getPassengers()) {
if (passengerId == nameTagEntity.getPassenger().getEntityId()) {
containsNameTagPassenger = true;
}
}

if (!containsNameTagPassenger) {

// Add our entity
int[] passengers = Arrays.copyOf(packet.getPassengers(), packet.getPassengers().length + 1);
Expand Down

0 comments on commit 372c768

Please sign in to comment.