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
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.geysermc.connector.network.translators.bedrock;
|
|
|
|
|
2020-03-24 04:24:17 +00:00
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
2020-04-23 04:40:49 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.data.LevelEventType;
|
|
|
|
import com.nukkitx.protocol.bedrock.data.SoundEvent;
|
|
|
|
import com.nukkitx.protocol.bedrock.packet.LevelEventPacket;
|
|
|
|
import com.nukkitx.protocol.bedrock.packet.LevelSoundEventPacket;
|
2020-03-24 04:24:17 +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;
|
|
|
|
|
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;
|
|
|
|
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;
|
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-04-23 04:40:49 +00:00
|
|
|
import org.geysermc.connector.network.translators.block.BlockTranslator;
|
2020-02-16 18:40:54 +00:00
|
|
|
|
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:
|
|
|
|
// Don't put anything here as respawn is already handled
|
2019-12-27 11:29:46 +00:00
|
|
|
// in BedrockRespawnTranslator
|
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);
|
|
|
|
session.getDownstream().getSession().send(startSwimPacket);
|
|
|
|
break;
|
|
|
|
case STOP_SWIMMING:
|
|
|
|
ClientPlayerStatePacket stopSwimPacket = new ClientPlayerStatePacket((int) entity.getEntityId(), PlayerState.STOP_SPRINTING);
|
|
|
|
session.getDownstream().getSession().send(stopSwimPacket);
|
|
|
|
break;
|
2019-09-14 01:12:36 +00:00
|
|
|
case START_GLIDE:
|
|
|
|
case STOP_GLIDE:
|
2019-10-16 20:32:53 +00:00
|
|
|
ClientPlayerStatePacket glidePacket = new ClientPlayerStatePacket((int) entity.getEntityId(), PlayerState.START_ELYTRA_FLYING);
|
2019-09-14 01:12:36 +00:00
|
|
|
session.getDownstream().getSession().send(glidePacket);
|
|
|
|
break;
|
|
|
|
case START_SNEAK:
|
2019-10-16 20:32:53 +00:00
|
|
|
ClientPlayerStatePacket startSneakPacket = new ClientPlayerStatePacket((int) entity.getEntityId(), PlayerState.START_SNEAKING);
|
2019-09-14 01:12:36 +00:00
|
|
|
session.getDownstream().getSession().send(startSneakPacket);
|
|
|
|
break;
|
|
|
|
case STOP_SNEAK:
|
2019-10-16 20:32:53 +00:00
|
|
|
ClientPlayerStatePacket stopSneakPacket = new ClientPlayerStatePacket((int) entity.getEntityId(), PlayerState.STOP_SNEAKING);
|
2019-09-14 01:12:36 +00:00
|
|
|
session.getDownstream().getSession().send(stopSneakPacket);
|
|
|
|
break;
|
|
|
|
case START_SPRINT:
|
2019-10-16 20:32:53 +00:00
|
|
|
ClientPlayerStatePacket startSprintPacket = new ClientPlayerStatePacket((int) entity.getEntityId(), PlayerState.START_SPRINTING);
|
|
|
|
session.getDownstream().getSession().send(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);
|
|
|
|
session.getDownstream().getSession().send(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()]);
|
2019-09-14 01:12:36 +00:00
|
|
|
session.getDownstream().getSession().send(dropItemPacket);
|
|
|
|
break;
|
|
|
|
case STOP_SLEEP:
|
2019-10-16 20:32:53 +00:00
|
|
|
ClientPlayerStatePacket stopSleepingPacket = new ClientPlayerStatePacket((int) entity.getEntityId(), PlayerState.LEAVE_BED);
|
2019-09-14 01:12:36 +00:00
|
|
|
session.getDownstream().getSession().send(stopSleepingPacket);
|
|
|
|
break;
|
2019-09-27 20:50:48 +00:00
|
|
|
case BLOCK_INTERACT:
|
2020-04-10 18:46:29 +00:00
|
|
|
// Handled in BedrockInventoryTransactionTranslator
|
2019-09-27 23:04:58 +00:00
|
|
|
break;
|
2019-09-27 21:38:52 +00:00
|
|
|
case START_BREAK:
|
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()]);
|
|
|
|
session.getDownstream().getSession().send(startBreakingPacket);
|
|
|
|
break;
|
|
|
|
case CONTINUE_BREAK:
|
2020-04-23 04:40:49 +00:00
|
|
|
LevelEventPacket continueBreakPacket = new LevelEventPacket();
|
|
|
|
continueBreakPacket.setType(LevelEventType.PUNCH_BLOCK);
|
|
|
|
continueBreakPacket.setData(BlockTranslator.getBedrockBlockId(session.getBreakingBlock() == null ? BlockTranslator.AIR : session.getBreakingBlock()));
|
|
|
|
continueBreakPacket.setPosition(packet.getBlockPosition().toFloat());
|
|
|
|
session.getUpstream().sendPacket(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);
|
|
|
|
session.getDownstream().getSession().send(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);
|
|
|
|
session.getUpstream().sendPacket(spawnPacket);
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|