Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Implemented health scaling which is pretty useful, developers can set…
… players health to anything they want and scale it down to normal amounts so client will see that scaled health instead of the real health.
  • Loading branch information
irrld committed Apr 23, 2022
commit 1faefbb4bea801c12340020e4adc0e6fb41d874b
42 changes: 38 additions & 4 deletions src/main/java/org/cloudburstmc/server/player/CloudPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ public class CloudPlayer extends EntityHuman implements CommandSender, Inventory

public long lastSkinChange;

private int healthScale;

public CloudPlayer(BedrockServerSession session, ClientChainData chainData) {
super(EntityTypes.PLAYER, Location.from(CloudServer.getInstance().getDefaultLevel()));
this.session = session;
Expand Down Expand Up @@ -270,6 +272,39 @@ public CloudPlayer(BedrockServerSession session, ClientChainData chainData) {
this.creationTime = System.currentTimeMillis();
}

public void setHealthScale(int scale) {
this.healthScale = scale;
}

public int getHealthScale(){
return healthScale;
}

public boolean isHealthScaled(){
return healthScale > 0;
}

public float getScaledHealth(){
if (isHealthScaled()){
float health = this.health + this.absorption;
float scaledHealth = health * healthScale / getMaxHealth();
if (health > 0 && scaledHealth < 1){
scaledHealth = 1;
}
return scaledHealth;
} else {
var max = getMaxHealth();
return health > 0 ? (health < max ? health : max) : 0;
}
}

public int getScaledMaxHealth(){
return isHealthScaled() ?
healthScale :
//TODO: Remove it in future! This a hack to solve the client-side absorption bug! WFT Mojang (Half a yellow heart cannot be shown, we can test it in local gaming)
this.getAbsorption() % 2 != 0 ? this.getMaxHealth() + 1 : this.getMaxHealth(); // this.getMaxHealth();
}

public int getStartActionTick() {
return startAction;
}
Expand Down Expand Up @@ -1157,7 +1192,7 @@ public void sendAttributes() {
UpdateAttributesPacket pk = new UpdateAttributesPacket();
pk.setRuntimeEntityId(this.getRuntimeId());
List<AttributeData> attributes = pk.getAttributes();
attributes.add(NetworkUtils.attributeToNetwork(Attribute.getAttribute(Attribute.MAX_HEALTH).setMaxValue(this.getMaxHealth()).setValue(health > 0 ? (health < getMaxHealth() ? health : getMaxHealth()) : 0)));
attributes.add(NetworkUtils.attributeToNetwork(Attribute.getAttribute(Attribute.MAX_HEALTH).setMaxValue(this.getScaledMaxHealth()).setValue(getScaledHealth())));
attributes.add(NetworkUtils.attributeToNetwork(Attribute.getAttribute(Attribute.MAX_HUNGER).setValue(this.getFoodData().getLevel())));
attributes.add(NetworkUtils.attributeToNetwork(Attribute.getAttribute(Attribute.MOVEMENT_SPEED).setValue(this.getMovementSpeed())));
attributes.add(NetworkUtils.attributeToNetwork(Attribute.getAttribute(Attribute.EXPERIENCE_LEVEL).setValue(this.getExperienceLevel())));
Expand Down Expand Up @@ -2133,9 +2168,8 @@ public void setHealth(float health) {
}

super.setHealth(health);
//TODO: Remove it in future! This a hack to solve the client-side absorption bug! WFT Mojang (Half a yellow heart cannot be shown, we can test it in local gaming)
Attribute attr = Attribute.getAttribute(Attribute.MAX_HEALTH).setMaxValue(this.getAbsorption() % 2 != 0 ? this.getMaxHealth() + 1 : this.getMaxHealth()).setValue(health > 0 ? (health < getMaxHealth() ? health : getMaxHealth()) : 0);
if (this.spawned) {
Attribute attr = Attribute.getAttribute(Attribute.MAX_HEALTH).setMaxValue(this.getScaledMaxHealth()).setValue(this.getScaledHealth());
UpdateAttributesPacket packet = new UpdateAttributesPacket();
packet.getAttributes().add(NetworkUtils.attributeToNetwork(attr));
packet.setRuntimeEntityId(this.getRuntimeId());
Expand All @@ -2147,8 +2181,8 @@ public void setHealth(float health) {
public void setMaxHealth(int maxHealth) {
super.setMaxHealth(maxHealth);

Attribute attr = Attribute.getAttribute(Attribute.MAX_HEALTH).setMaxValue(this.getAbsorption() % 2 != 0 ? this.getMaxHealth() + 1 : this.getMaxHealth()).setValue(health > 0 ? (health < getMaxHealth() ? health : getMaxHealth()) : 0);
if (this.spawned) {
Attribute attr = Attribute.getAttribute(Attribute.MAX_HEALTH).setMaxValue(this.getScaledMaxHealth()).setValue(this.getScaledHealth());
UpdateAttributesPacket packet = new UpdateAttributesPacket();
packet.getAttributes().add(NetworkUtils.attributeToNetwork(attr));
packet.setRuntimeEntityId(this.getRuntimeId());
Expand Down