2020-01-09 03:05:42 +00:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
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();
|
2020-02-06 00:55:34 +00:00
|
|
|
translate.setAction(packet.getAction() == PlayerListEntryAction.ADD_PLAYER ? PlayerListPacket.Action.ADD : PlayerListPacket.Action.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
|
|
|
}
|
|
|
|
}
|