2019-08-10 00:15:06 +00:00
|
|
|
/*
|
2020-01-09 03:05:42 +00:00
|
|
|
* Copyright (c) 2019-2020 GeyserMC. http://geysermc.org
|
2019-08-10 00:15:06 +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-08-10 00:15:06 +00:00
|
|
|
|
2019-09-14 01:12:36 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position;
|
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.player.PlayerAction;
|
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.player.PlayerState;
|
|
|
|
import com.github.steveice10.mc.protocol.data.game.world.block.BlockFace;
|
2020-07-18 20:57:37 +00:00
|
|
|
import com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerAbilitiesPacket;
|
2019-09-14 01:12:36 +00:00
|
|
|
import com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerActionPacket;
|
|
|
|
import com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerStatePacket;
|
2019-10-09 18:39:38 +00:00
|
|
|
import com.nukkitx.math.vector.Vector3i;
|
2020-06-23 00:11:09 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.data.LevelEventType;
|
2020-07-30 20:31:12 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.data.entity.EntityEventType;
|
|
|
|
import com.nukkitx.protocol.bedrock.packet.EntityEventPacket;
|
2020-06-23 00:11:09 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.LevelEventPacket;
|
2019-12-29 06:15:12 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.PlayStatusPacket;
|
2019-08-10 00:15:06 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.PlayerActionPacket;
|
2020-06-23 00:11:09 +00:00
|
|
|
import org.geysermc.connector.entity.Entity;
|
|
|
|
import org.geysermc.connector.network.session.GeyserSession;
|
|
|
|
import org.geysermc.connector.network.translators.PacketTranslator;
|
|
|
|
import org.geysermc.connector.network.translators.Translator;
|
2020-11-20 19:56:39 +00:00
|
|
|
import org.geysermc.connector.network.translators.collision.CollisionManager;
|
2020-04-29 20:01:53 +00:00
|
|
|
import org.geysermc.connector.network.translators.world.block.BlockTranslator;
|
2020-10-08 22:44:15 +00:00
|
|
|
import org.geysermc.connector.utils.BlockUtils;
|
2020-02-16 18:40:54 +00:00
|
|
|
|
2020-06-23 00:11:09 +00:00
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
2020-03-24 04:24:17 +00:00
|
|
|
@Translator(packet = PlayerActionPacket.class)
|
2019-08-10 00:15:06 +00:00
|
|
|
public class BedrockActionTranslator extends PacketTranslator<PlayerActionPacket> {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void translate(PlayerActionPacket packet, GeyserSession session) {
|
2019-10-18 07:54:20 +00:00
|
|
|
Entity entity = session.getPlayerEntity();
|
2019-09-14 01:12:36 +00:00
|
|
|
if (entity == null)
|
|
|
|
return;
|
|
|
|
|
2019-09-27 20:50:48 +00:00
|
|
|
Vector3i vector = packet.getBlockPosition();
|
|
|
|
Position position = new Position(vector.getX(), vector.getY(), vector.getZ());
|
|
|
|
|
2019-08-10 00:15:06 +00:00
|
|
|
switch (packet.getAction()) {
|
|
|
|
case RESPAWN:
|
2020-07-30 20:31:12 +00:00
|
|
|
// Respawn process is finished and the server and client are both OK with respawning.
|
|
|
|
EntityEventPacket eventPacket = new EntityEventPacket();
|
|
|
|
eventPacket.setRuntimeEntityId(entity.getGeyserId());
|
|
|
|
eventPacket.setType(EntityEventType.RESPAWN);
|
|
|
|
eventPacket.setData(0);
|
|
|
|
session.sendUpstreamPacket(eventPacket);
|
2020-10-29 20:44:45 +00:00
|
|
|
// Resend attributes or else in rare cases the user can think they're not dead when they are, upon joining the server
|
|
|
|
entity.updateBedrockAttributes(session);
|
2019-08-10 00:15:06 +00:00
|
|
|
break;
|
2020-02-15 10:18:41 +00:00
|
|
|
case START_SWIMMING:
|
|
|
|
ClientPlayerStatePacket startSwimPacket = new ClientPlayerStatePacket((int) entity.getEntityId(), PlayerState.START_SPRINTING);
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendDownstreamPacket(startSwimPacket);
|
2020-02-15 10:18:41 +00:00
|
|
|
break;
|
|
|
|
case STOP_SWIMMING:
|
|
|
|
ClientPlayerStatePacket stopSwimPacket = new ClientPlayerStatePacket((int) entity.getEntityId(), PlayerState.STOP_SPRINTING);
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendDownstreamPacket(stopSwimPacket);
|
2020-02-15 10:18:41 +00:00
|
|
|
break;
|
2019-09-14 01:12:36 +00:00
|
|
|
case START_GLIDE:
|
2020-07-18 20:57:37 +00:00
|
|
|
// Otherwise gliding will not work in creative
|
2020-07-20 23:02:18 +00:00
|
|
|
ClientPlayerAbilitiesPacket playerAbilitiesPacket = new ClientPlayerAbilitiesPacket(false);
|
2020-07-18 20:57:37 +00:00
|
|
|
session.sendDownstreamPacket(playerAbilitiesPacket);
|
2019-09-14 01:12:36 +00:00
|
|
|
case STOP_GLIDE:
|
2019-10-16 20:32:53 +00:00
|
|
|
ClientPlayerStatePacket glidePacket = new ClientPlayerStatePacket((int) entity.getEntityId(), PlayerState.START_ELYTRA_FLYING);
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendDownstreamPacket(glidePacket);
|
2019-09-14 01:12:36 +00:00
|
|
|
break;
|
|
|
|
case START_SNEAK:
|
2019-10-16 20:32:53 +00:00
|
|
|
ClientPlayerStatePacket startSneakPacket = new ClientPlayerStatePacket((int) entity.getEntityId(), PlayerState.START_SNEAKING);
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendDownstreamPacket(startSneakPacket);
|
2020-04-30 05:21:02 +00:00
|
|
|
session.setSneaking(true);
|
2019-09-14 01:12:36 +00:00
|
|
|
break;
|
|
|
|
case STOP_SNEAK:
|
2019-10-16 20:32:53 +00:00
|
|
|
ClientPlayerStatePacket stopSneakPacket = new ClientPlayerStatePacket((int) entity.getEntityId(), PlayerState.STOP_SNEAKING);
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendDownstreamPacket(stopSneakPacket);
|
2020-04-30 05:21:02 +00:00
|
|
|
session.setSneaking(false);
|
2019-09-14 01:12:36 +00:00
|
|
|
break;
|
|
|
|
case START_SPRINT:
|
2019-10-16 20:32:53 +00:00
|
|
|
ClientPlayerStatePacket startSprintPacket = new ClientPlayerStatePacket((int) entity.getEntityId(), PlayerState.START_SPRINTING);
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendDownstreamPacket(startSprintPacket);
|
2020-02-16 18:40:54 +00:00
|
|
|
session.setSprinting(true);
|
2019-09-14 01:12:36 +00:00
|
|
|
break;
|
|
|
|
case STOP_SPRINT:
|
2019-10-16 20:32:53 +00:00
|
|
|
ClientPlayerStatePacket stopSprintPacket = new ClientPlayerStatePacket((int) entity.getEntityId(), PlayerState.STOP_SPRINTING);
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendDownstreamPacket(stopSprintPacket);
|
2020-02-16 18:40:54 +00:00
|
|
|
session.setSprinting(false);
|
2019-09-14 01:12:36 +00:00
|
|
|
break;
|
|
|
|
case DROP_ITEM:
|
2019-09-27 20:50:48 +00:00
|
|
|
ClientPlayerActionPacket dropItemPacket = new ClientPlayerActionPacket(PlayerAction.DROP_ITEM, position, BlockFace.values()[packet.getFace()]);
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendDownstreamPacket(dropItemPacket);
|
2019-09-14 01:12:36 +00:00
|
|
|
break;
|
|
|
|
case STOP_SLEEP:
|
2019-10-16 20:32:53 +00:00
|
|
|
ClientPlayerStatePacket stopSleepingPacket = new ClientPlayerStatePacket((int) entity.getEntityId(), PlayerState.LEAVE_BED);
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendDownstreamPacket(stopSleepingPacket);
|
2019-09-14 01:12:36 +00:00
|
|
|
break;
|
2019-09-27 20:50:48 +00:00
|
|
|
case BLOCK_INTERACT:
|
2020-10-13 00:02:41 +00:00
|
|
|
// Client means to interact with a block; cancel bucket interaction, if any
|
|
|
|
if (session.getBucketScheduledFuture() != null) {
|
|
|
|
session.getBucketScheduledFuture().cancel(true);
|
|
|
|
session.setBucketScheduledFuture(null);
|
|
|
|
}
|
|
|
|
// Otherwise handled in BedrockInventoryTransactionTranslator
|
2019-09-27 23:04:58 +00:00
|
|
|
break;
|
2019-09-27 21:38:52 +00:00
|
|
|
case START_BREAK:
|
2020-08-08 22:59:03 +00:00
|
|
|
if (session.getConnector().getConfig().isCacheChunks()) {
|
2020-10-08 22:44:15 +00:00
|
|
|
// Account for fire - the client likes to hit the block behind.
|
|
|
|
Vector3i fireBlockPos = BlockUtils.getBlockPosition(packet.getBlockPosition(), packet.getFace());
|
|
|
|
int blockUp = session.getConnector().getWorldManager().getBlockAt(session, fireBlockPos);
|
|
|
|
String identifier = BlockTranslator.getJavaIdBlockMap().inverse().get(blockUp);
|
|
|
|
if (identifier.startsWith("minecraft:fire") || identifier.startsWith("minecraft:soul_fire")) {
|
|
|
|
ClientPlayerActionPacket startBreakingPacket = new ClientPlayerActionPacket(PlayerAction.START_DIGGING, new Position(fireBlockPos.getX(),
|
|
|
|
fireBlockPos.getY(), fireBlockPos.getZ()), BlockFace.values()[packet.getFace()]);
|
|
|
|
session.sendDownstreamPacket(startBreakingPacket);
|
|
|
|
break;
|
2020-08-08 22:59:03 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-30 22:17:36 +00:00
|
|
|
ClientPlayerActionPacket startBreakingPacket = new ClientPlayerActionPacket(PlayerAction.START_DIGGING, new Position(packet.getBlockPosition().getX(),
|
|
|
|
packet.getBlockPosition().getY(), packet.getBlockPosition().getZ()), BlockFace.values()[packet.getFace()]);
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendDownstreamPacket(startBreakingPacket);
|
2019-09-30 22:17:36 +00:00
|
|
|
break;
|
|
|
|
case CONTINUE_BREAK:
|
2020-04-23 04:40:49 +00:00
|
|
|
LevelEventPacket continueBreakPacket = new LevelEventPacket();
|
2020-07-07 01:38:10 +00:00
|
|
|
continueBreakPacket.setType(LevelEventType.PARTICLE_CRACK_BLOCK);
|
2020-06-19 13:06:29 +00:00
|
|
|
continueBreakPacket.setData(BlockTranslator.getBedrockBlockId(session.getBreakingBlock()));
|
2020-04-23 04:40:49 +00:00
|
|
|
continueBreakPacket.setPosition(packet.getBlockPosition().toFloat());
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendUpstreamPacket(continueBreakPacket);
|
2019-09-27 23:04:58 +00:00
|
|
|
break;
|
2019-09-27 21:38:52 +00:00
|
|
|
case ABORT_BREAK:
|
2019-09-30 22:17:36 +00:00
|
|
|
ClientPlayerActionPacket abortBreakingPacket = new ClientPlayerActionPacket(PlayerAction.CANCEL_DIGGING, new Position(packet.getBlockPosition().getX(),
|
|
|
|
packet.getBlockPosition().getY(), packet.getBlockPosition().getZ()), BlockFace.DOWN);
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendDownstreamPacket(abortBreakingPacket);
|
2019-09-27 23:04:58 +00:00
|
|
|
break;
|
2019-09-30 22:17:36 +00:00
|
|
|
case STOP_BREAK:
|
2019-10-27 09:56:47 +00:00
|
|
|
// Handled in BedrockInventoryTransactionTranslator
|
2019-09-27 23:04:58 +00:00
|
|
|
break;
|
2019-12-27 11:29:46 +00:00
|
|
|
case DIMENSION_CHANGE_SUCCESS:
|
2020-02-06 04:21:09 +00:00
|
|
|
if (session.getPendingDimSwitches().decrementAndGet() == 0) {
|
|
|
|
//sometimes the client doesn't feel like loading
|
|
|
|
PlayStatusPacket spawnPacket = new PlayStatusPacket();
|
|
|
|
spawnPacket.setStatus(PlayStatusPacket.Status.PLAYER_SPAWN);
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendUpstreamPacket(spawnPacket);
|
2020-02-06 04:21:09 +00:00
|
|
|
entity.updateBedrockAttributes(session);
|
2020-04-18 13:48:21 +00:00
|
|
|
session.getEntityCache().updateBossBars();
|
2020-02-06 04:21:09 +00:00
|
|
|
}
|
2019-12-27 11:29:46 +00:00
|
|
|
break;
|
2020-02-16 18:40:54 +00:00
|
|
|
case JUMP:
|
|
|
|
session.setJumping(true);
|
|
|
|
session.getConnector().getGeneralThreadPool().schedule(() -> {
|
|
|
|
session.setJumping(false);
|
|
|
|
}, 1, TimeUnit.SECONDS);
|
|
|
|
break;
|
2019-08-10 00:15:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|