2019-08-03 06:51:05 +00:00
|
|
|
/*
|
2020-01-09 03:05:42 +00:00
|
|
|
* Copyright (c) 2019-2020 GeyserMC. http://geysermc.org
|
2019-08-03 06:51:05 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2020-09-29 18:15:11 +00:00
|
|
|
package org.geysermc.connector.network.translators.bedrock;
|
2019-08-03 06:51:05 +00:00
|
|
|
|
2020-09-29 18:15:11 +00:00
|
|
|
import com.github.steveice10.mc.protocol.packet.ingame.client.ClientKeepAlivePacket;
|
|
|
|
import com.nukkitx.protocol.bedrock.packet.NetworkStackLatencyPacket;
|
2020-11-26 22:00:43 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.UpdateAttributesPacket;
|
|
|
|
import org.geysermc.connector.entity.attribute.Attribute;
|
|
|
|
import org.geysermc.connector.entity.attribute.AttributeType;
|
2020-09-29 18:15:11 +00:00
|
|
|
import org.geysermc.connector.network.session.GeyserSession;
|
|
|
|
import org.geysermc.connector.network.translators.PacketTranslator;
|
|
|
|
import org.geysermc.connector.network.translators.Translator;
|
2020-11-26 22:00:43 +00:00
|
|
|
import org.geysermc.connector.utils.AttributeUtils;
|
|
|
|
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
2019-08-03 06:51:05 +00:00
|
|
|
|
2020-09-29 18:15:11 +00:00
|
|
|
/**
|
|
|
|
* Used to send the keep alive packet back to the server
|
|
|
|
*/
|
|
|
|
@Translator(packet = NetworkStackLatencyPacket.class)
|
|
|
|
public class BedrockNetworkStackLatencyTranslator extends PacketTranslator<NetworkStackLatencyPacket> {
|
2019-08-03 06:51:05 +00:00
|
|
|
|
2020-09-29 18:15:11 +00:00
|
|
|
@Override
|
|
|
|
public void translate(NetworkStackLatencyPacket packet, GeyserSession session) {
|
2020-11-26 22:00:43 +00:00
|
|
|
if (packet.getTimestamp() > 0) {
|
|
|
|
// The client sends a timestamp back but it's rounded and therefore unreliable when we need the exact number
|
|
|
|
ClientKeepAlivePacket keepAlivePacket = new ClientKeepAlivePacket(session.getLastKeepAliveTimestamp());
|
|
|
|
session.sendDownstreamPacket(keepAlivePacket);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hack to fix the url image loading bug
|
|
|
|
UpdateAttributesPacket attributesPacket = new UpdateAttributesPacket();
|
|
|
|
attributesPacket.setRuntimeEntityId(session.getPlayerEntity().getGeyserId());
|
|
|
|
|
|
|
|
Attribute attribute = session.getPlayerEntity().getAttributes().get(AttributeType.EXPERIENCE_LEVEL);
|
|
|
|
if (attribute != null) {
|
|
|
|
attributesPacket.setAttributes(Collections.singletonList(AttributeUtils.getBedrockAttribute(attribute)));
|
|
|
|
} else {
|
|
|
|
attributesPacket.setAttributes(Collections.singletonList(AttributeUtils.getBedrockAttribute(AttributeType.EXPERIENCE_LEVEL.getAttribute(0))));
|
|
|
|
}
|
|
|
|
|
|
|
|
session.getConnector().getGeneralThreadPool().schedule(
|
|
|
|
() -> session.sendUpstreamPacket(attributesPacket),
|
|
|
|
500, TimeUnit.MILLISECONDS);
|
2020-09-29 18:15:11 +00:00
|
|
|
}
|
2019-08-03 06:51:05 +00:00
|
|
|
}
|