-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Move Forge client code into separate class because double suppli…
…er doesn't work anymore?
- Loading branch information
1 parent
f9f2ef5
commit 7726223
Showing
2 changed files
with
85 additions
and
76 deletions.
There are no files selected for viewing
77 changes: 1 addition & 76 deletions
77
kuma-api/forge/src/main/java/net/blay09/mods/kuma/forge/ForgeKumaAPI.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,15 @@ | ||
package net.blay09.mods.kuma.forge; | ||
|
||
import com.mojang.blaze3d.platform.InputConstants; | ||
import net.blay09.mods.kuma.ManagedKeyMappingRegistry; | ||
import net.blay09.mods.kuma.VanillaManagedKeyMapping; | ||
import net.blay09.mods.kuma.api.*; | ||
import net.minecraft.client.Minecraft; | ||
import net.minecraft.util.Mth; | ||
import net.minecraftforge.api.distmarker.Dist; | ||
import net.minecraftforge.client.event.InputEvent; | ||
import net.minecraftforge.client.event.RegisterKeyMappingsEvent; | ||
import net.minecraftforge.client.event.ScreenEvent; | ||
import net.minecraftforge.common.MinecraftForge; | ||
import net.minecraftforge.fml.DistExecutor; | ||
import net.minecraftforge.fml.common.Mod; | ||
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; | ||
|
||
@Mod(value = "kuma_api") | ||
public class ForgeKumaAPI { | ||
|
||
public ForgeKumaAPI() { | ||
DistExecutor.runWhenOn(Dist.CLIENT, () -> () -> { | ||
final var modEventBus = FMLJavaModLoadingContext.get().getModEventBus(); | ||
modEventBus.addListener((RegisterKeyMappingsEvent event) -> { | ||
for (final var managedKeyMapping : ManagedKeyMappingRegistry.getKeyMappings()) { | ||
if (managedKeyMapping instanceof VanillaManagedKeyMapping vanillaManagedKeyMapping) { | ||
event.register(vanillaManagedKeyMapping.register()); | ||
} | ||
} | ||
}); | ||
|
||
MinecraftForge.EVENT_BUS.addListener((InputEvent.MouseButton.Pre event) -> { | ||
if (Minecraft.getInstance().screen != null) { | ||
return; | ||
} | ||
|
||
for (final var keyMapping : ManagedKeyMappingRegistry.getKeyMappings()) { | ||
if (keyMapping.isActiveAndMatchesMouse(event.getButton())) { | ||
if (keyMapping.handleWorldInput(new WorldInputEvent())) { | ||
event.setCanceled(true); | ||
return; | ||
} | ||
} | ||
} | ||
}); | ||
|
||
MinecraftForge.EVENT_BUS.addListener((InputEvent.Key event) -> { | ||
if (Minecraft.getInstance().screen != null) { | ||
return; | ||
} | ||
|
||
for (final var keyMapping : ManagedKeyMappingRegistry.getKeyMappings()) { | ||
if (event.getAction() == 1 && keyMapping.isActiveAndMatchesKey(event.getKey(), event.getScanCode(), event.getModifiers())) { | ||
keyMapping.handleWorldInput(new WorldInputEvent()); | ||
// TODO cannot cancel? | ||
} | ||
} | ||
}); | ||
|
||
MinecraftForge.EVENT_BUS.addListener((ScreenEvent.KeyPressed.Pre event) -> { | ||
for (final var keyMapping : ManagedKeyMappingRegistry.getKeyMappings()) { | ||
if (keyMapping.isActiveAndMatchesKey(event.getKeyCode(), event.getScanCode(), event.getModifiers())) { | ||
final var client = Minecraft.getInstance(); | ||
final var window = client.getWindow(); | ||
int mouseX = Mth.floor(client.mouseHandler.xpos() * (double) window.getGuiScaledWidth() / (double) window.getScreenWidth()); | ||
int mouseY = Mth.floor(client.mouseHandler.ypos() * (double) window.getGuiScaledHeight() / (double) window.getScreenHeight()); | ||
if (keyMapping.handleScreenInput(new ScreenInputEvent(event.getScreen(), mouseX, mouseY))) { | ||
event.setCanceled(true); | ||
return; | ||
} | ||
} | ||
} | ||
}); | ||
|
||
MinecraftForge.EVENT_BUS.addListener((ScreenEvent.MouseButtonPressed.Pre event) -> { | ||
for (final var keyMapping : ManagedKeyMappingRegistry.getKeyMappings()) { | ||
if (keyMapping.isActiveAndMatchesMouse(event.getButton())) { | ||
if (keyMapping.handleScreenInput(new ScreenInputEvent(event.getScreen(), event.getMouseX(), event.getMouseY()))) { | ||
// We only cancel click events that aren't sole left clicks, otherwise people might get stuck in menu screens | ||
if (event.getButton() != InputConstants.MOUSE_BUTTON_LEFT || !keyMapping.getBinding().modifiers().isEmpty()) { | ||
event.setCanceled(true); | ||
} | ||
return; | ||
} | ||
} | ||
} | ||
}); | ||
ForgeKumaAPIClient.init(); | ||
}); | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
kuma-api/forge/src/main/java/net/blay09/mods/kuma/forge/ForgeKumaAPIClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package net.blay09.mods.kuma.forge; | ||
|
||
import com.mojang.blaze3d.platform.InputConstants; | ||
import net.blay09.mods.kuma.ManagedKeyMappingRegistry; | ||
import net.blay09.mods.kuma.VanillaManagedKeyMapping; | ||
import net.blay09.mods.kuma.api.ScreenInputEvent; | ||
import net.blay09.mods.kuma.api.WorldInputEvent; | ||
import net.minecraft.client.Minecraft; | ||
import net.minecraft.util.Mth; | ||
import net.minecraftforge.client.event.InputEvent; | ||
import net.minecraftforge.client.event.RegisterKeyMappingsEvent; | ||
import net.minecraftforge.client.event.ScreenEvent; | ||
import net.minecraftforge.common.MinecraftForge; | ||
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; | ||
|
||
public class ForgeKumaAPIClient { | ||
public static void init() { | ||
final var modEventBus = FMLJavaModLoadingContext.get().getModEventBus(); | ||
modEventBus.addListener((RegisterKeyMappingsEvent event) -> { | ||
for (final var managedKeyMapping : ManagedKeyMappingRegistry.getKeyMappings()) { | ||
if (managedKeyMapping instanceof VanillaManagedKeyMapping vanillaManagedKeyMapping) { | ||
event.register(vanillaManagedKeyMapping.register()); | ||
} | ||
} | ||
}); | ||
|
||
MinecraftForge.EVENT_BUS.addListener((InputEvent.MouseButton.Pre event) -> { | ||
if (Minecraft.getInstance().screen != null) { | ||
return; | ||
} | ||
|
||
for (final var keyMapping : ManagedKeyMappingRegistry.getKeyMappings()) { | ||
if (keyMapping.isActiveAndMatchesMouse(event.getButton())) { | ||
if (keyMapping.handleWorldInput(new WorldInputEvent())) { | ||
event.setCanceled(true); | ||
return; | ||
} | ||
} | ||
} | ||
}); | ||
|
||
MinecraftForge.EVENT_BUS.addListener((InputEvent.Key event) -> { | ||
if (Minecraft.getInstance().screen != null) { | ||
return; | ||
} | ||
|
||
for (final var keyMapping : ManagedKeyMappingRegistry.getKeyMappings()) { | ||
if (event.getAction() == 1 && keyMapping.isActiveAndMatchesKey(event.getKey(), event.getScanCode(), event.getModifiers())) { | ||
keyMapping.handleWorldInput(new WorldInputEvent()); | ||
// TODO cannot cancel? | ||
} | ||
} | ||
}); | ||
|
||
MinecraftForge.EVENT_BUS.addListener((ScreenEvent.KeyPressed.Pre event) -> { | ||
for (final var keyMapping : ManagedKeyMappingRegistry.getKeyMappings()) { | ||
if (keyMapping.isActiveAndMatchesKey(event.getKeyCode(), event.getScanCode(), event.getModifiers())) { | ||
final var client = Minecraft.getInstance(); | ||
final var window = client.getWindow(); | ||
int mouseX = Mth.floor(client.mouseHandler.xpos() * (double) window.getGuiScaledWidth() / (double) window.getScreenWidth()); | ||
int mouseY = Mth.floor(client.mouseHandler.ypos() * (double) window.getGuiScaledHeight() / (double) window.getScreenHeight()); | ||
if (keyMapping.handleScreenInput(new ScreenInputEvent(event.getScreen(), mouseX, mouseY))) { | ||
event.setCanceled(true); | ||
return; | ||
} | ||
} | ||
} | ||
}); | ||
|
||
MinecraftForge.EVENT_BUS.addListener((ScreenEvent.MouseButtonPressed.Pre event) -> { | ||
for (final var keyMapping : ManagedKeyMappingRegistry.getKeyMappings()) { | ||
if (keyMapping.isActiveAndMatchesMouse(event.getButton())) { | ||
if (keyMapping.handleScreenInput(new ScreenInputEvent(event.getScreen(), event.getMouseX(), event.getMouseY()))) { | ||
// We only cancel click events that aren't sole left clicks, otherwise people might get stuck in menu screens | ||
if (event.getButton() != InputConstants.MOUSE_BUTTON_LEFT || !keyMapping.getBinding().modifiers().isEmpty()) { | ||
event.setCanceled(true); | ||
} | ||
return; | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
} |