PlayerTicker.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package chylex.bettercontrols.player;
  2. import chylex.bettercontrols.BetterControlsMod;
  3. import chylex.bettercontrols.config.BetterControlsConfig;
  4. import chylex.bettercontrols.gui.BetterControlsScreen;
  5. import chylex.bettercontrols.mixin.AccessCameraFields;
  6. import chylex.bettercontrols.mixin.AccessClientPlayerFields;
  7. import net.minecraft.client.MinecraftClient;
  8. import net.minecraft.client.network.ClientPlayerEntity;
  9. import java.lang.ref.WeakReference;
  10. public final class PlayerTicker{
  11. private static PlayerTicker ticker = new PlayerTicker(null);
  12. public static PlayerTicker get(final ClientPlayerEntity player){
  13. if (ticker.ref.get() != player){
  14. ticker = new PlayerTicker(player);
  15. }
  16. return ticker;
  17. }
  18. private static MinecraftClient mc(){
  19. return MinecraftClient.getInstance();
  20. }
  21. private static BetterControlsConfig cfg(){
  22. return BetterControlsMod.config;
  23. }
  24. private final WeakReference<ClientPlayerEntity> ref;
  25. private PlayerTicker(final ClientPlayerEntity player){
  26. this.ref = new WeakReference<>(player);
  27. }
  28. // Logic
  29. private boolean wasHittingObstacle = false;
  30. private boolean wasSprintingBeforeHittingObstacle = false;
  31. private boolean wasSneakingBeforeTouchingGround = false;
  32. private boolean holdingSneakWhileTouchingGround = false;
  33. public void atHead(final ClientPlayerEntity player){
  34. if (FlightHelper.shouldFlyOnGround(player)){
  35. player.setOnGround(false);
  36. }
  37. if (!cfg().doubleTapForwardToSprint){
  38. ((AccessClientPlayerFields)player).setTicksLeftToDoubleTapSprint(0);
  39. }
  40. }
  41. public void afterInputTick(final ClientPlayerEntity player){
  42. final float flightSpeed = FlightHelper.getFlightSpeed(player);
  43. if (flightSpeed > 0F){
  44. player.abilities.setFlySpeed(flightSpeed);
  45. }
  46. if (cfg().resumeSprintingAfterHittingObstacle){
  47. if (wasHittingObstacle != player.horizontalCollision){
  48. if (!wasHittingObstacle){
  49. wasSprintingBeforeHittingObstacle = player.isSprinting() || mc().options.keySprint.isPressed();
  50. }
  51. else if (wasSprintingBeforeHittingObstacle){
  52. wasSprintingBeforeHittingObstacle = false;
  53. player.setSprinting(true);
  54. }
  55. // collision also stops when the player lets go of movement keys
  56. wasHittingObstacle = player.horizontalCollision;
  57. }
  58. }
  59. else{
  60. wasHittingObstacle = player.horizontalCollision;
  61. wasSprintingBeforeHittingObstacle = false;
  62. }
  63. }
  64. public void afterSuperCall(final ClientPlayerEntity player){
  65. if (FlightHelper.shouldFlyOnGround(player)){
  66. final boolean isSneaking = player.isSneaking();
  67. final boolean isOnGround = player.isOnGround();
  68. if (!isSneaking){
  69. wasSneakingBeforeTouchingGround = false;
  70. }
  71. else if (!isOnGround){
  72. wasSneakingBeforeTouchingGround = true;
  73. }
  74. if (!isOnGround){
  75. holdingSneakWhileTouchingGround = false;
  76. }
  77. else{
  78. boolean cancelLanding = true;
  79. if (!wasSneakingBeforeTouchingGround){
  80. if (isSneaking){
  81. holdingSneakWhileTouchingGround = true;
  82. }
  83. else if (holdingSneakWhileTouchingGround){
  84. player.abilities.flying = false;
  85. player.sendAbilitiesUpdate();
  86. cancelLanding = false;
  87. }
  88. }
  89. if (cancelLanding){
  90. player.setOnGround(false);
  91. }
  92. }
  93. }
  94. else{
  95. wasSneakingBeforeTouchingGround = false;
  96. holdingSneakWhileTouchingGround = false;
  97. }
  98. if (!cfg().sneakingMovesCameraSmoothly){
  99. final AccessCameraFields camera = (AccessCameraFields)mc().gameRenderer.getCamera();
  100. if (camera.getFocusedEntity() == player){
  101. camera.setCameraY(player.getStandingEyeHeight());
  102. }
  103. }
  104. if (cfg().keyOpenMenu.isPressed()){
  105. mc().openScreen(new BetterControlsScreen(null));
  106. }
  107. }
  108. }