PlayerTicker.java 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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.input.SprintMode;
  6. import chylex.bettercontrols.input.ToggleTracker;
  7. import chylex.bettercontrols.input.ToggleTrackerForStickyKey;
  8. import chylex.bettercontrols.mixin.AccessCameraFields;
  9. import chylex.bettercontrols.mixin.AccessClientPlayerFields;
  10. import chylex.bettercontrols.mixin.AccessGameRendererFields;
  11. import chylex.bettercontrols.mixin.AccessPlayerFields;
  12. import chylex.bettercontrols.mixin.AccessStickyKeyBindingStateGetter;
  13. import chylex.bettercontrols.util.Key;
  14. import net.minecraft.client.input.Input;
  15. import net.minecraft.client.input.KeyboardInput;
  16. import net.minecraft.client.network.ClientPlayerEntity;
  17. import java.lang.ref.WeakReference;
  18. import java.util.function.BooleanSupplier;
  19. import static chylex.bettercontrols.util.Statics.KEY_FORWARD;
  20. import static chylex.bettercontrols.util.Statics.KEY_JUMP;
  21. import static chylex.bettercontrols.util.Statics.KEY_SNEAK;
  22. import static chylex.bettercontrols.util.Statics.KEY_SPRINT;
  23. import static chylex.bettercontrols.util.Statics.MINECRAFT;
  24. import static chylex.bettercontrols.util.Statics.OPTIONS;
  25. public final class PlayerTicker{
  26. private static PlayerTicker ticker = new PlayerTicker(null);
  27. public static PlayerTicker get(final ClientPlayerEntity player){
  28. if (ticker.ref.get() != player){
  29. ticker = new PlayerTicker(player);
  30. }
  31. return ticker;
  32. }
  33. private static BetterControlsConfig cfg(){
  34. return BetterControlsMod.config;
  35. }
  36. private final WeakReference<ClientPlayerEntity> ref;
  37. private PlayerTicker(final ClientPlayerEntity player){
  38. this.ref = new WeakReference<>(player);
  39. setup();
  40. }
  41. // Logic
  42. private final ToggleTracker toggleSprint = new ToggleTrackerForStickyKey(cfg().keyToggleSprint, KEY_SPRINT, toggled -> OPTIONS.sprintToggled = toggled);
  43. private final ToggleTracker toggleSneak = new ToggleTrackerForStickyKey(cfg().keyToggleSneak, KEY_SNEAK, toggled -> OPTIONS.sneakToggled = toggled);
  44. private final ToggleTracker toggleWalkForward = new ToggleTracker(cfg().keyToggleWalkForward, KEY_FORWARD);
  45. private final ToggleTracker toggleJump = new ToggleTracker(cfg().keyToggleJump, KEY_JUMP);
  46. private boolean waitingForSprintKeyRelease = false;
  47. private boolean stopSprintingAfterReleasingSprintKey = false;
  48. private boolean wasHittingObstacle = false;
  49. private boolean wasSprintingBeforeHittingObstacle = false;
  50. private int temporarySprintTimer = 0;
  51. private boolean wasSneakingBeforeTouchingGround = false;
  52. private boolean holdingSneakWhileTouchingGround = false;
  53. private int temporaryFlyOnGroundTimer = 0;
  54. private void setup(){
  55. final AccessStickyKeyBindingStateGetter sprint = (AccessStickyKeyBindingStateGetter)KEY_SPRINT;
  56. BooleanSupplier getter = sprint.getToggleGetter();
  57. if (getter instanceof SprintPressGetter){
  58. getter = ((SprintPressGetter)getter).getWrapped();
  59. }
  60. sprint.setToggleGetter(new SprintPressGetter(getter, () -> temporarySprintTimer > 0));
  61. }
  62. public void atHead(final ClientPlayerEntity player){
  63. if (FlightHelper.shouldFlyOnGround(player)){
  64. player.setOnGround(false);
  65. }
  66. if (!cfg().doubleTapForwardToSprint){
  67. ((AccessClientPlayerFields)player).setTicksLeftToDoubleTapSprint(0);
  68. }
  69. if (!cfg().doubleTapJumpToToggleFlight){
  70. ((AccessPlayerFields)player).setTicksLeftToDoubleTapFlight(0);
  71. }
  72. final SprintMode sprintMode = cfg().sprintMode;
  73. final boolean wasSprintToggled = OPTIONS.sprintToggled;
  74. final boolean isSprintToggled = toggleSprint.tick();
  75. if (temporarySprintTimer > 0){
  76. stopSprintingAfterReleasingSprintKey = false;
  77. waitingForSprintKeyRelease = false;
  78. final int nextTemporarySprintTimer = temporarySprintTimer - 1;
  79. temporarySprintTimer = 0;
  80. if (!Key.isPressed(KEY_SPRINT) && Key.isPressed(KEY_FORWARD)){
  81. temporarySprintTimer = nextTemporarySprintTimer;
  82. }
  83. else if (sprintMode == SprintMode.TAP_TO_TOGGLE){
  84. stopSprintingAfterReleasingSprintKey = true;
  85. }
  86. }
  87. if (isSprintToggled){
  88. stopSprintingAfterReleasingSprintKey = false;
  89. waitingForSprintKeyRelease = false;
  90. }
  91. else if (wasSprintToggled){
  92. stopSprintingAfterReleasingSprintKey = true;
  93. waitingForSprintKeyRelease = true;
  94. }
  95. else if (sprintMode == SprintMode.TAP_TO_TOGGLE){
  96. if (Key.isPressed(KEY_SPRINT)){
  97. if (!waitingForSprintKeyRelease){
  98. waitingForSprintKeyRelease = true;
  99. stopSprintingAfterReleasingSprintKey = player.isSprinting();
  100. }
  101. }
  102. else{
  103. if (player.isSprinting() && !waitingForSprintKeyRelease){
  104. stopSprintingAfterReleasingSprintKey = false;
  105. }
  106. waitingForSprintKeyRelease = false;
  107. }
  108. }
  109. else if (sprintMode == SprintMode.HOLD){
  110. if (Key.isPressed(KEY_SPRINT)){
  111. stopSprintingAfterReleasingSprintKey = true;
  112. }
  113. }
  114. if (stopSprintingAfterReleasingSprintKey && !Key.isPressed(KEY_SPRINT)){
  115. stopSprintingAfterReleasingSprintKey = false;
  116. waitingForSprintKeyRelease = false;
  117. player.setSprinting(false);
  118. }
  119. toggleSneak.tick();
  120. }
  121. public void afterInputAssignsPressingForward(final KeyboardInput input){
  122. if (MINECRAFT.currentScreen == null){
  123. input.pressingForward |= toggleWalkForward.tick();
  124. }
  125. }
  126. public void afterInputTick(final ClientPlayerEntity player){
  127. final Input input = player.input;
  128. if (MINECRAFT.currentScreen == null && !player.abilities.flying){
  129. input.jumping |= toggleJump.tick();
  130. }
  131. if (FlightHelper.isFlyingCreativeOrSpectator(player)){
  132. final boolean boost = Key.isPressed(KEY_SPRINT);
  133. final float flightSpeed = FlightHelper.getFlightSpeed(player, boost);
  134. final float verticalVelocity = FlightHelper.getExtraVerticalVelocity(player, boost);
  135. if (flightSpeed > 0F){
  136. player.abilities.setFlySpeed(flightSpeed);
  137. }
  138. if (Math.abs(verticalVelocity) > 1E-5F && player == MINECRAFT.getCameraEntity()){
  139. int direction = 0;
  140. if (input.sneaking){
  141. --direction;
  142. }
  143. if (input.jumping){
  144. ++direction;
  145. }
  146. if (direction != 0){
  147. player.setVelocity(player.getVelocity().add(0D, flightSpeed * verticalVelocity * direction, 0D));
  148. }
  149. }
  150. if (cfg().disableFlightInertia){
  151. if (input.movementForward == 0F && input.movementSideways == 0F){
  152. player.setVelocity(player.getVelocity().multiply(0.0, 1.0, 0.0));
  153. }
  154. if (!input.jumping && !input.sneaking){
  155. player.setVelocity(player.getVelocity().multiply(1.0, 0.0, 1.0));
  156. }
  157. }
  158. }
  159. if (cfg().resumeSprintingAfterHittingObstacle){
  160. if (wasHittingObstacle != player.horizontalCollision){
  161. if (!wasHittingObstacle){
  162. wasSprintingBeforeHittingObstacle = player.isSprinting() || Key.isPressed(KEY_SPRINT);
  163. }
  164. else if (wasSprintingBeforeHittingObstacle){
  165. wasSprintingBeforeHittingObstacle = false;
  166. temporarySprintTimer = 10;
  167. }
  168. // collision also stops when the player lets go of movement keys
  169. wasHittingObstacle = player.horizontalCollision;
  170. }
  171. }
  172. else{
  173. wasHittingObstacle = player.horizontalCollision;
  174. wasSprintingBeforeHittingObstacle = false;
  175. }
  176. if (cfg().disableChangingFovWhileFlying && FlightHelper.isFlyingCreativeOrSpectator(player)){
  177. ((AccessGameRendererFields)MINECRAFT.gameRenderer).setMovementFovMultiplier(1F);
  178. }
  179. }
  180. public void afterSuperCall(final ClientPlayerEntity player){
  181. if (FlightHelper.shouldFlyOnGround(player)){
  182. final boolean isSneaking = player.isSneaking();
  183. final boolean isOnGround = player.isOnGround();
  184. if (!isSneaking){
  185. wasSneakingBeforeTouchingGround = false;
  186. }
  187. else if (!isOnGround){
  188. wasSneakingBeforeTouchingGround = true;
  189. }
  190. if (!isOnGround){
  191. holdingSneakWhileTouchingGround = false;
  192. }
  193. else{
  194. boolean cancelLanding = true;
  195. if (!wasSneakingBeforeTouchingGround){
  196. if (isSneaking){
  197. holdingSneakWhileTouchingGround = true;
  198. }
  199. else if (holdingSneakWhileTouchingGround){
  200. player.abilities.flying = false;
  201. player.sendAbilitiesUpdate();
  202. cancelLanding = false;
  203. }
  204. }
  205. if (cancelLanding){
  206. player.setOnGround(false);
  207. }
  208. }
  209. }
  210. else{
  211. wasSneakingBeforeTouchingGround = false;
  212. holdingSneakWhileTouchingGround = false;
  213. }
  214. if (player.isCreative()){
  215. if (Key.wasPressed(cfg().keyToggleFlight)){
  216. final boolean isFlying = !player.abilities.flying;
  217. player.abilities.flying = isFlying;
  218. player.sendAbilitiesUpdate();
  219. if (isFlying){
  220. temporaryFlyOnGroundTimer = 10;
  221. }
  222. }
  223. if (temporaryFlyOnGroundTimer > 0){
  224. if (player.isSneaking()){
  225. temporaryFlyOnGroundTimer = 0;
  226. }
  227. else{
  228. --temporaryFlyOnGroundTimer;
  229. player.setOnGround(false);
  230. }
  231. }
  232. }
  233. else{
  234. temporaryFlyOnGroundTimer = 0;
  235. }
  236. if (!cfg().sneakingMovesCameraSmoothly){
  237. final AccessCameraFields camera = (AccessCameraFields)MINECRAFT.gameRenderer.getCamera();
  238. if (camera.getFocusedEntity() == player){
  239. camera.setCameraY(player.getStandingEyeHeight());
  240. }
  241. }
  242. if (Key.wasPressed(cfg().keyResetAllToggles)){
  243. toggleSprint.reset();
  244. toggleSneak.reset();
  245. toggleWalkForward.reset();
  246. toggleJump.reset();
  247. }
  248. if (Key.isPressed(cfg().keyOpenMenu)){
  249. MINECRAFT.openScreen(new BetterControlsScreen(null));
  250. }
  251. }
  252. }