| 1 | package nicotine.util;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.platform.InputConstants;
|
| 4 | import net.minecraft.ChatFormatting;
|
| 5 | import net.minecraft.client.gui.screens.ChatScreen;
|
| 6 | import net.minecraft.client.gui.screens.inventory.SignEditScreen;
|
| 7 | import nicotine.mod.mods.combat.AutoArmor;
|
| 8 | import nicotine.mod.mods.general.GUI;
|
| 9 | import nicotine.mod.mods.general.HUD;
|
| 10 | import nicotine.mod.mods.render.Peek;
|
| 11 | import nicotine.mod.mods.render.Zoom;
|
| 12 | import nicotine.screens.clickgui.BaseScreen;
|
| 13 | import nicotine.events.ClientLevelTickEvent;
|
| 14 | import nicotine.mod.Mod;
|
| 15 | import nicotine.mod.ModCategory;
|
| 16 | import nicotine.mod.ModManager;
|
| 17 | import org.lwjgl.sdl.SDLMouse;
|
| 18 |
|
| 19 | import java.util.*;
|
| 20 |
|
| 21 | import static nicotine.util.Common.*;
|
| 22 |
|
| 23 | public class Keybind {
|
| 24 |
|
| 25 | private static final HashMap<String, Integer> keysPressed = new HashMap<>();
|
| 26 |
|
| 27 | private static final List<Class> denyList = Arrays.asList(
|
| 28 | Zoom.class, AutoArmor.class, Peek.class, HUD.class, GUI.class
|
| 29 | );
|
| 30 |
|
| 31 | public static void init() {
|
| 32 | EventBus.register(ClientLevelTickEvent.class, event -> {
|
| 33 | for (ModCategory modCategory : ModCategory.values()) {
|
| 34 | for (Mod mod : ModManager.modules.get(modCategory)) {
|
| 35 | if (!denyList.contains(mod.getClass()) && Keybind.keyReleased(mod, mod.keybind.keyCode)) {
|
| 36 | mod.toggle();
|
| 37 |
|
| 38 | return true;
|
| 39 | }
|
| 40 | }
|
| 41 | }
|
| 42 |
|
| 43 | return true;
|
| 44 | });
|
| 45 | }
|
| 46 |
|
| 47 | public static int mouseButtonMask(int button) {
|
| 48 | return 1 << (button - 1);
|
| 49 | }
|
| 50 |
|
| 51 | public static boolean keyDown(int keycode) {
|
| 52 | return (keycode < 1000 && InputConstants.isKeyDown(keycode)) || (keycode > 1000 && (SDLMouse.SDL_GetMouseState(null, null) & mouseButtonMask(keycode - 1000)) != 0);
|
| 53 | }
|
| 54 |
|
| 55 | public static boolean keyReleased(Mod mod, int keycode) {
|
| 56 | return keyReleased(mod.name, mod.enabled, keycode);
|
| 57 | }
|
| 58 |
|
| 59 | public static boolean keyReleased(String name, boolean enabled, int keycode) {
|
| 60 | return keyReleased(name, enabled, keycode, true);
|
| 61 | }
|
| 62 |
|
| 63 | public static boolean keyReleased(String name, boolean enabled, int keycode, boolean showToggleMsg) {
|
| 64 | if (keycode == -1)
|
| 65 | return false;
|
| 66 |
|
| 67 | if (keyDown(keycode) && !(mc.gui.screen() instanceof ChatScreen) && !(mc.gui.screen() instanceof BaseScreen) && !(mc.gui.screen() instanceof SignEditScreen)) {
|
| 68 | if (!keysPressed.containsKey(keycode)) {
|
| 69 | keysPressed.put(name, keycode);
|
| 70 | }
|
| 71 | } else if (keysPressed.getOrDefault(name, -1) != -1) {
|
| 72 | keysPressed.remove(name);
|
| 73 |
|
| 74 | if (showToggleMsg)
|
| 75 | Message.send(String.format("%s [%s%s%s]", name, enabled ? ChatFormatting.RED : ChatFormatting.GREEN, enabled ? "OFF" : "ON", ChatFormatting.DARK_GRAY));
|
| 76 |
|
| 77 | return true;
|
| 78 | }
|
| 79 |
|
| 80 | return false;
|
| 81 | }
|
| 82 | }
|
| 83 |
|