Skip to content

Commit

Permalink
Implement entity shaking when freezing and more
Browse files Browse the repository at this point in the history
- When freezing ticks is at 140 or more, entities will shake
- Implement skeleton and zombie conversion shaking
  • Loading branch information
Camotoy committed Jun 18, 2021
1 parent 3c9f628 commit 35450f7
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 15 deletions.
20 changes: 17 additions & 3 deletions connector/src/main/java/org/geysermc/connector/entity/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ public class Entity {
*/
protected boolean onGround;

protected float scale = 1;

protected EntityType entityType;

protected boolean valid;
Expand Down Expand Up @@ -309,7 +307,7 @@ public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession s
// The value that Java edition gives us is in ticks, but Bedrock uses a float percentage of the strength 0.0 -> 1.0
// The Java client caps its freezing tick percentage at 140
int freezingTicks = Math.min((int) entityMetadata.getValue(), 140);
metadata.put(EntityData.FREEZING_EFFECT_STRENGTH, (freezingTicks / 140f));
setFreezing(session, freezingTicks / 140f);
break;
}
}
Expand All @@ -327,6 +325,15 @@ public void updateBedrockMetadata(GeyserSession session) {
session.sendUpstreamPacket(entityDataPacket);
}

/**
* If true, the entity should be shaking on the client's end.
*
* @return whether {@link EntityFlag#SHAKING} should be set to true.
*/
protected boolean isShaking(GeyserSession session) {
return false;
}

/**
* Set the height and width of the entity's bounding box
*/
Expand All @@ -336,6 +343,13 @@ protected void setDimensions(Pose pose) {
metadata.put(EntityData.BOUNDING_BOX_HEIGHT, entityType.getHeight());
}

/**
* Set a float from 0-1 - how strong the "frozen" overlay should be on screen.
*/
protected void setFreezing(GeyserSession session, float amount) {
metadata.put(EntityData.FREEZING_EFFECT_STRENGTH, amount);
}

/**
* x = Pitch, y = HeadYaw, z = Yaw
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ public class LivingEntity extends Entity {
protected ItemData hand = ItemData.AIR;
protected ItemData offHand = ItemData.AIR;

/**
* A convenience variable for if the entity has reached the maximum frozen ticks and should be shaking
*/
private boolean isMaxFrozenState = false;

public LivingEntity(long entityId, long geyserId, EntityType entityType, Vector3f position, Vector3f motion, Vector3f rotation) {
super(entityId, geyserId, entityType, position, motion, rotation);
}
Expand Down Expand Up @@ -112,6 +117,11 @@ public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession s
super.updateBedrockMetadata(entityMetadata, session);
}

@Override
protected boolean isShaking(GeyserSession session) {
return isMaxFrozenState;
}

@Override
protected void setDimensions(Pose pose) {
if (pose == Pose.SLEEPING) {
Expand All @@ -122,6 +132,13 @@ protected void setDimensions(Pose pose) {
}
}

@Override
protected void setFreezing(GeyserSession session, float amount) {
super.setFreezing(session, amount);
this.isMaxFrozenState = amount >= 1.0f;
metadata.getFlags().setFlag(EntityFlag.SHAKING, isShaking(session));
}

public void updateAllEquipment(GeyserSession session) {
if (!valid) return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.geysermc.connector.utils.DimensionUtils;

public class HoglinEntity extends AnimalEntity {
private boolean isImmuneToZombification;

public HoglinEntity(long entityId, long geyserId, EntityType entityType, Vector3f position, Vector3f motion, Vector3f rotation) {
super(entityId, geyserId, entityType, position, motion, rotation);
Expand All @@ -44,11 +45,17 @@ public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession s
if (entityMetadata.getId() == 17) {
// Immune to zombification?
// Apply shaking effect if not in the nether and zombification is possible
metadata.getFlags().setFlag(EntityFlag.SHAKING, !((boolean) entityMetadata.getValue()) && !session.getDimension().equals(DimensionUtils.NETHER));
this.isImmuneToZombification = (boolean) entityMetadata.getValue();
metadata.getFlags().setFlag(EntityFlag.SHAKING, isShaking(session));
}
super.updateBedrockMetadata(entityMetadata, session);
}

@Override
protected boolean isShaking(GeyserSession session) {
return (!isImmuneToZombification && !session.getDimension().equals(DimensionUtils.NETHER)) || super.isShaking(session);
}

@Override
public boolean canEat(GeyserSession session, String javaIdentifierStripped, ItemEntry itemEntry) {
return javaIdentifierStripped.equals("crimson_fungus");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

public class StriderEntity extends AnimalEntity {

private boolean shaking = false;
private boolean isCold = false;

public StriderEntity(long entityId, long geyserId, EntityType entityType, Vector3f position, Vector3f motion, Vector3f rotation) {
super(entityId, geyserId, entityType, position, motion, rotation);
Expand All @@ -47,7 +47,7 @@ public StriderEntity(long entityId, long geyserId, EntityType entityType, Vector
@Override
public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession session) {
if (entityMetadata.getId() == 18) {
shaking = (boolean) entityMetadata.getValue();
isCold = (boolean) entityMetadata.getValue();
}
if (entityMetadata.getId() == 19) {
metadata.getFlags().setFlag(EntityFlag.SADDLED, (boolean) entityMetadata.getValue());
Expand All @@ -72,8 +72,8 @@ public void updateBedrockMetadata(GeyserSession session) {
metadata.getFlags().setFlag(EntityFlag.BREATHING, !parentShaking);
metadata.getFlags().setFlag(EntityFlag.SHAKING, parentShaking);
} else {
metadata.getFlags().setFlag(EntityFlag.BREATHING, !shaking);
metadata.getFlags().setFlag(EntityFlag.SHAKING, shaking);
metadata.getFlags().setFlag(EntityFlag.BREATHING, !isCold);
metadata.getFlags().setFlag(EntityFlag.SHAKING, isShaking(session));
}

// Update the passengers if we have any
Expand All @@ -87,6 +87,11 @@ public void updateBedrockMetadata(GeyserSession session) {
super.updateBedrockMetadata(session);
}

@Override
protected boolean isShaking(GeyserSession session) {
return isCold || super.isShaking(session);
}

@Override
public boolean canEat(GeyserSession session, String javaIdentifierStripped, ItemEntry itemEntry) {
return javaIdentifierStripped.equals("warped_fungus");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.geysermc.connector.utils.DimensionUtils;

public class BasePiglinEntity extends MonsterEntity {
private boolean isImmuneToZombification;

public BasePiglinEntity(long entityId, long geyserId, EntityType entityType, Vector3f position, Vector3f motion, Vector3f rotation) {
super(entityId, geyserId, entityType, position, motion, rotation);
Expand All @@ -43,8 +44,14 @@ public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession s
if (entityMetadata.getId() == 16) {
// Immune to zombification?
// Apply shaking effect if not in the nether and zombification is possible
metadata.getFlags().setFlag(EntityFlag.SHAKING, !((boolean) entityMetadata.getValue()) && !session.getDimension().equals(DimensionUtils.NETHER));
this.isImmuneToZombification = (boolean) entityMetadata.getValue();
metadata.getFlags().setFlag(EntityFlag.SHAKING, isShaking(session));
}
super.updateBedrockMetadata(entityMetadata, session);
}

@Override
protected boolean isShaking(GeyserSession session) {
return (!isImmuneToZombification && !session.getDimension().equals(DimensionUtils.NETHER)) || super.isShaking(session);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2019-2021 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.connector.entity.living.monster;

import com.github.steveice10.mc.protocol.data.game.entity.metadata.EntityMetadata;
import com.nukkitx.math.vector.Vector3f;
import com.nukkitx.protocol.bedrock.data.entity.EntityFlag;
import org.geysermc.connector.entity.type.EntityType;
import org.geysermc.connector.network.session.GeyserSession;

public class SkeletonEntity extends AbstractSkeletonEntity {
private boolean convertingToStray = false;

public SkeletonEntity(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) {
super.updateBedrockMetadata(entityMetadata, session);
if (entityMetadata.getId() == 16) {
this.convertingToStray = (boolean) entityMetadata.getValue();
metadata.getFlags().setFlag(EntityFlag.SHAKING, isShaking(session));
}
}

@Override
protected boolean isShaking(GeyserSession session) {
return convertingToStray;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.geysermc.connector.network.session.GeyserSession;

public class ZombieEntity extends MonsterEntity {
private boolean convertingToDrowned = false;

public ZombieEntity(long entityId, long geyserId, EntityType entityType, Vector3f position, Vector3f motion, Vector3f rotation) {
super(entityId, geyserId, entityType, position, motion, rotation);
Expand All @@ -42,11 +43,17 @@ public ZombieEntity(long entityId, long geyserId, EntityType entityType, Vector3
public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession session) {
if (entityMetadata.getId() == 16) {
boolean isBaby = (boolean) entityMetadata.getValue();
if (isBaby) {
metadata.put(EntityData.SCALE, .55f);
metadata.getFlags().setFlag(EntityFlag.BABY, true);
}
metadata.put(EntityData.SCALE, isBaby ? .55f : 1.0f);
metadata.getFlags().setFlag(EntityFlag.BABY, isBaby);
} else if (entityMetadata.getId() == 18) {
convertingToDrowned = (boolean) entityMetadata.getValue();
metadata.getFlags().setFlag(EntityFlag.SHAKING, isShaking(session));
}
super.updateBedrockMetadata(entityMetadata, session);
}

@Override
protected boolean isShaking(GeyserSession session) {
return convertingToDrowned || super.isShaking(session);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.geysermc.connector.network.session.GeyserSession;

public class ZombieVillagerEntity extends ZombieEntity {
private boolean isTransforming;

public ZombieVillagerEntity(long entityId, long geyserId, EntityType entityType, Vector3f position, Vector3f motion, Vector3f rotation) {
super(entityId, geyserId, entityType, position, motion, rotation);
Expand All @@ -43,8 +44,9 @@ public ZombieVillagerEntity(long entityId, long geyserId, EntityType entityType,
@Override
public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession session) {
if (entityMetadata.getId() == 19) {
isTransforming = (boolean) entityMetadata.getValue();
metadata.getFlags().setFlag(EntityFlag.IS_TRANSFORMING, (boolean) entityMetadata.getValue());
metadata.getFlags().setFlag(EntityFlag.SHAKING, (boolean) entityMetadata.getValue());
metadata.getFlags().setFlag(EntityFlag.SHAKING, isShaking(session));
}
if (entityMetadata.getId() == 20) {
VillagerData villagerData = (VillagerData) entityMetadata.getValue();
Expand All @@ -55,4 +57,9 @@ public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession s
}
super.updateBedrockMetadata(entityMetadata, session);
}

@Override
protected boolean isShaking(GeyserSession session) {
return isTransforming || super.isShaking(session);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public enum EntityType {
ZOMBIE(ZombieEntity.class, 32, 1.8f, 0.6f, 0.6f, 1.62f),
GIANT(GiantEntity.class, 32, 1.8f, 0.6f, 0.6f, 1.62f, "minecraft:zombie"),
CREEPER(CreeperEntity.class, 33, 1.7f, 0.6f, 0.6f, 1.62f),
SKELETON(AbstractSkeletonEntity.class, 34, 1.8f, 0.6f, 0.6f, 1.62f),
SKELETON(SkeletonEntity.class, 34, 1.8f, 0.6f, 0.6f, 1.62f),
SPIDER(SpiderEntity.class, 35, 0.9f, 1.4f, 1.4f, 1f),
ZOMBIFIED_PIGLIN(ZombifiedPiglinEntity.class, 36, 1.95f, 0.6f, 0.6f, 1.62f, "minecraft:zombie_pigman"),
SLIME(SlimeEntity.class, 37, 0.51f),
Expand Down

0 comments on commit 35450f7

Please sign in to comment.