Skip to content

Commit

Permalink
Check whether the entity definition has a type
Browse files Browse the repository at this point in the history
Fixes #5078
  • Loading branch information
Tim203 committed Oct 10, 2024
1 parent 521df04 commit 123c5cc
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
15 changes: 11 additions & 4 deletions core/src/main/java/org/geysermc/geyser/util/EntityUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

import java.util.Locale;
import net.kyori.adventure.key.Key;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.cloudburstmc.math.vector.Vector3f;
import org.cloudburstmc.protocol.bedrock.data.GameType;
import org.cloudburstmc.protocol.bedrock.data.entity.EntityDataTypes;
Expand Down Expand Up @@ -292,22 +294,27 @@ public static GameType toBedrockGamemode(GameMode gamemode) {
};
}

private static String translatedEntityName(String namespace, String name, GeyserSession session) {
// MinecraftLocale would otherwise invoke getBootstrap (which doesn't exist) and create some folders
private static String translatedEntityName(@NonNull String namespace, @NonNull String name, @NonNull GeyserSession session) {
// MinecraftLocale would otherwise invoke getBootstrap (which doesn't exist) and create some folders,
// so use the default fallback value as used in Minecraft Java
if (EnvironmentUtils.isUnitTesting) {
return "entity." + namespace + "." + name;
}
return MinecraftLocale.getLocaleString("entity." + namespace + "." + name, session.locale());
}

public static String translatedEntityName(Key type, GeyserSession session) {
public static String translatedEntityName(@NonNull Key type, @NonNull GeyserSession session) {
return translatedEntityName(type.namespace(), type.value(), session);
}

public static String translatedEntityName(EntityType type, GeyserSession session) {
public static String translatedEntityName(@Nullable EntityType type, @NonNull GeyserSession session) {
if (type == EntityType.PLAYER) {
return "Player"; // the player's name is always shown instead
}
// default fallback value as used in Minecraft Java
if (type == null) {
return "entity.unregistered_sadface";
}
// this works at least with all 1.20.5 entities, except the killer bunny since that's not an entity type.
String typeName = type.name().toLowerCase(Locale.ROOT);
return translatedEntityName("minecraft", typeName, session);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,25 @@
import static org.geysermc.geyser.scoreboard.network.util.AssertUtils.assertNextPacketType;
import static org.geysermc.geyser.scoreboard.network.util.GeyserMockContextScoreboard.mockContextScoreboard;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.cloudburstmc.protocol.bedrock.packet.AddEntityPacket;
import org.cloudburstmc.protocol.bedrock.packet.RemoveEntityPacket;
import org.geysermc.geyser.entity.type.living.monster.EnderDragonPartEntity;
import org.geysermc.geyser.session.cache.EntityCache;
import org.geysermc.geyser.translator.protocol.java.entity.JavaRemoveEntitiesTranslator;
import org.geysermc.geyser.translator.protocol.java.entity.spawn.JavaAddExperienceOrbTranslator;
import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.entity.ClientboundRemoveEntitiesPacket;
import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.entity.spawn.ClientboundAddExperienceOrbPacket;
import org.junit.jupiter.api.Test;

public class TeamIdentifierTest {
/**
* Tests that don't fit in a larger system (e.g. sidebar objective) that were reported on GitHub
*/
public class ScoreboardIssueTests {
/**
* Test for <a href="https://github.com/GeyserMC/Geyser/issues/5075">#5075</a>
*/
@Test
void entityWithoutUuid() {
// experience orbs are the only known entities without an uuid, see Entity#teamIdentifier for more info
Expand All @@ -50,6 +59,10 @@ void entityWithoutUuid() {
// because the entity would be registered and deregistered to the scoreboard.
assertDoesNotThrow(() -> {
context.translate(addExperienceOrbTranslator, new ClientboundAddExperienceOrbPacket(2, 0, 0, 0, 1));

String displayName = context.mockOrSpy(EntityCache.class).getEntityByJavaId(2).getDisplayName();
assertEquals("entity.minecraft.experience_orb", displayName);

context.translate(removeEntitiesTranslator, new ClientboundRemoveEntitiesPacket(new int[] { 2 }));
});

Expand All @@ -58,4 +71,23 @@ void entityWithoutUuid() {
assertNextPacketType(context, RemoveEntityPacket.class);
});
}

/**
* Test for <a href="https://github.com/GeyserMC/Geyser/issues/5078">#5078</a>
*/
@Test
void entityWithoutType() {
// dragon entity parts are an entity in Geyser, but do not have an entity type
mockContextScoreboard(context -> {
// EntityUtils#translatedEntityName used to not take null EntityType's into account,
// so it used to throw an exception
assertDoesNotThrow(() -> {
// dragon entity parts are not spawned using a packet, so we manually create an instance
var dragonHeadPart = new EnderDragonPartEntity(context.session(), 2, 2, 1, 1);

String displayName = dragonHeadPart.getDisplayName();
assertEquals("entity.unregistered_sadface", displayName);
});
});
}
}

0 comments on commit 123c5cc

Please sign in to comment.