From e2ce553d3acad24042d6bb0c33d262bd00eea426 Mon Sep 17 00:00:00 2001 From: Camotoy <20743703+Camotoy@users.noreply.github.com> Date: Thu, 26 May 2022 18:41:19 -0400 Subject: [PATCH] Warden time --- .../geyser/entity/EntityDefinitions.java | 6 ++ .../type/living/monster/WardenEntity.java | 58 +++++++++++++++++++ .../org/geysermc/geyser/util/MathUtils.java | 19 ++++++ 3 files changed, 83 insertions(+) create mode 100644 core/src/main/java/org/geysermc/geyser/entity/type/living/monster/WardenEntity.java diff --git a/core/src/main/java/org/geysermc/geyser/entity/EntityDefinitions.java b/core/src/main/java/org/geysermc/geyser/entity/EntityDefinitions.java index 5d1a00968..198710349 100644 --- a/core/src/main/java/org/geysermc/geyser/entity/EntityDefinitions.java +++ b/core/src/main/java/org/geysermc/geyser/entity/EntityDefinitions.java @@ -156,6 +156,7 @@ public final class EntityDefinitions { public static final EntityDefinition VILLAGER; public static final EntityDefinition VINDICATOR; public static final EntityDefinition WANDERING_TRADER; + public static final EntityDefinition WARDEN; public static final EntityDefinition WITCH; public static final EntityDefinition WITHER; public static final EntityDefinition WITHER_SKELETON; @@ -561,6 +562,11 @@ public final class EntityDefinitions { .height(0.8f).width(0.4f) .addTranslator(MetadataType.BYTE, VexEntity::setVexFlags) .build(); + WARDEN = EntityDefinition.inherited(WardenEntity::new, mobEntityBase) + .type(EntityType.WARDEN) + .height(2.9f).width(0.9f) + .addTranslator(MetadataType.INT, WardenEntity::setAngerLevel) + .build(); WITHER = EntityDefinition.inherited(WitherEntity::new, mobEntityBase) .type(EntityType.WITHER) .height(3.5f).width(0.9f) diff --git a/core/src/main/java/org/geysermc/geyser/entity/type/living/monster/WardenEntity.java b/core/src/main/java/org/geysermc/geyser/entity/type/living/monster/WardenEntity.java new file mode 100644 index 000000000..3a8e9d351 --- /dev/null +++ b/core/src/main/java/org/geysermc/geyser/entity/type/living/monster/WardenEntity.java @@ -0,0 +1,58 @@ +/* + * 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.living.monster; + +import com.github.steveice10.mc.protocol.data.game.entity.metadata.Pose; +import com.github.steveice10.mc.protocol.data.game.entity.metadata.type.IntEntityMetadata; +import com.nukkitx.math.GenericMath; +import com.nukkitx.math.vector.Vector3f; +import com.nukkitx.protocol.bedrock.data.entity.EntityData; +import com.nukkitx.protocol.bedrock.data.entity.EntityFlag; +import org.geysermc.geyser.entity.EntityDefinition; +import org.geysermc.geyser.session.GeyserSession; +import org.geysermc.geyser.util.MathUtils; + +import java.util.UUID; + +public class WardenEntity extends MonsterEntity { + public WardenEntity(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 + public void setPose(Pose pose) { + setFlag(EntityFlag.DIGGING, pose == Pose.DIGGING); + setFlag(EntityFlag.EMERGING, pose == Pose.EMERGING); + setFlag(EntityFlag.ROARING, pose == Pose.ROARING); + setFlag(EntityFlag.SNIFFING, pose == Pose.SNIFFING); + super.setPose(pose); + } + + public void setAngerLevel(IntEntityMetadata entityMetadata) { + float anger = (float) entityMetadata.getPrimitiveValue() / 80f; + dirtyMetadata.put(EntityData.HEARTBEAT_INTERVAL_TICKS, 40 - GenericMath.floor(MathUtils.clamp(anger, 0.0F, 1.0F) * 30F)); + } +} diff --git a/core/src/main/java/org/geysermc/geyser/util/MathUtils.java b/core/src/main/java/org/geysermc/geyser/util/MathUtils.java index 50cfa001e..a89240f25 100644 --- a/core/src/main/java/org/geysermc/geyser/util/MathUtils.java +++ b/core/src/main/java/org/geysermc/geyser/util/MathUtils.java @@ -101,6 +101,25 @@ public class MathUtils { return num; } + /** + * Clamps the value between the low and high boundaries + * Copied from {@link com.nukkitx.math.GenericMath} with floats instead. + * + * @param value The value to clamp + * @param low The low bound of the clamp + * @param high The high bound of the clamp + * @return the clamped value + */ + public static double clamp(float value, float low, float high) { + if (value < low) { + return low; + } + if (value > high) { + return high; + } + return value; + } + /** * Ensures the resulting object is a byte. Java Edition does not care whether a byte is encoded as an integer or not; * it converts it into a byte anyway.