Skip to content

Commit

Permalink
fix: Move Forge client code into separate class because double suppli…
Browse files Browse the repository at this point in the history
…er doesn't work anymore?
  • Loading branch information
BlayTheNinth committed Jun 18, 2024
1 parent f9f2ef5 commit 7726223
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 76 deletions.
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();
});
}
}
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;
}
}
}
});
}
}

0 comments on commit 7726223

Please sign in to comment.