Skip to content

Commit

Permalink
Strider mounting fixes; update mappings
Browse files Browse the repository at this point in the history
This commit refactors health visual logic to make it a global system for each living entity.
  • Loading branch information
Camotoy committed Jun 30, 2020
1 parent eb3bde1 commit ba6adc9
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,23 @@

import com.github.steveice10.mc.protocol.data.game.entity.metadata.EntityMetadata;
import com.nukkitx.math.vector.Vector3f;
import com.nukkitx.protocol.bedrock.data.AttributeData;
import com.nukkitx.protocol.bedrock.data.entity.EntityData;
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.Getter;
import lombok.Setter;
import org.geysermc.connector.entity.attribute.AttributeType;
import org.geysermc.connector.entity.type.EntityType;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.utils.AttributeUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Getter
@Setter
Expand Down Expand Up @@ -98,4 +106,34 @@ public void updateEquipment(GeyserSession session) {
session.sendUpstreamPacket(handPacket);
session.sendUpstreamPacket(offHandPacket);
}

@Override
public void updateBedrockAttributes(GeyserSession session) {
if (!valid) return;

float maxHealth = this.attributes.containsKey(AttributeType.MAX_HEALTH) ? this.attributes.get(AttributeType.MAX_HEALTH).getValue() : getDefaultMaxHealth();

List<AttributeData> attributes = new ArrayList<>();
for (Map.Entry<AttributeType, org.geysermc.connector.entity.attribute.Attribute> entry : this.attributes.entrySet()) {
if (!entry.getValue().getType().isBedrockAttribute())
continue;

attributes.add(AttributeUtils.getBedrockAttribute(entry.getValue()));
}
// Add health attribute to properly show hearts when mounting
attributes.add(new AttributeData("minecraft:health", 0.0f, maxHealth, metadata.getFloat(EntityData.HEALTH, 20f), maxHealth));

UpdateAttributesPacket updateAttributesPacket = new UpdateAttributesPacket();
updateAttributesPacket.setRuntimeEntityId(geyserId);
updateAttributesPacket.setAttributes(attributes);
session.sendUpstreamPacket(updateAttributesPacket);
}

/**
* Used for the health visual when mounting an entity.
* @return the default maximum health for the entity.
*/
protected float getDefaultMaxHealth() {
return 20f;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,18 @@

import com.github.steveice10.mc.protocol.data.game.entity.metadata.EntityMetadata;
import com.nukkitx.math.vector.Vector3f;
import com.nukkitx.protocol.bedrock.data.AttributeData;
import com.nukkitx.protocol.bedrock.data.entity.EntityFlag;
import com.nukkitx.protocol.bedrock.packet.UpdateAttributesPacket;
import org.geysermc.connector.entity.attribute.AttributeType;
import org.geysermc.connector.entity.type.EntityType;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.utils.AttributeUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class PigEntity extends AnimalEntity {

// For updating the pig heart visual easier
private float health = 20f;

public PigEntity(long entityId, long geyserId, EntityType entityType, Vector3f position, Vector3f motion, Vector3f rotation) {
super(entityId, geyserId, entityType, position, motion, rotation);
}

@Override
public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession session) {
if (entityMetadata.getId() == 8) {
health = (float) entityMetadata.getValue();
updateBedrockAttributes(session);
}

if (entityMetadata.getId() == 16) {
metadata.getFlags().setFlag(EntityFlag.SADDLED, (boolean) entityMetadata.getValue());
Expand All @@ -62,23 +47,7 @@ public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession s
}

@Override
public void updateBedrockAttributes(GeyserSession session) {
if (!valid) return;

float maxHealth = attributes.containsKey(AttributeType.MAX_HEALTH) ? attributes.get(AttributeType.MAX_HEALTH).getValue() : 20f;

List<AttributeData> attributesLocal = new ArrayList<>();
for (Map.Entry<AttributeType, org.geysermc.connector.entity.attribute.Attribute> entry : this.attributes.entrySet()) {
if (!entry.getValue().getType().isBedrockAttribute())
continue;

attributesLocal.add(AttributeUtils.getBedrockAttribute(entry.getValue()));
}
attributesLocal.add(new AttributeData("minecraft:health", 0.0f, maxHealth, health, maxHealth));

UpdateAttributesPacket updateAttributesPacket = new UpdateAttributesPacket();
updateAttributesPacket.setRuntimeEntityId(geyserId);
updateAttributesPacket.setAttributes(attributesLocal);
session.sendUpstreamPacket(updateAttributesPacket);
protected float getDefaultMaxHealth() {
return 10f;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,20 @@

import com.github.steveice10.mc.protocol.data.game.entity.metadata.EntityMetadata;
import com.nukkitx.math.vector.Vector3f;
import com.nukkitx.protocol.bedrock.data.AttributeData;
import com.nukkitx.protocol.bedrock.data.entity.EntityFlag;
import com.nukkitx.protocol.bedrock.packet.UpdateAttributesPacket;
import org.geysermc.connector.entity.attribute.AttributeType;
import org.geysermc.connector.entity.living.animal.AnimalEntity;
import org.geysermc.connector.entity.type.EntityType;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.utils.AttributeUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class AbstractHorseEntity extends AnimalEntity {

// For updating the horse visual easier
private float health = 20f;

public AbstractHorseEntity(long entityId, long geyserId, EntityType entityType, Vector3f position, Vector3f motion, Vector3f rotation) {
super(entityId, geyserId, entityType, position, motion, rotation);
}

@Override
public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession session) {

if (entityMetadata.getId() == 8) {
health = (float) entityMetadata.getValue();
updateBedrockAttributes(session);
}

if (entityMetadata.getId() == 16) {
byte xd = (byte) entityMetadata.getValue();
metadata.getFlags().setFlag(EntityFlag.TAMED, (xd & 0x02) == 0x02);
Expand All @@ -71,25 +55,4 @@ public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession s

super.updateBedrockMetadata(entityMetadata, session);
}

@Override
public void updateBedrockAttributes(GeyserSession session) {
if (!valid) return;

float maxHealth = attributes.containsKey(AttributeType.MAX_HEALTH) ? attributes.get(AttributeType.MAX_HEALTH).getValue() : 20f;

List<AttributeData> attributesLocal = new ArrayList<>();
for (Map.Entry<AttributeType, org.geysermc.connector.entity.attribute.Attribute> entry : this.attributes.entrySet()) {
if (!entry.getValue().getType().isBedrockAttribute())
continue;

attributesLocal.add(AttributeUtils.getBedrockAttribute(entry.getValue()));
}
attributesLocal.add(new AttributeData("minecraft:health", 0.0f, maxHealth, health, maxHealth));

UpdateAttributesPacket updateAttributesPacket = new UpdateAttributesPacket();
updateAttributesPacket.setRuntimeEntityId(geyserId);
updateAttributesPacket.setAttributes(attributesLocal);
session.sendUpstreamPacket(updateAttributesPacket);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,14 @@ private void updateOffset(Entity passenger, EntityType mountType, GeyserSession
case ARMOR_STAND:
yOffset = 1.3f;
break;
case STRIDER:
yOffset = 2.8200102f;
break;
}
Vector3f offset = Vector3f.from(0f, yOffset, 0f);
if (mountType == EntityType.STRIDER) {
offset = offset.add(0f, 0f, -0.2f);
}
// Without the X offset, more than one entity on a boat is stacked on top of each other
if (rider && moreThanOneEntity) {
offset = offset.add(Vector3f.from(0.2, 0, 0));
Expand Down
2 changes: 1 addition & 1 deletion connector/src/main/resources/mappings
Submodule mappings updated 2 files
+70 −9 blocks.json
+4 −4 items.json

0 comments on commit ba6adc9

Please sign in to comment.