1+ package namidevelopment .kiriyaga .api .core ;
2+
3+ import namidevelopment .kiriyaga .api .annotation .SubscribeEvent ;
4+ import namidevelopment .kiriyaga .api .core .macro .model .Macro ;
5+ import namidevelopment .kiriyaga .api .event .impl .KeyboardHandlerEvent ;
6+ import namidevelopment .kiriyaga .api .event .impl .MouseHandlerEvent ;
7+ import namidevelopment .kiriyaga .api .model .feature .Feature ;
8+ import namidevelopment .kiriyaga .api .model .setting .KeyBindSetting ;
9+
10+ import java .util .HashSet ;
11+ import java .util .Set ;
12+
13+ import static namidevelopment .kiriyaga .api .NamiApi .*;
14+
15+ public class KeyBindService {
16+ private final Set <Integer > pressedKeys = new HashSet <>();
17+
18+ public void init () {
19+ EVENT_SERVICE .register (this );
20+ }
21+
22+ @ SubscribeEvent
23+ public void onKeyboardHandler (KeyboardHandlerEvent event ) {
24+ if (MC .screen != null )
25+ return ;
26+ int key = event .getKey ();
27+ boolean handled = false ;
28+
29+ if (event .isPress ()) {
30+ pressedKeys .add (key );
31+ handled = handlePress (key );
32+ }
33+
34+ if (event .isRelease ()) {
35+ pressedKeys .remove (key );
36+ handled = handleRelease (key );
37+ }
38+
39+ if (handled )
40+ event .cancel ();
41+ }
42+
43+ @ SubscribeEvent
44+ public void onMouseHandler (MouseHandlerEvent event ) {
45+ if (MC .screen != null )
46+ return ;
47+ int key = event .getButton ();
48+ boolean handled = false ;
49+
50+ if (event .isPress ()) {
51+ pressedKeys .add (key );
52+ handled = handlePress (key );
53+ }
54+
55+ if (event .isRelease ()) {
56+ pressedKeys .remove (key );
57+ handled = handleRelease (key );
58+ }
59+
60+ if (handled )
61+ event .cancel ();
62+ }
63+
64+ private boolean handlePress (int key ) {
65+ boolean handled = false ;
66+ for (Feature feature : FEATURE_SERVICE .getStorage ().getAll ()) {
67+ KeyBindSetting bind = feature .getKeyBind ();
68+ if (bind == null )
69+ continue ;
70+ if (bind .get () != key )
71+ continue ;
72+
73+ handled = true ;
74+
75+ if (bind .isHoldMode ()) {
76+ feature .setEnabled (true );
77+ } else {
78+ feature .toggle ();
79+ }
80+ }
81+
82+ for (Macro macro : MACRO_SERVICE .getAll ()) {
83+ if (macro .getKeyCode () == key && MC .player != null ) {
84+ MC .player .connection .sendChat (macro .getMessage ());
85+ handled = true ;
86+ }
87+ }
88+
89+ return handled ;
90+ }
91+
92+ private boolean handleRelease (int key ) {
93+ boolean handled = false ;
94+ for (Feature feature : FEATURE_SERVICE .getStorage ().getAll ()) {
95+ KeyBindSetting bind = feature .getKeyBind ();
96+ if (bind == null )
97+ continue ;
98+
99+ if (bind .get () != key )
100+ continue ;
101+
102+ if (bind .isHoldMode ()) {
103+ feature .setEnabled (false );
104+ handled = true ;
105+ }
106+ }
107+ return handled ;
108+ }
109+
110+ public boolean isKeyDown (int key ) {
111+ return pressedKeys .contains (key );
112+ }
113+
114+ public boolean isPressed (KeyBindSetting bind ) {
115+ return bind != null && bind .isBound () && pressedKeys .contains (bind .get ());
116+ }
117+
118+ public boolean isPressedToggle (KeyBindSetting bind ) {
119+ if (bind == null || !bind .isBound ())
120+ return false ;
121+ if (pressedKeys .contains (bind .get ())) {
122+ pressedKeys .remove (bind .get ());
123+ return true ;
124+ }
125+
126+ return false ;
127+ }
128+ }
0 commit comments