From 44ab103b650ac4acc4c255f4debd61e3b5858026 Mon Sep 17 00:00:00 2001 From: isXander Date: Tue, 8 Aug 2023 11:01:45 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20reach-around=20policy=20no?= =?UTF-8?q?t=20being=20loaded=20from=20server=20config=20=E2=9C=8F?= =?UTF-8?q?=EF=B8=8F=20Force-enable=20reach-around=20on=20the=20client=20i?= =?UTF-8?q?f=20the=20server=20specifically=20allows=20it,=20regardless=20o?= =?UTF-8?q?f=20option=20in=20settings?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dev/isxander/controlify/Controlify.java | 6 +++-- .../screen/GlobalSettingsScreenFactory.java | 16 +++++++++---- .../reacharound/ReachAroundHandler.java | 8 +++---- .../reacharound/ReachAroundPolicy.java | 23 +++++++++++++++++++ .../server/ControlifyServerConfig.java | 3 ++- .../assets/controlify/lang/en_us.json | 4 ++-- 6 files changed, 46 insertions(+), 14 deletions(-) create mode 100644 src/main/java/dev/isxander/controlify/reacharound/ReachAroundPolicy.java diff --git a/src/main/java/dev/isxander/controlify/Controlify.java b/src/main/java/dev/isxander/controlify/Controlify.java index 5ac80e0..6ee04fa 100644 --- a/src/main/java/dev/isxander/controlify/Controlify.java +++ b/src/main/java/dev/isxander/controlify/Controlify.java @@ -15,6 +15,7 @@ import dev.isxander.controlify.gui.screen.SubmitUnknownControllerScreen; import dev.isxander.controlify.ingame.ControllerPlayerMovement; import dev.isxander.controlify.reacharound.ReachAroundHandler; import dev.isxander.controlify.reacharound.ReachAroundMode; +import dev.isxander.controlify.reacharound.ReachAroundPolicy; import dev.isxander.controlify.screenop.ScreenProcessorProvider; import dev.isxander.controlify.config.ControlifyConfig; import dev.isxander.controlify.hid.ControllerHIDService; @@ -248,7 +249,7 @@ public class Controlify implements ControlifyApi { }); ClientPlayNetworking.registerGlobalReceiver(ReachAroundPolicyPacket.TYPE, (packet, player, sender) -> { Log.LOGGER.info("Connected server specified reach around policy is {}.", packet.allowed() ? "ALLOWED" : "DISALLOWED"); - ReachAroundHandler.reachAroundPolicy = packet.allowed(); + ReachAroundHandler.reachAroundPolicy = ReachAroundPolicy.fromServer(packet.allowed()); if (config().globalSettings().reachAround == ReachAroundMode.EVERYWHERE && !packet.allowed()) { ToastUtils.sendToast( @@ -260,7 +261,7 @@ public class Controlify implements ControlifyApi { }); ClientPlayConnectionEvents.DISCONNECT.register((handler, client) -> { DebugLog.log("Disconnected from server, resetting reach around policy"); - ReachAroundHandler.reachAroundPolicy = true; + ReachAroundHandler.reachAroundPolicy = ReachAroundPolicy.UNSET; }); FabricLoader.getInstance().getEntrypoints("controlify", ControlifyEntrypoint.class).forEach(entrypoint -> { @@ -602,6 +603,7 @@ public class Controlify implements ControlifyApi { } if (foundVersion != null) { + Log.LOGGER.info("Sending new features toast for {}", foundVersion); ToastUtils.sendToast( Component.translatable("controlify.new_features.title", foundVersion), Component.translatable("controlify.new_features." + foundVersion), diff --git a/src/main/java/dev/isxander/controlify/gui/screen/GlobalSettingsScreenFactory.java b/src/main/java/dev/isxander/controlify/gui/screen/GlobalSettingsScreenFactory.java index 43011e8..5f65383 100644 --- a/src/main/java/dev/isxander/controlify/gui/screen/GlobalSettingsScreenFactory.java +++ b/src/main/java/dev/isxander/controlify/gui/screen/GlobalSettingsScreenFactory.java @@ -5,6 +5,7 @@ import dev.isxander.controlify.api.ControlifyApi; import dev.isxander.controlify.config.GlobalSettings; import dev.isxander.controlify.reacharound.ReachAroundHandler; import dev.isxander.controlify.reacharound.ReachAroundMode; +import dev.isxander.controlify.reacharound.ReachAroundPolicy; import dev.isxander.yacl3.api.*; import dev.isxander.yacl3.api.controller.BooleanControllerBuilder; import dev.isxander.yacl3.api.controller.EnumControllerBuilder; @@ -13,6 +14,7 @@ import dev.isxander.yacl3.api.controller.TickBoxControllerBuilder; import net.minecraft.ChatFormatting; import net.minecraft.Util; import net.minecraft.client.gui.screens.Screen; +import net.minecraft.network.chat.CommonComponents; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; @@ -40,11 +42,17 @@ public class GlobalSettingsScreenFactory { .text(Component.translatable("controlify.gui.reach_around.tooltip")) .text(Component.translatable("controlify.gui.reach_around.tooltip.parity").withStyle(ChatFormatting.GRAY)) .text(state == ReachAroundMode.EVERYWHERE ? Component.translatable("controlify.gui.reach_around.tooltip.warning").withStyle(ChatFormatting.RED) : Component.empty()) - .text(!ReachAroundHandler.reachAroundPolicy ? Component.translatable("controlify.gui.reach_around.tooltip.server_disabled").withStyle(ChatFormatting.GOLD) : Component.empty()) + .text(ReachAroundHandler.reachAroundPolicy != ReachAroundPolicy.UNSET ? Component.translatable("controlify.gui.reach_around.tooltip.server_controlled").withStyle(ChatFormatting.GOLD) : Component.empty()) .build()) - .binding(GlobalSettings.DEFAULT.reachAround, () -> ReachAroundHandler.reachAroundPolicy ? globalSettings.reachAround : ReachAroundMode.OFF, v -> globalSettings.reachAround = v) - .controller(opt -> EnumControllerBuilder.create(opt).enumClass(ReachAroundMode.class)) - .available(ReachAroundHandler.reachAroundPolicy) + .binding(GlobalSettings.DEFAULT.reachAround, () -> globalSettings.reachAround, v -> globalSettings.reachAround = v) + .controller(opt -> EnumControllerBuilder.create(opt) + .enumClass(ReachAroundMode.class) + .valueFormatter(mode -> switch (ReachAroundHandler.reachAroundPolicy) { + case UNSET -> mode.getDisplayName(); + case ALLOWED -> CommonComponents.OPTION_ON; + case DISALLOWED -> CommonComponents.OPTION_OFF; + })) + .available(ReachAroundHandler.reachAroundPolicy == ReachAroundPolicy.UNSET) .build()) .option(Option.createBuilder() .name(Component.translatable("controlify.gui.ui_sounds")) diff --git a/src/main/java/dev/isxander/controlify/reacharound/ReachAroundHandler.java b/src/main/java/dev/isxander/controlify/reacharound/ReachAroundHandler.java index ef545b1..7ff5564 100644 --- a/src/main/java/dev/isxander/controlify/reacharound/ReachAroundHandler.java +++ b/src/main/java/dev/isxander/controlify/reacharound/ReachAroundHandler.java @@ -8,7 +8,7 @@ import net.minecraft.world.phys.BlockHitResult; import net.minecraft.world.phys.HitResult; public class ReachAroundHandler { - public static boolean reachAroundPolicy = true; + public static ReachAroundPolicy reachAroundPolicy = ReachAroundPolicy.UNSET; public static HitResult getReachAroundHitResult(Entity entity, HitResult hitResult) { // if there is already a valid hit, we don't want to override it @@ -34,7 +34,7 @@ public class ReachAroundHandler { } private static boolean canReachAround(Entity cameraEntity) { - return reachAroundPolicy + return reachAroundPolicy.canReachAround(Controlify.instance().config().globalSettings().reachAround) // don't want to place blocks while riding an entity && cameraEntity.getVehicle() == null // straight ahead = 0deg, up = -90deg, down = 90deg @@ -42,8 +42,6 @@ public class ReachAroundHandler { && cameraEntity.getXRot() >= 45 // if the player is not standing on a block, this is inappropriate // this also prevents selecting fluids as a valid position - && cameraEntity.onGround() - // must respect config option - && Controlify.instance().config().globalSettings().reachAround.canReachAround(); + && cameraEntity.onGround(); } } diff --git a/src/main/java/dev/isxander/controlify/reacharound/ReachAroundPolicy.java b/src/main/java/dev/isxander/controlify/reacharound/ReachAroundPolicy.java new file mode 100644 index 0000000..999ea13 --- /dev/null +++ b/src/main/java/dev/isxander/controlify/reacharound/ReachAroundPolicy.java @@ -0,0 +1,23 @@ +package dev.isxander.controlify.reacharound; + +import java.util.function.Function; + +public enum ReachAroundPolicy { + ALLOWED(mode -> true), + DISALLOWED(mode -> false), + UNSET(ReachAroundMode::canReachAround); + + private final Function canReachAround; + + ReachAroundPolicy(Function canReachAround) { + this.canReachAround = canReachAround; + } + + public boolean canReachAround(ReachAroundMode mode) { + return canReachAround.apply(mode); + } + + public static ReachAroundPolicy fromServer(boolean allowed) { + return allowed ? ALLOWED : DISALLOWED; + } +} diff --git a/src/main/java/dev/isxander/controlify/server/ControlifyServerConfig.java b/src/main/java/dev/isxander/controlify/server/ControlifyServerConfig.java index 71fbecc..31eb048 100644 --- a/src/main/java/dev/isxander/controlify/server/ControlifyServerConfig.java +++ b/src/main/java/dev/isxander/controlify/server/ControlifyServerConfig.java @@ -1,5 +1,6 @@ package dev.isxander.controlify.server; +import dev.isxander.yacl3.config.ConfigEntry; import dev.isxander.yacl3.config.ConfigInstance; import dev.isxander.yacl3.config.GsonConfigInstance; import net.fabricmc.loader.api.FabricLoader; @@ -9,5 +10,5 @@ public class ControlifyServerConfig { .setPath(FabricLoader.getInstance().getConfigDir().resolve("controlify.json")) .build(); - public boolean reachAroundPolicy = false; + @ConfigEntry public boolean reachAroundPolicy = false; } diff --git a/src/main/resources/assets/controlify/lang/en_us.json b/src/main/resources/assets/controlify/lang/en_us.json index f9d78e7..bf62b0c 100644 --- a/src/main/resources/assets/controlify/lang/en_us.json +++ b/src/main/resources/assets/controlify/lang/en_us.json @@ -14,8 +14,8 @@ "controlify.gui.reach_around.tooltip": "If enabled, you can interact with the block you are standing on in the direction you are looking.", "controlify.gui.reach_around.tooltip.parity": "This is parity with bedrock edition where you can also do this.", "controlify.gui.reach_around.tooltip.warning": "WARNING: This is an unfair advantage over other players without Controlify, and you will likely be flagged by many anti-cheats. This should only be used in situations where everyone playing recognises that you have this ability and are okay with it.", - "controlify.gui.reach_around.tooltip.server_disabled": "The server you are playing on has does not allow you to use this feature.", - "controlify.reach_around.off": "Off", + "controlify.gui.reach_around.tooltip.server_controlled": "The server you are playing on is controlling this setting.", + "controlify.reach_around.off": "OFF", "controlify.reach_around.singleplayer_only": "Singleplayer Only", "controlify.reach_around.singleplayer_and_lan": "Singleplayer and LAN", "controlify.reach_around.everywhere": "Everywhere",