/* * Copyright (c) 2019-2022 GeyserMC. http://geysermc.org * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. * * @author GeyserMC * @link https://github.com/GeyserMC/Geyser */ package org.geysermc.geyser.entity.type; import com.github.steveice10.mc.protocol.data.game.entity.attribute.Attribute; import com.github.steveice10.mc.protocol.data.game.entity.attribute.AttributeType; import com.github.steveice10.mc.protocol.data.game.entity.metadata.EntityMetadata; import com.github.steveice10.mc.protocol.data.game.entity.metadata.Pose; import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position; import com.github.steveice10.mc.protocol.data.game.entity.metadata.type.ByteEntityMetadata; import com.github.steveice10.mc.protocol.data.game.entity.metadata.type.FloatEntityMetadata; import com.github.steveice10.mc.protocol.data.game.entity.metadata.type.IntEntityMetadata; import com.github.steveice10.mc.protocol.data.game.entity.player.Hand; import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import com.github.steveice10.opennbt.tag.builtin.StringTag; import com.nukkitx.math.vector.Vector3f; import com.nukkitx.math.vector.Vector3i; import com.nukkitx.protocol.bedrock.data.AttributeData; import com.nukkitx.protocol.bedrock.data.entity.EntityData; import com.nukkitx.protocol.bedrock.data.entity.EntityFlag; import com.nukkitx.protocol.bedrock.data.inventory.ContainerId; import com.nukkitx.protocol.bedrock.data.inventory.ItemData; import com.nukkitx.protocol.bedrock.packet.MobArmorEquipmentPacket; import com.nukkitx.protocol.bedrock.packet.MobEquipmentPacket; import com.nukkitx.protocol.bedrock.packet.UpdateAttributesPacket; import lombok.AccessLevel; import lombok.Getter; import lombok.Setter; import org.geysermc.geyser.entity.EntityDefinition; import org.geysermc.geyser.entity.attribute.GeyserAttributeType; import org.geysermc.geyser.inventory.GeyserItemStack; import org.geysermc.geyser.session.GeyserSession; import org.geysermc.geyser.registry.type.ItemMapping; import org.geysermc.geyser.util.AttributeUtils; import org.geysermc.geyser.util.ChunkUtils; import org.geysermc.geyser.util.InteractionResult; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Optional; import java.util.UUID; @Getter @Setter public class LivingEntity extends Entity { protected ItemData helmet = ItemData.AIR; protected ItemData chestplate = ItemData.AIR; protected ItemData leggings = ItemData.AIR; protected ItemData boots = ItemData.AIR; protected ItemData hand = ItemData.AIR; protected ItemData offHand = ItemData.AIR; @Getter(value = AccessLevel.NONE) protected float health = 1f; // The default value in Java Edition before any entity metadata is sent @Getter(value = AccessLevel.NONE) protected float maxHealth = 20f; // The value Java Edition defaults to if no attribute is given /** * A convenience variable for if the entity has reached the maximum frozen ticks and should be shaking */ private boolean isMaxFrozenState = false; public LivingEntity(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); } @Override protected void initializeMetadata() { super.initializeMetadata(); // Matches Bedrock behavior; is always set to this dirtyMetadata.put(EntityData.HEALTH, 1); } public void setLivingEntityFlags(ByteEntityMetadata entityMetadata) { byte xd = entityMetadata.getPrimitiveValue(); boolean isUsingItem = (xd & 0x01) == 0x01; boolean isUsingOffhand = (xd & 0x02) == 0x02; ItemMapping shield = session.getItemMappings().getStoredItems().shield(); boolean isUsingShield = hasShield(isUsingOffhand, shield); setFlag(EntityFlag.USING_ITEM, isUsingItem && !isUsingShield); // Override the blocking setFlag(EntityFlag.BLOCKING, isUsingItem && isUsingShield); // Riptide spin attack setFlag(EntityFlag.DAMAGE_NEARBY_MOBS, (xd & 0x04) == 0x04); } public void setHealth(FloatEntityMetadata entityMetadata) { this.health = entityMetadata.getPrimitiveValue(); AttributeData healthData = createHealthAttribute(); UpdateAttributesPacket attributesPacket = new UpdateAttributesPacket(); attributesPacket.setRuntimeEntityId(geyserId); attributesPacket.setAttributes(Collections.singletonList(healthData)); session.sendUpstreamPacket(attributesPacket); } public Vector3i setBedPosition(EntityMetadata, ?> entityMetadata) { Optional optionalPos = entityMetadata.getValue(); if (optionalPos.isPresent()) { Position bedPosition = optionalPos.get(); Vector3i vector = Vector3i.from(bedPosition.getX(), bedPosition.getY(), bedPosition.getZ()); dirtyMetadata.put(EntityData.BED_POSITION, vector); int bed = session.getGeyser().getWorldManager().getBlockAt(session, bedPosition); // Bed has to be updated, or else player is floating in the air ChunkUtils.updateBlock(session, bed, bedPosition); // Indicate that the player should enter the sleep cycle // Has to be a byte or it does not work // (Bed position is what actually triggers sleep - "pose" is only optional) dirtyMetadata.put(EntityData.PLAYER_FLAGS, (byte) 2); return vector; } else { // Player is no longer sleeping dirtyMetadata.put(EntityData.PLAYER_FLAGS, (byte) 0); return null; } } protected boolean hasShield(boolean offhand, ItemMapping shieldMapping) { if (offhand) { return offHand.getId() == shieldMapping.getBedrockId(); } else { return hand.getId() == shieldMapping.getBedrockId(); } } @Override protected boolean isShaking() { return isMaxFrozenState; } @Override protected void setDimensions(Pose pose) { if (pose == Pose.SLEEPING) { setBoundingBoxWidth(0.2f); setBoundingBoxHeight(0.2f); } else { super.setDimensions(pose); } } @Override public float setFreezing(IntEntityMetadata entityMetadata) { float freezingPercentage = super.setFreezing(entityMetadata); this.isMaxFrozenState = freezingPercentage >= 1.0f; setFlag(EntityFlag.SHAKING, isShaking()); return freezingPercentage; } /** * @return a Bedrock health attribute constructed from the data sent from the server */ protected AttributeData createHealthAttribute() { // Default health needs to be specified as the max health in order for maximum hearts to show correctly on mounted entities // Round health value up, so that Bedrock doesn't consider the entity to be dead when health is between 0 and 1 return new AttributeData(GeyserAttributeType.HEALTH.getBedrockIdentifier(), 0f, this.maxHealth, (float) Math.ceil(this.health), this.maxHealth); } @Override public boolean isAlive() { return this.valid && health > 0f; } @Override public InteractionResult interact(Hand hand) { GeyserItemStack itemStack = session.getPlayerInventory().getItemInHand(hand); if (itemStack.getJavaId() == session.getItemMappings().getStoredItems().nameTag()) { InteractionResult result = checkInteractWithNameTag(itemStack); if (result.consumesAction()) { return result; } } return super.interact(hand); } /** * Checks to see if a nametag interaction would go through. */ protected final InteractionResult checkInteractWithNameTag(GeyserItemStack itemStack) { CompoundTag nbt = itemStack.getNbt(); if (nbt != null && nbt.get("display") instanceof CompoundTag displayTag && displayTag.get("Name") instanceof StringTag) { // The mob shall be named return InteractionResult.SUCCESS; } return InteractionResult.PASS; } public void updateArmor(GeyserSession session) { if (!valid) return; ItemData helmet = this.helmet; ItemData chestplate = this.chestplate; // If an entity has a banner on them, it will be in the helmet slot in Java but the chestplate spot in Bedrock // But don't overwrite the chestplate if it isn't empty ItemMapping banner = session.getItemMappings().getStoredItems().banner(); if (chestplate.getId() == ItemData.AIR.getId() && helmet.getId() == banner.getBedrockId()) { chestplate = this.helmet; helmet = ItemData.AIR; } else if (chestplate.getId() == banner.getBedrockId()) { // Prevent chestplate banners from showing erroneously chestplate = ItemData.AIR; } MobArmorEquipmentPacket armorEquipmentPacket = new MobArmorEquipmentPacket(); armorEquipmentPacket.setRuntimeEntityId(geyserId); armorEquipmentPacket.setHelmet(helmet); armorEquipmentPacket.setChestplate(chestplate); armorEquipmentPacket.setLeggings(leggings); armorEquipmentPacket.setBoots(boots); session.sendUpstreamPacket(armorEquipmentPacket); } public void updateMainHand(GeyserSession session) { if (!valid) return; MobEquipmentPacket handPacket = new MobEquipmentPacket(); handPacket.setRuntimeEntityId(geyserId); handPacket.setItem(hand); handPacket.setHotbarSlot(-1); handPacket.setInventorySlot(0); handPacket.setContainerId(ContainerId.INVENTORY); session.sendUpstreamPacket(handPacket); } public void updateOffHand(GeyserSession session) { if (!valid) return; MobEquipmentPacket offHandPacket = new MobEquipmentPacket(); offHandPacket.setRuntimeEntityId(geyserId); offHandPacket.setItem(offHand); offHandPacket.setHotbarSlot(-1); offHandPacket.setInventorySlot(0); offHandPacket.setContainerId(ContainerId.OFFHAND); session.sendUpstreamPacket(offHandPacket); } /** * Attributes are properties of an entity that are generally more runtime-based instead of permanent properties. * Movement speed, current attack damage with a weapon, current knockback resistance. * * @param attributes the Java list of attributes sent from the server */ public void updateBedrockAttributes(GeyserSession session, List attributes) { if (!valid) return; List newAttributes = new ArrayList<>(); for (Attribute attribute : attributes) { // Convert the attribute to a Bedrock version, if relevant updateAttribute(attribute, newAttributes); } if (newAttributes.isEmpty()) { // If there are Java-only attributes or only attributes that are not translated by us return; } UpdateAttributesPacket updateAttributesPacket = new UpdateAttributesPacket(); updateAttributesPacket.setRuntimeEntityId(geyserId); updateAttributesPacket.setAttributes(newAttributes); session.sendUpstreamPacket(updateAttributesPacket); } /** * Takes the Java attribute and adds it to newAttributes as a Bedrock-formatted attribute */ protected void updateAttribute(Attribute javaAttribute, List newAttributes) { if (javaAttribute.getType() instanceof AttributeType.Builtin type) { switch (type) { case GENERIC_MAX_HEALTH -> { this.maxHealth = (float) AttributeUtils.calculateValue(javaAttribute); newAttributes.add(createHealthAttribute()); } case GENERIC_ATTACK_DAMAGE -> newAttributes.add(calculateAttribute(javaAttribute, GeyserAttributeType.ATTACK_DAMAGE)); case GENERIC_FLYING_SPEED -> newAttributes.add(calculateAttribute(javaAttribute, GeyserAttributeType.FLYING_SPEED)); case GENERIC_MOVEMENT_SPEED -> newAttributes.add(calculateAttribute(javaAttribute, GeyserAttributeType.MOVEMENT_SPEED)); case GENERIC_FOLLOW_RANGE -> newAttributes.add(calculateAttribute(javaAttribute, GeyserAttributeType.FOLLOW_RANGE)); case GENERIC_KNOCKBACK_RESISTANCE -> newAttributes.add(calculateAttribute(javaAttribute, GeyserAttributeType.KNOCKBACK_RESISTANCE)); case HORSE_JUMP_STRENGTH -> newAttributes.add(calculateAttribute(javaAttribute, GeyserAttributeType.HORSE_JUMP_STRENGTH)); } } } /** * Calculates the complete attribute value to send to Bedrock. Will be overriden if attributes need to be cached. */ protected AttributeData calculateAttribute(Attribute javaAttribute, GeyserAttributeType type) { return type.getAttribute((float) AttributeUtils.calculateValue(javaAttribute)); } }