From a42f6e87f8c33a385bb47f1f9271bdb254a4c331 Mon Sep 17 00:00:00 2001 From: "yrsegal@gmail.com" Date: Sat, 2 Jul 2022 22:33:54 -0400 Subject: [PATCH] Close #161, #117 --- .../petrak/hexcasting/api/mod/HexConfig.java | 6 ++ .../client/ShiftScrollListener.java | 39 +++++++---- .../hexcasting/client/gui/GuiSpellcasting.kt | 32 ++++----- .../client/ktxt/ClientAccessorWrappers.kt | 9 +++ .../common/network/MsgShiftScrollSyn.java | 65 ++++++++++++------- .../accessor/client/AccessorMouseHandler.java | 14 ++++ Common/src/main/resources/hexplat.mixins.json | 16 ++++- .../fabric/FabricHexClientInitializer.kt | 7 +- .../hexcasting/fabric/FabricHexConfig.java | 23 ++++++- .../forge/ForgeHexClientInitializer.java | 3 +- .../hexcasting/forge/ForgeHexConfig.java | 18 +++++ 11 files changed, 177 insertions(+), 55 deletions(-) create mode 100644 Common/src/main/java/at/petrak/hexcasting/client/ktxt/ClientAccessorWrappers.kt create mode 100644 Common/src/main/java/at/petrak/hexcasting/mixin/accessor/client/AccessorMouseHandler.java diff --git a/Common/src/main/java/at/petrak/hexcasting/api/mod/HexConfig.java b/Common/src/main/java/at/petrak/hexcasting/api/mod/HexConfig.java index 6ff9e67b..f6350f95 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/mod/HexConfig.java +++ b/Common/src/main/java/at/petrak/hexcasting/api/mod/HexConfig.java @@ -32,8 +32,14 @@ public class HexConfig { boolean ctrlTogglesOffStrokeOrder(); + boolean invertSpellbookScrollDirection(); + + boolean invertAbacusScrollDirection(); + double DEFAULT_PATTERN_POINT_SPEED_MULTIPLIER = 1; boolean DEFAULT_CTRL_TOGGLES_OFF_STROKE_ORDER = false; + boolean DEFAULT_INVERT_SPELLBOOK_SCROLL = false; + boolean DEFAULT_INVERT_ABACUS_SCROLL = false; } public interface ServerConfigAccess { diff --git a/Common/src/main/java/at/petrak/hexcasting/client/ShiftScrollListener.java b/Common/src/main/java/at/petrak/hexcasting/client/ShiftScrollListener.java index 4dfe93cf..9b4c21c7 100644 --- a/Common/src/main/java/at/petrak/hexcasting/client/ShiftScrollListener.java +++ b/Common/src/main/java/at/petrak/hexcasting/client/ShiftScrollListener.java @@ -1,30 +1,36 @@ package at.petrak.hexcasting.client; +import at.petrak.hexcasting.api.mod.HexConfig; import at.petrak.hexcasting.common.lib.HexItems; import at.petrak.hexcasting.common.network.MsgShiftScrollSyn; import at.petrak.hexcasting.xplat.IClientXplatAbstractions; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.screens.Screen; import net.minecraft.client.player.LocalPlayer; -import net.minecraft.world.InteractionHand; import net.minecraft.world.item.Item; public class ShiftScrollListener { - public static boolean onScroll(double delta) { + private static double mainHandDelta = 0; + private static double offHandDelta = 0; + + public static boolean onScrollInGameplay(double delta) { + if (Minecraft.getInstance().screen != null) { + return false; + } + + return onScroll(delta, true); + } + + public static boolean onScroll(double delta, boolean needsSneaking) { LocalPlayer player = Minecraft.getInstance().player; // not .isCrouching! that fails for players who are not on the ground // yes, this does work if you remap your sneak key - if (player.isShiftKeyDown()) { - InteractionHand hand = null; + if (player != null && (player.isShiftKeyDown() || !needsSneaking)) { if (IsScrollableItem(player.getMainHandItem().getItem())) { - hand = InteractionHand.MAIN_HAND; + mainHandDelta += delta; + return true; } else if (IsScrollableItem(player.getOffhandItem().getItem())) { - hand = InteractionHand.OFF_HAND; - } - - if (hand != null) { - IClientXplatAbstractions.INSTANCE.sendPacketToServer( - new MsgShiftScrollSyn(hand, delta, Screen.hasControlDown())); + offHandDelta += delta; return true; } } @@ -32,6 +38,17 @@ public class ShiftScrollListener { return false; } + public static void clientTickEnd() { + if (mainHandDelta != 0 || offHandDelta != 0) { + IClientXplatAbstractions.INSTANCE.sendPacketToServer( + new MsgShiftScrollSyn(mainHandDelta, offHandDelta, Screen.hasControlDown(), + HexConfig.client().invertSpellbookScrollDirection(), + HexConfig.client().invertAbacusScrollDirection())); + mainHandDelta = 0; + offHandDelta = 0; + } + } + private static boolean IsScrollableItem(Item item) { return item == HexItems.SPELLBOOK || item == HexItems.ABACUS; } diff --git a/Common/src/main/java/at/petrak/hexcasting/client/gui/GuiSpellcasting.kt b/Common/src/main/java/at/petrak/hexcasting/client/gui/GuiSpellcasting.kt index bce7fc6f..4b4b3efe 100644 --- a/Common/src/main/java/at/petrak/hexcasting/client/gui/GuiSpellcasting.kt +++ b/Common/src/main/java/at/petrak/hexcasting/client/gui/GuiSpellcasting.kt @@ -11,17 +11,13 @@ import at.petrak.hexcasting.api.spell.math.HexPattern import at.petrak.hexcasting.api.utils.asTranslatedComponent import at.petrak.hexcasting.api.utils.gold import at.petrak.hexcasting.api.utils.otherHand -import at.petrak.hexcasting.client.ClientTickCounter -import at.petrak.hexcasting.client.drawPatternFromPoints -import at.petrak.hexcasting.client.drawSpot -import at.petrak.hexcasting.client.renderQuad +import at.petrak.hexcasting.client.* +import at.petrak.hexcasting.client.ktxt.accumulatedScroll import at.petrak.hexcasting.client.sound.GridSoundInstance -import at.petrak.hexcasting.common.items.ItemSpellbook import at.petrak.hexcasting.common.lib.HexIotaTypes import at.petrak.hexcasting.common.lib.HexItems import at.petrak.hexcasting.common.lib.HexSounds import at.petrak.hexcasting.common.network.MsgNewSpellPatternSyn -import at.petrak.hexcasting.common.network.MsgShiftScrollSyn import at.petrak.hexcasting.xplat.IClientXplatAbstractions import com.mojang.blaze3d.systems.RenderSystem import com.mojang.blaze3d.vertex.PoseStack @@ -273,15 +269,21 @@ class GuiSpellcasting constructor( override fun mouseScrolled(pMouseX: Double, pMouseY: Double, pDelta: Double): Boolean { super.mouseScrolled(pMouseX, pMouseY, pDelta) - val otherHand = otherHand(this.handOpenedWith) - if (Minecraft.getInstance().player!!.getItemInHand(otherHand).item is ItemSpellbook) - IClientXplatAbstractions.INSTANCE.sendPacketToServer( - MsgShiftScrollSyn( - otherHand, - pDelta, - hasControlDown() - ) - ) + val mouseHandler = Minecraft.getInstance().mouseHandler + + if (mouseHandler.accumulatedScroll != 0.0 && sign(pDelta) != sign(mouseHandler.accumulatedScroll)) { + mouseHandler.accumulatedScroll = 0.0 + } + + mouseHandler.accumulatedScroll += pDelta + val accumulation: Int = mouseHandler.accumulatedScroll.toInt() + if (accumulation == 0) { + return true + } + + mouseHandler.accumulatedScroll -= accumulation.toDouble() + + ShiftScrollListener.onScroll(pDelta, false) return true } diff --git a/Common/src/main/java/at/petrak/hexcasting/client/ktxt/ClientAccessorWrappers.kt b/Common/src/main/java/at/petrak/hexcasting/client/ktxt/ClientAccessorWrappers.kt new file mode 100644 index 00000000..5863d7f0 --- /dev/null +++ b/Common/src/main/java/at/petrak/hexcasting/client/ktxt/ClientAccessorWrappers.kt @@ -0,0 +1,9 @@ +@file:JvmName("ClientAccessorWrappers") +package at.petrak.hexcasting.client.ktxt + +import at.petrak.hexcasting.mixin.accessor.client.AccessorMouseHandler +import net.minecraft.client.MouseHandler + +var MouseHandler.accumulatedScroll: Double + get() = (this as AccessorMouseHandler).`hex$getAccumulatedScroll`() + set(value) = (this as AccessorMouseHandler).`hex$setAccumulatedScroll`(value) diff --git a/Common/src/main/java/at/petrak/hexcasting/common/network/MsgShiftScrollSyn.java b/Common/src/main/java/at/petrak/hexcasting/common/network/MsgShiftScrollSyn.java index 3f1d9206..76d24275 100644 --- a/Common/src/main/java/at/petrak/hexcasting/common/network/MsgShiftScrollSyn.java +++ b/Common/src/main/java/at/petrak/hexcasting/common/network/MsgShiftScrollSyn.java @@ -25,7 +25,7 @@ import static at.petrak.hexcasting.api.HexAPI.modLoc; * Sent client->server when the client shift+scrolls with a shift-scrollable item * or scrolls in the spellcasting UI. */ -public record MsgShiftScrollSyn(InteractionHand hand, double scrollDelta, boolean isCtrl) implements IMessage { +public record MsgShiftScrollSyn(double mainHandDelta, double offHandDelta, boolean isCtrl, boolean invertSpellbook, boolean invertAbacus) implements IMessage { public static final ResourceLocation ID = modLoc("scroll"); @Override @@ -35,32 +35,47 @@ public record MsgShiftScrollSyn(InteractionHand hand, double scrollDelta, boolea public static MsgShiftScrollSyn deserialize(ByteBuf buffer) { var buf = new FriendlyByteBuf(buffer); - var hand = buf.readEnum(InteractionHand.class); - var scrollDelta = buf.readDouble(); + var mainHandDelta = buf.readDouble(); + var offHandDelta = buf.readDouble(); var isCtrl = buf.readBoolean(); - return new MsgShiftScrollSyn(hand, scrollDelta, isCtrl); + var invertSpellbook = buf.readBoolean(); + var invertAbacus = buf.readBoolean(); + return new MsgShiftScrollSyn(mainHandDelta, offHandDelta, isCtrl, invertSpellbook, invertAbacus); } public void serialize(FriendlyByteBuf buf) { - buf.writeEnum(this.hand); - buf.writeDouble(this.scrollDelta); + buf.writeDouble(this.mainHandDelta); + buf.writeDouble(this.offHandDelta); buf.writeBoolean(this.isCtrl); + buf.writeBoolean(this.invertSpellbook); + buf.writeBoolean(this.invertAbacus); } public void handle(MinecraftServer server, ServerPlayer sender) { server.execute(() -> { - var stack = sender.getItemInHand(hand); - - if (stack.getItem() == HexItems.SPELLBOOK) { - spellbook(sender, stack); - } else if (stack.getItem() == HexItems.ABACUS) { - abacus(sender, stack); - } + handleForHand(sender, InteractionHand.MAIN_HAND, mainHandDelta); + handleForHand(sender, InteractionHand.OFF_HAND, offHandDelta); }); } - private void spellbook(ServerPlayer sender, ItemStack stack) { - var newIdx = ItemSpellbook.rotatePageIdx(stack, this.scrollDelta < 0.0); + private void handleForHand(ServerPlayer sender, InteractionHand hand, double delta) { + if (delta != 0) { + var stack = sender.getItemInHand(hand); + + if (stack.getItem() == HexItems.SPELLBOOK) { + spellbook(sender, hand, stack, delta); + } else if (stack.getItem() == HexItems.ABACUS) { + abacus(sender, hand, stack, delta); + } + } + } + + private void spellbook(ServerPlayer sender, InteractionHand hand, ItemStack stack, double delta) { + if (invertSpellbook) { + delta = -delta; + } + + var newIdx = ItemSpellbook.rotatePageIdx(stack, delta < 0.0); var len = ItemSpellbook.highestPage(stack); @@ -99,21 +114,27 @@ public record MsgShiftScrollSyn(InteractionHand hand, double scrollDelta, boolea sender.displayClientMessage(component.withStyle(ChatFormatting.GRAY), true); } - private void abacus(ServerPlayer sender, ItemStack stack) { - var increase = this.scrollDelta < 0; + private void abacus(ServerPlayer sender, InteractionHand hand, ItemStack stack, double delta) { + if (invertAbacus) { + delta = -delta; + } + + var increase = delta < 0; double num = NBTHelper.getDouble(stack, ItemAbacus.TAG_VALUE); - double delta; + double shiftDelta; float pitch; - if (this.hand == InteractionHand.MAIN_HAND) { - delta = this.isCtrl ? 10 : 1; + if (hand == InteractionHand.MAIN_HAND) { + shiftDelta = this.isCtrl ? 10 : 1; pitch = this.isCtrl ? 0.7f : 0.9f; } else { - delta = this.isCtrl ? 0.01 : 0.1; + shiftDelta = this.isCtrl ? 0.01 : 0.1; pitch = this.isCtrl ? 1.3f : 1.0f; } - num += delta * (increase ? 1 : -1); + int scale = Math.max((int) Math.floor(Math.abs(delta)), 1); + + num += scale * shiftDelta * (increase ? 1 : -1); NBTHelper.putDouble(stack, ItemAbacus.TAG_VALUE, num); pitch *= (increase ? 1.05f : 0.95f); diff --git a/Common/src/main/java/at/petrak/hexcasting/mixin/accessor/client/AccessorMouseHandler.java b/Common/src/main/java/at/petrak/hexcasting/mixin/accessor/client/AccessorMouseHandler.java new file mode 100644 index 00000000..d663d132 --- /dev/null +++ b/Common/src/main/java/at/petrak/hexcasting/mixin/accessor/client/AccessorMouseHandler.java @@ -0,0 +1,14 @@ +package at.petrak.hexcasting.mixin.accessor.client; + +import net.minecraft.client.MouseHandler; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; + +@Mixin(MouseHandler.class) +public interface AccessorMouseHandler { + @Accessor("accumulatedScroll") + double hex$getAccumulatedScroll(); + + @Accessor("accumulatedScroll") + void hex$setAccumulatedScroll(double scroll); +} diff --git a/Common/src/main/resources/hexplat.mixins.json b/Common/src/main/resources/hexplat.mixins.json index a7d1e366..d0cc4b1c 100644 --- a/Common/src/main/resources/hexplat.mixins.json +++ b/Common/src/main/resources/hexplat.mixins.json @@ -5,13 +5,23 @@ "refmap": "hexcasting.mixins.refmap.json", "package": "at.petrak.hexcasting.mixin", "mixins": [ - "MixinAbstractVillager", "MixinMob", "MixinRaider", "MixinReloadableServerResources", "MixinVillager", "MixinWitch", - "accessor.AccessorLivingEntity", "accessor.AccessorLootTable", "accessor.AccessorPoiType", - "accessor.AccessorUseOnContext", "accessor.AccessorVillager", "accessor.CriteriaTriggersAccessor" + "MixinAbstractVillager", + "MixinMob", + "MixinRaider", + "MixinReloadableServerResources", + "MixinVillager", + "MixinWitch", + "accessor.AccessorLivingEntity", + "accessor.AccessorLootTable", + "accessor.AccessorPoiType", + "accessor.AccessorUseOnContext", + "accessor.AccessorVillager", + "accessor.CriteriaTriggersAccessor" ], "client": [ "accessor.client.AccessorCompositeRenderType", "accessor.client.AccessorEmptyTextureStateShard", + "accessor.client.AccessorMouseHandler", "accessor.client.AccessorRenderStateShard", "accessor.client.AccessorRenderType", "client.MixinClientLevel" diff --git a/Fabric/src/main/java/at/petrak/hexcasting/fabric/FabricHexClientInitializer.kt b/Fabric/src/main/java/at/petrak/hexcasting/fabric/FabricHexClientInitializer.kt index 8804f134..c7d3cc0c 100644 --- a/Fabric/src/main/java/at/petrak/hexcasting/fabric/FabricHexClientInitializer.kt +++ b/Fabric/src/main/java/at/petrak/hexcasting/fabric/FabricHexClientInitializer.kt @@ -32,9 +32,12 @@ object FabricHexClientInitializer : ClientModInitializer { } HudRenderCallback.EVENT.register(HexAdditionalRenderers::overlayGui) WorldRenderEvents.START.register { ClientTickCounter.renderTickStart(it.tickDelta()) } - ClientTickEvents.END_CLIENT_TICK.register { ClientTickCounter.clientTickEnd() } + ClientTickEvents.END_CLIENT_TICK.register { + ClientTickCounter.clientTickEnd() + ShiftScrollListener.clientTickEnd() + } - MouseScrollCallback.EVENT.register(ShiftScrollListener::onScroll) + MouseScrollCallback.EVENT.register(ShiftScrollListener::onScrollInGameplay) RegisterClientStuff.init() RegisterClientStuff.registerParticles() diff --git a/Fabric/src/main/java/at/petrak/hexcasting/fabric/FabricHexConfig.java b/Fabric/src/main/java/at/petrak/hexcasting/fabric/FabricHexConfig.java index 4775ad2f..e61c5324 100644 --- a/Fabric/src/main/java/at/petrak/hexcasting/fabric/FabricHexConfig.java +++ b/Fabric/src/main/java/at/petrak/hexcasting/fabric/FabricHexConfig.java @@ -128,6 +128,8 @@ public class FabricHexConfig { private final PropertyMirror patternPointSpeedMultiplier = PropertyMirror.create( ConfigTypes.DOUBLE.withMinimum(0d)); private final PropertyMirror ctrlTogglesOffStrokeOrder = PropertyMirror.create(ConfigTypes.BOOLEAN); + private final PropertyMirror invertSpellbookScrollDirection = PropertyMirror.create(ConfigTypes.BOOLEAN); + private final PropertyMirror invertAbacusScrollDirection = PropertyMirror.create(ConfigTypes.BOOLEAN); public ConfigTree configure(ConfigTreeBuilder bob) { bob @@ -137,7 +139,16 @@ public class FabricHexConfig { .beginValue("ctrlTogglesOffStrokeOrder", ConfigTypes.BOOLEAN, DEFAULT_CTRL_TOGGLES_OFF_STROKE_ORDER) .withComment("Whether the ctrl key will instead turn *off* the color gradient on patterns") - .finishValue(ctrlTogglesOffStrokeOrder::mirror); + .finishValue(ctrlTogglesOffStrokeOrder::mirror) + + .beginValue("invertSpellbookScrollDirection", ConfigTypes.BOOLEAN, DEFAULT_INVERT_SPELLBOOK_SCROLL) + .withComment("Whether scrolling up (as opposed to down) will increase the page index of the spellbook, and vice versa") + .finishValue(invertSpellbookScrollDirection::mirror) + + .beginValue("invertAbacusScrollDirection", ConfigTypes.BOOLEAN, DEFAULT_INVERT_ABACUS_SCROLL) + .withComment("Whether scrolling up (as opposed to down) will increase the value of the abacus, and vice versa") + .finishValue(invertAbacusScrollDirection::mirror); + return bob.build(); } @@ -151,6 +162,16 @@ public class FabricHexConfig { public boolean ctrlTogglesOffStrokeOrder() { return ctrlTogglesOffStrokeOrder.getValue(); } + + @Override + public boolean invertSpellbookScrollDirection() { + return invertSpellbookScrollDirection.getValue(); + } + + @Override + public boolean invertAbacusScrollDirection() { + return invertAbacusScrollDirection.getValue(); + } } private static final class Server implements HexConfig.ServerConfigAccess { diff --git a/Forge/src/main/java/at/petrak/hexcasting/forge/ForgeHexClientInitializer.java b/Forge/src/main/java/at/petrak/hexcasting/forge/ForgeHexClientInitializer.java index fb66672f..582a8c62 100644 --- a/Forge/src/main/java/at/petrak/hexcasting/forge/ForgeHexClientInitializer.java +++ b/Forge/src/main/java/at/petrak/hexcasting/forge/ForgeHexClientInitializer.java @@ -53,11 +53,12 @@ public class ForgeHexClientInitializer { evBus.addListener((TickEvent.ClientTickEvent e) -> { if (e.phase == TickEvent.Phase.END) { ClientTickCounter.clientTickEnd(); + ShiftScrollListener.clientTickEnd(); } }); evBus.addListener((InputEvent.MouseScrollEvent e) -> { - var cancel = ShiftScrollListener.onScroll(e.getScrollDelta()); + var cancel = ShiftScrollListener.onScrollInGameplay(e.getScrollDelta()); e.setCanceled(cancel); }); } diff --git a/Forge/src/main/java/at/petrak/hexcasting/forge/ForgeHexConfig.java b/Forge/src/main/java/at/petrak/hexcasting/forge/ForgeHexConfig.java index 646cecd4..52cc6493 100644 --- a/Forge/src/main/java/at/petrak/hexcasting/forge/ForgeHexConfig.java +++ b/Forge/src/main/java/at/petrak/hexcasting/forge/ForgeHexConfig.java @@ -52,6 +52,8 @@ public class ForgeHexConfig implements HexConfig.CommonConfigAccess { public static class Client implements HexConfig.ClientConfigAccess { private static ForgeConfigSpec.DoubleValue patternPointSpeedMultiplier; private static ForgeConfigSpec.BooleanValue ctrlTogglesOffStrokeOrder; + private static ForgeConfigSpec.BooleanValue invertSpellbookScrollDirection; + private static ForgeConfigSpec.BooleanValue invertAbacusScrollDirection; public Client(ForgeConfigSpec.Builder builder) { patternPointSpeedMultiplier = builder.comment( @@ -61,6 +63,12 @@ public class ForgeHexConfig implements HexConfig.CommonConfigAccess { ctrlTogglesOffStrokeOrder = builder.comment( "Whether the ctrl key will instead turn *off* the color gradient on patterns") .define("ctrlTogglesOffStrokeOrder", DEFAULT_CTRL_TOGGLES_OFF_STROKE_ORDER); + invertSpellbookScrollDirection = builder.comment( + "Whether scrolling up (as opposed to down) will increase the page index of the spellbook, and vice versa") + .define("invertSpellbookScrollDirection", DEFAULT_INVERT_SPELLBOOK_SCROLL); + invertAbacusScrollDirection = builder.comment( + "Whether scrolling up (as opposed to down) will increase the value of the abacus, and vice versa") + .define("invertAbacusScrollDirection", DEFAULT_INVERT_ABACUS_SCROLL); } @Override @@ -72,6 +80,16 @@ public class ForgeHexConfig implements HexConfig.CommonConfigAccess { public boolean ctrlTogglesOffStrokeOrder() { return ctrlTogglesOffStrokeOrder.get(); } + + @Override + public boolean invertSpellbookScrollDirection() { + return invertSpellbookScrollDirection.get(); + } + + @Override + public boolean invertAbacusScrollDirection() { + return invertAbacusScrollDirection.get(); + } } public static class Server implements HexConfig.ServerConfigAccess {