-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add client state to invert controls and swap movement
See #200
- Loading branch information
1 parent
65bfe79
commit bd26bb9
Showing
10 changed files
with
120 additions
and
22 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
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
20 changes: 20 additions & 0 deletions
20
...vetropics/minigames/common/core/game/client_state/instance/InvertControlsClientState.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,20 @@ | ||
package com.lovetropics.minigames.common.core.game.client_state.instance; | ||
|
||
import com.lovetropics.minigames.common.core.game.client_state.GameClientState; | ||
import com.lovetropics.minigames.common.core.game.client_state.GameClientStateType; | ||
import com.lovetropics.minigames.common.core.game.client_state.GameClientStateTypes; | ||
import com.mojang.serialization.Codec; | ||
import com.mojang.serialization.MapCodec; | ||
import com.mojang.serialization.codecs.RecordCodecBuilder; | ||
|
||
public record InvertControlsClientState(boolean xAxis, boolean yAxis) implements GameClientState { | ||
public static final MapCodec<InvertControlsClientState> CODEC = RecordCodecBuilder.mapCodec(in -> in.group( | ||
Codec.BOOL.optionalFieldOf("x_axis", false).forGetter(InvertControlsClientState::xAxis), | ||
Codec.BOOL.optionalFieldOf("y_axis", true).forGetter(InvertControlsClientState::yAxis) | ||
).apply(in, InvertControlsClientState::new)); | ||
|
||
@Override | ||
public GameClientStateType<?> getType() { | ||
return GameClientStateTypes.INVERT_CONTROLS.get(); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...lovetropics/minigames/common/core/game/client_state/instance/SwapMovementClientState.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,15 @@ | ||
package com.lovetropics.minigames.common.core.game.client_state.instance; | ||
|
||
import com.lovetropics.minigames.common.core.game.client_state.GameClientState; | ||
import com.lovetropics.minigames.common.core.game.client_state.GameClientStateType; | ||
import com.lovetropics.minigames.common.core.game.client_state.GameClientStateTypes; | ||
import com.mojang.serialization.MapCodec; | ||
|
||
public record SwapMovementClientState() implements GameClientState { | ||
public static final MapCodec<SwapMovementClientState> CODEC = MapCodec.unit(SwapMovementClientState::new); | ||
|
||
@Override | ||
public GameClientStateType<?> getType() { | ||
return GameClientStateTypes.SWAP_MOVEMENT.get(); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/lovetropics/minigames/mixin/client/KeyboardInputMixin.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,22 @@ | ||
package com.lovetropics.minigames.mixin.client; | ||
|
||
import com.lovetropics.minigames.client.game.ClientGameStateManager; | ||
import com.lovetropics.minigames.common.core.game.client_state.GameClientStateTypes; | ||
import net.minecraft.client.player.Input; | ||
import net.minecraft.client.player.KeyboardInput; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
|
||
@Mixin(KeyboardInput.class) | ||
public class KeyboardInputMixin extends Input { | ||
@Inject(method = "tick", at = @At("TAIL")) | ||
private void respectSwappedMovement(boolean isSneaking, float sneakingSpeedMultiplier, CallbackInfo ci) { | ||
if (ClientGameStateManager.getOrNull(GameClientStateTypes.SWAP_MOVEMENT) != null) { | ||
float oldLeft = leftImpulse; | ||
leftImpulse = forwardImpulse; | ||
forwardImpulse = oldLeft; | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/com/lovetropics/minigames/mixin/client/MouseHandlerMixin.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,37 @@ | ||
package com.lovetropics.minigames.mixin.client; | ||
|
||
import com.llamalad7.mixinextras.injector.wrapoperation.Operation; | ||
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; | ||
import com.lovetropics.minigames.client.game.ClientGameStateManager; | ||
import com.lovetropics.minigames.common.core.game.client_state.GameClientStateTypes; | ||
import net.minecraft.client.Minecraft; | ||
import net.minecraft.client.MouseHandler; | ||
import net.minecraft.client.player.LocalPlayer; | ||
import org.spongepowered.asm.mixin.Final; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
|
||
@Mixin(MouseHandler.class) | ||
public class MouseHandlerMixin { | ||
@Shadow | ||
@Final | ||
private Minecraft minecraft; | ||
|
||
@WrapOperation(method = "turnPlayer", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/player/LocalPlayer;turn(DD)V")) | ||
private void respectControlModificationState(LocalPlayer player, double dx, double dy, Operation<Void> original) { | ||
var state = ClientGameStateManager.getOrNull(GameClientStateTypes.INVERT_CONTROLS); | ||
if (state != null) { | ||
if (state.xAxis()) { | ||
dx = dx * -1; | ||
} | ||
|
||
// Avoid allowing the bypass of the setting by modifying the options | ||
if (state.yAxis() && !minecraft.options.invertYMouse().get()) { | ||
dy = dy * -1; | ||
} | ||
} | ||
|
||
original.call(player, dx, dy); | ||
} | ||
} |
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