Fixed metadata issue. (#1378)

This commit is contained in:
Obyvante 2022-08-30 22:37:25 +03:00 committed by GitHub
parent 978a398f64
commit c694c4074e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 1 deletions

View File

@ -180,9 +180,9 @@ public final class Metadata {
if (index >= entries.length) {
final int newLength = Math.max(entries.length * 2, index + 1);
this.entries = entries = Arrays.copyOf(entries, newLength);
this.entryMap = null;
}
entries[index] = entry;
this.entryMap = null;
// Send metadata packet to update viewers and self
final Entity entity = this.entity;
if (entity != null && entity.isActive()) {

View File

@ -1,5 +1,6 @@
package net.minestom.server.entity;
import net.kyori.adventure.text.Component;
import net.minestom.server.api.Env;
import net.minestom.server.api.EnvTest;
import net.minestom.server.coordinate.Pos;
@ -76,4 +77,58 @@ public class EntityMetaIntegrationTest {
}
}
}
@Test
public void customName(Env env) {
//Base things.
var connection = env.createConnection();
var instance = env.createFlatInstance();
Pos startPos = new Pos(0, 42, 1);
//Viewer.
var player = connection.connect(instance, startPos).join();
//Tracks incoming packets.
var incomingPackets = connection.trackIncoming(EntityMetaDataPacket.class);
//Creates entity and name.
Entity entity = new Entity(EntityType.BEE);
entity.setAutoViewable(false);
entity.getEntityMeta().setNotifyAboutChanges(false);
entity.setCustomName(Component.text("Custom Name"));
entity.setCustomNameVisible(true);
entity.setInstance(instance, startPos);
entity.getEntityMeta().setNotifyAboutChanges(true);
entity.addViewer(player);
//Listen packets to check if entity name is "Custom Name".
//This is first test, and it is not related to "custom name" bug. Therefore, it should work.
var packets = incomingPackets.collect();
validMetaDataPackets(packets, entity.getEntityId(), entry -> {
if (entry.type() != Metadata.TYPE_OPTCHAT) return;
assertEquals(Component.text("Custom Name"), entry.value());
});
//Removes viewer.
entity.removeViewer(player);
//Tracks incoming packets again. (resets previous)
incomingPackets = connection.trackIncoming(EntityMetaDataPacket.class);
//Sets entity name again.
entity.setCustomName(Component.text("Custom Name 2"));
//After setting entity's name, we add viewer again to see if the entity name is "Custom Name 2"
entity.addViewer(player);
//Checks if entity name is "Custom Name 2" in the metadata entry.
assertEquals(Component.text("Custom Name 2"), entity.getCustomName());
//Listen packets to check if entity name is "Custom Name 2".
packets = incomingPackets.collect();
validMetaDataPackets(packets, entity.getEntityId(), entry -> {
if (entry.type() != Metadata.TYPE_OPTCHAT) return;
assertEquals(Component.text("Custom Name 2"), entry.value());
});
}
}