2019-09-30 16:44:25 +00:00
|
|
|
/*
|
2022-01-01 19:03:05 +00:00
|
|
|
* Copyright (c) 2019-2022 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
|
|
|
|
*/
|
|
|
|
|
2021-11-20 23:29:46 +00:00
|
|
|
package org.geysermc.geyser.translator.protocol.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;
|
2022-05-26 17:11:39 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.type.EntityType;
|
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;
|
2022-10-30 00:23:21 +00:00
|
|
|
import org.cloudburstmc.protocol.bedrock.data.entity.EntityDataTypes;
|
2023-02-14 22:09:48 +00:00
|
|
|
import org.cloudburstmc.protocol.bedrock.data.entity.EntityLinkData;
|
2022-10-30 00:23:21 +00:00
|
|
|
import org.cloudburstmc.protocol.bedrock.data.inventory.ContainerType;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.packet.ContainerOpenPacket;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.packet.InteractPacket;
|
2023-02-14 22:09:48 +00:00
|
|
|
import org.cloudburstmc.protocol.bedrock.packet.SetEntityLinkPacket;
|
2021-11-20 23:29:46 +00:00
|
|
|
import org.geysermc.geyser.entity.type.Entity;
|
|
|
|
import org.geysermc.geyser.entity.type.living.animal.horse.AbstractHorseEntity;
|
2022-12-29 20:10:40 +00:00
|
|
|
import org.geysermc.geyser.item.Items;
|
2021-11-22 19:52:26 +00:00
|
|
|
import org.geysermc.geyser.session.GeyserSession;
|
2021-11-20 23:29:46 +00:00
|
|
|
import org.geysermc.geyser.translator.protocol.PacketTranslator;
|
|
|
|
import org.geysermc.geyser.translator.protocol.Translator;
|
2024-03-02 02:21:31 +00:00
|
|
|
import org.geysermc.geyser.util.InventoryUtils;
|
2020-11-12 00:28:45 +00:00
|
|
|
|
2022-12-22 18:19:46 +00:00
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
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-11-22 19:52:26 +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:
|
2022-12-29 20:10:40 +00:00
|
|
|
if (session.getPlayerInventory().getItemInHand().asItem() == Items.SHIELD) {
|
2020-04-25 03:11:28 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-12-21 00:25:11 +00:00
|
|
|
ServerboundInteractPacket interactPacket = new ServerboundInteractPacket(entity.getEntityId(),
|
2020-06-21 02:24:45 +00:00
|
|
|
InteractAction.INTERACT, Hand.MAIN_HAND, session.isSneaking());
|
2023-10-12 13:02:57 +00:00
|
|
|
session.sendDownstreamGamePacket(interactPacket);
|
2019-10-16 20:32:53 +00:00
|
|
|
break;
|
2020-02-14 23:27:08 +00:00
|
|
|
case DAMAGE:
|
2021-12-21 00:25:11 +00:00
|
|
|
ServerboundInteractPacket attackPacket = new ServerboundInteractPacket(entity.getEntityId(),
|
2020-06-21 02:24:45 +00:00
|
|
|
InteractAction.ATTACK, Hand.MAIN_HAND, session.isSneaking());
|
2023-10-12 13:02:57 +00:00
|
|
|
session.sendDownstreamGamePacket(attackPacket);
|
2019-10-16 20:32:53 +00:00
|
|
|
break;
|
2020-05-23 21:39:17 +00:00
|
|
|
case LEAVE_VEHICLE:
|
2021-12-21 00:25:11 +00:00
|
|
|
ServerboundPlayerCommandPacket sneakPacket = new ServerboundPlayerCommandPacket(entity.getEntityId(), PlayerState.START_SNEAKING);
|
2023-10-12 13:02:57 +00:00
|
|
|
session.sendDownstreamGamePacket(sneakPacket);
|
2022-12-22 18:19:46 +00:00
|
|
|
|
|
|
|
Entity currentVehicle = session.getPlayerEntity().getVehicle();
|
2023-03-21 19:07:51 +00:00
|
|
|
if (currentVehicle != null) {
|
|
|
|
session.setMountVehicleScheduledFuture(session.scheduleInEventLoop(() -> {
|
|
|
|
if (session.getPlayerEntity().getVehicle() == null) {
|
|
|
|
return;
|
|
|
|
}
|
2022-12-22 18:19:46 +00:00
|
|
|
|
2023-03-21 19:07:51 +00:00
|
|
|
long vehicleBedrockId = currentVehicle.getGeyserId();
|
|
|
|
if (session.getPlayerEntity().getVehicle().getGeyserId() == vehicleBedrockId) {
|
|
|
|
// The Bedrock client, as of 1.19.51, dismounts on its end. The server may not agree with this.
|
|
|
|
// If the server doesn't agree with our dismount (sends a packet saying we dismounted),
|
|
|
|
// then remount the player.
|
|
|
|
SetEntityLinkPacket linkPacket = new SetEntityLinkPacket();
|
|
|
|
linkPacket.setEntityLink(new EntityLinkData(vehicleBedrockId, session.getPlayerEntity().getGeyserId(), EntityLinkData.Type.PASSENGER, true, false));
|
|
|
|
session.sendUpstreamPacket(linkPacket);
|
|
|
|
}
|
|
|
|
}, 1, TimeUnit.SECONDS));
|
|
|
|
}
|
2020-05-23 21:39:17 +00:00
|
|
|
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
|
|
|
|
2022-02-25 03:49:10 +00:00
|
|
|
interactEntity.updateInteractiveTag();
|
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);
|
2022-10-30 03:02:11 +00:00
|
|
|
session.getPlayerEntity().getDirtyMetadata().put(EntityDataTypes.INTERACT_TEXT, "");
|
2021-11-18 03:02:38 +00:00
|
|
|
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) {
|
2021-12-18 16:43:57 +00:00
|
|
|
Entity ridingEntity = session.getPlayerEntity().getVehicle();
|
2022-05-26 17:11:39 +00:00
|
|
|
if (ridingEntity instanceof AbstractHorseEntity || (ridingEntity != null && ridingEntity.getDefinition().entityType() == EntityType.CHEST_BOAT)) {
|
|
|
|
// This mob has an inventory of its own that we should open instead.
|
|
|
|
ServerboundPlayerCommandPacket openVehicleWindowPacket = new ServerboundPlayerCommandPacket(session.getPlayerEntity().getEntityId(), PlayerState.OPEN_VEHICLE_INVENTORY);
|
2023-10-12 13:02:57 +00:00
|
|
|
session.sendDownstreamGamePacket(openVehicleWindowPacket);
|
2020-12-26 17:20:59 +00:00
|
|
|
} 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);
|
|
|
|
}
|
2024-03-02 02:21:31 +00:00
|
|
|
} else {
|
|
|
|
// Case: Player opens a player inventory, while we think it shouldn't have!
|
|
|
|
// Close all inventories, reset to player inventory.
|
|
|
|
InventoryUtils.closeInventory(session, session.getOpenInventory().getJavaId(), false);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|