Skip to content

Commit

Permalink
Add client state to invert controls and swap movement
Browse files Browse the repository at this point in the history
See #200
  • Loading branch information
Matyrobbrt committed Oct 26, 2024
1 parent 65bfe79 commit bd26bb9
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.lovetropics.minigames.common.content.crafting_bee.CraftingBeeTexts;
import com.lovetropics.minigames.common.content.crafting_bee.SelectedRecipe;
import com.lovetropics.minigames.common.core.game.client_state.GameClientStateTypes;
import com.lovetropics.minigames.common.core.game.client_state.instance.CraftingBeeCrafts;
import com.lovetropics.minigames.common.core.game.client_state.instance.CraftingBeeCraftsClientState;
import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.platform.Lighting;
import com.mojang.blaze3d.systems.RenderSystem;
Expand Down Expand Up @@ -188,7 +188,7 @@ protected void updateWidgetNarration(NarrationElementOutput narrationElementOutp
}

@Nullable
private static CraftingBeeCrafts getState() {
private static CraftingBeeCraftsClientState getState() {
return ClientGameStateManager.getOrNull(GameClientStateTypes.CRAFTING_BEE_CRAFTS);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
import com.lovetropics.minigames.common.core.game.behavior.event.GamePhaseEvents;
import com.lovetropics.minigames.common.core.game.behavior.event.GamePlayerEvents;
import com.lovetropics.minigames.common.core.game.client_state.GameClientState;
import com.lovetropics.minigames.common.core.game.client_state.instance.CraftingBeeCrafts;
import com.lovetropics.minigames.common.core.game.client_state.instance.CraftingBeeCraftsClientState;
import com.lovetropics.minigames.common.core.game.player.PlayerSet;
import com.lovetropics.minigames.common.core.game.state.statistics.PlayerKey;
import com.lovetropics.minigames.common.core.game.state.statistics.StatisticKey;
import com.lovetropics.minigames.common.core.game.state.team.GameTeam;
import com.lovetropics.minigames.common.core.game.state.team.GameTeamKey;
Expand Down Expand Up @@ -183,7 +182,7 @@ private void sync(GameTeamKey team) {

private void sync(Player player) {
if (player instanceof ServerPlayer sp) {
GameClientState.sendToPlayer(new CraftingBeeCrafts(tasks.get(teams.getTeamForPlayer(player)).stream().map(CraftingTask::toCraft).toList(),
GameClientState.sendToPlayer(new CraftingBeeCraftsClientState(tasks.get(teams.getTeamForPlayer(player)).stream().map(CraftingTask::toCraft).toList(),
game.gameUuid(), allowedHints), sp);
}
}
Expand All @@ -203,8 +202,8 @@ private CraftingTask(ItemStack output, SelectedRecipe recipe) {
this.recipe = recipe;
}

public CraftingBeeCrafts.Craft toCraft() {
return new CraftingBeeCrafts.Craft(output, recipe.id(), done);
public CraftingBeeCraftsClientState.Craft toCraft() {
return new CraftingBeeCraftsClientState.Craft(output, recipe.id(), done);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@

import com.lovetropics.minigames.LoveTropics;
import com.lovetropics.minigames.common.core.game.client_state.instance.BeaconClientState;
import com.lovetropics.minigames.common.core.game.client_state.instance.CraftingBeeCrafts;
import com.lovetropics.minigames.common.core.game.client_state.instance.CraftingBeeCraftsClientState;
import com.lovetropics.minigames.common.core.game.client_state.instance.FogClientState;
import com.lovetropics.minigames.common.core.game.client_state.instance.GlowTeamMembersState;
import com.lovetropics.minigames.common.core.game.client_state.instance.HealthTagClientState;
import com.lovetropics.minigames.common.core.game.client_state.instance.HideRecipeBookState;
import com.lovetropics.minigames.common.core.game.client_state.instance.HideRecipeBookClientState;
import com.lovetropics.minigames.common.core.game.client_state.instance.InvertControlsClientState;
import com.lovetropics.minigames.common.core.game.client_state.instance.PointTagClientState;
import com.lovetropics.minigames.common.core.game.client_state.instance.ReplaceTexturesClientState;
import com.lovetropics.minigames.common.core.game.client_state.instance.ResourcePackClientState;
import com.lovetropics.minigames.common.core.game.client_state.instance.SidebarClientState;
import com.lovetropics.minigames.common.core.game.client_state.instance.SpectatingClientState;
import com.lovetropics.minigames.common.core.game.client_state.instance.SwapMovementClientState;
import com.lovetropics.minigames.common.core.game.client_state.instance.TeamMembersClientState;
import com.lovetropics.minigames.common.core.game.client_state.instance.TimeInterpolationClientState;
import com.lovetropics.minigames.common.util.registry.GameClientTweakEntry;
Expand Down Expand Up @@ -45,8 +47,10 @@ public final class GameClientStateTypes {
public static final GameClientTweakEntry<TeamMembersClientState> TEAM_MEMBERS = register("team_members", TeamMembersClientState.CODEC);
public static final GameClientTweakEntry<GlowTeamMembersState> GLOW_TEAM_MEMBERS = register("glow_team_members", MapCodec.unit(GlowTeamMembersState.INSTANCE));
public static final GameClientTweakEntry<PointTagClientState> POINT_TAGS = register("point_tags", PointTagClientState.CODEC);
public static final GameClientTweakEntry<HideRecipeBookState> HIDE_RECIPE_BOOK = register("hide_recipe_book", HideRecipeBookState.CODEC);
public static final GameClientTweakEntry<CraftingBeeCrafts> CRAFTING_BEE_CRAFTS = register("crafting_bee_crafts", CraftingBeeCrafts.CODEC);
public static final GameClientTweakEntry<HideRecipeBookClientState> HIDE_RECIPE_BOOK = register("hide_recipe_book", HideRecipeBookClientState.CODEC);
public static final GameClientTweakEntry<CraftingBeeCraftsClientState> CRAFTING_BEE_CRAFTS = register("crafting_bee_crafts", CraftingBeeCraftsClientState.CODEC);
public static final GameClientTweakEntry<InvertControlsClientState> INVERT_CONTROLS = register("invert_controls", InvertControlsClientState.CODEC);
public static final GameClientTweakEntry<SwapMovementClientState> SWAP_MOVEMENT = register("swap_movement", SwapMovementClientState.CODEC);

public static <T extends GameClientState> GameClientTweakEntry<T> register(final String name, final MapCodec<T> codec) {
return REGISTRATE.object(name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.core.UUIDUtil;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.ExtraCodecs;
import net.minecraft.world.item.ItemStack;

import java.util.List;
import java.util.UUID;

public record CraftingBeeCrafts(List<Craft> crafts, UUID gameId, int allowedHints) implements GameClientState {
public static final MapCodec<CraftingBeeCrafts> CODEC = RecordCodecBuilder.mapCodec(in -> in.group(
Craft.CODEC.listOf().fieldOf("crafts").forGetter(CraftingBeeCrafts::crafts),
UUIDUtil.CODEC.fieldOf("gameId").forGetter(CraftingBeeCrafts::gameId),
Codec.INT.fieldOf("allowedHints").forGetter(CraftingBeeCrafts::allowedHints)
).apply(in, CraftingBeeCrafts::new));
public record CraftingBeeCraftsClientState(List<Craft> crafts, UUID gameId, int allowedHints) implements GameClientState {
public static final MapCodec<CraftingBeeCraftsClientState> CODEC = RecordCodecBuilder.mapCodec(in -> in.group(
Craft.CODEC.listOf().fieldOf("crafts").forGetter(CraftingBeeCraftsClientState::crafts),
UUIDUtil.CODEC.fieldOf("gameId").forGetter(CraftingBeeCraftsClientState::gameId),
Codec.INT.fieldOf("allowedHints").forGetter(CraftingBeeCraftsClientState::allowedHints)
).apply(in, CraftingBeeCraftsClientState::new));

@Override
public GameClientStateType<?> getType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.ComponentSerialization;

public record HideRecipeBookState(Component message) implements GameClientState {
public static final MapCodec<HideRecipeBookState> CODEC = ComponentSerialization.CODEC
.fieldOf("message").xmap(HideRecipeBookState::new, HideRecipeBookState::message);
public record HideRecipeBookClientState(Component message) implements GameClientState {
public static final MapCodec<HideRecipeBookClientState> CODEC = ComponentSerialization.CODEC
.fieldOf("message").xmap(HideRecipeBookClientState::new, HideRecipeBookClientState::message);

@Override
public GameClientStateType<?> getType() {
Expand Down
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();
}
}
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();
}
}
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;
}
}
}
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);
}
}
4 changes: 3 additions & 1 deletion src/main/resources/ltminigames.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
"client.MinecraftMixin",
"client.PlayerTabOverlayGuiMixin",
"client.PoseStackAccessor",
"client.HiddenRecipeBookMixin"
"client.HiddenRecipeBookMixin",
"client.MouseHandlerMixin",
"client.KeyboardInputMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down

0 comments on commit bd26bb9

Please sign in to comment.