-
-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathInGameInputHandler.java
More file actions
501 lines (427 loc) · 20.3 KB
/
Copy pathInGameInputHandler.java
File metadata and controls
501 lines (427 loc) · 20.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
package dev.isxander.controlify.ingame;
import dev.isxander.controlify.Controlify;
import dev.isxander.controlify.api.ingameinput.LookInputModifier;
import dev.isxander.controlify.api.event.ControlifyEvents;
import dev.isxander.controlify.bindings.ControlifyBindings;
import dev.isxander.controlify.controller.gyro.GyroState;
import dev.isxander.controlify.controller.ControllerEntity;
import dev.isxander.controlify.controller.gyro.GyroButtonMode;
import dev.isxander.controlify.controller.gyro.GyroComponent;
import dev.isxander.controlify.controller.input.InputComponent;
import dev.isxander.controlify.driver.steamdeck.SteamDeckDriver;
import dev.isxander.controlify.gui.screen.RadialItems;
import dev.isxander.controlify.gui.screen.RadialMenuScreen;
import dev.isxander.controlify.mixins.feature.steamdeck.ScreenshotAccessor;
import dev.isxander.controlify.server.ServerPolicies;
import dev.isxander.controlify.utils.ControllerUtils;
import dev.isxander.controlify.utils.DebugOverlayHelper;
import dev.isxander.controlify.utils.HoldRepeatHelper;
import dev.isxander.controlify.utils.animation.api.Animation;
import dev.isxander.controlify.utils.animation.api.EasingFunction;
import net.minecraft.client.Camera;
import net.minecraft.client.CameraType;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.client.Screenshot;
import net.minecraft.client.gui.screens.inventory.InventoryScreen;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.network.chat.Component;
import net.minecraft.network.protocol.game.ServerboundPlayerActionPacket;
import net.minecraft.util.Mth;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.phys.Vec2;
import net.minecraft.world.phys.Vec3;
import org.joml.Vector2d;
import org.joml.Vector2f;
import java.io.File;
public class InGameInputHandler {
private final ControllerEntity controller;
private final Controlify controlify;
private final Minecraft minecraft;
private final Camera camera;
private double lookInputX, lookInputY; // in degrees per tick
private final GyroState gyroInput = new GyroState();
private boolean gyroToggledOn;
private boolean wasAiming;
private Animation flickAnimation;
private float yawOrigin;
private boolean shouldShowPlayerList;
private final HoldRepeatHelper dropRepeatHelper;
private boolean dropRepeating;
private final HoldRepeatHelper hotbarNextRepeatHelper;
private final HoldRepeatHelper hotbarPrevRepeatHelper;
public InGameInputHandler(ControllerEntity controller) {
this.controller = controller;
this.minecraft = Minecraft.getInstance();
this.camera = minecraft.gameRenderer.getMainCamera();
this.controlify = Controlify.instance();
this.dropRepeatHelper = new HoldRepeatHelper(20, 1);
this.hotbarNextRepeatHelper = new HoldRepeatHelper(10, 4);
this.hotbarPrevRepeatHelper = new HoldRepeatHelper(10, 4);
this.gyroToggledOn = false;
}
public void inputTick() {
boolean isController = ControllerPlayerMovement.shouldBeControllerInput();
handlePlayerLookInput(isController);
ControllerPlayerMovement.ensureCorrectInput(minecraft.player);
if (isController) {
handleKeybinds();
preventFlyDrifting();
}
}
protected void handleKeybinds() {
if (minecraft.screen != null)
return;
if (ControlifyBindings.PAUSE.on(controller).justPressed()) {
minecraft.pauseGame(false);
}
if (minecraft.player != null) {
Inventory inventory = minecraft.player.getInventory();
if (hotbarNextRepeatHelper.shouldAction(ControlifyBindings.NEXT_SLOT.on(controller))) {
hotbarNextRepeatHelper.onNavigate();
//? if >=1.21.5 {
inventory.setSelectedSlot((inventory.getSelectedSlot() + 1) % Inventory.getSelectionSize());
//?} elif >=1.21.2 {
/*inventory.setSelectedHotbarSlot((inventory.selected + 1) % Inventory.getSelectionSize());
*///?} else {
/*minecraft.player.getInventory().swapPaint(-1);
*///?}
}
if (hotbarPrevRepeatHelper.shouldAction(ControlifyBindings.PREV_SLOT.on(controller))) {
hotbarPrevRepeatHelper.onNavigate();
//? if >=1.21.5 {
inventory.setSelectedSlot((inventory.getSelectedSlot() - 1 + Inventory.getSelectionSize()) % Inventory.getSelectionSize());
//?} elif >=1.21.2 {
/*inventory.setSelectedHotbarSlot((inventory.selected - 1 + Inventory.getSelectionSize()) % Inventory.getSelectionSize());
*///?} else {
/*minecraft.player.getInventory().swapPaint(1);
*///?}
}
if (!minecraft.player.isSpectator()) {
if (ControlifyBindings.DROP_STACK.on(controller).justPressed()) {
if (minecraft.player.drop(true)) {
minecraft.player.swing(InteractionHand.MAIN_HAND);
}
} else {
if (ControlifyBindings.DROP_INGAME.on(controller).justPressed()) {
dropRepeating = true;
} else if (ControlifyBindings.DROP_INGAME.on(controller).justReleased()) {
dropRepeating = false;
}
if (dropRepeating && dropRepeatHelper.shouldAction(ControlifyBindings.DROP_INGAME.on(controller))) {
if (minecraft.player.drop(false)) {
dropRepeatHelper.onNavigate();
minecraft.player.swing(InteractionHand.MAIN_HAND);
}
}
}
if (ControlifyBindings.SWAP_HANDS.on(controller).justPressed()) {
minecraft.player.connection.send(new ServerboundPlayerActionPacket(ServerboundPlayerActionPacket.Action.SWAP_ITEM_WITH_OFFHAND, BlockPos.ZERO, Direction.DOWN));
}
}
if (ControlifyBindings.INVENTORY.on(controller).justPressed()) {
if (minecraft.gameMode.isServerControlledInventory()) {
minecraft.player.sendOpenInventory();
} else {
minecraft.getTutorial().onOpenInventory();
minecraft.setScreen(new InventoryScreen(minecraft.player));
}
}
if (ControlifyBindings.CHANGE_PERSPECTIVE.on(controller).justPressed()) {
CameraType cameraType = minecraft.options.getCameraType();
minecraft.options.setCameraType(minecraft.options.getCameraType().cycle());
if (cameraType.isFirstPerson() != minecraft.options.getCameraType().isFirstPerson()) {
minecraft.gameRenderer.checkEntityPostEffect(minecraft.options.getCameraType().isFirstPerson() ? minecraft.getCameraEntity() : null);
}
minecraft.levelRenderer.needsUpdate();
}
}
if (ControlifyBindings.TOGGLE_HUD_VISIBILITY.on(controller).justPressed()) {
minecraft.options.hideGui = !minecraft.options.hideGui;
}
if (ControlifyBindings.SHOW_PLAYER_LIST.on(controller).justPressed()) {
shouldShowPlayerList = !shouldShowPlayerList;
}
if (ControlifyBindings.TOGGLE_DEBUG_MENU.on(controller).justPressed()) {
DebugOverlayHelper.toggleOverlay();
}
if (ControlifyBindings.TOGGLE_DEBUG_MENU_FPS.on(controller).justPressed()) {
DebugOverlayHelper.toggleFpsOverlay();
}
if (ControlifyBindings.TOGGLE_DEBUG_MENU_NET.on(controller).justPressed()) {
DebugOverlayHelper.toggleNetworkOverlay();
}
if (ControlifyBindings.TOGGLE_DEBUG_MENU_PROF.on(controller).justPressed()) {
DebugOverlayHelper.toggleProfilerOverlay();
}
if (ControlifyBindings.DEBUG_RADIAL.on(controller).justPressed()) {
minecraft.setScreen(new RadialMenuScreen(
controller,
ControlifyBindings.DEBUG_RADIAL.on(controller),
RadialItems.createDebug(),
Component.empty(),
null, null
));
}
if (ControlifyBindings.TAKE_SCREENSHOT.on(controller).justPressed()) {
// get file before it takes and writes the screenshot (which changes the next name)
File screenshotFile = ScreenshotAccessor.invokeGetFile(
new File(minecraft.gameDirectory, "screenshots")
);
Screenshot.grab(
this.minecraft.gameDirectory,
this.minecraft.getMainRenderTarget(),
component -> this.minecraft.execute(() -> {
this.minecraft.gui.getChat().addMessage(component);
// TODO: this currently does not work, yet to debug why not
SteamDeckDriver.getDeck().ifPresent(deck -> {
deck.doSteamScreenshot(screenshotFile.getAbsoluteFile().toPath(), "");
});
})
);
}
if (ControlifyBindings.PICK_BLOCK.on(controller).justPressed()) {
((PickBlockAccessor) minecraft).controlify$pickBlock();
}
if (ControlifyBindings.PICK_BLOCK_NBT.on(controller).justPressed()) {
((PickBlockAccessor) minecraft).controlify$pickBlockWithNbt();
}
if (ControlifyBindings.RADIAL_MENU.on(controller).justPressed()) {
minecraft.setScreen(new RadialMenuScreen(
controller,
ControlifyBindings.RADIAL_MENU.on(controller),
RadialItems.createBindings(controller),
Component.translatable("controlify.radial_menu.configure_hint"),
null, null
));
}
if (ControlifyBindings.GAME_MODE_SWITCHER.on(controller).justPressed()) {
minecraft.setScreen(new RadialMenuScreen(
controller,
ControlifyBindings.GAME_MODE_SWITCHER.on(controller),
RadialItems.createGameModes(),
Component.empty(),
null, null)
);
}
if (ControlifyBindings.HOTBAR_SLOT_SELECT.on(controller).justPressed()) {
minecraft.setScreen(new RadialMenuScreen(
controller,
ControlifyBindings.HOTBAR_SLOT_SELECT.on(controller),
RadialItems.createHotbarItemSelect(),
Component.empty(),
null, null
));
}
if (/*? if >=1.21.5 {*/ minecraft.player.hasInfiniteMaterials() /*?} else {*/ /*this.minecraft.gameMode.hasInfiniteItems() *//*?}*/) {
if (ControlifyBindings.HOTBAR_LOAD_RADIAL.on(controller).justPressed()) {
minecraft.setScreen(new RadialMenuScreen(
controller,
ControlifyBindings.HOTBAR_LOAD_RADIAL.on(controller),
RadialItems.createHotbarLoad(),
Component.translatable("controlify.radial.hotbar_load_hint"),
null, null
));
}
if (ControlifyBindings.HOTBAR_SAVE_RADIAL.on(controller).justPressed()) {
minecraft.setScreen(new RadialMenuScreen(
controller,
ControlifyBindings.HOTBAR_SAVE_RADIAL.on(controller),
RadialItems.createHotbarSave(),
Component.translatable("controlify.radial.hotbar_save_hint"),
null, null
));
}
}
}
protected void handlePlayerLookInput(boolean isController) {
LocalPlayer player = this.minecraft.player;
if (!isController || !canProcessLookInput()) {
lookInputX = 0;
lookInputY = 0;
return;
}
boolean aiming = isAiming(player);
Vector2d lookImpulse = new Vector2d();
controller.gyro().ifPresent(gyro -> handleGyroLook(gyro, lookImpulse, aiming));
if (controller.gyro().map(gyro -> gyro.confObj().lookSensitivity > 0 && gyro.confObj().flickStick).orElse(false)) {
controller.gyro().ifPresent(gyro -> handleFlickStick(gyro, player));
} else {
controller.input().ifPresent(input -> handleRegularLook(input, lookImpulse, aiming, player));
}
var modifier = new LookInputModifier(new Vector2f((float) lookImpulse.x, (float) lookImpulse.y), controller);
ControlifyEvents.LOOK_INPUT_MODIFIER.invoke(modifier);
lookImpulse.set(modifier.lookInput());
lookInputX = lookImpulse.x;
lookInputY = lookImpulse.y;
wasAiming = aiming;
}
protected void handleRegularLook(InputComponent input, Vector2d impulse, boolean aiming, LocalPlayer player) {
InputComponent.Config config = input.confObj();
// normal look input
Vector2d regularImpulse = new Vector2d(
ControlifyBindings.LOOK_RIGHT.on(controller).analogueNow()
- ControlifyBindings.LOOK_LEFT.on(controller).analogueNow(),
ControlifyBindings.LOOK_DOWN.on(controller).analogueNow()
- ControlifyBindings.LOOK_UP.on(controller).analogueNow()
);
if (config.vLookInvert) {
regularImpulse.y *= -1;
}
InputCurve curve = config.lookInputCurve;
if (!config.isLCE) {
// apply the easing on its length to preserve circularity
regularImpulse = ControllerUtils.applyEasingToLength(
regularImpulse,
curve::apply
);
} else {
// LCE doesn't preserve circularity
regularImpulse.x = curve.apply(regularImpulse.x);
regularImpulse.y = curve.apply(regularImpulse.y);
}
if (config.reduceAimingSensitivity && player.isUsingItem()) {
float aimMultiplier = config.isLCE
? switch (player.getUseItem().getUseAnimation()) {
case BOW, CROSSBOW, SPEAR, SPYGLASS -> 0.15f;
default -> 1f;
}
: switch (player.getUseItem().getUseAnimation()) {
case BOW, CROSSBOW, SPEAR -> 0.6f;
case SPYGLASS -> 0.2f;
default -> 1f;
};
regularImpulse.mul(aimMultiplier);
}
// 10 degrees per second at 100% sensitivity
regularImpulse.x *= config.hLookSensitivity * 10f;
regularImpulse.y *= config.vLookSensitivity * 10f;
impulse.add(regularImpulse);
}
protected void handleGyroLook(GyroComponent gyro, Vector2d impulse, boolean aiming) {
GyroComponent.Config config = gyro.confObj();
var gyroButton = ControlifyBindings.GYRO_BUTTON.on(controller);
if (config.requiresButton.equals(GyroButtonMode.ON) && (!gyroButton.digitalNow() && !aiming)) {
gyroInput.set(0);
} else if(config.requiresButton.equals(GyroButtonMode.INVERT) && (gyroButton.digitalNow() && !aiming)) {
gyroInput.set(0);
} else if(config.requiresButton.equals(GyroButtonMode.TOGGLE) && (!gyroToggledOn && !aiming)) {
gyroInput.set(0);
} else {
if (config.relativeGyroMode)
gyroInput.add(new GyroState(gyro.getState()).mul(0.1f));
else
gyroInput.set(gyro.getState());
}
if(config.requiresButton.equals(GyroButtonMode.TOGGLE) && gyroButton.justPressed()) {
gyroToggledOn = !gyroToggledOn;
}
// convert radians per second into degrees per tick
GyroState thisInput = new GyroState(gyroInput)
.mul(Mth.RAD_TO_DEG)
.div(20)
.mul(config.lookSensitivity);
impulse.y += -thisInput.pitch() * (config.invertY ? -1 : 1);
impulse.x += switch (config.yawMode) {
case YAW -> -thisInput.yaw();
case ROLL -> -thisInput.roll();
case BOTH -> -thisInput.yaw() - thisInput.roll();
} * (config.invertX ? -1 : 1);
}
protected void handleFlickStick(GyroComponent gyro, LocalPlayer player) {
GyroComponent.Config config = gyro.confObj();
float y = ControlifyBindings.LOOK_DOWN.on(controller).analogueNow()
- ControlifyBindings.LOOK_UP.on(controller).analogueNow();
float x = ControlifyBindings.LOOK_RIGHT.on(controller).analogueNow()
- ControlifyBindings.LOOK_LEFT.on(controller).analogueNow();
if (y == 0f && x == 0f) {
yawOrigin = camera.getYRot();
} else {
float yawCurrent = camera.getYRot();
float flickAngle = Mth.wrapDegrees((float) Mth.atan2(y, x) * Mth.RAD_TO_DEG + 90f);
float yawTurn = Mth.wrapDegrees((float) (yawOrigin + flickAngle) - yawCurrent);
if (flickAnimation != null && flickAnimation.isPlaying()) {
flickAnimation.skipToEnd();
}
if (config.flickAnimationTicks != 0) {
flickAnimation = Animation.of(config.flickAnimationTicks)
.easing(EasingFunction.EASE_OUT_EXPO)
.deltaConsumerD(angle -> player.turn(angle, 0), 0, yawTurn / 0.15)
.play();
} else {
player.turn(yawTurn / 0.15, 0);
}
}
}
public void processPlayerLook(float deltaTime) {
if (minecraft.player != null) {
double velX = lookInputX / 0.15 * deltaTime;
double velY = lookInputY / 0.15 * deltaTime;
//noinspection SuspiciousNameCombination
minecraft.player.turn(velX, velY);
minecraft.getTutorial().onMouse(velX, velY);
}
}
public boolean shouldShowPlayerList() {
return this.shouldShowPlayerList;
}
public void preventFlyDrifting() {
if (!controller.genericConfig().config().disableFlyDrifting || !ServerPolicies.DISABLE_FLY_DRIFTING.get()) {
return;
}
LocalPlayer player = minecraft.player;
if (player != null && player.getAbilities().flying && !player.onGround()) {
Vec3 motion = player.getDeltaMovement();
double x = motion.x;
double y = motion.y;
double z = motion.z;
//? if >=1.21.2 {
boolean jumping = player.input.keyPresses.jump();
boolean shiftKeyDown = player.input.keyPresses.shift();
//?} else {
/*boolean jumping = player.input.jumping;
boolean shiftKeyDown = player.input.shiftKeyDown;
*///?}
if (!jumping)
y = Math.min(y, 0);
if (!shiftKeyDown)
y = Math.max(y, 0);
Vec2 moveVec = getMoveVec(player.input);
if (moveVec.x == 0 && moveVec.y == 0) {
x = 0;
z = 0;
}
player.setDeltaMovement(x, y, z);
}
}
private boolean isAiming(Player player) {
return player.isUsingItem() && switch (player.getUseItem().getUseAnimation()) {
case BOW, CROSSBOW, SPEAR, SPYGLASS -> true;
default -> false;
};
}
private boolean canProcessLookInput() {
boolean mouseNotGrabbed = !minecraft.mouseHandler.isMouseGrabbed() && !controlify.config().globalSettings().outOfFocusInput;
boolean outOfFocus = !minecraft.isWindowActive() && !controlify.config().globalSettings().outOfFocusInput;
boolean screenVisible = minecraft.screen != null;
boolean playerExists = minecraft.player != null;
return !mouseNotGrabbed && !outOfFocus && !screenVisible && playerExists;
}
public static Vec2 getMoveVec(
//? if >=1.21.2 {
net.minecraft.client.player.ClientInput input
//?} else {
/*net.minecraft.client.player.Input input
*///?}
) {
//? if >=1.21.5 {
return input.getMoveVector();
//?} else {
/*return new Vec2(input.leftImpulse, input.forwardImpulse);
*///?}
}
}