2019-12-01 00:19:03 +00:00
|
|
|
/*
|
2021-01-01 15:10:36 +00:00
|
|
|
* Copyright (c) 2019-2021 GeyserMC. http://geysermc.org
|
2019-12-01 00:19:03 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2021-11-20 23:29:46 +00:00
|
|
|
package org.geysermc.geyser.entity.type;
|
2019-12-01 00:19:03 +00:00
|
|
|
|
2021-11-18 03:02:38 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.type.EntityType;
|
2019-12-01 00:19:03 +00:00
|
|
|
import com.nukkitx.math.vector.Vector3f;
|
2020-10-26 15:54:37 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.data.LevelEventType;
|
|
|
|
import com.nukkitx.protocol.bedrock.data.entity.EntityFlag;
|
|
|
|
import com.nukkitx.protocol.bedrock.packet.LevelEventPacket;
|
2021-02-19 16:12:36 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.MoveEntityDeltaPacket;
|
2021-11-20 23:29:46 +00:00
|
|
|
import org.geysermc.geyser.entity.EntityDefinition;
|
2021-11-22 19:52:26 +00:00
|
|
|
import org.geysermc.geyser.session.GeyserSession;
|
2021-11-20 23:29:46 +00:00
|
|
|
import org.geysermc.geyser.level.block.BlockStateValues;
|
2020-10-26 15:54:37 +00:00
|
|
|
|
2021-11-18 03:02:38 +00:00
|
|
|
import java.util.UUID;
|
|
|
|
|
2020-11-05 23:42:33 +00:00
|
|
|
/**
|
|
|
|
* Used as a class for any object-like entity that moves as a projectile
|
|
|
|
*/
|
2021-01-05 23:41:20 +00:00
|
|
|
public class ThrowableEntity extends Entity implements Tickable {
|
2019-12-01 00:19:03 +00:00
|
|
|
|
2021-02-19 16:12:36 +00:00
|
|
|
protected Vector3f lastJavaPosition;
|
2020-10-26 15:54:37 +00:00
|
|
|
|
2021-12-21 00:25:11 +00:00
|
|
|
public ThrowableEntity(GeyserSession session, int entityId, long geyserId, UUID uuid, EntityDefinition<?> definition, Vector3f position, Vector3f motion, float yaw, float pitch, float headYaw) {
|
2021-11-18 03:02:38 +00:00
|
|
|
super(session, entityId, geyserId, uuid, definition, position, motion, yaw, pitch, headYaw);
|
2021-02-19 16:12:36 +00:00
|
|
|
this.lastJavaPosition = position;
|
2020-10-26 15:54:37 +00:00
|
|
|
}
|
|
|
|
|
2021-01-05 23:41:20 +00:00
|
|
|
/**
|
|
|
|
* Updates the position for the Bedrock client.
|
|
|
|
*
|
|
|
|
* Java clients assume the next positions of moving items. Bedrock needs to be explicitly told positions
|
|
|
|
*/
|
2020-10-26 15:54:37 +00:00
|
|
|
@Override
|
2021-11-18 03:02:38 +00:00
|
|
|
public void tick() {
|
|
|
|
moveAbsoluteImmediate(position.add(motion), yaw, pitch, headYaw, onGround, false);
|
|
|
|
float drag = getDrag();
|
|
|
|
float gravity = getGravity();
|
2020-11-05 23:42:33 +00:00
|
|
|
motion = motion.mul(drag).down(gravity);
|
|
|
|
}
|
|
|
|
|
2021-11-18 03:02:38 +00:00
|
|
|
protected void moveAbsoluteImmediate(Vector3f position, float yaw, float pitch, float headYaw, boolean isOnGround, boolean teleported) {
|
2021-02-19 16:12:36 +00:00
|
|
|
MoveEntityDeltaPacket moveEntityDeltaPacket = new MoveEntityDeltaPacket();
|
|
|
|
moveEntityDeltaPacket.setRuntimeEntityId(geyserId);
|
|
|
|
|
|
|
|
if (isOnGround) {
|
|
|
|
moveEntityDeltaPacket.getFlags().add(MoveEntityDeltaPacket.Flag.ON_GROUND);
|
|
|
|
}
|
|
|
|
setOnGround(isOnGround);
|
|
|
|
|
|
|
|
if (teleported) {
|
|
|
|
moveEntityDeltaPacket.getFlags().add(MoveEntityDeltaPacket.Flag.TELEPORTING);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.position.getX() != position.getX()) {
|
|
|
|
moveEntityDeltaPacket.getFlags().add(MoveEntityDeltaPacket.Flag.HAS_X);
|
|
|
|
moveEntityDeltaPacket.setX(position.getX());
|
|
|
|
}
|
|
|
|
if (this.position.getY() != position.getY()) {
|
|
|
|
moveEntityDeltaPacket.getFlags().add(MoveEntityDeltaPacket.Flag.HAS_Y);
|
|
|
|
moveEntityDeltaPacket.setY(position.getY());
|
|
|
|
}
|
|
|
|
if (this.position.getZ() != position.getZ()) {
|
|
|
|
moveEntityDeltaPacket.getFlags().add(MoveEntityDeltaPacket.Flag.HAS_Z);
|
|
|
|
moveEntityDeltaPacket.setZ(position.getZ());
|
|
|
|
}
|
|
|
|
setPosition(position);
|
|
|
|
|
2021-11-18 03:02:38 +00:00
|
|
|
if (this.yaw != yaw) {
|
2021-02-19 16:12:36 +00:00
|
|
|
moveEntityDeltaPacket.getFlags().add(MoveEntityDeltaPacket.Flag.HAS_YAW);
|
2021-11-18 03:02:38 +00:00
|
|
|
moveEntityDeltaPacket.setYaw(yaw);
|
|
|
|
this.yaw = yaw;
|
2021-02-19 16:12:36 +00:00
|
|
|
}
|
2021-11-18 03:02:38 +00:00
|
|
|
if (this.pitch != pitch) {
|
2021-02-19 16:12:36 +00:00
|
|
|
moveEntityDeltaPacket.getFlags().add(MoveEntityDeltaPacket.Flag.HAS_PITCH);
|
2021-11-18 03:02:38 +00:00
|
|
|
moveEntityDeltaPacket.setPitch(pitch);
|
|
|
|
this.pitch = pitch;
|
2021-02-19 16:12:36 +00:00
|
|
|
}
|
2021-11-18 03:02:38 +00:00
|
|
|
if (this.headYaw != headYaw) {
|
2021-02-19 16:12:36 +00:00
|
|
|
moveEntityDeltaPacket.getFlags().add(MoveEntityDeltaPacket.Flag.HAS_HEAD_YAW);
|
2021-11-18 03:02:38 +00:00
|
|
|
moveEntityDeltaPacket.setHeadYaw(headYaw);
|
|
|
|
this.headYaw = headYaw;
|
2021-02-19 16:12:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!moveEntityDeltaPacket.getFlags().isEmpty()) {
|
|
|
|
session.sendUpstreamPacket(moveEntityDeltaPacket);
|
|
|
|
}
|
2021-01-05 23:41:20 +00:00
|
|
|
}
|
|
|
|
|
2020-11-05 23:42:33 +00:00
|
|
|
/**
|
|
|
|
* Get the gravity of this entity type. Used for applying gravity while the entity is in motion.
|
|
|
|
*
|
|
|
|
* @return the amount of gravity to apply to this entity while in motion.
|
|
|
|
*/
|
2021-11-18 03:02:38 +00:00
|
|
|
protected float getGravity() {
|
|
|
|
if (getFlag(EntityFlag.HAS_GRAVITY)) {
|
|
|
|
switch (definition.entityType()) {
|
2021-11-27 04:03:46 +00:00
|
|
|
case POTION:
|
2020-11-05 23:42:33 +00:00
|
|
|
return 0.05f;
|
2021-11-27 04:03:46 +00:00
|
|
|
case EXPERIENCE_BOTTLE:
|
2020-11-05 23:42:33 +00:00
|
|
|
return 0.07f;
|
|
|
|
case FIREBALL:
|
2021-02-19 16:12:36 +00:00
|
|
|
case SHULKER_BULLET:
|
2020-11-05 23:42:33 +00:00
|
|
|
return 0;
|
|
|
|
case SNOWBALL:
|
2021-11-27 04:03:46 +00:00
|
|
|
case EGG:
|
|
|
|
case ENDER_PEARL:
|
2020-11-05 23:42:33 +00:00
|
|
|
return 0.03f;
|
2021-02-19 16:12:36 +00:00
|
|
|
case LLAMA_SPIT:
|
|
|
|
return 0.06f;
|
2020-11-05 23:42:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the drag that should be multiplied to the entity's motion
|
|
|
|
*/
|
2021-11-18 03:02:38 +00:00
|
|
|
protected float getDrag() {
|
|
|
|
if (isInWater()) {
|
2020-11-05 23:42:33 +00:00
|
|
|
return 0.8f;
|
|
|
|
} else {
|
2021-11-18 03:02:38 +00:00
|
|
|
switch (definition.entityType()) {
|
2021-11-27 04:03:46 +00:00
|
|
|
case POTION:
|
|
|
|
case EXPERIENCE_BOTTLE:
|
2020-11-05 23:42:33 +00:00
|
|
|
case SNOWBALL:
|
2021-11-27 04:03:46 +00:00
|
|
|
case EGG:
|
|
|
|
case ENDER_PEARL:
|
2021-02-19 16:12:36 +00:00
|
|
|
case LLAMA_SPIT:
|
2020-11-05 23:42:33 +00:00
|
|
|
return 0.99f;
|
|
|
|
case FIREBALL:
|
|
|
|
case SMALL_FIREBALL:
|
|
|
|
case DRAGON_FIREBALL:
|
|
|
|
return 0.95f;
|
2021-02-19 16:12:36 +00:00
|
|
|
case SHULKER_BULLET:
|
|
|
|
return 1;
|
2020-11-05 23:42:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return true if this entity is currently in water.
|
|
|
|
*/
|
2021-11-18 03:02:38 +00:00
|
|
|
protected boolean isInWater() {
|
2021-11-20 21:34:30 +00:00
|
|
|
int block = session.getGeyser().getWorldManager().getBlockAt(session, position.toInt());
|
2021-06-01 19:36:33 +00:00
|
|
|
return BlockStateValues.getWaterLevel(block) != -1;
|
2020-11-05 23:42:33 +00:00
|
|
|
}
|
|
|
|
|
2020-10-26 15:54:37 +00:00
|
|
|
@Override
|
2021-11-18 03:02:38 +00:00
|
|
|
public boolean despawnEntity() {
|
2021-11-27 04:03:46 +00:00
|
|
|
if (definition.entityType() == EntityType.ENDER_PEARL) {
|
2020-10-26 15:54:37 +00:00
|
|
|
LevelEventPacket particlePacket = new LevelEventPacket();
|
|
|
|
particlePacket.setType(LevelEventType.PARTICLE_TELEPORT);
|
|
|
|
particlePacket.setPosition(position);
|
|
|
|
session.sendUpstreamPacket(particlePacket);
|
|
|
|
}
|
2021-11-18 03:02:38 +00:00
|
|
|
return super.despawnEntity();
|
2020-10-26 15:54:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-11-18 03:02:38 +00:00
|
|
|
public void moveRelative(double relX, double relY, double relZ, float yaw, float pitch, float headYaw, boolean isOnGround) {
|
|
|
|
moveAbsoluteImmediate(lastJavaPosition.add(relX, relY, relZ), yaw, pitch, headYaw, isOnGround, false);
|
2021-02-19 16:12:36 +00:00
|
|
|
lastJavaPosition = position;
|
2020-10-26 15:54:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-11-18 03:02:38 +00:00
|
|
|
public void moveAbsolute(Vector3f position, float yaw, float pitch, float headYaw, boolean isOnGround, boolean teleported) {
|
|
|
|
moveAbsoluteImmediate(position, yaw, pitch, headYaw, isOnGround, teleported);
|
2021-02-19 16:12:36 +00:00
|
|
|
lastJavaPosition = position;
|
2019-12-01 00:19:03 +00:00
|
|
|
}
|
|
|
|
}
|