Geyser/connector/src/main/java/org/geysermc/connector/entity/PlayerEntity.java

117 lines
4.6 KiB
Java

/*
* Copyright (c) 2019-2020 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;
import com.github.steveice10.mc.auth.data.GameProfile;
import com.nukkitx.math.vector.Vector3f;
import com.nukkitx.protocol.bedrock.packet.AddPlayerPacket;
import com.nukkitx.protocol.bedrock.packet.PlayerListPacket;
import lombok.Getter;
import lombok.Setter;
import org.geysermc.api.Geyser;
import org.geysermc.connector.entity.type.EntityType;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.utils.SkinUtils;
import java.util.UUID;
@Getter @Setter
public class PlayerEntity extends LivingEntity {
private GameProfile profile;
private UUID uuid;
private String username;
private long lastSkinUpdate = -1;
private boolean playerList = true;
public PlayerEntity(GameProfile gameProfile, long entityId, long geyserId, Vector3f position, Vector3f motion, Vector3f rotation) {
super(entityId, geyserId, EntityType.PLAYER, position, motion, rotation);
profile = gameProfile;
uuid = gameProfile.getId();
username = gameProfile.getName();
if (geyserId == 1) valid = true;
}
@Override
public boolean despawnEntity(GeyserSession session) {
super.despawnEntity(session);
return !playerList; // don't remove from cache when still on playerlist
}
@Override
public void spawnEntity(GeyserSession session) {
if (geyserId == 1) return;
AddPlayerPacket addPlayerPacket = new AddPlayerPacket();
addPlayerPacket.setUuid(uuid);
addPlayerPacket.setUsername(username);
addPlayerPacket.setRuntimeEntityId(geyserId);
addPlayerPacket.setUniqueEntityId(geyserId);
addPlayerPacket.setPosition(position);
addPlayerPacket.setRotation(getBedrockRotation());
addPlayerPacket.setMotion(motion);
addPlayerPacket.setHand(hand);
addPlayerPacket.setPlayerFlags(0);
addPlayerPacket.setCommandPermission(0);
addPlayerPacket.setWorldFlags(0);
addPlayerPacket.setPlayerPermission(0);
addPlayerPacket.setCustomFlags(0);
addPlayerPacket.setDeviceId("");
addPlayerPacket.setPlatformChatId("");
addPlayerPacket.getMetadata().putAll(getMetadata());
valid = true;
session.getUpstream().sendPacket(addPlayerPacket);
}
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);
});
}
}
}