Jelajahi Sumber

Fix mixins and remove FAPI dep. Close #102

Jared 3 tahun lalu
induk
melakukan
1f2242aedc

+ 2 - 1
Common/src/main/java/com/blamejared/controlling/platform/IEventHelper.java

@@ -9,6 +9,7 @@ import com.blamejared.controlling.client.NewKeyBindsList;
 import com.mojang.blaze3d.vertex.PoseStack;
 import com.mojang.datafixers.util.Either;
 import net.minecraft.client.gui.components.events.GuiEventListener;
+import net.minecraft.util.Unit;
 
 import java.util.List;
 
@@ -20,5 +21,5 @@ public interface IEventHelper {
     
     Either<IKeyEntryMouseReleasedEvent, Boolean> fireKeyEntryMouseReleasedEvent(NewKeyBindsList.KeyEntry entry, double mouseX, double mouseY, int buttonId);
     
-    Either<IKeyEntryRenderEvent, Void> fireKeyEntryRenderEvent(NewKeyBindsList.KeyEntry entry, PoseStack stack, int slotIndex, int y, int x, int rowLeft, int rowWidth, int mouseX, int mouseY, boolean hovered, float partialTicks);
+    Either<IKeyEntryRenderEvent, Unit> fireKeyEntryRenderEvent(NewKeyBindsList.KeyEntry entry, PoseStack stack, int slotIndex, int y, int x, int rowLeft, int rowWidth, int mouseX, int mouseY, boolean hovered, float partialTicks);
 }

+ 3 - 2
Fabric/src/main/java/com/blamejared/controlling/api/event/ControllingEvents.java

@@ -3,6 +3,7 @@ package com.blamejared.controlling.api.event;
 import net.fabricmc.fabric.api.event.Event;
 import net.fabricmc.fabric.api.event.EventFactory;
 import net.minecraft.client.gui.components.events.GuiEventListener;
+import net.minecraft.util.Unit;
 
 import java.util.Arrays;
 import java.util.List;
@@ -12,9 +13,9 @@ public class ControllingEvents {
     public static final Event<IEventHandler<KeyEntryListenersEvent, List<GuiEventListener>>> KEY_ENTRY_LISTENERS_EVENT = EventFactory.createArrayBacked(IEventHandler.class, listeners -> event -> Arrays.stream(listeners).flatMap(handler -> handler.handle(event).stream()).toList());
     public static final Event<IEventHandler<KeyEntryMouseClickedEvent, Boolean>> KEY_ENTRY_MOUSE_CLICKED_EVENT = EventFactory.createArrayBacked(IEventHandler.class, listeners -> event -> Arrays.stream(listeners).anyMatch(handler -> handler.handle(event)));
     public static final Event<IEventHandler<KeyEntryMouseReleasedEvent, Boolean>> KEY_ENTRY_MOUSE_RELEASED_EVENT = EventFactory.createArrayBacked(IEventHandler.class, listeners -> event -> Arrays.stream(listeners).anyMatch(handler -> handler.handle(event)));
-    public static final Event<IEventHandler<KeyEntryRenderEvent, Void>> KEY_ENTRY_RENDER_EVENT = EventFactory.createArrayBacked(IEventHandler.class, listeners -> event -> {
+    public static final Event<IEventHandler<KeyEntryRenderEvent, Unit>> KEY_ENTRY_RENDER_EVENT = EventFactory.createArrayBacked(IEventHandler.class, listeners -> event -> {
         Arrays.stream(listeners).forEach(handler -> handler.handle(event));
-        return null;
+        return Unit.INSTANCE;
     });
     
 }

+ 0 - 7
Fabric/src/main/java/com/blamejared/controlling/mixin/OpenGuiMixin.java

@@ -7,9 +7,7 @@ import net.minecraft.client.gui.screens.controls.KeyBindsScreen;
 import org.spongepowered.asm.mixin.Mixin;
 import org.spongepowered.asm.mixin.Shadow;
 import org.spongepowered.asm.mixin.injection.At;
-import org.spongepowered.asm.mixin.injection.Inject;
 import org.spongepowered.asm.mixin.injection.ModifyVariable;
-import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
 
 @Mixin(Minecraft.class)
 public class OpenGuiMixin {
@@ -17,11 +15,6 @@ public class OpenGuiMixin {
     @Shadow
     public Screen screen;
     
-    @Inject(method = "setScreen", at = @At("HEAD"))
-    private void dummyGenerateRefmap(Screen screen, CallbackInfo ci) {
-        // NO-OP this injection is only here to generate the refmap
-    }
-    
     @ModifyVariable(method = "setScreen", at = @At("HEAD"), argsOnly = true)
     private Screen upgradeControlScreen(Screen opened) {
         // Swap the control options screen with our own instance whenever something tries to open one

+ 26 - 5
Fabric/src/main/java/com/blamejared/controlling/platform/FabricEventHandler.java

@@ -12,7 +12,9 @@ import com.blamejared.controlling.api.events.IKeyEntryRenderEvent;
 import com.blamejared.controlling.client.NewKeyBindsList;
 import com.mojang.blaze3d.vertex.PoseStack;
 import com.mojang.datafixers.util.Either;
+import net.fabricmc.loader.api.FabricLoader;
 import net.minecraft.client.gui.components.events.GuiEventListener;
+import net.minecraft.util.Unit;
 
 import java.util.List;
 
@@ -20,21 +22,40 @@ public class FabricEventHandler implements IEventHelper {
     
     @Override
     public Either<IKeyEntryListenersEvent, List<GuiEventListener>> fireKeyEntryListenersEvent(NewKeyBindsList.KeyEntry entry) {
-        return Either.right(ControllingEvents.KEY_ENTRY_LISTENERS_EVENT.invoker().handle(new KeyEntryListenersEvent(entry)));
+        KeyEntryListenersEvent event = new KeyEntryListenersEvent(entry);
+        if(FabricLoader.getInstance().isModLoaded("fabric")) {
+            return Either.right(ControllingEvents.KEY_ENTRY_LISTENERS_EVENT.invoker().handle(event));
+        }
+    
+        return Either.right(event.getListeners());
     }
     
     @Override
     public Either<IKeyEntryMouseClickedEvent, Boolean> fireKeyEntryMouseClickedEvent(NewKeyBindsList.KeyEntry entry, double mouseX, double mouseY, int buttonId) {
-        return Either.right(ControllingEvents.KEY_ENTRY_MOUSE_CLICKED_EVENT.invoker().handle(new KeyEntryMouseClickedEvent(entry, mouseX, mouseY, buttonId)));
+        KeyEntryMouseClickedEvent event = new KeyEntryMouseClickedEvent(entry, mouseX, mouseY, buttonId);
+        if(FabricLoader.getInstance().isModLoaded("fabric")) {
+            return Either.right(ControllingEvents.KEY_ENTRY_MOUSE_CLICKED_EVENT.invoker().handle(event));
+        }
+        
+        return Either.right(event.isHandled());
     }
     
     @Override
     public Either<IKeyEntryMouseReleasedEvent, Boolean> fireKeyEntryMouseReleasedEvent(NewKeyBindsList.KeyEntry entry, double mouseX, double mouseY, int buttonId) {
-        return Either.right(ControllingEvents.KEY_ENTRY_MOUSE_RELEASED_EVENT.invoker().handle(new KeyEntryMouseReleasedEvent(entry, mouseX, mouseY, buttonId)));
+        KeyEntryMouseReleasedEvent event = new KeyEntryMouseReleasedEvent(entry, mouseX, mouseY, buttonId);
+        if(FabricLoader.getInstance().isModLoaded("fabric")) {
+            return Either.right(ControllingEvents.KEY_ENTRY_MOUSE_RELEASED_EVENT.invoker().handle(event));
+        }
+        
+        return Either.right(event.isHandled());
     }
     
     @Override
-    public Either<IKeyEntryRenderEvent, Void> fireKeyEntryRenderEvent(NewKeyBindsList.KeyEntry entry, PoseStack stack, int slotIndex, int y, int x, int rowLeft, int rowWidth, int mouseX, int mouseY, boolean hovered, float partialTicks) {
-        return Either.right(ControllingEvents.KEY_ENTRY_RENDER_EVENT.invoker().handle(new KeyEntryRenderEvent(entry, stack, slotIndex, y, x, rowLeft, rowWidth, mouseX, mouseY, hovered, partialTicks)));
+    public Either<IKeyEntryRenderEvent, Unit> fireKeyEntryRenderEvent(NewKeyBindsList.KeyEntry entry, PoseStack stack, int slotIndex, int y, int x, int rowLeft, int rowWidth, int mouseX, int mouseY, boolean hovered, float partialTicks) {
+        
+        if(FabricLoader.getInstance().isModLoaded("fabric")) {
+            return Either.right(ControllingEvents.KEY_ENTRY_RENDER_EVENT.invoker().handle(new KeyEntryRenderEvent(entry, stack, slotIndex, y, x, rowLeft, rowWidth, mouseX, mouseY, hovered, partialTicks)));
+        }
+        return Either.right(Unit.INSTANCE);
     }
 }

+ 2 - 3
Fabric/src/main/resources/controlling.mixins.json

@@ -6,12 +6,11 @@
     "AccessInputConstantsKey",
     "AccessKeyBindsScreen",
     "AccessKeyMapping",
-    "AccessScreen",
     "AccessOptionsSubScreen",
+    "AccessScreen",
     "OpenGuiMixin"
   ],
   "injectors": {
     "defaultRequire": 1
-  },
-  "refmap": "controlling.refmap.json"
+  }
 }

+ 12 - 1
Fabric/src/main/resources/fabric.mod.json

@@ -16,7 +16,18 @@
   "description": "Adds the ability to search for keybinds using their name in the KeyBinding menu, this allows players to easily find a key binding in the menu.\n",
   "entrypoints": {
   },
+  "depends": {
+    "fabricloader": ">=0.12",
+    "java": ">=17",
+    "minecraft": "1.18.x"
+  },
+  "recommends": {
+    "fabric": "*"
+  },
   "mixins": [
-    "controlling.mixins.json"
+    {
+      "config": "controlling.mixins.json",
+      "environment": "client"
+    }
   ]
 }

+ 2 - 1
Forge/src/main/java/com/blamejared/controlling/platform/ForgeEventHandler.java

@@ -12,6 +12,7 @@ import com.blamejared.controlling.client.NewKeyBindsList;
 import com.mojang.blaze3d.vertex.PoseStack;
 import com.mojang.datafixers.util.Either;
 import net.minecraft.client.gui.components.events.GuiEventListener;
+import net.minecraft.util.Unit;
 import net.minecraftforge.common.MinecraftForge;
 
 import java.util.List;
@@ -40,7 +41,7 @@ public class ForgeEventHandler implements IEventHelper {
     }
     
     @Override
-    public Either<IKeyEntryRenderEvent, Void> fireKeyEntryRenderEvent(NewKeyBindsList.KeyEntry entry, PoseStack stack, int slotIndex, int y, int x, int rowLeft, int rowWidth, int mouseX, int mouseY, boolean hovered, float partialTicks) {
+    public Either<IKeyEntryRenderEvent, Unit> fireKeyEntryRenderEvent(NewKeyBindsList.KeyEntry entry, PoseStack stack, int slotIndex, int y, int x, int rowLeft, int rowWidth, int mouseX, int mouseY, boolean hovered, float partialTicks) {
         KeyEntryRenderEvent event = new KeyEntryRenderEvent(entry, stack, slotIndex, y, x, rowLeft, rowWidth, mouseX, mouseY, hovered, partialTicks);
         MinecraftForge.EVENT_BUS.post(event);
         return Either.left(event);

+ 2 - 2
Forge/src/main/resources/controlling.mixins.json

@@ -6,8 +6,8 @@
     "AccessInputConstantsKey",
     "AccessKeyBindsScreen",
     "AccessKeyMapping",
-    "AccessScreen",
-    "AccessOptionsSubScreen"
+    "AccessOptionsSubScreen",
+    "AccessScreen"
   ],
   "injectors": {
     "defaultRequire": 1