Skip to content

Commit

Permalink
Merge pull request #120 from INotDen/pull/1.19.4/midnight
Browse files Browse the repository at this point in the history
MidnightControls Support
  • Loading branch information
enjarai authored Jan 20, 2024
2 parents 9df0be5 + fd1c963 commit 54fbee4
Show file tree
Hide file tree
Showing 10 changed files with 213 additions and 2 deletions.
8 changes: 8 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ dependencies {
exclude group: "com.github.CaffeineMC"
}

modImplementation "dev.lambdaurora:spruceui:${project.spruceui_version}"
modImplementation "maven.modrinth:midnightlib:${project.midnightlib_version}"
modImplementation "maven.modrinth:midnightcontrols:${project.midnightcontrols_version}"
api('org.aperlambda:lambdajcommon:1.8.1') {
exclude group: 'com.google.code.gson'
exclude group: 'com.google.guava'
}

// Mod Menu integration.
modImplementation "com.terraformersmc:modmenu:${project.modmenu_version}"

Expand Down
4 changes: 4 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ controlify_version=1.2.0+1.19.4
mixin_extras_version=0.2.0
mixin_squared_version=0.1.1
permissions_api_version=0.2-SNAPSHOT
# https://github.com/TeamMidnightDust/MidnightControls
midnightcontrols_version=1.7.1+1.19
midnightlib_version=0.6.1
spruceui_version=4.0.0+1.19
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());
}

}
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");
}

}
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();

}
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);
}

}
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
}
}
2 changes: 1 addition & 1 deletion src/main/resources/assets/do_a_barrel_roll/lang/en_us.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ config.do_a_barrel_roll:
roll: Roll
controller:
.: Controller Sensitivity
description: Only applicable when Controlify is installed.
description: Only applicable when Controlify or MidnightControls is installed.
pitch: Pitch
yaw: Yaw
roll: Roll
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/assets/do_a_barrel_roll/lang/pt_br.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ config.do_a_barrel_roll:
roll: Roll
controller:
.: Sensibilidade do controlador
description: Aplicável apenas quando Controlify está instalado.
description: Aplicável apenas quando Controlify ou MidnightControls está instalado.
pitch: Pitch
yaw: Yaw
roll: Roll
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@
{
"config": "do-a-barrel-roll.compat.controlify.mixins.json",
"environment": "client"
},
{
"config": "do-a-barrel-roll.compat.midnightcontrols.mixins.json",
"environment": "client"
}
],
"accessWidener": "do-a-barrel-roll.accesswidener",
Expand Down

0 comments on commit 54fbee4

Please sign in to comment.