2019-09-30 16:44:25 +00:00
|
|
|
/*
|
2021-01-01 15:10:36 +00:00
|
|
|
* Copyright (c) 2019-2021 GeyserMC. http://geysermc.org
|
2019-09-30 16:44:25 +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-08-08 22:41:12 +00:00
|
|
|
package org.geysermc.connector.network.translators.bedrock.entity.player;
|
2019-09-30 16:44:25 +00:00
|
|
|
|
2020-11-12 00:28:45 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.player.Hand;
|
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.player.InteractAction;
|
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.player.PlayerState;
|
2021-11-13 03:44:15 +00:00
|
|
|
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.player.ServerboundInteractPacket;
|
|
|
|
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.player.ServerboundPlayerCommandPacket;
|
2020-06-23 00:11:09 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.data.entity.EntityData;
|
|
|
|
import com.nukkitx.protocol.bedrock.data.entity.EntityFlag;
|
2020-06-27 02:51:09 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.data.inventory.ContainerType;
|
|
|
|
import com.nukkitx.protocol.bedrock.packet.ContainerOpenPacket;
|
2020-11-12 00:28:45 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.InteractPacket;
|
2020-03-24 04:24:17 +00:00
|
|
|
import org.geysermc.connector.entity.Entity;
|
2020-12-26 17:20:59 +00:00
|
|
|
import org.geysermc.connector.entity.living.animal.horse.AbstractHorseEntity;
|
2020-03-24 04:24:17 +00:00
|
|
|
import org.geysermc.connector.network.session.GeyserSession;
|
|
|
|
import org.geysermc.connector.network.translators.PacketTranslator;
|
|
|
|
import org.geysermc.connector.network.translators.Translator;
|
2021-03-18 05:11:21 +00:00
|
|
|
import org.geysermc.connector.utils.InteractiveTagManager;
|
2020-11-12 00:28:45 +00:00
|
|
|
|
2020-03-24 04:24:17 +00:00
|
|
|
@Translator(packet = InteractPacket.class)
|
2019-09-30 16:44:25 +00:00
|
|
|
public class BedrockInteractTranslator extends PacketTranslator<InteractPacket> {
|
|
|
|
|
|
|
|
@Override
|
2021-08-30 17:55:01 +00:00
|
|
|
public void translate(GeyserSession session, InteractPacket packet) {
|
2020-06-27 02:51:09 +00:00
|
|
|
Entity entity;
|
|
|
|
if (packet.getRuntimeEntityId() == session.getPlayerEntity().getGeyserId()) {
|
|
|
|
//Player is not in entity cache
|
|
|
|
entity = session.getPlayerEntity();
|
|
|
|
} else {
|
|
|
|
entity = session.getEntityCache().getEntityByGeyserId(packet.getRuntimeEntityId());
|
|
|
|
}
|
2019-10-16 20:32:53 +00:00
|
|
|
if (entity == null)
|
|
|
|
return;
|
2019-09-30 16:44:25 +00:00
|
|
|
|
2019-10-16 20:32:53 +00:00
|
|
|
switch (packet.getAction()) {
|
2020-02-16 19:07:35 +00:00
|
|
|
case INTERACT:
|
2021-07-13 01:19:40 +00:00
|
|
|
if (session.getPlayerInventory().getItemInHand().getJavaId() == session.getItemMappings().getStoredItems().shield().getJavaId()) {
|
2020-04-25 03:11:28 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-11-13 03:44:15 +00:00
|
|
|
ServerboundInteractPacket interactPacket = new ServerboundInteractPacket((int) entity.getEntityId(),
|
2020-06-21 02:24:45 +00:00
|
|
|
InteractAction.INTERACT, Hand.MAIN_HAND, session.isSneaking());
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendDownstreamPacket(interactPacket);
|
2019-10-16 20:32:53 +00:00
|
|
|
break;
|
2020-02-14 23:27:08 +00:00
|
|
|
case DAMAGE:
|
2021-11-13 03:44:15 +00:00
|
|
|
ServerboundInteractPacket attackPacket = new ServerboundInteractPacket((int) entity.getEntityId(),
|
2020-06-21 02:24:45 +00:00
|
|
|
InteractAction.ATTACK, Hand.MAIN_HAND, session.isSneaking());
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendDownstreamPacket(attackPacket);
|
2019-10-16 20:32:53 +00:00
|
|
|
break;
|
2020-05-23 21:39:17 +00:00
|
|
|
case LEAVE_VEHICLE:
|
2021-11-13 03:44:15 +00:00
|
|
|
ServerboundPlayerCommandPacket sneakPacket = new ServerboundPlayerCommandPacket((int) entity.getEntityId(), PlayerState.START_SNEAKING);
|
2020-05-23 21:39:17 +00:00
|
|
|
session.sendDownstreamPacket(sneakPacket);
|
|
|
|
session.setRidingVehicleEntity(null);
|
|
|
|
break;
|
|
|
|
case MOUSEOVER:
|
|
|
|
// Handle the buttons for mobile - "Mount", etc; and the suggestions for console - "ZL: Mount", etc
|
|
|
|
if (packet.getRuntimeEntityId() != 0) {
|
|
|
|
Entity interactEntity = session.getEntityCache().getEntityByGeyserId(packet.getRuntimeEntityId());
|
2021-03-18 05:11:21 +00:00
|
|
|
session.setMouseoverEntity(interactEntity);
|
|
|
|
if (interactEntity == null) {
|
2020-05-23 21:39:17 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-03-18 05:11:21 +00:00
|
|
|
|
|
|
|
InteractiveTagManager.updateTag(session, interactEntity);
|
2020-05-23 21:39:17 +00:00
|
|
|
} else {
|
2021-03-18 05:11:21 +00:00
|
|
|
if (session.getMouseoverEntity() != null) {
|
2020-05-23 21:39:17 +00:00
|
|
|
// No interactive tag should be sent
|
2021-03-18 05:11:21 +00:00
|
|
|
session.setMouseoverEntity(null);
|
2021-11-18 03:02:38 +00:00
|
|
|
session.getPlayerEntity().getDirtyMetadata().put(EntityData.INTERACTIVE_TAG, "");
|
|
|
|
session.getPlayerEntity().updateBedrockMetadata();
|
2020-05-23 21:39:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2020-06-27 02:51:09 +00:00
|
|
|
case OPEN_INVENTORY:
|
2020-10-16 23:25:05 +00:00
|
|
|
if (session.getOpenInventory() == null) {
|
2020-12-26 17:20:59 +00:00
|
|
|
Entity ridingEntity = session.getRidingVehicleEntity();
|
|
|
|
if (ridingEntity instanceof AbstractHorseEntity) {
|
2021-11-18 03:02:38 +00:00
|
|
|
if (ridingEntity.getDirtyMetadata().getFlags().getFlag(EntityFlag.TAMED)) {
|
2020-12-26 17:20:59 +00:00
|
|
|
// We should request to open the horse inventory instead
|
2021-11-13 03:44:15 +00:00
|
|
|
ServerboundPlayerCommandPacket openHorseWindowPacket = new ServerboundPlayerCommandPacket((int) session.getPlayerEntity().getEntityId(), PlayerState.OPEN_HORSE_INVENTORY);
|
2020-12-26 17:20:59 +00:00
|
|
|
session.sendDownstreamPacket(openHorseWindowPacket);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
session.setOpenInventory(session.getPlayerInventory());
|
2020-10-16 23:25:05 +00:00
|
|
|
|
2020-12-26 17:20:59 +00:00
|
|
|
ContainerOpenPacket containerOpenPacket = new ContainerOpenPacket();
|
|
|
|
containerOpenPacket.setId((byte) 0);
|
|
|
|
containerOpenPacket.setType(ContainerType.INVENTORY);
|
|
|
|
containerOpenPacket.setUniqueEntityId(-1);
|
|
|
|
containerOpenPacket.setBlockPosition(entity.getPosition().toInt());
|
|
|
|
session.sendUpstreamPacket(containerOpenPacket);
|
|
|
|
}
|
2020-07-27 15:17:55 +00:00
|
|
|
}
|
2020-06-27 02:51:09 +00:00
|
|
|
break;
|
2019-09-30 16:44:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|