Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Shanwer committed Oct 9, 2024
2 parents f779275 + ef4acb1 commit 12f4c65
Show file tree
Hide file tree
Showing 42 changed files with 4,899 additions and 1,178 deletions.
1 change: 1 addition & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ dependencies {

// Test
testImplementation(libs.junit)
testImplementation(libs.mockito)

// Annotation Processors
compileOnly(projects.ap)
Expand Down
104 changes: 92 additions & 12 deletions core/src/main/java/org/geysermc/geyser/entity/type/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@

package org.geysermc.geyser.entity.type;

import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
Expand All @@ -35,12 +41,18 @@
import org.cloudburstmc.protocol.bedrock.data.entity.EntityDataTypes;
import org.cloudburstmc.protocol.bedrock.data.entity.EntityEventType;
import org.cloudburstmc.protocol.bedrock.data.entity.EntityFlag;
import org.cloudburstmc.protocol.bedrock.packet.*;
import org.cloudburstmc.protocol.bedrock.packet.AddEntityPacket;
import org.cloudburstmc.protocol.bedrock.packet.EntityEventPacket;
import org.cloudburstmc.protocol.bedrock.packet.MoveEntityAbsolutePacket;
import org.cloudburstmc.protocol.bedrock.packet.MoveEntityDeltaPacket;
import org.cloudburstmc.protocol.bedrock.packet.RemoveEntityPacket;
import org.cloudburstmc.protocol.bedrock.packet.SetEntityDataPacket;
import org.geysermc.geyser.api.entity.type.GeyserEntity;
import org.geysermc.geyser.entity.EntityDefinition;
import org.geysermc.geyser.entity.GeyserDirtyMetadata;
import org.geysermc.geyser.entity.properties.GeyserEntityPropertyManager;
import org.geysermc.geyser.item.Items;
import org.geysermc.geyser.scoreboard.Team;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.translator.text.MessageTranslator;
import org.geysermc.geyser.util.EntityUtils;
Expand All @@ -55,19 +67,22 @@
import org.geysermc.mcprotocollib.protocol.data.game.entity.player.Hand;
import org.geysermc.mcprotocollib.protocol.data.game.entity.type.EntityType;

import java.util.*;

@Getter
@Setter
public class Entity implements GeyserEntity {

private static final boolean PRINT_ENTITY_SPAWN_DEBUG = Boolean.parseBoolean(System.getProperty("Geyser.PrintEntitySpawnDebug", "false"));

protected final GeyserSession session;

protected int entityId;
protected final long geyserId;
protected UUID uuid;
/**
* Do not call this setter directly!
* This will bypass the scoreboard and setting the metadata
*/
@Setter(AccessLevel.NONE)
protected String nametag = "";

protected Vector3f position;
protected Vector3f motion;
Expand Down Expand Up @@ -97,7 +112,7 @@ public class Entity implements GeyserEntity {
@Setter(AccessLevel.NONE)
private float boundingBoxWidth;
@Setter(AccessLevel.NONE)
protected String nametag = "";
private String displayName;
@Setter(AccessLevel.NONE)
protected boolean silent = false;
/* Metadata end */
Expand Down Expand Up @@ -126,11 +141,12 @@ public class Entity implements GeyserEntity {

public Entity(GeyserSession session, int entityId, long geyserId, UUID uuid, EntityDefinition<?> definition, Vector3f position, Vector3f motion, float yaw, float pitch, float headYaw) {
this.session = session;
this.definition = definition;
this.displayName = standardDisplayName();

this.entityId = entityId;
this.geyserId = geyserId;
this.uuid = uuid;
this.definition = definition;
this.motion = motion;
this.yaw = yaw;
this.pitch = pitch;
Expand Down Expand Up @@ -341,7 +357,7 @@ public final void setFlag(EntityFlag flag, boolean value) {
* Sends the Bedrock metadata to the client
*/
public void updateBedrockMetadata() {
if (!valid) {
if (!isValid()) {
return;
}

Expand Down Expand Up @@ -410,17 +426,81 @@ protected short getMaxAir() {
return 300;
}

public String teamIdentifier() {
return uuid.toString();
}

public void setDisplayName(EntityMetadata<Optional<Component>, ?> entityMetadata) {
// displayName is shown when always display name is enabled. Either with or without team.
// That's why there are both a displayName and a nametag variable.
// Displayname is ignored for players, and is always their username.
Optional<Component> name = entityMetadata.getValue();
if (name.isPresent()) {
nametag = MessageTranslator.convertMessage(name.get(), session.locale());
dirtyMetadata.put(EntityDataTypes.NAME, nametag);
} else if (!nametag.isEmpty()) {
// Clear nametag
dirtyMetadata.put(EntityDataTypes.NAME, "");
String displayName = MessageTranslator.convertMessage(name.get(), session.locale());
this.displayName = displayName;
setNametag(displayName, true);
return;
}

// if no displayName is set, use entity name (ENDER_DRAGON -> Ender Dragon)
// maybe we can/should use a translatable here instead?
this.displayName = standardDisplayName();
setNametag(null, true);
}

protected String standardDisplayName() {
return EntityUtils.translatedEntityName(definition.entityType(), session);
}

protected void setNametag(@Nullable String nametag, boolean fromDisplayName) {
// ensure that the team format is used when nametag changes
if (nametag != null && fromDisplayName) {
var team = session.getWorldCache().getScoreboard().getTeamFor(teamIdentifier());
if (team != null) {
updateNametag(team);
return;
}
}

if (nametag == null) {
nametag = "";
}
boolean changed = !Objects.equals(this.nametag, nametag);
this.nametag = nametag;
// we only update metadata if the value has changed
if (!changed) {
return;
}

dirtyMetadata.put(EntityDataTypes.NAME, nametag);
// if nametag (player with team) is hidden for player, so should the score (belowname)
scoreVisibility(!nametag.isEmpty());
}

public void updateNametag(@Nullable Team team) {
// allow LivingEntity+ to have a different visibility check
updateNametag(team, true);
}

protected void updateNametag(@Nullable Team team, boolean visible) {
if (team != null) {
String newNametag;
// (team) visibility is LivingEntity+, team displayName is Entity+
if (visible) {
newNametag = team.displayName(getDisplayName());
} else {
// The name is not visible to the session player; clear name
newNametag = "";
}
setNametag(newNametag, false);
return;
}
// The name has reset, if it was previously something else
setNametag(null, false);
}

protected void scoreVisibility(boolean show) {}

public void setDisplayNameVisible(BooleanEntityMetadata entityMetadata) {
dirtyMetadata.put(EntityDataTypes.NAMETAG_ALWAYS_SHOW, (byte) (entityMetadata.getPrimitiveValue() ? 1 : 0));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@

package org.geysermc.geyser.entity.type;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
Expand All @@ -45,6 +50,7 @@
import org.geysermc.geyser.inventory.GeyserItemStack;
import org.geysermc.geyser.item.Items;
import org.geysermc.geyser.registry.type.ItemMapping;
import org.geysermc.geyser.scoreboard.Team;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.translator.item.ItemTranslator;
import org.geysermc.geyser.util.AttributeUtils;
Expand All @@ -65,12 +71,9 @@
import org.geysermc.mcprotocollib.protocol.data.game.level.particle.Particle;
import org.geysermc.mcprotocollib.protocol.data.game.level.particle.ParticleType;

import java.util.*;

@Getter
@Setter
public class LivingEntity extends Entity {

protected ItemData helmet = ItemData.AIR;
protected ItemData chestplate = ItemData.AIR;
protected ItemData leggings = ItemData.AIR;
Expand Down Expand Up @@ -150,6 +153,16 @@ protected void initializeMetadata() {
dirtyMetadata.put(EntityDataTypes.STRUCTURAL_INTEGRITY, 1);
}

@Override
public void updateNametag(@Nullable Team team) {
// if name not visible, don't mark it as visible
updateNametag(team, team == null || team.isVisibleFor(session.getPlayerEntity().getUsername()));
}

public void hideNametag() {
setNametag("", false);
}

public void setLivingEntityFlags(ByteEntityMetadata entityMetadata) {
byte xd = entityMetadata.getPrimitiveValue();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,21 @@

package org.geysermc.geyser.entity.type.living.animal;

import net.kyori.adventure.key.Key;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.cloudburstmc.math.vector.Vector3f;
import org.cloudburstmc.protocol.bedrock.data.entity.EntityDataTypes;
import org.cloudburstmc.protocol.bedrock.data.entity.EntityFlag;
import org.geysermc.geyser.entity.EntityDefinition;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.session.cache.tags.ItemTag;
import org.geysermc.geyser.util.EntityUtils;
import org.geysermc.mcprotocollib.protocol.data.game.entity.metadata.type.IntEntityMetadata;

import java.util.UUID;

public class RabbitEntity extends AnimalEntity {
private boolean isKillerBunny;

public RabbitEntity(GeyserSession session, int entityId, long geyserId, UUID uuid, EntityDefinition<?> definition, Vector3f position, Vector3f motion, float yaw, float pitch, float headYaw) {
super(session, entityId, geyserId, uuid, definition, position, motion, yaw, pitch, headYaw);
Expand All @@ -46,7 +49,7 @@ public void setRabbitVariant(IntEntityMetadata entityMetadata) {
int variant = entityMetadata.getPrimitiveValue();

// Change the killer bunny to display as white since it only exists on Java Edition
boolean isKillerBunny = variant == 99;
isKillerBunny = variant == 99;
if (isKillerBunny) {
variant = 1;
}
Expand All @@ -56,6 +59,14 @@ public void setRabbitVariant(IntEntityMetadata entityMetadata) {
dirtyMetadata.put(EntityDataTypes.VARIANT, variant);
}

@Override
protected String standardDisplayName() {
if (isKillerBunny) {
return EntityUtils.translatedEntityName(Key.key("killer_bunny"), session);
}
return super.standardDisplayName();
}

@Override
protected float getAdultSize() {
return 0.55f;
Expand All @@ -71,4 +82,4 @@ protected float getBabySize() {
protected ItemTag getFoodTag() {
return ItemTag.RABBIT_FOOD;
}
}
}
Loading

0 comments on commit 12f4c65

Please sign in to comment.