2019-08-03 03:38:09 +00:00
|
|
|
/*
|
2020-01-09 03:05:42 +00:00
|
|
|
* Copyright (c) 2019-2020 GeyserMC. http://geysermc.org
|
2019-08-03 03:38:09 +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
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.geysermc.connector.entity;
|
|
|
|
|
2019-11-28 01:30:30 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.metadata.EntityMetadata;
|
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.metadata.MetadataType;
|
2020-04-25 03:11:28 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position;
|
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.player.Hand;
|
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.player.PlayerAction;
|
|
|
|
import com.github.steveice10.mc.protocol.data.game.world.block.BlockFace;
|
2019-11-28 01:30:30 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.message.TextMessage;
|
2020-04-25 03:11:28 +00:00
|
|
|
import com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerActionPacket;
|
|
|
|
import com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerUseItemPacket;
|
2019-10-09 18:39:38 +00:00
|
|
|
import com.nukkitx.math.vector.Vector3f;
|
2020-05-11 05:09:16 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.data.EntityData;
|
|
|
|
import com.nukkitx.protocol.bedrock.data.EntityDataMap;
|
|
|
|
import com.nukkitx.protocol.bedrock.data.EntityFlag;
|
|
|
|
import com.nukkitx.protocol.bedrock.data.EntityFlags;
|
2019-11-28 01:30:30 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.*;
|
2019-11-30 19:26:51 +00:00
|
|
|
import it.unimi.dsi.fastutil.longs.LongOpenHashSet;
|
|
|
|
import it.unimi.dsi.fastutil.longs.LongSet;
|
2019-08-03 03:38:09 +00:00
|
|
|
import lombok.Getter;
|
|
|
|
import lombok.Setter;
|
2019-08-06 06:38:03 +00:00
|
|
|
import org.geysermc.connector.entity.attribute.Attribute;
|
|
|
|
import org.geysermc.connector.entity.attribute.AttributeType;
|
2020-05-11 05:09:16 +00:00
|
|
|
import org.geysermc.connector.entity.living.ArmorStandEntity;
|
2019-08-03 03:38:09 +00:00
|
|
|
import org.geysermc.connector.entity.type.EntityType;
|
|
|
|
import org.geysermc.connector.network.session.GeyserSession;
|
2020-04-25 03:11:28 +00:00
|
|
|
import org.geysermc.connector.network.translators.item.ItemTranslator;
|
2019-08-06 06:38:03 +00:00
|
|
|
import org.geysermc.connector.utils.AttributeUtils;
|
2019-11-28 01:30:30 +00:00
|
|
|
import org.geysermc.connector.utils.MessageUtils;
|
2019-08-03 03:38:09 +00:00
|
|
|
|
2020-05-11 05:09:16 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2019-08-03 03:38:09 +00:00
|
|
|
|
|
|
|
@Getter
|
|
|
|
@Setter
|
|
|
|
public class Entity {
|
|
|
|
protected long entityId;
|
|
|
|
protected long geyserId;
|
|
|
|
|
|
|
|
protected int dimension;
|
|
|
|
|
|
|
|
protected Vector3f position;
|
|
|
|
protected Vector3f motion;
|
2019-08-03 06:51:05 +00:00
|
|
|
|
2019-10-02 20:45:29 +00:00
|
|
|
/**
|
|
|
|
* x = Yaw, y = Pitch, z = HeadYaw
|
|
|
|
*/
|
2019-08-03 03:38:09 +00:00
|
|
|
protected Vector3f rotation;
|
|
|
|
|
2019-12-01 00:19:03 +00:00
|
|
|
protected float scale = 1;
|
2019-08-03 03:38:09 +00:00
|
|
|
|
|
|
|
protected EntityType entityType;
|
|
|
|
|
|
|
|
protected boolean valid;
|
|
|
|
|
2019-11-30 19:26:51 +00:00
|
|
|
protected LongSet passengers = new LongOpenHashSet();
|
2019-09-21 07:42:44 +00:00
|
|
|
protected Map<AttributeType, Attribute> attributes = new HashMap<>();
|
2020-02-06 00:55:34 +00:00
|
|
|
protected EntityDataMap metadata = new EntityDataMap();
|
2019-08-03 03:38:09 +00:00
|
|
|
|
|
|
|
public Entity(long entityId, long geyserId, EntityType entityType, Vector3f position, Vector3f motion, Vector3f rotation) {
|
|
|
|
this.entityId = entityId;
|
|
|
|
this.geyserId = geyserId;
|
|
|
|
this.entityType = entityType;
|
|
|
|
this.motion = motion;
|
|
|
|
this.rotation = rotation;
|
|
|
|
|
|
|
|
this.valid = false;
|
2019-10-16 02:47:28 +00:00
|
|
|
this.dimension = 0;
|
2019-10-18 07:54:20 +00:00
|
|
|
|
2020-02-15 23:39:34 +00:00
|
|
|
setPosition(position);
|
|
|
|
|
2019-10-18 07:54:20 +00:00
|
|
|
metadata.put(EntityData.SCALE, 1f);
|
2020-05-05 03:01:34 +00:00
|
|
|
metadata.put(EntityData.COLOR, 0);
|
2019-10-18 07:54:20 +00:00
|
|
|
metadata.put(EntityData.MAX_AIR, (short) 400);
|
|
|
|
metadata.put(EntityData.AIR, (short) 0);
|
|
|
|
metadata.put(EntityData.LEAD_HOLDER_EID, -1L);
|
|
|
|
metadata.put(EntityData.BOUNDING_BOX_HEIGHT, entityType.getHeight());
|
|
|
|
metadata.put(EntityData.BOUNDING_BOX_WIDTH, entityType.getWidth());
|
|
|
|
EntityFlags flags = new EntityFlags();
|
|
|
|
flags.setFlag(EntityFlag.HAS_GRAVITY, true);
|
|
|
|
flags.setFlag(EntityFlag.HAS_COLLISION, true);
|
|
|
|
flags.setFlag(EntityFlag.CAN_SHOW_NAME, true);
|
|
|
|
flags.setFlag(EntityFlag.CAN_CLIMB, true);
|
|
|
|
metadata.putFlags(flags);
|
2019-08-03 03:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void spawnEntity(GeyserSession session) {
|
|
|
|
AddEntityPacket addEntityPacket = new AddEntityPacket();
|
2020-05-05 05:09:36 +00:00
|
|
|
addEntityPacket.setIdentifier(entityType.getIdentifier());
|
2019-08-03 03:38:09 +00:00
|
|
|
addEntityPacket.setRuntimeEntityId(geyserId);
|
2019-08-03 06:51:05 +00:00
|
|
|
addEntityPacket.setUniqueEntityId(geyserId);
|
2019-08-03 03:38:09 +00:00
|
|
|
addEntityPacket.setPosition(position);
|
|
|
|
addEntityPacket.setMotion(motion);
|
2019-10-02 20:45:29 +00:00
|
|
|
addEntityPacket.setRotation(getBedrockRotation());
|
2019-08-03 03:38:09 +00:00
|
|
|
addEntityPacket.setEntityType(entityType.getType());
|
2019-10-18 07:54:20 +00:00
|
|
|
addEntityPacket.getMetadata().putAll(metadata);
|
2019-08-03 03:38:09 +00:00
|
|
|
|
|
|
|
valid = true;
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendUpstreamPacket(addEntityPacket);
|
2019-08-03 06:51:05 +00:00
|
|
|
|
2019-12-21 17:35:48 +00:00
|
|
|
session.getConnector().getLogger().debug("Spawned entity " + entityType + " at location " + position + " with id " + geyserId + " (java id " + entityId + ")");
|
2019-08-03 03:38:09 +00:00
|
|
|
}
|
|
|
|
|
2019-10-02 20:45:29 +00:00
|
|
|
/**
|
2020-04-22 06:03:46 +00:00
|
|
|
* Despawns the entity
|
|
|
|
*
|
|
|
|
* @param session The GeyserSession
|
2019-10-02 20:45:29 +00:00
|
|
|
* @return can be deleted
|
|
|
|
*/
|
|
|
|
public boolean despawnEntity(GeyserSession session) {
|
|
|
|
if (!valid) return true;
|
2019-08-03 03:38:09 +00:00
|
|
|
|
|
|
|
RemoveEntityPacket removeEntityPacket = new RemoveEntityPacket();
|
2019-08-03 06:51:05 +00:00
|
|
|
removeEntityPacket.setUniqueEntityId(geyserId);
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendUpstreamPacket(removeEntityPacket);
|
2019-09-15 04:19:13 +00:00
|
|
|
|
|
|
|
valid = false;
|
2019-10-02 20:45:29 +00:00
|
|
|
return true;
|
2019-08-03 03:38:09 +00:00
|
|
|
}
|
|
|
|
|
2020-02-15 23:39:34 +00:00
|
|
|
public void moveRelative(GeyserSession session, double relX, double relY, double relZ, float yaw, float pitch, boolean isOnGround) {
|
|
|
|
moveRelative(session, relX, relY, relZ, Vector3f.from(yaw, pitch, yaw), isOnGround);
|
2019-08-03 06:51:05 +00:00
|
|
|
}
|
2019-08-03 03:38:09 +00:00
|
|
|
|
2020-02-15 23:39:34 +00:00
|
|
|
public void moveRelative(GeyserSession session, double relX, double relY, double relZ, Vector3f rotation, boolean isOnGround) {
|
2019-10-02 20:45:29 +00:00
|
|
|
setRotation(rotation);
|
2019-10-09 18:39:38 +00:00
|
|
|
this.position = Vector3f.from(position.getX() + relX, position.getY() + relY, position.getZ() + relZ);
|
2020-02-15 23:39:34 +00:00
|
|
|
|
|
|
|
MoveEntityAbsolutePacket moveEntityPacket = new MoveEntityAbsolutePacket();
|
|
|
|
moveEntityPacket.setRuntimeEntityId(geyserId);
|
|
|
|
moveEntityPacket.setPosition(position);
|
|
|
|
moveEntityPacket.setRotation(getBedrockRotation());
|
|
|
|
moveEntityPacket.setOnGround(isOnGround);
|
|
|
|
moveEntityPacket.setTeleported(false);
|
|
|
|
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendUpstreamPacket(moveEntityPacket);
|
2019-08-03 03:38:09 +00:00
|
|
|
}
|
|
|
|
|
2020-04-29 16:06:25 +00:00
|
|
|
public void moveAbsolute(GeyserSession session, Vector3f position, float yaw, float pitch, boolean isOnGround, boolean teleported) {
|
|
|
|
moveAbsolute(session, position, Vector3f.from(yaw, pitch, yaw), isOnGround, teleported);
|
2019-08-03 06:51:05 +00:00
|
|
|
}
|
|
|
|
|
2020-04-29 16:06:25 +00:00
|
|
|
public void moveAbsolute(GeyserSession session, Vector3f position, Vector3f rotation, boolean isOnGround, boolean teleported) {
|
2019-09-25 21:52:28 +00:00
|
|
|
setPosition(position);
|
2019-10-02 20:45:29 +00:00
|
|
|
setRotation(rotation);
|
2019-10-16 02:47:28 +00:00
|
|
|
|
2020-02-15 23:39:34 +00:00
|
|
|
MoveEntityAbsolutePacket moveEntityPacket = new MoveEntityAbsolutePacket();
|
|
|
|
moveEntityPacket.setRuntimeEntityId(geyserId);
|
|
|
|
moveEntityPacket.setPosition(position);
|
|
|
|
moveEntityPacket.setRotation(getBedrockRotation());
|
|
|
|
moveEntityPacket.setOnGround(isOnGround);
|
2020-04-29 16:06:25 +00:00
|
|
|
moveEntityPacket.setTeleported(teleported);
|
2020-02-15 23:39:34 +00:00
|
|
|
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendUpstreamPacket(moveEntityPacket);
|
2019-08-03 03:38:09 +00:00
|
|
|
}
|
|
|
|
|
2019-08-06 06:38:03 +00:00
|
|
|
public void updateBedrockAttributes(GeyserSession session) {
|
2020-02-14 02:04:22 +00:00
|
|
|
if (!valid) return;
|
|
|
|
|
2019-08-06 06:38:03 +00:00
|
|
|
List<com.nukkitx.protocol.bedrock.data.Attribute> attributes = new ArrayList<>();
|
|
|
|
for (Map.Entry<AttributeType, Attribute> entry : this.attributes.entrySet()) {
|
|
|
|
if (!entry.getValue().getType().isBedrockAttribute())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
attributes.add(AttributeUtils.getBedrockAttribute(entry.getValue()));
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdateAttributesPacket updateAttributesPacket = new UpdateAttributesPacket();
|
|
|
|
updateAttributesPacket.setRuntimeEntityId(geyserId);
|
|
|
|
updateAttributesPacket.setAttributes(attributes);
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendUpstreamPacket(updateAttributesPacket);
|
2019-11-28 01:30:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession session) {
|
|
|
|
switch (entityMetadata.getId()) {
|
|
|
|
case 0:
|
|
|
|
if (entityMetadata.getType() == MetadataType.BYTE) {
|
|
|
|
byte xd = (byte) entityMetadata.getValue();
|
|
|
|
metadata.getFlags().setFlag(EntityFlag.ON_FIRE, (xd & 0x01) == 0x01);
|
|
|
|
metadata.getFlags().setFlag(EntityFlag.SNEAKING, (xd & 0x02) == 0x02);
|
|
|
|
metadata.getFlags().setFlag(EntityFlag.SPRINTING, (xd & 0x08) == 0x08);
|
|
|
|
metadata.getFlags().setFlag(EntityFlag.SWIMMING, (xd & 0x10) == 0x10);
|
|
|
|
metadata.getFlags().setFlag(EntityFlag.GLIDING, (xd & 0x80) == 0x80);
|
2020-04-25 03:11:28 +00:00
|
|
|
|
2020-05-11 05:09:16 +00:00
|
|
|
if ((xd & 0x20) == 0x20) {
|
|
|
|
if (this.is(ArmorStandEntity.class)) {
|
|
|
|
metadata.put(EntityData.SCALE, 0.0f);
|
|
|
|
} else {
|
|
|
|
metadata.getFlags().setFlag(EntityFlag.INVISIBLE, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-25 03:11:28 +00:00
|
|
|
// Shield code
|
|
|
|
if (session.getPlayerEntity().getEntityId() == entityId && metadata.getFlags().getFlag(EntityFlag.SNEAKING)) {
|
|
|
|
if ((session.getInventory().getItemInHand() != null && session.getInventory().getItemInHand().getId() == ItemTranslator.SHIELD) ||
|
|
|
|
(session.getInventoryCache().getPlayerInventory().getItem(45) != null && session.getInventoryCache().getPlayerInventory().getItem(45).getId() == ItemTranslator.SHIELD)) {
|
|
|
|
ClientPlayerUseItemPacket useItemPacket;
|
|
|
|
metadata.getFlags().setFlag(EntityFlag.BLOCKING, true);
|
|
|
|
if (session.getInventory().getItemInHand() != null && session.getInventory().getItemInHand().getId() == ItemTranslator.SHIELD) {
|
|
|
|
useItemPacket = new ClientPlayerUseItemPacket(Hand.MAIN_HAND);
|
|
|
|
}
|
|
|
|
// Else we just assume it's the offhand, to simplify logic and to assure the packet gets sent
|
|
|
|
else {
|
|
|
|
useItemPacket = new ClientPlayerUseItemPacket(Hand.OFF_HAND);
|
|
|
|
}
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendDownstreamPacket(useItemPacket);
|
2020-04-25 03:11:28 +00:00
|
|
|
}
|
|
|
|
} else if (session.getPlayerEntity().getEntityId() == entityId && !metadata.getFlags().getFlag(EntityFlag.SNEAKING) && metadata.getFlags().getFlag(EntityFlag.BLOCKING)) {
|
2020-05-11 05:09:16 +00:00
|
|
|
metadata.getFlags().setFlag(EntityFlag.BLOCKING, false);
|
|
|
|
metadata.getFlags().setFlag(EntityFlag.DISABLE_BLOCKING, true);
|
|
|
|
ClientPlayerActionPacket releaseItemPacket = new ClientPlayerActionPacket(PlayerAction.RELEASE_USE_ITEM, new Position(0, 0, 0), BlockFace.DOWN);
|
|
|
|
session.sendDownstreamPacket(releaseItemPacket);
|
|
|
|
}
|
2019-11-28 01:30:30 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2: // custom name
|
|
|
|
TextMessage name = (TextMessage) entityMetadata.getValue();
|
|
|
|
if (name != null)
|
|
|
|
metadata.put(EntityData.NAMETAG, MessageUtils.getBedrockMessage(name));
|
|
|
|
break;
|
|
|
|
case 3: // is custom name visible
|
2020-02-14 02:04:22 +00:00
|
|
|
if (!this.is(PlayerEntity.class))
|
|
|
|
metadata.put(EntityData.ALWAYS_SHOW_NAMETAG, (byte) ((boolean) entityMetadata.getValue() ? 1 : 0));
|
2019-11-28 01:30:30 +00:00
|
|
|
break;
|
|
|
|
case 4: // silent
|
|
|
|
metadata.getFlags().setFlag(EntityFlag.SILENT, (boolean) entityMetadata.getValue());
|
|
|
|
break;
|
|
|
|
case 5: // no gravity
|
|
|
|
metadata.getFlags().setFlag(EntityFlag.HAS_GRAVITY, !(boolean) entityMetadata.getValue());
|
|
|
|
break;
|
2020-04-25 03:11:28 +00:00
|
|
|
case 7: // blocking
|
|
|
|
if (entityMetadata.getType() == MetadataType.BYTE) {
|
|
|
|
byte xd = (byte) entityMetadata.getValue();
|
|
|
|
metadata.getFlags().setFlag(EntityFlag.BLOCKING, (xd & 0x01) == 0x01);
|
|
|
|
}
|
|
|
|
break;
|
2019-11-28 01:30:30 +00:00
|
|
|
}
|
2019-09-16 01:25:16 +00:00
|
|
|
|
2020-02-14 02:04:22 +00:00
|
|
|
updateBedrockMetadata(session);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void updateBedrockMetadata(GeyserSession session) {
|
|
|
|
if (!valid) return;
|
|
|
|
|
2019-09-16 01:25:16 +00:00
|
|
|
SetEntityDataPacket entityDataPacket = new SetEntityDataPacket();
|
|
|
|
entityDataPacket.setRuntimeEntityId(geyserId);
|
2019-10-18 07:54:20 +00:00
|
|
|
entityDataPacket.getMetadata().putAll(metadata);
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendUpstreamPacket(entityDataPacket);
|
2019-08-06 06:38:03 +00:00
|
|
|
}
|
|
|
|
|
2019-10-02 20:45:29 +00:00
|
|
|
/**
|
|
|
|
* x = Pitch, y = HeadYaw, z = Yaw
|
2020-05-11 05:09:16 +00:00
|
|
|
*
|
2020-04-22 06:03:46 +00:00
|
|
|
* @return the bedrock rotation
|
2019-10-02 20:45:29 +00:00
|
|
|
*/
|
|
|
|
public Vector3f getBedrockRotation() {
|
2019-10-09 18:39:38 +00:00
|
|
|
return Vector3f.from(rotation.getY(), rotation.getZ(), rotation.getX());
|
2019-10-02 20:45:29 +00:00
|
|
|
}
|
|
|
|
|
2019-09-25 21:52:28 +00:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public <I extends Entity> I as(Class<I> entityClass) {
|
|
|
|
return entityClass.isInstance(this) ? (I) this : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public <I extends Entity> boolean is(Class<I> entityClass) {
|
|
|
|
return entityClass.isInstance(this);
|
|
|
|
}
|
2019-08-03 03:38:09 +00:00
|
|
|
}
|