Skip to content

Commit

Permalink
Changed how keybind button presses is simulated. Should hopefully mak…
Browse files Browse the repository at this point in the history
…e it work in more instances.

Fixed Clipboard/Send button & Toggle/Press button reverting, everytime you edit a button
  • Loading branch information
GirafiStudios committed Oct 29, 2023
1 parent 0732df2 commit fbb859e
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 39 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
@@ -1,6 +1,6 @@
minecraft_version=1.19.4
forge_version=45.0.9
mod_version=1.11.4
mod_version=1.11.5

org.gradle.jvmargs=-Xmx4G
org.gradle.daemon=false
2 changes: 2 additions & 0 deletions src/main/java/dmillerw/menu/MineMenu.java
Expand Up @@ -2,6 +2,7 @@

import dmillerw.menu.data.json.MenuLoader;
import dmillerw.menu.handler.ConfigHandler;
import dmillerw.menu.helper.KeyReflectionHelper;
import dmillerw.menu.network.PacketHandler;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.IEventBus;
Expand Down Expand Up @@ -31,6 +32,7 @@ public MineMenu() {
}

private void setupCommon(FMLCommonSetupEvent event) {
KeyReflectionHelper.gatherFields();
PacketHandler.initialize();
}

Expand Down
Expand Up @@ -37,6 +37,6 @@ public void onClicked() {

@Override
public void onRemoved() {
clipboard = !clipboard;

}
}
7 changes: 4 additions & 3 deletions src/main/java/dmillerw/menu/data/click/ClickActionKey.java
@@ -1,12 +1,13 @@
package dmillerw.menu.data.click;

import dmillerw.menu.handler.KeyboardHandler;
import dmillerw.menu.helper.KeyReflectionHelper;
import net.minecraft.client.KeyMapping;
import net.minecraft.client.Minecraft;

public class ClickActionKey implements ClickAction.IClickAction {
public final String key;
public boolean toggle;
public static boolean toggle;

public ClickActionKey(String key, boolean toggle) {
this.key = key;
Expand Down Expand Up @@ -48,8 +49,8 @@ public boolean deactivates() {
public void onRemoved() {
KeyMapping keyBinding = getKeyBinding();
if (keyBinding != null) {
KeyMapping.set(keyBinding.getKey(), false);
keyBinding.setDown(false);
KeyReflectionHelper.setClickCount(keyBinding, 0);
}
toggle = !toggle;
}
}
25 changes: 13 additions & 12 deletions src/main/java/dmillerw/menu/gui/menu/ClickActionScreen.java
Expand Up @@ -26,8 +26,8 @@ public class ClickActionScreen extends Screen {
@Nonnull
public static ItemStack item;
public static KeyMapping keyBinding;
private static boolean toggle = false;
private static boolean clipboard = false;
private boolean toggle;
private boolean clipboard;
private EditBox textCommand;
private EditBox textCategory;
private ExtendedButton modeCommand;
Expand Down Expand Up @@ -76,7 +76,7 @@ public void init() {
mode = EditSessionData.clickAction != null ? EditSessionData.clickAction.getClickAction().ordinal() : 0;
}

addRenderableWidget(this.buttonConfirm = Button.builder(Component.translatable("gui.done"), (screen) -> {
addRenderableWidget(this.buttonConfirm = Button.builder(Component.translatable("gui.done"), (button) -> {
if (mode == 0) {
EditSessionData.clickAction = !textCommand.getValue().trim().isEmpty() ? new ClickActionCommand(textCommand.getValue().trim(), clipboard) : null;
} else if (mode == 1 && ClickActionScreen.keyBinding != null) {
Expand All @@ -90,15 +90,15 @@ public void init() {
}).bounds(this.width / 2 - 4 - 150, this.height - 60, 150, 20).build());


addRenderableWidget(this.buttonCancel = Button.builder(Component.translatable("gui.cancel"), (screen) -> ScreenStack.pop()).bounds(this.width / 2 + 4, this.height - 60, 150, 20).build());
addRenderableWidget(this.buttonCancel = Button.builder(Component.translatable("gui.cancel"), (button) -> ScreenStack.pop()).bounds(this.width / 2 + 4, this.height - 60, 150, 20).build());

Component commandString;
if (EditSessionData.clickAction instanceof ClickActionCommand) {
commandString = Component.translatable(((ClickActionCommand) EditSessionData.clickAction).clipboard ? "mine_menu.clipboard" : "mine_menu.send");
} else {
commandString = Component.translatable(clipboard ? "mine_menu.clipboard" : "mine_menu.send");
}
addRenderableWidget(this.commandClipboardButton = Button.builder(commandString, (screen) -> {
addRenderableWidget(this.commandClipboardButton = Button.builder(commandString, (button) -> {
clipboard = !clipboard;
commandClipboardButton.setMessage(Component.translatable(clipboard ? "mine_menu.clipboard" : "mine_menu.send"));
}).bounds(this.width / 2 - 75, 80, 150, 20).build());
Expand All @@ -113,16 +113,17 @@ public void init() {
keyString = Component.translatable("mine_menu.selectKey");
}
}
addRenderableWidget(this.keybindButton = Button.builder(keyString, (screen) -> ScreenStack.push(new PickKeyScreen())).bounds(this.width / 2 - 75, 50, 150, 20).build());
addRenderableWidget(this.keybindButton = Button.builder(keyString, (button) -> ScreenStack.push(new PickKeyScreen())).bounds(this.width / 2 - 75, 50, 150, 20).build());

Component keyToggleString;
if (EditSessionData.clickAction instanceof ClickActionKey) {
keyToggleString = Component.translatable(((ClickActionKey) EditSessionData.clickAction).toggle ? "mine_menu.toggle" : "mine_menu.press");
} else {
keyToggleString = Component.translatable(toggle ? "mine_menu.toggle" : "mine_menu.press");
}
addRenderableWidget(this.keybindToggleButton = Button.builder(keyToggleString, (screen) -> {
addRenderableWidget(this.keybindToggleButton = Button.builder(keyToggleString, (button) -> {
toggle = !toggle;
ClickActionKey.toggle = toggle;
keybindToggleButton.setMessage(Component.translatable(toggle ? "mine_menu.toggle" : "mine_menu.press"));
}).bounds(this.width / 2 - 75, 80, 150, 20).build());

Expand All @@ -136,9 +137,9 @@ public void init() {
itemString = Component.translatable("mine_menu.selectSlot");
}
}
addRenderableWidget(this.selectItemButton = Button.builder(itemString, (screen) -> ScreenStack.push(new PickItemScreen())).bounds(this.width / 2 - 75, 50, 150, 20).build());
addRenderableWidget(this.selectItemButton = Button.builder(itemString, (button) -> ScreenStack.push(new PickItemScreen())).bounds(this.width / 2 - 75, 50, 150, 20).build());

addRenderableWidget(this.modeCommand = new ItemButton(this.width / 2 - 55, this.height - 90, 20, 20, new ItemStack(Items.PAPER), (screen) -> {
addRenderableWidget(this.modeCommand = new ItemButton(this.width / 2 - 55, this.height - 90, 20, 20, new ItemStack(Items.PAPER), (button) -> {
// Command
mode = 0;

Expand All @@ -155,7 +156,7 @@ public void init() {
keybindToggleButton.visible = false;
}));

addRenderableWidget(this.modeKeybinding = new ItemButton(this.width / 2 - 25, this.height - 90, 20, 20, new ItemStack(Blocks.OAK_BUTTON), (screen) -> {
addRenderableWidget(this.modeKeybinding = new ItemButton(this.width / 2 - 25, this.height - 90, 20, 20, new ItemStack(Blocks.OAK_BUTTON), (button) -> {
// Keybinding
mode = 1;

Expand All @@ -172,7 +173,7 @@ public void init() {
keybindToggleButton.visible = true;
}));

addRenderableWidget(this.modeUseItem = new ItemButton(this.width / 2 + 5, this.height - 90, 20, 20, new ItemStack(Items.DIAMOND_SWORD), (screen) -> {
addRenderableWidget(this.modeUseItem = new ItemButton(this.width / 2 + 5, this.height - 90, 20, 20, new ItemStack(Items.DIAMOND_SWORD), (button) -> {
// Select item
mode = 2;

Expand All @@ -189,7 +190,7 @@ public void init() {
keybindToggleButton.visible = false;
}));

addRenderableWidget(this.modeCategory = new ItemButton(this.width / 2 + 35, this.height - 90, 20, 20, new ItemStack(Blocks.CHEST), (screen) -> {
addRenderableWidget(this.modeCategory = new ItemButton(this.width / 2 + 35, this.height - 90, 20, 20, new ItemStack(Blocks.CHEST), (button) -> {
// Category
mode = 3;

Expand Down
32 changes: 10 additions & 22 deletions src/main/java/dmillerw/menu/handler/KeyboardHandler.java
Expand Up @@ -4,6 +4,7 @@
import dmillerw.menu.MineMenu;
import dmillerw.menu.data.menu.RadialMenu;
import dmillerw.menu.gui.RadialMenuScreen;
import dmillerw.menu.helper.KeyReflectionHelper;
import net.minecraft.client.KeyMapping;
import net.minecraft.client.Minecraft;
import net.minecraftforge.api.distmarker.Dist;
Expand All @@ -20,32 +21,25 @@ public class KeyboardHandler {
public static final KeyboardHandler INSTANCE = new KeyboardHandler();
private static boolean lastWheelState = false;
private static final List<KeyMapping> FIRED_KEYS = new ArrayList<>();
private static final List<KeyMapping> TOGGLED_KEYS = new ArrayList<>();
private static boolean ignoreNextTick = false;

private KeyboardHandler() {
}

public void fireKey(KeyMapping keyMapping) {
FIRED_KEYS.add(keyMapping);
keyMapping.setDown(true);
activateKeybind(keyMapping, true);
ignoreNextTick = true;
}

public void toggleKey(KeyMapping keyMapping) {
if (!TOGGLED_KEYS.contains(keyMapping)) {
TOGGLED_KEYS.add(keyMapping);
keyMapping.setDown(true);
} else {
TOGGLED_KEYS.remove(keyMapping);
keyMapping.setDown(false);
}
activateKeybind(keyMapping, true);
ignoreNextTick = true;
}

@SubscribeEvent
public static void onClientTick(TickEvent.ClientTickEvent event) {
if (event.phase == TickEvent.Phase.END) {
if (event.phase == TickEvent.Phase.END || event.type != TickEvent.Type.CLIENT) {
return;
}
Minecraft mc = Minecraft.getInstance();
Expand Down Expand Up @@ -100,20 +94,14 @@ public static void onClientTick(TickEvent.ClientTickEvent event) {
Iterator<KeyMapping> iterator = FIRED_KEYS.iterator();
while (iterator.hasNext()) {
KeyMapping keyBinding = iterator.next();
KeyMapping.set(keyBinding.getKey(), false);
activateKeybind(keyBinding, false);
iterator.remove();
}
}

iterator = TOGGLED_KEYS.iterator();
while (iterator.hasNext()) {
KeyMapping keyBinding = iterator.next();
if ((keyBinding.getKey().getValue() >= 0 ? keyBinding.consumeClick() : InputConstants.isKeyDown(handle, keyBinding.getKey().getValue() + 100)) || mc.screen != null) {
iterator.remove();
}
}

for (KeyMapping keyBinding : TOGGLED_KEYS) {
KeyMapping.click(keyBinding.getKey());
}
public static void activateKeybind(KeyMapping keyMapping, boolean setDown) {
//if (keyMapping.getCategory().equals("key.categories.movement"))
keyMapping.setDown(setDown);
KeyReflectionHelper.setClickCount(keyMapping, setDown ? 1 : 0);
}
}
36 changes: 36 additions & 0 deletions src/main/java/dmillerw/menu/helper/KeyReflectionHelper.java
@@ -0,0 +1,36 @@
package dmillerw.menu.helper;

import cpw.mods.modlauncher.api.INameMappingService;
import dmillerw.menu.handler.LogHandler;
import net.minecraft.client.KeyMapping;
import net.minecraftforge.fml.util.ObfuscationReflectionHelper;

import java.lang.reflect.Field;

public class KeyReflectionHelper {
private static Field pressTimeField;

public static void gatherFields() {
try {
pressTimeField = KeyMapping.class.getDeclaredField(ObfuscationReflectionHelper.remapName(INameMappingService.Domain.FIELD, "f_90818_"));
pressTimeField.setAccessible(true);
} catch (NoSuchFieldException e) {
throwReflectionError("f_90818_", KeyMapping.class);
}
}

public static void setClickCount(KeyMapping keyBinding, int clickCount) {
try {
pressTimeField.set(keyBinding, clickCount == 0 ? 0 : pressTimeField.getInt(keyBinding) + clickCount);
} catch (IllegalAccessException e) {
e.printStackTrace();
throwReflectionError("f_90818_", KeyMapping.class);
}
}

private static void throwReflectionError(String field, Class<?> clazz) {
String error = String.format("Ran into an issue regarding reflection with field %s from %s. REPORT THIS TO THE MOD AUTHOR!", field, clazz.getName());
LogHandler.fatal(error);
throw new RuntimeException(error);
}
}

0 comments on commit fbb859e

Please sign in to comment.