dnuhax

Untitled

Mar 27th, 2020
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. package wode;
  2.  
  3. import java.util.ArrayList;
  4. import com.nukkitx.math.vector.Vector3i;
  5. import com.nukkitx.protocol.bedrock.BedrockClientSession;
  6. import com.nukkitx.protocol.bedrock.handler.BedrockPacketHandler;
  7. import com.nukkitx.protocol.bedrock.packet.*;
  8.  
  9. public class PacketHandler implements BedrockPacketHandler {
  10.  
  11. private final BedrockClientSession session;
  12. public long runtimeId = 0;
  13. public Vector3i spawnPosition = Vector3i.ZERO;
  14. public boolean initialized = false;
  15. private static ArrayList<BedrockClientSession> sessions = new ArrayList<BedrockClientSession>();
  16. public static void stop() {
  17. for (int i = 0;i < getSessions().size();i++) {
  18. BedrockClientSession session = getSessions().get(i);
  19. try {
  20. session.disconnect();
  21. }catch (Exception e) {
  22. }
  23. }
  24. }
  25. public PacketHandler(BedrockClientSession session) {
  26. this.session = session;
  27. getSessions().add(session);
  28. }
  29.  
  30. @Override
  31. public boolean handle(DisconnectPacket packet) {
  32. this.session.disconnect();
  33. return true;
  34. }
  35.  
  36. @Override
  37. public boolean handle(ResourcePacksInfoPacket packet) {
  38. ResourcePackClientResponsePacket pk = new ResourcePackClientResponsePacket();
  39. pk.setStatus(ResourcePackClientResponsePacket.Status.HAVE_ALL_PACKS);
  40. this.session.sendPacket(pk);
  41. return true;
  42. }
  43.  
  44. @Override
  45. public boolean handle(ResourcePackStackPacket packet) {
  46. ResourcePackClientResponsePacket pk = new ResourcePackClientResponsePacket();
  47. pk.setStatus(ResourcePackClientResponsePacket.Status.COMPLETED);
  48. this.session.sendPacket(pk);
  49.  
  50. return true;
  51. }
  52.  
  53. @Override
  54. public boolean handle(StartGamePacket packet) {
  55. this.runtimeId = packet.getRuntimeEntityId();
  56. this.spawnPosition = packet.getDefaultSpawn();
  57.  
  58. RequestChunkRadiusPacket pk = new RequestChunkRadiusPacket();
  59. pk.setRadius(1);
  60. this.session.sendPacket(pk);
  61.  
  62. return true;
  63. }
  64.  
  65. @Override
  66. public boolean handle(PlayStatusPacket packet) {
  67. switch (packet.getStatus()) {
  68. case PLAYER_SPAWN:
  69. if (!this.initialized) {
  70. this.initialized = true;
  71.  
  72. SetLocalPlayerAsInitializedPacket pk = new SetLocalPlayerAsInitializedPacket();
  73. pk.setRuntimeEntityId(this.runtimeId);
  74. this.session.sendPacket(pk);
  75. }
  76. break;
  77. case LOGIN_SUCCESS:
  78. ClientCacheStatusPacket clientCacheStatusPacket = new ClientCacheStatusPacket();
  79. clientCacheStatusPacket.setSupported(false);
  80. this.session.sendPacket(clientCacheStatusPacket);
  81. break;
  82. }
  83.  
  84. return true;
  85. }
  86.  
  87. @Override
  88. public boolean handle(RespawnPacket packet) {
  89. if (packet.getState() == RespawnPacket.State.SERVER_SEARCHING) {
  90. RespawnPacket pk = new RespawnPacket();
  91. pk.setState(RespawnPacket.State.CLIENT_READY);
  92. this.session.sendPacket(pk);
  93. }
  94.  
  95. return true;
  96. }
  97.  
  98. @Override
  99. public boolean handle(SetHealthPacket packet) {
  100. if (packet.getHealth() < 1) {
  101. PlayerActionPacket pk = new PlayerActionPacket();
  102. pk.setAction(PlayerActionPacket.Action.RESPAWN);
  103. pk.setBlockPosition(this.spawnPosition);
  104. pk.setFace(0);
  105. pk.setRuntimeEntityId(this.runtimeId);
  106. this.session.sendPacket(pk);
  107. }
  108. return true;
  109. }
  110.  
  111. @Override
  112. public boolean handle(UpdateAttributesPacket packet) {
  113. if (packet.getRuntimeEntityId() == this.runtimeId) {
  114. packet.getAttributes().forEach(attr -> {
  115. if (attr.getName().equals("minecraft:health") && attr.getValue() < 1) {
  116. PlayerActionPacket pk = new PlayerActionPacket();
  117. pk.setAction(PlayerActionPacket.Action.RESPAWN);
  118. pk.setBlockPosition(this.spawnPosition);
  119. pk.setFace(0);
  120. pk.setRuntimeEntityId(this.runtimeId);
  121. this.session.sendPacket(pk);
  122. }
  123. });
  124. }
  125. return true;
  126. }
  127. public static synchronized ArrayList<BedrockClientSession> getSessions() {
  128. return sessions;
  129. }
  130. public static synchronized void setSessions(ArrayList<BedrockClientSession> sessions) {
  131. PacketHandler.sessions = sessions;
  132. }
  133. }
Add Comment
Please, Sign In to add comment