2019-10-09 18:39:38 +00:00
|
|
|
package org.geysermc.connector.network.translators.java.entity.player;
|
2019-09-16 22:28:29 +00:00
|
|
|
|
|
|
|
import com.github.steveice10.mc.protocol.data.game.PlayerListEntry;
|
|
|
|
import com.github.steveice10.mc.protocol.data.game.PlayerListEntryAction;
|
|
|
|
import com.github.steveice10.mc.protocol.packet.ingame.server.ServerPlayerListEntryPacket;
|
2019-10-09 18:39:38 +00:00
|
|
|
import com.nukkitx.math.vector.Vector3f;
|
2019-09-16 22:28:29 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.PlayerListPacket;
|
|
|
|
import org.geysermc.connector.entity.PlayerEntity;
|
|
|
|
import org.geysermc.connector.network.session.GeyserSession;
|
|
|
|
import org.geysermc.connector.network.translators.PacketTranslator;
|
2019-10-10 21:16:07 +00:00
|
|
|
import org.geysermc.connector.utils.SkinUtils;
|
2019-09-16 22:28:29 +00:00
|
|
|
|
|
|
|
public class JavaPlayerListEntryTranslator extends PacketTranslator<ServerPlayerListEntryPacket> {
|
|
|
|
@Override
|
|
|
|
public void translate(ServerPlayerListEntryPacket packet, GeyserSession session) {
|
|
|
|
if (packet.getAction() != PlayerListEntryAction.ADD_PLAYER && packet.getAction() != PlayerListEntryAction.REMOVE_PLAYER) return;
|
|
|
|
|
|
|
|
PlayerListPacket translate = new PlayerListPacket();
|
2019-10-10 21:16:07 +00:00
|
|
|
translate.setType(packet.getAction() == PlayerListEntryAction.ADD_PLAYER ? PlayerListPacket.Type.ADD : PlayerListPacket.Type.REMOVE);
|
2019-09-16 22:28:29 +00:00
|
|
|
|
|
|
|
for (PlayerListEntry entry : packet.getEntries()) {
|
|
|
|
if (packet.getAction() == PlayerListEntryAction.ADD_PLAYER) {
|
2019-10-04 17:25:08 +00:00
|
|
|
boolean self = entry.getProfile().getId().equals(session.getPlayerEntity().getUuid());
|
2019-09-16 22:28:29 +00:00
|
|
|
|
2019-10-02 20:45:29 +00:00
|
|
|
PlayerEntity playerEntity = session.getPlayerEntity();
|
2019-10-09 18:39:38 +00:00
|
|
|
if (self) playerEntity.setProfile(entry.getProfile());
|
|
|
|
else {
|
2019-10-02 20:45:29 +00:00
|
|
|
playerEntity = new PlayerEntity(
|
|
|
|
entry.getProfile(),
|
|
|
|
-1,
|
|
|
|
session.getEntityCache().getNextEntityId().incrementAndGet(),
|
|
|
|
Vector3f.ZERO,
|
|
|
|
Vector3f.ZERO,
|
|
|
|
Vector3f.ZERO
|
|
|
|
);
|
|
|
|
}
|
2019-09-25 21:52:28 +00:00
|
|
|
|
2019-10-02 20:45:29 +00:00
|
|
|
session.getEntityCache().addPlayerEntity(playerEntity);
|
|
|
|
playerEntity.setPlayerList(true);
|
2019-10-10 21:16:07 +00:00
|
|
|
|
|
|
|
translate.getEntries().add(SkinUtils.buildCachedEntry(entry.getProfile(), playerEntity.getGeyserId()));
|
2019-09-16 22:28:29 +00:00
|
|
|
} else {
|
2019-09-25 21:52:28 +00:00
|
|
|
PlayerEntity entity = session.getEntityCache().getPlayerEntity(entry.getProfile().getId());
|
2019-10-02 20:45:29 +00:00
|
|
|
if (entity != null && entity.isValid()) {
|
|
|
|
// remove from tablist but player entity is still there
|
|
|
|
entity.setPlayerList(false);
|
|
|
|
} else {
|
|
|
|
// just remove it from caching
|
2019-09-25 21:52:28 +00:00
|
|
|
session.getEntityCache().removePlayerEntity(entry.getProfile().getId());
|
|
|
|
}
|
2019-10-09 18:39:38 +00:00
|
|
|
translate.getEntries().add(new PlayerListPacket.Entry(entry.getProfile().getId()));
|
2019-09-16 22:28:29 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-09 18:39:38 +00:00
|
|
|
|
2019-10-10 21:16:07 +00:00
|
|
|
if (packet.getAction() == PlayerListEntryAction.REMOVE_PLAYER || session.getUpstream().isInitialized()) {
|
2019-10-09 18:39:38 +00:00
|
|
|
session.getUpstream().sendPacket(translate);
|
|
|
|
}
|
2019-09-16 22:28:29 +00:00
|
|
|
}
|
|
|
|
}
|