Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package wode;
- import java.util.ArrayList;
- import com.nukkitx.math.vector.Vector3i;
- import com.nukkitx.protocol.bedrock.BedrockClientSession;
- import com.nukkitx.protocol.bedrock.handler.BedrockPacketHandler;
- import com.nukkitx.protocol.bedrock.packet.*;
- public class PacketHandler implements BedrockPacketHandler {
- private final BedrockClientSession session;
- public long runtimeId = 0;
- public Vector3i spawnPosition = Vector3i.ZERO;
- public boolean initialized = false;
- private static ArrayList<BedrockClientSession> sessions = new ArrayList<BedrockClientSession>();
- public static void stop() {
- for (int i = 0;i < getSessions().size();i++) {
- BedrockClientSession session = getSessions().get(i);
- try {
- session.disconnect();
- }catch (Exception e) {
- }
- }
- }
- public PacketHandler(BedrockClientSession session) {
- this.session = session;
- getSessions().add(session);
- }
- @Override
- public boolean handle(DisconnectPacket packet) {
- this.session.disconnect();
- return true;
- }
- @Override
- public boolean handle(ResourcePacksInfoPacket packet) {
- ResourcePackClientResponsePacket pk = new ResourcePackClientResponsePacket();
- pk.setStatus(ResourcePackClientResponsePacket.Status.HAVE_ALL_PACKS);
- this.session.sendPacket(pk);
- return true;
- }
- @Override
- public boolean handle(ResourcePackStackPacket packet) {
- ResourcePackClientResponsePacket pk = new ResourcePackClientResponsePacket();
- pk.setStatus(ResourcePackClientResponsePacket.Status.COMPLETED);
- this.session.sendPacket(pk);
- return true;
- }
- @Override
- public boolean handle(StartGamePacket packet) {
- this.runtimeId = packet.getRuntimeEntityId();
- this.spawnPosition = packet.getDefaultSpawn();
- RequestChunkRadiusPacket pk = new RequestChunkRadiusPacket();
- pk.setRadius(1);
- this.session.sendPacket(pk);
- return true;
- }
- @Override
- public boolean handle(PlayStatusPacket packet) {
- switch (packet.getStatus()) {
- case PLAYER_SPAWN:
- if (!this.initialized) {
- this.initialized = true;
- SetLocalPlayerAsInitializedPacket pk = new SetLocalPlayerAsInitializedPacket();
- pk.setRuntimeEntityId(this.runtimeId);
- this.session.sendPacket(pk);
- }
- break;
- case LOGIN_SUCCESS:
- ClientCacheStatusPacket clientCacheStatusPacket = new ClientCacheStatusPacket();
- clientCacheStatusPacket.setSupported(false);
- this.session.sendPacket(clientCacheStatusPacket);
- break;
- }
- return true;
- }
- @Override
- public boolean handle(RespawnPacket packet) {
- if (packet.getState() == RespawnPacket.State.SERVER_SEARCHING) {
- RespawnPacket pk = new RespawnPacket();
- pk.setState(RespawnPacket.State.CLIENT_READY);
- this.session.sendPacket(pk);
- }
- return true;
- }
- @Override
- public boolean handle(SetHealthPacket packet) {
- if (packet.getHealth() < 1) {
- PlayerActionPacket pk = new PlayerActionPacket();
- pk.setAction(PlayerActionPacket.Action.RESPAWN);
- pk.setBlockPosition(this.spawnPosition);
- pk.setFace(0);
- pk.setRuntimeEntityId(this.runtimeId);
- this.session.sendPacket(pk);
- }
- return true;
- }
- @Override
- public boolean handle(UpdateAttributesPacket packet) {
- if (packet.getRuntimeEntityId() == this.runtimeId) {
- packet.getAttributes().forEach(attr -> {
- if (attr.getName().equals("minecraft:health") && attr.getValue() < 1) {
- PlayerActionPacket pk = new PlayerActionPacket();
- pk.setAction(PlayerActionPacket.Action.RESPAWN);
- pk.setBlockPosition(this.spawnPosition);
- pk.setFace(0);
- pk.setRuntimeEntityId(this.runtimeId);
- this.session.sendPacket(pk);
- }
- });
- }
- return true;
- }
- public static synchronized ArrayList<BedrockClientSession> getSessions() {
- return sessions;
- }
- public static synchronized void setSessions(ArrayList<BedrockClientSession> sessions) {
- PacketHandler.sessions = sessions;
- }
- }
Add Comment
Please, Sign In to add comment