Skip to content

Commit

Permalink
add OnKeybindEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
kurrycat2004 committed Jun 27, 2024
1 parent 8b19a01 commit 786c838
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,26 @@
import io.github.kurrycat.mpkmod.Main;
import io.github.kurrycat.mpkmod.compatibility.MCClasses.FunctionHolder;
import io.github.kurrycat.mpkmod.compatibility.MCClasses.Minecraft;
import io.github.kurrycat.mpkmod.compatibility.MCClasses.WorldInteraction;
import io.github.kurrycat.mpkmod.discord.DiscordRPC;
import io.github.kurrycat.mpkmod.events.*;
import io.github.kurrycat.mpkmod.gui.MPKGuiScreen;
import io.github.kurrycat.mpkmod.gui.infovars.InfoString;
import io.github.kurrycat.mpkmod.gui.screens.LandingBlockGuiScreen;
import io.github.kurrycat.mpkmod.gui.screens.main_gui.MainGuiScreen;
import io.github.kurrycat.mpkmod.gui.screens.options_gui.Option;
import io.github.kurrycat.mpkmod.gui.screens.options_gui.OptionsGuiScreen;
import io.github.kurrycat.mpkmod.landingblock.LandingBlock;
import io.github.kurrycat.mpkmod.modules.MPKModule;
import io.github.kurrycat.mpkmod.modules.MPKModuleImpl;
import io.github.kurrycat.mpkmod.modules.ModuleFinder;
import io.github.kurrycat.mpkmod.modules.ModuleManager;
import io.github.kurrycat.mpkmod.save.Serializer;
import io.github.kurrycat.mpkmod.ticks.TimingStorage;
import io.github.kurrycat.mpkmod.util.*;
import io.github.kurrycat.mpkmod.util.ClassUtil;
import io.github.kurrycat.mpkmod.util.JSONConfig;
import io.github.kurrycat.mpkmod.util.Mouse;
import io.github.kurrycat.mpkmod.util.Procedure;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;

import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

Expand Down Expand Up @@ -175,5 +170,15 @@ public static void onKeyInput(int keyCode, String key, boolean pressed) {
public static void onMouseInput(Mouse.Button button, Mouse.State state, int x, int y, int dx, int dy, int dwheel, long nanos) {
EventAPI.postEvent(new OnMouseInputEvent(button, state, x, y, dx, dy, dwheel, nanos));
}

public static void onKeybind(String id) {
MPKGuiScreen guiScreen = guiScreenMap.get(id);
if (guiScreen != null) guiScreen.onKeybindPressed();

Procedure keyBinding = keyBindingMap.get(id);
if (keyBinding != null) keyBinding.run();

EventAPI.postEvent(new OnKeybindEvent(id));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public enum EventType {
SERVER_CONNECT(OnServerConnect.class),
SERVER_DISCONNECT(OnServerDisconnect.class),
KEY_INPUT(OnKeyInputEvent.class),
MOUSE_INPUT(OnMouseInputEvent.class);
MOUSE_INPUT(OnMouseInputEvent.class),
KEYBIND(OnKeybindEvent.class);

public final Class<? extends Event> eventClass;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.github.kurrycat.mpkmod.events;

public class OnKeybindEvent extends Event {
public String id;

public OnKeybindEvent(String id) {
super(EventType.KEYBIND);
this.id = id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ public boolean shouldCreateKeyBind() {
return false;
}

public void onKeybindPressed() {
Minecraft.displayGuiScreen(this);
}

public final void close() {
Minecraft.displayGuiScreen(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,8 @@ public void onKey(int key, int scanCode, int action) {
API.Events.onKeyInput(key, inputKey.getLocalizedText().getString(), action == 1);

MPKMod.keyBindingMap.forEach((id, keyBinding) -> {
boolean keyBindingPressed = keyBinding.isPressed();
if (keyBindingPressed && API.guiScreenMap.containsKey(id)) {
MinecraftClient.getInstance().setScreen(
new MPKGuiScreen(API.guiScreenMap.get(id))
);
}

if (keyBindingPressed && API.keyBindingMap.containsKey(id)) {
API.keyBindingMap.get(id).run();
if (keyBinding.isPressed()) {
API.Events.onKeybind(id);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,8 @@ public void onKey(int key, int scanCode, int action) {
API.Events.onKeyInput(key, inputKey.getLocalizedText().getString(), action == 1);

MPKMod.keyBindingMap.forEach((id, keyBinding) -> {
boolean keyBindingPressed = keyBinding.isPressed();
if (keyBindingPressed && API.guiScreenMap.containsKey(id)) {
MinecraftClient.getInstance().setScreen(
new MPKGuiScreen(API.guiScreenMap.get(id))
);
}

if (keyBindingPressed && API.keyBindingMap.containsKey(id)) {
API.keyBindingMap.get(id).run();
if (keyBinding.isPressed()) {
API.Events.onKeybind(id);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,8 @@ public void onKey(int key, int scanCode, int action) {
API.Events.onKeyInput(key, inputKey.getLocalizedText().getString(), action == 1);

MPKMod.keyBindingMap.forEach((id, keyBinding) -> {
boolean keyBindingPressed = keyBinding.isPressed();
if (keyBindingPressed && API.guiScreenMap.containsKey(id)) {
MinecraftClient.getInstance().setScreen(
new MPKGuiScreen(API.guiScreenMap.get(id))
);
}

if (keyBindingPressed && API.keyBindingMap.containsKey(id)) {
API.keyBindingMap.get(id).run();
if (keyBinding.isPressed()) {
API.Events.onKeybind(id);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,8 @@ public void onEvent(InputEvent.KeyInputEvent event) {
API.Events.onKeyInput(InputConstants.convert(keyCode), key, pressed);

MPKMod.keyBindingMap.forEach((id, keyBinding) -> {
boolean keyBindingPressed = keyBinding.isPressed();
if (keyBindingPressed && API.guiScreenMap.containsKey(id)) {
Minecraft.getMinecraft().displayGuiScreen(
new MPKGuiScreen(API.guiScreenMap.get(id))
);
}

if (keyBindingPressed && API.keyBindingMap.containsKey(id)) {
API.keyBindingMap.get(id).run();
if (keyBinding.isPressed()) {
API.Events.onKeybind(id);
}
});
}
Expand Down

0 comments on commit 786c838

Please sign in to comment.