2019-08-03 03:38:09 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019 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;
|
|
|
|
|
2019-08-06 06:38:03 +00:00
|
|
|
import com.github.steveice10.mc.protocol.packet.ingame.server.entity.ServerEntityPropertiesPacket;
|
2019-10-09 18:39:38 +00:00
|
|
|
import com.nukkitx.math.vector.Vector3f;
|
2019-08-03 03:38:09 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.data.EntityData;
|
|
|
|
import com.nukkitx.protocol.bedrock.data.EntityDataDictionary;
|
2019-09-13 10:49:18 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.data.EntityFlag;
|
|
|
|
import com.nukkitx.protocol.bedrock.data.EntityFlags;
|
2019-08-03 03:38:09 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.AddEntityPacket;
|
|
|
|
import com.nukkitx.protocol.bedrock.packet.RemoveEntityPacket;
|
2019-09-16 01:25:16 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.SetEntityDataPacket;
|
2019-08-06 06:38:03 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.UpdateAttributesPacket;
|
2019-08-03 03:38:09 +00:00
|
|
|
import lombok.Getter;
|
|
|
|
import lombok.Setter;
|
2019-08-03 06:51:05 +00:00
|
|
|
import org.geysermc.connector.console.GeyserLogger;
|
2019-08-06 06:38:03 +00:00
|
|
|
import org.geysermc.connector.entity.attribute.Attribute;
|
|
|
|
import org.geysermc.connector.entity.attribute.AttributeType;
|
2019-08-03 03:38:09 +00:00
|
|
|
import org.geysermc.connector.entity.type.EntityType;
|
|
|
|
import org.geysermc.connector.network.session.GeyserSession;
|
2019-08-06 06:38:03 +00:00
|
|
|
import org.geysermc.connector.utils.AttributeUtils;
|
2019-08-03 03:38:09 +00:00
|
|
|
|
2019-09-13 10:49:18 +00:00
|
|
|
import java.util.*;
|
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;
|
|
|
|
|
|
|
|
protected int scale = 1;
|
2019-08-03 06:51:05 +00:00
|
|
|
protected boolean movePending;
|
2019-08-03 03:38:09 +00:00
|
|
|
|
|
|
|
protected EntityType entityType;
|
|
|
|
|
|
|
|
protected boolean valid;
|
|
|
|
|
2019-09-21 07:42:44 +00:00
|
|
|
protected Set<Long> passengers = new HashSet<>();
|
|
|
|
protected Map<AttributeType, Attribute> attributes = new HashMap<>();
|
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.position = position;
|
|
|
|
this.motion = motion;
|
|
|
|
this.rotation = rotation;
|
|
|
|
|
|
|
|
this.valid = false;
|
2019-08-03 06:51:05 +00:00
|
|
|
this.movePending = false;
|
2019-10-16 02:47:28 +00:00
|
|
|
this.dimension = 0;
|
2019-08-03 03:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void spawnEntity(GeyserSession session) {
|
|
|
|
AddEntityPacket addEntityPacket = new AddEntityPacket();
|
2019-08-03 06:51:05 +00:00
|
|
|
addEntityPacket.setIdentifier("minecraft:" + entityType.name().toLowerCase());
|
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-08-03 06:51:05 +00:00
|
|
|
addEntityPacket.getMetadata().putAll(getMetadata());
|
2019-08-03 03:38:09 +00:00
|
|
|
|
|
|
|
valid = true;
|
2019-08-03 06:51:05 +00:00
|
|
|
session.getUpstream().sendPacket(addEntityPacket);
|
|
|
|
|
2019-09-16 22:45:39 +00:00
|
|
|
GeyserLogger.DEFAULT.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
|
|
|
/**
|
|
|
|
* @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);
|
2019-08-03 03:38:09 +00:00
|
|
|
session.getUpstream().sendPacket(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
|
|
|
}
|
|
|
|
|
2019-10-02 20:45:29 +00:00
|
|
|
public void moveRelative(double relX, double relY, double relZ, float yaw, float pitch) {
|
2019-10-09 18:39:38 +00:00
|
|
|
moveRelative(relX, relY, relZ, Vector3f.from(yaw, pitch, yaw));
|
2019-08-03 06:51:05 +00:00
|
|
|
}
|
2019-08-03 03:38:09 +00:00
|
|
|
|
2019-08-03 06:51:05 +00:00
|
|
|
public void moveRelative(double relX, double relY, double relZ, Vector3f rotation) {
|
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);
|
2019-08-03 06:51:05 +00:00
|
|
|
this.movePending = true;
|
2019-08-03 03:38:09 +00:00
|
|
|
}
|
|
|
|
|
2019-10-02 20:45:29 +00:00
|
|
|
public void moveAbsolute(Vector3f position, float yaw, float pitch) {
|
2019-10-09 18:39:38 +00:00
|
|
|
moveAbsolute(position, Vector3f.from(yaw, pitch, yaw));
|
2019-08-03 06:51:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void moveAbsolute(Vector3f position, Vector3f rotation) {
|
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
|
|
|
|
2019-08-03 06:51:05 +00:00
|
|
|
this.movePending = true;
|
2019-08-03 03:38:09 +00:00
|
|
|
}
|
|
|
|
|
2019-08-03 06:51:05 +00:00
|
|
|
public EntityDataDictionary getMetadata() {
|
2019-09-13 10:49:18 +00:00
|
|
|
EntityFlags flags = new EntityFlags();
|
2019-10-09 18:39:38 +00:00
|
|
|
flags.setFlag(EntityFlag.HAS_GRAVITY, true);
|
2019-09-15 04:19:13 +00:00
|
|
|
flags.setFlag(EntityFlag.HAS_COLLISION, true);
|
|
|
|
flags.setFlag(EntityFlag.CAN_SHOW_NAME, true);
|
2019-09-25 21:52:28 +00:00
|
|
|
flags.setFlag(EntityFlag.CAN_CLIMB, true);
|
2019-09-13 10:49:18 +00:00
|
|
|
|
2019-08-03 03:38:09 +00:00
|
|
|
EntityDataDictionary dictionary = new EntityDataDictionary();
|
|
|
|
dictionary.put(EntityData.SCALE, 1f);
|
|
|
|
dictionary.put(EntityData.MAX_AIR, (short) 400);
|
|
|
|
dictionary.put(EntityData.AIR, (short) 0);
|
2019-09-25 21:52:28 +00:00
|
|
|
dictionary.put(EntityData.LEAD_HOLDER_EID, -1L);
|
2019-08-03 03:38:09 +00:00
|
|
|
dictionary.put(EntityData.BOUNDING_BOX_HEIGHT, entityType.getHeight());
|
|
|
|
dictionary.put(EntityData.BOUNDING_BOX_WIDTH, entityType.getWidth());
|
2019-09-13 10:49:18 +00:00
|
|
|
dictionary.putFlags(flags);
|
2019-08-03 03:38:09 +00:00
|
|
|
return dictionary;
|
|
|
|
}
|
2019-08-06 06:38:03 +00:00
|
|
|
|
|
|
|
public void updateBedrockAttributes(GeyserSession session) {
|
|
|
|
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);
|
|
|
|
session.getUpstream().sendPacket(updateAttributesPacket);
|
2019-09-16 01:25:16 +00:00
|
|
|
|
|
|
|
SetEntityDataPacket entityDataPacket = new SetEntityDataPacket();
|
|
|
|
entityDataPacket.setRuntimeEntityId(geyserId);
|
|
|
|
entityDataPacket.getMetadata().putAll(getMetadata());
|
|
|
|
session.getUpstream().sendPacket(entityDataPacket);
|
2019-08-06 06:38:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// To be used at a later date
|
|
|
|
public void updateJavaAttributes(GeyserSession session) {
|
|
|
|
List<com.github.steveice10.mc.protocol.data.game.entity.attribute.Attribute> attributes = new ArrayList<>();
|
|
|
|
for (Map.Entry<AttributeType, Attribute> entry : this.attributes.entrySet()) {
|
|
|
|
if (!entry.getValue().getType().isBedrockAttribute())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
attributes.add(AttributeUtils.getJavaAttribute(entry.getValue()));
|
|
|
|
}
|
|
|
|
|
|
|
|
ServerEntityPropertiesPacket entityPropertiesPacket = new ServerEntityPropertiesPacket((int) entityId, attributes);
|
|
|
|
session.getDownstream().getSession().send(entityPropertiesPacket);
|
|
|
|
}
|
2019-09-25 21:52:28 +00:00
|
|
|
|
|
|
|
public void setPosition(Vector3f position) {
|
|
|
|
if (is(PlayerEntity.class)) {
|
|
|
|
this.position = position.add(0, entityType.getOffset(), 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.position = position;
|
|
|
|
}
|
|
|
|
|
2019-10-02 20:45:29 +00:00
|
|
|
/**
|
|
|
|
* x = Pitch, y = HeadYaw, z = Yaw
|
|
|
|
*/
|
|
|
|
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
|
|
|
}
|