Skip to content

Commit

Permalink
Merge pull request #80 from GroobleDierne/linux
Browse files Browse the repository at this point in the history
Linux Experience Enhancement
  • Loading branch information
Ninjabrain1 authored Feb 2, 2024
2 parents 689d7c9 + 9a05fa2 commit 88e7dd0
Show file tree
Hide file tree
Showing 8 changed files with 278 additions and 10 deletions.
7 changes: 5 additions & 2 deletions src/main/java/ninjabrainbot/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ninjabrainbot;

import java.util.Locale;
import java.util.Optional;

import ninjabrainbot.gui.GUI;
import ninjabrainbot.gui.splash.Progress;
Expand All @@ -18,8 +19,10 @@ public class Main {
public static final String VERSION = "1.4.2";

public static void main(String[] args) {
System.setProperty("sun.java2d.opengl", "true");
Progress.init(new Splash());
Optional<String> sessionType = Optional.ofNullable(System.getenv("XDG_SESSION_TYPE")); // Hardware acceleration isn't supported when running java on wayland so we turn it off
System.setProperty("sun.java2d.opengl", sessionType.orElse("").equals("wayland") ? "false" : "True");
boolean isSplashScreenDisabled = sessionType.isPresent();
Progress.init(new Splash(isSplashScreenDisabled));
Progress.setTask("Loading language", 0.02f);
Profiler.start("Initialize language");
Logger.log("Language: " + I18n.get("lang"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;

import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

Expand Down Expand Up @@ -72,7 +70,7 @@ private void clicked() {
KeyboardListener.instance.setConsumer((code, modifier) -> {
if (code == -1) {
// Canceled, dont change anything
} else if (code == KeyEvent.VK_ESCAPE) {
} else if (code == NativeKeyEvent.VC_ESCAPE) {
preference.setCode(-1);
preference.setModifier(-1);
} else {
Expand All @@ -91,7 +89,7 @@ private void clicked() {
private String getKeyText() {
if (preference.getCode() == -1)
return I18n.get("settings.not_in_use");
String k = KeyEvent.getKeyText(preference.getCode());
String k = NativeKeyEvent.getKeyText(preference.getCode());
if (k.startsWith("Unknown")) {
k = k.substring(17);
}
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/ninjabrainbot/gui/splash/Splash.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,17 @@ public class Splash {
private static final Color LOADING_BAR_COLOR = new Color(249, 255, 173);
private static final Color TEXT_COLOR = LOADING_BAR_COLOR;

public Splash() {
public Splash(boolean disabled) {
splashScreen = SplashScreen.getSplashScreen();
if (splashScreen == null) {
System.err.println("Could not load splash screen");
return;
}
if (disabled) {
splashScreen.close();
return;
}

g = splashScreen.createGraphics();
g.setFont(new Font("Arial", Font.PLAIN, LOADING_PROGRESS_FONT_SIZE));
if (g == null) {
Expand Down
235 changes: 235 additions & 0 deletions src/main/java/ninjabrainbot/io/KeyConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
package ninjabrainbot.io;

import java.awt.event.KeyEvent;

import com.github.kwhat.jnativehook.keyboard.NativeKeyEvent;

public class KeyConverter {

public static int convertKeyCodeToNativeKeyCode(int keyCode) {
switch (keyCode) {
case KeyEvent.VK_ESCAPE:
return NativeKeyEvent.VC_ESCAPE;
case KeyEvent.VK_F1:
return NativeKeyEvent.VC_F1;
case KeyEvent.VK_F2:
return NativeKeyEvent.VC_F2;
case KeyEvent.VK_F3:
return NativeKeyEvent.VC_F3;
case KeyEvent.VK_F4:
return NativeKeyEvent.VC_F4;
case KeyEvent.VK_F5:
return NativeKeyEvent.VC_F5;
case KeyEvent.VK_F6:
return NativeKeyEvent.VC_F6;
case KeyEvent.VK_F7:
return NativeKeyEvent.VC_F7;
case KeyEvent.VK_F8:
return NativeKeyEvent.VC_F8;
case KeyEvent.VK_F9:
return NativeKeyEvent.VC_F9;
case KeyEvent.VK_F10:
return NativeKeyEvent.VC_F10;
case KeyEvent.VK_F11:
return NativeKeyEvent.VC_F11;
case KeyEvent.VK_F12:
return NativeKeyEvent.VC_F12;
case KeyEvent.VK_F13:
return NativeKeyEvent.VC_F13;
case KeyEvent.VK_F14:
return NativeKeyEvent.VC_F14;
case KeyEvent.VK_F15:
return NativeKeyEvent.VC_F15;
case KeyEvent.VK_F16:
return NativeKeyEvent.VC_F16;
case KeyEvent.VK_F17:
return NativeKeyEvent.VC_F17;
case KeyEvent.VK_F18:
return NativeKeyEvent.VC_F18;
case KeyEvent.VK_F19:
return NativeKeyEvent.VC_F19;
case KeyEvent.VK_F20:
return NativeKeyEvent.VC_F20;
case KeyEvent.VK_F21:
return NativeKeyEvent.VC_F21;
case KeyEvent.VK_F22:
return NativeKeyEvent.VC_F22;
case KeyEvent.VK_F23:
return NativeKeyEvent.VC_F23;
case KeyEvent.VK_F24:
return NativeKeyEvent.VC_F24;
case KeyEvent.VK_BACK_QUOTE:
return NativeKeyEvent.VC_BACKQUOTE;
case KeyEvent.VK_1:
return NativeKeyEvent.VC_1;
case KeyEvent.VK_2:
return NativeKeyEvent.VC_2;
case KeyEvent.VK_3:
return NativeKeyEvent.VC_3;
case KeyEvent.VK_4:
return NativeKeyEvent.VC_4;
case KeyEvent.VK_5:
return NativeKeyEvent.VC_5;
case KeyEvent.VK_6:
return NativeKeyEvent.VC_6;
case KeyEvent.VK_7:
return NativeKeyEvent.VC_7;
case KeyEvent.VK_8:
return NativeKeyEvent.VC_8;
case KeyEvent.VK_9:
return NativeKeyEvent.VC_9;
case KeyEvent.VK_0:
return NativeKeyEvent.VC_0;
case KeyEvent.VK_MINUS:
return NativeKeyEvent.VC_MINUS;
case KeyEvent.VK_EQUALS:
return NativeKeyEvent.VC_EQUALS;
case KeyEvent.VK_BACK_SPACE:
return NativeKeyEvent.VC_BACKSPACE;
case KeyEvent.VK_TAB:
return NativeKeyEvent.VC_TAB;
case KeyEvent.VK_CAPS_LOCK:
return NativeKeyEvent.VC_CAPS_LOCK;
case KeyEvent.VK_A:
return NativeKeyEvent.VC_A;
case KeyEvent.VK_B:
return NativeKeyEvent.VC_B;
case KeyEvent.VK_C:
return NativeKeyEvent.VC_C;
case KeyEvent.VK_D:
return NativeKeyEvent.VC_D;
case KeyEvent.VK_E:
return NativeKeyEvent.VC_E;
case KeyEvent.VK_F:
return NativeKeyEvent.VC_F;
case KeyEvent.VK_G:
return NativeKeyEvent.VC_G;
case KeyEvent.VK_H:
return NativeKeyEvent.VC_H;
case KeyEvent.VK_I:
return NativeKeyEvent.VC_I;
case KeyEvent.VK_J:
return NativeKeyEvent.VC_J;
case KeyEvent.VK_K:
return NativeKeyEvent.VC_K;
case KeyEvent.VK_L:
return NativeKeyEvent.VC_L;
case KeyEvent.VK_M:
return NativeKeyEvent.VC_M;
case KeyEvent.VK_N:
return NativeKeyEvent.VC_N;
case KeyEvent.VK_O:
return NativeKeyEvent.VC_O;
case KeyEvent.VK_P:
return NativeKeyEvent.VC_P;
case KeyEvent.VK_Q:
return NativeKeyEvent.VC_Q;
case KeyEvent.VK_R:
return NativeKeyEvent.VC_R;
case KeyEvent.VK_S:
return NativeKeyEvent.VC_S;
case KeyEvent.VK_T:
return NativeKeyEvent.VC_T;
case KeyEvent.VK_U:
return NativeKeyEvent.VC_U;
case KeyEvent.VK_V:
return NativeKeyEvent.VC_V;
case KeyEvent.VK_W:
return NativeKeyEvent.VC_W;
case KeyEvent.VK_X:
return NativeKeyEvent.VC_X;
case KeyEvent.VK_Y:
return NativeKeyEvent.VC_Y;
case KeyEvent.VK_Z:
return NativeKeyEvent.VC_Z;
case KeyEvent.VK_OPEN_BRACKET:
return NativeKeyEvent.VC_OPEN_BRACKET;
case KeyEvent.VK_CLOSE_BRACKET:
return NativeKeyEvent.VC_CLOSE_BRACKET;
case KeyEvent.VK_BACK_SLASH:
return NativeKeyEvent.VC_BACK_SLASH;
case KeyEvent.VK_SEMICOLON:
return NativeKeyEvent.VC_SEMICOLON;
case KeyEvent.VK_QUOTE:
return NativeKeyEvent.VC_QUOTE;
case KeyEvent.VK_ENTER:
return NativeKeyEvent.VC_ENTER;
case KeyEvent.VK_COMMA:
return NativeKeyEvent.VC_COMMA;
case KeyEvent.VK_PERIOD:
return NativeKeyEvent.VC_PERIOD;
case KeyEvent.VK_SLASH:
return NativeKeyEvent.VC_SLASH;
case KeyEvent.VK_SPACE:
return NativeKeyEvent.VC_SPACE;
case KeyEvent.VK_PRINTSCREEN:
return NativeKeyEvent.VC_PRINTSCREEN;
case KeyEvent.VK_SCROLL_LOCK:
return NativeKeyEvent.VC_SCROLL_LOCK;
case KeyEvent.VK_PAUSE:
return NativeKeyEvent.VC_PAUSE;
case KeyEvent.VK_INSERT:
return NativeKeyEvent.VC_INSERT;
case KeyEvent.VK_DELETE:
return NativeKeyEvent.VC_DELETE;
case KeyEvent.VK_HOME:
return NativeKeyEvent.VC_HOME;
case KeyEvent.VK_END:
return NativeKeyEvent.VC_END;
case KeyEvent.VK_PAGE_UP:
return NativeKeyEvent.VC_PAGE_UP;
case KeyEvent.VK_PAGE_DOWN:
return NativeKeyEvent.VC_PAGE_DOWN;
case KeyEvent.VK_UP:
return NativeKeyEvent.VC_UP;
case KeyEvent.VK_LEFT:
return NativeKeyEvent.VC_LEFT;
case KeyEvent.VK_CLEAR:
return NativeKeyEvent.VC_CLEAR;
case KeyEvent.VK_RIGHT:
return NativeKeyEvent.VC_RIGHT;
case KeyEvent.VK_DOWN:
return NativeKeyEvent.VC_DOWN;
case KeyEvent.VK_NUM_LOCK:
return NativeKeyEvent.VC_NUM_LOCK;
case KeyEvent.VK_SEPARATOR:
return NativeKeyEvent.VC_SEPARATOR;
case KeyEvent.VK_SHIFT:
return NativeKeyEvent.VC_SHIFT;
case KeyEvent.VK_CONTROL:
return NativeKeyEvent.VC_CONTROL;
case KeyEvent.VK_ALT:
return NativeKeyEvent.VC_ALT;
case KeyEvent.VK_META:
return NativeKeyEvent.VC_META;
case KeyEvent.VK_CONTEXT_MENU:
return NativeKeyEvent.VC_CONTEXT_MENU;
case KeyEvent.VK_KATAKANA:
return NativeKeyEvent.VC_KATAKANA;
case KeyEvent.VK_UNDERSCORE:
return NativeKeyEvent.VC_UNDERSCORE;
case KeyEvent.VK_KANJI:
return NativeKeyEvent.VC_KANJI;
case KeyEvent.VK_HIRAGANA:
return NativeKeyEvent.VC_HIRAGANA;
case KeyEvent.VK_HELP:
return NativeKeyEvent.VC_SUN_HELP;
case KeyEvent.VK_STOP:
return NativeKeyEvent.VC_SUN_STOP;
case KeyEvent.VK_PROPS:
return NativeKeyEvent.VC_SUN_PROPS;
case KeyEvent.VK_FIND:
return NativeKeyEvent.VC_SUN_FIND;
case KeyEvent.VK_AGAIN:
return NativeKeyEvent.VC_SUN_AGAIN;
case KeyEvent.VK_COPY:
return NativeKeyEvent.VC_SUN_COPY;
case KeyEvent.VK_CUT:
return NativeKeyEvent.VC_SUN_CUT;

default:
return keyCode;
}
}

}
4 changes: 3 additions & 1 deletion src/main/java/ninjabrainbot/io/KeyPresser.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ public KeyPresser() throws AWTException {
}
}

public void paste(String s) {
public void paste(String s) throws InterruptedException {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
StringSelection selection = new StringSelection(s);
clipboard.setContents(selection, null);
robot.keyPress(CTRL_KEYCODE);
Thread.sleep(50);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(CTRL_KEYCODE);
Thread.sleep(50);
robot.keyRelease(KeyEvent.VK_V);
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/ninjabrainbot/io/KeyboardListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ public void nativeKeyPressed(NativeKeyEvent e) {
if (c == NativeKeyEvent.VC_SHIFT || c == NativeKeyEvent.VC_CONTROL || c == NativeKeyEvent.VC_ALT)
return;
if (consumer != null) {
consumer.accept(e.getRawCode(), e.getModifiers());
consumer.accept(c, e.getModifiers());
consumer = null;
return;
}
for (HotkeyPreference h : HotkeyPreference.hotkeys) {
if (h.getCode() == e.getRawCode() && (h.getModifier() & e.getModifiers()) == h.getModifier()) {
if (h.getCode() == c && (h.getModifier() & e.getModifiers()) == h.getModifier()) {
h.execute();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package ninjabrainbot.io.preferences;

import com.github.kwhat.jnativehook.keyboard.NativeKeyEvent;
import ninjabrainbot.io.KeyConverter;
import ninjabrainbot.io.preferences.enums.AllAdvancementsToggleType;
import ninjabrainbot.io.preferences.enums.MainViewType;
import ninjabrainbot.io.preferences.enums.McVersion;
import ninjabrainbot.io.preferences.enums.SizeSetting;
import ninjabrainbot.io.preferences.enums.StrongholdDisplayType;
import ninjabrainbot.util.Assert;

public class NinjabrainBotPreferences {

Expand All @@ -13,6 +16,7 @@ public class NinjabrainBotPreferences {
public final IntPreference windowX;
public final IntPreference windowY;
public final IntPreference theme;
private final IntPreference settingsVersion;
public final HotkeyPreference hotkeyIncrement;
public final HotkeyPreference hotkeyDecrement;
public final HotkeyPreference hotkeyBoat;
Expand Down Expand Up @@ -68,6 +72,7 @@ public NinjabrainBotPreferences(IPreferenceSource source) {
windowX = new IntPreference("window_x", 100, source);
windowY = new IntPreference("window_y", 100, source);
theme = new IntPreference("theme", 1, source);
settingsVersion = new IntPreference("settings_version", 0, source);
// Hotkey
hotkeyIncrement = new HotkeyPreference("hotkey_increment", source);
hotkeyDecrement = new HotkeyPreference("hotkey_decrement", source);
Expand Down Expand Up @@ -123,6 +128,22 @@ public NinjabrainBotPreferences(IPreferenceSource source) {
view = new MultipleChoicePreference<>("view", MainViewType.BASIC, new int[] { 0, 1 }, new MainViewType[] { MainViewType.BASIC, MainViewType.DETAILED }, source);
mcVersion = new MultipleChoicePreference<>("mc_version", McVersion.PRE_119, new int[] { 0, 1 }, new McVersion[] { McVersion.PRE_119, McVersion.POST_119 }, source);
allAdvancementsToggleType = new MultipleChoicePreference<>("aa_toggle_type", AllAdvancementsToggleType.Automatic, new int[] { 0, 1 }, new AllAdvancementsToggleType[] { AllAdvancementsToggleType.Automatic, AllAdvancementsToggleType.Hotkey }, source);

// Upgrade if necessary
if (settingsVersion.get() == 0) {
upgradeSettings_From_0_To_1();
}
Assert.isEqual(settingsVersion.get(), 1);
}

private void upgradeSettings_From_0_To_1(){
for (HotkeyPreference hotkeyPreference : HotkeyPreference.hotkeys){
if (hotkeyPreference.getCode() == -1)
continue;
int nativeKeyCode = KeyConverter.convertKeyCodeToNativeKeyCode(hotkeyPreference.getCode());
hotkeyPreference.setCode(nativeKeyCode);
}
settingsVersion.set(1);
}

}
Loading

0 comments on commit 88e7dd0

Please sign in to comment.