forked from Jorbon/cool_elytra
-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #120 from INotDen/pull/1.19.4/midnight
MidnightControls Support
- Loading branch information
Showing
10 changed files
with
213 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/client/java/nl/enjarai/doabarrelroll/compat/midnightcontrols/MidnightControlsCompat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package nl.enjarai.doabarrelroll.compat.midnightcontrols; | ||
|
||
import eu.midnightdust.midnightcontrols.client.controller.ButtonBinding; | ||
import eu.midnightdust.midnightcontrols.client.controller.InputManager; | ||
import it.unimi.dsi.fastutil.ints.Int2FloatMap; | ||
import it.unimi.dsi.fastutil.ints.Int2FloatOpenHashMap; | ||
import nl.enjarai.doabarrelroll.DoABarrelRollClient; | ||
import nl.enjarai.doabarrelroll.ModKeybindings; | ||
import nl.enjarai.doabarrelroll.api.event.RollContext; | ||
import nl.enjarai.doabarrelroll.api.event.RollEvents; | ||
import nl.enjarai.doabarrelroll.api.event.ThrustEvents; | ||
import nl.enjarai.doabarrelroll.api.rotation.RotationInstant; | ||
import nl.enjarai.doabarrelroll.compat.midnightcontrols.mixin.ButtonBindingAccessor; | ||
import nl.enjarai.doabarrelroll.config.ModConfig; | ||
import nl.enjarai.doabarrelroll.config.Sensitivity; | ||
import java.util.HashMap; | ||
|
||
import static org.lwjgl.glfw.GLFW.GLFW_GAMEPAD_AXIS_LAST; | ||
|
||
public class MidnightControlsCompat { | ||
|
||
public static final Int2FloatMap AXIS_VALUES = new Int2FloatOpenHashMap(GLFW_GAMEPAD_AXIS_LAST); | ||
|
||
public static ButtonBinding PITCH_UP; | ||
public static ButtonBinding PITCH_DOWN; | ||
public static ButtonBinding ROLL_LEFT; | ||
public static ButtonBinding ROLL_RIGHT; | ||
public static ButtonBinding YAW_LEFT; | ||
public static ButtonBinding YAW_RIGHT; | ||
public static ButtonBinding THRUST_FORWARD; | ||
public static ButtonBinding THRUST_BACKWARD; | ||
|
||
private RotationInstant applyToRotation(RotationInstant rotationDelta, RollContext context) { | ||
Sensitivity sensitivity = ModConfig.INSTANCE.getControllerSensitivity(); | ||
|
||
double multiplier = context.getRenderDelta() * 1200; | ||
|
||
double pitchAxis = Math.pow(AXIS_VALUES.getOrDefault(PITCH_DOWN.getButton()[0], InputManager.BUTTON_VALUES.getOrDefault(PITCH_DOWN.getButton()[0], 0f)), 2f) - | ||
Math.pow(AXIS_VALUES.getOrDefault(PITCH_UP.getButton()[0], InputManager.BUTTON_VALUES.getOrDefault(PITCH_UP.getButton()[0], 0f)), 2f); | ||
double yawAxis = Math.pow(AXIS_VALUES.getOrDefault(YAW_RIGHT.getButton()[0], InputManager.BUTTON_VALUES.getOrDefault(YAW_RIGHT.getButton()[0], 0f)), 2f) - | ||
Math.pow(AXIS_VALUES.getOrDefault(YAW_LEFT.getButton()[0], InputManager.BUTTON_VALUES.getOrDefault(YAW_LEFT.getButton()[0], 0f)), 2f); | ||
double rollAxis = Math.pow(AXIS_VALUES.getOrDefault(ROLL_RIGHT.getButton()[0], InputManager.BUTTON_VALUES.getOrDefault(ROLL_RIGHT.getButton()[0], 0f)), 2f) - | ||
Math.pow(AXIS_VALUES.getOrDefault(ROLL_LEFT.getButton()[0], InputManager.BUTTON_VALUES.getOrDefault(ROLL_LEFT.getButton()[0], 0f)), 2f); | ||
|
||
pitchAxis *= multiplier * sensitivity.pitch; | ||
yawAxis *= multiplier * sensitivity.yaw; | ||
rollAxis *= multiplier * sensitivity.roll; | ||
|
||
return rotationDelta.add(pitchAxis, yawAxis, rollAxis); | ||
} | ||
|
||
public static double getThrustModifier() { | ||
float forward = InputManager.BUTTON_VALUES.getOrDefault(THRUST_FORWARD.getButton()[0], 0f); | ||
float backward = InputManager.BUTTON_VALUES.getOrDefault(THRUST_BACKWARD.getButton()[0], 0f); | ||
return forward - backward; | ||
} | ||
|
||
public MidnightControlsCompat() { | ||
HashMap<String, ButtonBinding> bindings = new HashMap<>(); | ||
InputManager.streamCategories() | ||
.filter(c -> c.getIdentifier().equals(new org.aperlambda.lambdacommon.Identifier("minecraft", ModKeybindings.PITCH_UP.getCategory()))) | ||
.findFirst().ifPresent(list -> list.getBindings().forEach(bind -> bindings.put(bind.getName(), bind))); | ||
|
||
PITCH_UP = bindings.get(ModKeybindings.PITCH_UP.getTranslationKey()); | ||
((ButtonBindingAccessor)PITCH_UP).getActions().clear(); | ||
PITCH_DOWN = bindings.get(ModKeybindings.PITCH_DOWN.getTranslationKey()); | ||
((ButtonBindingAccessor)PITCH_DOWN).getActions().clear(); | ||
ROLL_LEFT = bindings.get(ModKeybindings.ROLL_LEFT.getTranslationKey()); | ||
((ButtonBindingAccessor)ROLL_LEFT).getActions().clear(); | ||
ROLL_RIGHT = bindings.get(ModKeybindings.ROLL_RIGHT.getTranslationKey()); | ||
((ButtonBindingAccessor)ROLL_RIGHT).getActions().clear(); | ||
YAW_LEFT = bindings.get(ModKeybindings.YAW_LEFT.getTranslationKey()); | ||
((ButtonBindingAccessor)YAW_LEFT).getActions().clear(); | ||
YAW_RIGHT = bindings.get(ModKeybindings.YAW_RIGHT.getTranslationKey()); | ||
((ButtonBindingAccessor)YAW_RIGHT).getActions().clear(); | ||
THRUST_FORWARD = bindings.get(ModKeybindings.THRUST_FORWARD.getTranslationKey()); | ||
((ButtonBindingAccessor)THRUST_FORWARD).getActions().clear(); | ||
THRUST_BACKWARD = bindings.get(ModKeybindings.THRUST_BACKWARD.getTranslationKey()); | ||
((ButtonBindingAccessor)THRUST_BACKWARD).getActions().clear(); | ||
|
||
RollEvents.LATE_CAMERA_MODIFIERS.register(context -> context | ||
.useModifier(this::applyToRotation), | ||
5, DoABarrelRollClient::isFallFlying); | ||
|
||
ThrustEvents.MODIFY_THRUST_INPUT.register(input -> input + getThrustModifier()); | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
src/client/java/nl/enjarai/doabarrelroll/compat/midnightcontrols/MidnightControlsPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package nl.enjarai.doabarrelroll.compat.midnightcontrols; | ||
|
||
import nl.enjarai.cicada.api.compat.CompatMixinPlugin; | ||
|
||
import java.util.Set; | ||
|
||
public class MidnightControlsPlugin implements CompatMixinPlugin { | ||
@Override | ||
public Set<String> getRequiredMods() { | ||
return Set.of("midnightcontrols"); | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
...nt/java/nl/enjarai/doabarrelroll/compat/midnightcontrols/mixin/ButtonBindingAccessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package nl.enjarai.doabarrelroll.compat.midnightcontrols.mixin; | ||
|
||
import eu.midnightdust.midnightcontrols.client.controller.ButtonBinding; | ||
import eu.midnightdust.midnightcontrols.client.controller.PressAction; | ||
import org.spongepowered.asm.mixin.*; | ||
import org.spongepowered.asm.mixin.gen.Accessor; | ||
|
||
import java.util.List; | ||
|
||
@Pseudo | ||
@Mixin(ButtonBinding.class) | ||
public interface ButtonBindingAccessor { | ||
|
||
@Accessor("actions") | ||
List<PressAction> getActions(); | ||
|
||
} |
61 changes: 61 additions & 0 deletions
61
...lient/java/nl/enjarai/doabarrelroll/compat/midnightcontrols/mixin/MidnightInputMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package nl.enjarai.doabarrelroll.compat.midnightcontrols.mixin; | ||
|
||
import eu.midnightdust.midnightcontrols.client.MidnightControlsConfig; | ||
import eu.midnightdust.midnightcontrols.client.MidnightInput; | ||
import eu.midnightdust.midnightcontrols.client.controller.ButtonBinding; | ||
import net.minecraft.client.MinecraftClient; | ||
import nl.enjarai.doabarrelroll.api.RollEntity; | ||
import nl.enjarai.doabarrelroll.compat.midnightcontrols.MidnightControlsCompat; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.spongepowered.asm.mixin.*; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
|
||
import static nl.enjarai.doabarrelroll.compat.midnightcontrols.MidnightControlsCompat.AXIS_VALUES; | ||
import static org.lwjgl.glfw.GLFW.*; | ||
|
||
@Pseudo | ||
@Mixin(MidnightInput.class) | ||
public abstract class MidnightInputMixin { | ||
|
||
@Unique | ||
private static MidnightControlsCompat compat; | ||
|
||
@Dynamic | ||
@Inject( | ||
method = "handleLook(Lnet/minecraft/client/MinecraftClient;IFI)V", | ||
at = @At(value = "HEAD"), | ||
cancellable = true | ||
) | ||
private void handleElytraLook(@NotNull MinecraftClient client, int axis, float value, int state, CallbackInfo ci) { | ||
if (compat == null) | ||
compat = new MidnightControlsCompat(); | ||
if (client.player instanceof RollEntity rollEntity && rollEntity.doABarrelRoll$isRolling()) { | ||
ci.cancel(); | ||
} | ||
} | ||
|
||
@Dynamic | ||
@Inject( | ||
method = "handleAxe(Lnet/minecraft/client/MinecraftClient;IFFI)V", | ||
at = @At(value = "HEAD") | ||
) | ||
private void handleAxe(@NotNull MinecraftClient client, int axis, float value, float absValue, int state, CallbackInfo ci) { | ||
if(client.currentScreen != null) { | ||
AXIS_VALUES.put(ButtonBinding.axisAsButton(axis, true), 0f); | ||
AXIS_VALUES.put(ButtonBinding.axisAsButton(axis, false), 0f); | ||
return; | ||
} | ||
|
||
double deadZone = (axis == GLFW_GAMEPAD_AXIS_LEFT_X || axis == GLFW_GAMEPAD_AXIS_LEFT_Y) ? | ||
MidnightControlsConfig.leftDeadZone : MidnightControlsConfig.rightDeadZone; | ||
float axisValue = absValue < deadZone ? 0.f : (float) (absValue - deadZone); | ||
axisValue /= (1.0 - deadZone); | ||
|
||
axisValue = (float) Math.min(axisValue / MidnightControlsConfig.getAxisMaxValue(axis), 1); | ||
AXIS_VALUES.put(ButtonBinding.axisAsButton(axis, true), value > 0 ? axisValue : 0f); | ||
AXIS_VALUES.put(ButtonBinding.axisAsButton(axis, false), value < 0 ? axisValue : 0f); | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
src/client/resources/do-a-barrel-roll.compat.midnightcontrols.mixins.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"required": true, | ||
"minVersion": "0.8", | ||
"package": "nl.enjarai.doabarrelroll.compat.midnightcontrols.mixin", | ||
"plugin": "nl.enjarai.doabarrelroll.compat.midnightcontrols.MidnightControlsPlugin", | ||
"compatibilityLevel": "JAVA_17", | ||
"client": [ | ||
"MidnightInputMixin", | ||
"ButtonBindingAccessor" | ||
], | ||
"mixins": [ | ||
], | ||
"injectors": { | ||
"defaultRequire": 1 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters