Skip to content

Commit 1280486

Browse files
committed
Extend sever policy to enforce keyboard-like movement (close #366)
1 parent 63c221e commit 1280486

File tree

8 files changed

+54
-35
lines changed

8 files changed

+54
-35
lines changed

src/main/java/dev/isxander/controlify/config/GlobalSettings.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.google.gson.annotations.SerializedName;
55
import dev.isxander.controlify.driver.steamdeck.SteamDeckUtil;
66
import dev.isxander.controlify.reacharound.ReachAroundMode;
7+
import dev.isxander.controlify.server.ServerPolicies;
78
import net.minecraft.client.Minecraft;
89
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
910
import net.minecraft.client.multiplayer.ServerData;
@@ -39,7 +40,8 @@ public class GlobalSettings {
3940
public boolean shouldUseKeyboardMovement() {
4041
ServerData server = Minecraft.getInstance().getCurrentServer();
4142
return alwaysKeyboardMovement
42-
|| (server != null && keyboardMovementWhitelist.stream().anyMatch(server.ip::endsWith));
43+
|| (server != null && keyboardMovementWhitelist.stream().anyMatch(server.ip::endsWith))
44+
|| ServerPolicies.KEYBOARD_LIKE_MOVEMENT.get();
4345
}
4446

4547
// Quiet mode does not work on Steam Deck

src/main/java/dev/isxander/controlify/gui/screen/ControllerConfigScreenFactory.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,11 @@ private Optional<OptionGroup> makeControlsGroup(ControllerEntity controller) {
209209
.name(Component.translatable("controlify.gui.no_fly_drifting"))
210210
.description(OptionDescription.createBuilder()
211211
.text(Component.translatable("controlify.gui.no_fly_drifting.tooltip"))
212-
.text(ServerPolicies.DISABLE_FLY_DRIFTING.get() != ServerPolicy.UNSET ? Component.translatable("controlify.gui.server_controlled").withStyle(ChatFormatting.GOLD) : Component.empty())
212+
.text(ServerPolicies.DISABLE_FLY_DRIFTING.isUnset() ? Component.translatable("controlify.gui.server_controlled").withStyle(ChatFormatting.GOLD) : Component.empty())
213213
.build())
214-
.binding(def.disableFlyDrifting, () -> ServerPolicies.DISABLE_FLY_DRIFTING.get().isAllowed() && config.disableFlyDrifting, v -> config.disableFlyDrifting = v)
214+
.binding(def.disableFlyDrifting, () -> ServerPolicies.DISABLE_FLY_DRIFTING.isUnset() ? config.disableFlyDrifting : ServerPolicies.DISABLE_FLY_DRIFTING.get(), v -> config.disableFlyDrifting = v)
215215
.controller(TickBoxControllerBuilder::create)
216-
.available(ServerPolicies.DISABLE_FLY_DRIFTING.get().isAllowed())
216+
.available(!ServerPolicies.DISABLE_FLY_DRIFTING.isUnset())
217217
.build())
218218
.build());
219219
}

src/main/java/dev/isxander/controlify/gui/screen/GlobalSettingsScreenFactory.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,16 @@ public static Screen createGlobalSettingsScreen(Screen parent) {
7474
.text(Component.translatable("controlify.gui.reach_around.tooltip"))
7575
.text(Component.translatable("controlify.gui.reach_around.tooltip.parity").withStyle(ChatFormatting.GRAY))
7676
.text(state == ReachAroundMode.EVERYWHERE ? Component.translatable("controlify.gui.reach_around.tooltip.warning").withStyle(ChatFormatting.RED) : Component.empty())
77-
.text(ServerPolicies.REACH_AROUND.get() != ServerPolicy.DISALLOWED ? Component.translatable("controlify.gui.server_controlled").withStyle(ChatFormatting.GOLD) : Component.empty())
77+
.text(ServerPolicies.REACH_AROUND.getPolicy() == ServerPolicy.DISALLOWED ? Component.translatable("controlify.gui.server_controlled").withStyle(ChatFormatting.GOLD) : Component.empty())
7878
.build())
7979
.binding(GlobalSettings.DEFAULT.reachAround, () -> globalSettings.reachAround, v -> globalSettings.reachAround = v)
8080
.controller(opt -> EnumControllerBuilder.create(opt)
8181
.enumClass(ReachAroundMode.class)
82-
.formatValue(mode -> switch (ServerPolicies.REACH_AROUND.get()) {
82+
.formatValue(mode -> switch (ServerPolicies.REACH_AROUND.getPolicy()) {
8383
case UNSET, ALLOWED -> mode.getDisplayName();
8484
case DISALLOWED -> CommonComponents.OPTION_OFF;
8585
}))
86-
.available(ServerPolicies.REACH_AROUND.get().isAllowed())
86+
.available(ServerPolicies.REACH_AROUND.get())
8787
.build())
8888
.option(Option.<Boolean>createBuilder()
8989
.name(Component.translatable("controlify.gui.allow_server_rumble"))
@@ -92,7 +92,7 @@ public static Screen createGlobalSettingsScreen(Screen parent) {
9292
.build())
9393
.binding(GlobalSettings.DEFAULT.allowServerRumble, () -> globalSettings.allowServerRumble, v -> globalSettings.allowServerRumble = v)
9494
.controller(TickBoxControllerBuilder::create)
95-
.listener((opt, val) -> {
95+
.addListener((opt, val) -> {
9696
ControlifyApi.get().getCurrentController()
9797
.flatMap(ControllerEntity::rumble)
9898
.ifPresent(rumble -> rumble.rumbleManager().clearEffects());

src/main/java/dev/isxander/controlify/ingame/InGameInputHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ public boolean shouldShowPlayerList() {
418418
}
419419

420420
public void preventFlyDrifting() {
421-
if (!controller.genericConfig().config().disableFlyDrifting || !ServerPolicies.DISABLE_FLY_DRIFTING.get().isAllowed()) {
421+
if (!controller.genericConfig().config().disableFlyDrifting || !ServerPolicies.DISABLE_FLY_DRIFTING.get()) {
422422
return;
423423
}
424424

src/main/java/dev/isxander/controlify/reacharound/ReachAroundHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static HitResult getReachAroundHitResult(Entity entity, HitResult hitResu
3131
}
3232

3333
private static boolean canReachAround(Entity cameraEntity) {
34-
boolean allowedToReachAround = switch (ServerPolicies.REACH_AROUND.get()) {
34+
boolean serverAllowed = switch (ServerPolicies.REACH_AROUND.getPolicy()) {
3535
// straight no, not allowed
3636
case DISALLOWED -> false;
3737
// if unset, respect the global setting
@@ -40,7 +40,7 @@ private static boolean canReachAround(Entity cameraEntity) {
4040
case ALLOWED -> Controlify.instance().config().globalSettings().reachAround != ReachAroundMode.OFF;
4141
};
4242

43-
return allowedToReachAround
43+
return serverAllowed
4444
// don't want to place blocks while riding an entity
4545
&& cameraEntity.getVehicle() == null
4646
// straight ahead = 0deg, up = -90deg, down = 90deg

src/main/java/dev/isxander/controlify/server/ControlifyServer.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import dev.isxander.controlify.platform.main.PlatformMainUtil;
55
import dev.isxander.controlify.server.packets.*;
66
import dev.isxander.controlify.utils.CUtil;
7+
import net.minecraft.server.level.ServerPlayer;
78

89
public class ControlifyServer {
910
private static ControlifyServer INSTANCE;
@@ -32,24 +33,24 @@ public void onInitializeServer() {
3233

3334
CUtil.LOGGER.log("Reach-around policy: {}", ControlifyServerConfig.HANDLER.instance().reachAroundPolicy);
3435
CUtil.LOGGER.log("No-fly drift policy: {}", ControlifyServerConfig.HANDLER.instance().noFlyDriftPolicy);
36+
CUtil.LOGGER.log("Enforce keyboard-like movement: {}", ControlifyServerConfig.HANDLER.instance().enforceKeyboardLikeMovement);
3537

38+
ControlifyServerConfig config = ControlifyServerConfig.HANDLER.instance();
3639
PlatformMainUtil.registerPlayerJoinedEvent(player -> {
37-
SidedNetworkApi.S2C().sendPacket(
38-
player,
39-
ServerPolicyPacket.CHANNEL,
40-
new ServerPolicyPacket(
41-
ServerPolicies.REACH_AROUND.getId(),
42-
ControlifyServerConfig.HANDLER.instance().reachAroundPolicy
43-
)
44-
);
45-
SidedNetworkApi.S2C().sendPacket(
46-
player,
47-
ServerPolicyPacket.CHANNEL,
48-
new ServerPolicyPacket(
49-
ServerPolicies.DISABLE_FLY_DRIFTING.getId(),
50-
ControlifyServerConfig.HANDLER.instance().noFlyDriftPolicy
51-
)
52-
);
40+
setServerPolicy(ServerPolicies.REACH_AROUND, player, config.reachAroundPolicy);
41+
setServerPolicy(ServerPolicies.DISABLE_FLY_DRIFTING, player, config.noFlyDriftPolicy);
42+
setServerPolicy(ServerPolicies.KEYBOARD_LIKE_MOVEMENT, player, config.enforceKeyboardLikeMovement);
5343
});
5444
}
45+
46+
private void setServerPolicy(ServerPolicies policy, ServerPlayer player, boolean option) {
47+
SidedNetworkApi.S2C().sendPacket(
48+
player,
49+
ServerPolicyPacket.CHANNEL,
50+
new ServerPolicyPacket(
51+
policy.getId(),
52+
option
53+
)
54+
);
55+
}
5556
}

src/main/java/dev/isxander/controlify/server/ControlifyServerConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ public class ControlifyServerConfig {
1616

1717
@SerialEntry public boolean reachAroundPolicy = true;
1818
@SerialEntry public boolean noFlyDriftPolicy = true;
19+
@SerialEntry public boolean enforceKeyboardLikeMovement = false;
1920
}

src/main/java/dev/isxander/controlify/server/ServerPolicies.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,40 @@
11
package dev.isxander.controlify.server;
22

3+
import java.util.Arrays;
34
import java.util.Map;
5+
import java.util.stream.Collectors;
46

57
public enum ServerPolicies {
6-
REACH_AROUND("reachAround"),
7-
DISABLE_FLY_DRIFTING("disableFlyDrifting");
8+
REACH_AROUND("reachAround", true),
9+
DISABLE_FLY_DRIFTING("disableFlyDrifting", false),
10+
KEYBOARD_LIKE_MOVEMENT("keyboardLikeMovement", false);
811

9-
private static final Map<String, ServerPolicies> BY_ID = Map.of(
10-
REACH_AROUND.getId(), REACH_AROUND,
11-
DISABLE_FLY_DRIFTING.getId(), DISABLE_FLY_DRIFTING
12-
);
12+
private static final Map<String, ServerPolicies> BY_ID = Arrays.stream(values())
13+
.collect(Collectors.toMap(ServerPolicies::getId, e -> e));
1314

1415
private final String id;
1516
private ServerPolicy value;
17+
private final boolean unsetValue;
1618

17-
ServerPolicies(String id) {
19+
ServerPolicies(String id, boolean unsetValue) {
1820
this.id = id;
1921
this.value = ServerPolicy.UNSET;
22+
this.unsetValue = unsetValue;
2023
}
2124

22-
public ServerPolicy get() {
25+
public boolean get() {
26+
return switch (value) {
27+
case ALLOWED -> true;
28+
case DISALLOWED -> false;
29+
case UNSET -> unsetValue;
30+
};
31+
}
32+
33+
public boolean isUnset() {
34+
return value == ServerPolicy.UNSET;
35+
}
36+
37+
public ServerPolicy getPolicy() {
2338
return value;
2439
}
2540

0 commit comments

Comments
 (0)