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-09-16 22:28:29 +00:00
|
|
|
import com.github.steveice10.mc.auth.data.GameProfile;
|
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.packet.AddPlayerPacket;
|
2019-10-09 18:39:38 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.PlayerListPacket;
|
2019-08-03 03:38:09 +00:00
|
|
|
import lombok.Getter;
|
|
|
|
import lombok.Setter;
|
2019-10-09 18:39:38 +00:00
|
|
|
import org.geysermc.api.Geyser;
|
2019-08-03 03:38:09 +00:00
|
|
|
import org.geysermc.connector.entity.type.EntityType;
|
|
|
|
import org.geysermc.connector.network.session.GeyserSession;
|
2019-10-09 18:39:38 +00:00
|
|
|
import org.geysermc.connector.utils.SkinUtils;
|
2019-08-03 03:38:09 +00:00
|
|
|
|
2019-08-03 06:51:05 +00:00
|
|
|
import java.util.UUID;
|
|
|
|
|
2019-09-16 22:28:29 +00:00
|
|
|
@Getter @Setter
|
2019-12-01 00:19:03 +00:00
|
|
|
public class PlayerEntity extends LivingEntity {
|
2019-09-25 21:52:28 +00:00
|
|
|
private GameProfile profile;
|
2019-08-03 06:51:05 +00:00
|
|
|
private UUID uuid;
|
2019-09-16 22:28:29 +00:00
|
|
|
private String username;
|
2019-09-25 21:52:28 +00:00
|
|
|
private long lastSkinUpdate = -1;
|
2019-10-02 20:45:29 +00:00
|
|
|
private boolean playerList = true;
|
2019-08-03 03:38:09 +00:00
|
|
|
|
2019-09-21 07:42:44 +00:00
|
|
|
public PlayerEntity(GameProfile gameProfile, long entityId, long geyserId, Vector3f position, Vector3f motion, Vector3f rotation) {
|
|
|
|
super(entityId, geyserId, EntityType.PLAYER, position, motion, rotation);
|
2019-08-03 03:38:09 +00:00
|
|
|
|
2019-09-25 21:52:28 +00:00
|
|
|
profile = gameProfile;
|
2019-09-16 22:28:29 +00:00
|
|
|
uuid = gameProfile.getId();
|
|
|
|
username = gameProfile.getName();
|
2019-09-25 21:52:28 +00:00
|
|
|
if (geyserId == 1) valid = true;
|
2019-08-03 03:38:09 +00:00
|
|
|
}
|
|
|
|
|
2019-10-02 20:45:29 +00:00
|
|
|
@Override
|
|
|
|
public boolean despawnEntity(GeyserSession session) {
|
|
|
|
super.despawnEntity(session);
|
|
|
|
return !playerList; // don't remove from cache when still on playerlist
|
|
|
|
}
|
|
|
|
|
2019-08-03 03:38:09 +00:00
|
|
|
@Override
|
|
|
|
public void spawnEntity(GeyserSession session) {
|
2019-09-25 21:52:28 +00:00
|
|
|
if (geyserId == 1) return;
|
|
|
|
|
2019-08-03 03:38:09 +00:00
|
|
|
AddPlayerPacket addPlayerPacket = new AddPlayerPacket();
|
2019-08-03 06:51:05 +00:00
|
|
|
addPlayerPacket.setUuid(uuid);
|
2019-09-16 22:28:29 +00:00
|
|
|
addPlayerPacket.setUsername(username);
|
2019-09-25 21:52:28 +00:00
|
|
|
addPlayerPacket.setRuntimeEntityId(geyserId);
|
|
|
|
addPlayerPacket.setUniqueEntityId(geyserId);
|
2019-08-03 03:38:09 +00:00
|
|
|
addPlayerPacket.setPosition(position);
|
2019-10-02 20:45:29 +00:00
|
|
|
addPlayerPacket.setRotation(getBedrockRotation());
|
2019-09-25 21:52:28 +00:00
|
|
|
addPlayerPacket.setMotion(motion);
|
2019-08-03 03:38:09 +00:00
|
|
|
addPlayerPacket.setHand(hand);
|
|
|
|
addPlayerPacket.setPlayerFlags(0);
|
|
|
|
addPlayerPacket.setCommandPermission(0);
|
|
|
|
addPlayerPacket.setWorldFlags(0);
|
|
|
|
addPlayerPacket.setPlayerPermission(0);
|
|
|
|
addPlayerPacket.setCustomFlags(0);
|
2019-09-25 21:52:28 +00:00
|
|
|
addPlayerPacket.setDeviceId("");
|
|
|
|
addPlayerPacket.setPlatformChatId("");
|
|
|
|
addPlayerPacket.getMetadata().putAll(getMetadata());
|
2019-08-03 06:51:05 +00:00
|
|
|
|
|
|
|
valid = true;
|
2019-08-03 03:38:09 +00:00
|
|
|
session.getUpstream().sendPacket(addPlayerPacket);
|
|
|
|
}
|
2019-10-09 18:39:38 +00:00
|
|
|
|
|
|
|
public void sendPlayer(GeyserSession session) {
|
|
|
|
if (getLastSkinUpdate() == -1) {
|
|
|
|
if (playerList) {
|
|
|
|
PlayerListPacket playerList = new PlayerListPacket();
|
|
|
|
playerList.setType(PlayerListPacket.Type.ADD);
|
|
|
|
playerList.getEntries().add(SkinUtils.buildDefaultEntry(profile, geyserId));
|
|
|
|
session.getUpstream().sendPacket(playerList);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (session.getUpstream().isInitialized() && session.getEntityCache().getEntityByGeyserId(geyserId) == null) {
|
|
|
|
session.getEntityCache().spawnEntity(this);
|
|
|
|
} else {
|
|
|
|
spawnEntity(session);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!playerList) {
|
|
|
|
// remove from playerlist if player isn't on playerlist
|
|
|
|
Geyser.getGeneralThreadPool().execute(() -> {
|
|
|
|
PlayerListPacket playerList = new PlayerListPacket();
|
|
|
|
playerList.setType(PlayerListPacket.Type.REMOVE);
|
|
|
|
playerList.getEntries().add(new PlayerListPacket.Entry(uuid));
|
|
|
|
session.getUpstream().sendPacket(playerList);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2019-08-03 03:38:09 +00:00
|
|
|
}
|