-
Notifications
You must be signed in to change notification settings - Fork 10
/
AmecsKeyBinding.java
106 lines (95 loc) · 3.71 KB
/
AmecsKeyBinding.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
* Copyright 2020-2023 Siphalor
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.siphalor.amecs.api;
import de.siphalor.amecs.impl.duck.IKeyBinding;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.options.KeyBinding;
import net.minecraft.client.util.InputUtil;
import net.minecraft.util.Identifier;
/**
* A {@link net.minecraft.client.options.KeyBinding} base class to be used when you want to define modifiers keys as default
*/
@Environment(EnvType.CLIENT)
public class AmecsKeyBinding extends KeyBinding {
private final KeyModifiers defaultModifiers;
/**
* Constructs a new amecs keybinding. And because how the vanilla key binding works. It is automatically registered.
* <br>
* See {@link KeyBindingUtils#unregisterKeyBinding(KeyBinding)} for how to unregister it
* If you want to set the key's translationKey directly use {@link #AmecsKeyBinding(String, net.minecraft.client.util.InputUtil.Type, int, String, KeyModifiers)} instead
*
* @param id the id to use
* @param type the input type which triggers this keybinding
* @param code the default key code
* @param category the id of the category which should include this keybinding
* @param defaultModifiers the default modifiers
*/
public AmecsKeyBinding(Identifier id, InputUtil.Type type, int code, String category, KeyModifiers defaultModifiers) {
this("key." + id.getNamespace() + "." + id.getPath(), type, code, category, defaultModifiers);
}
/**
* Constructs a new amecs keybinding. And because how the vanilla key binding works. It is automatically registered.
* <br>
* See {@link KeyBindingUtils#unregisterKeyBinding(KeyBinding)} for how to unregister it
*
* @param id the id to use
* @param type the input type which triggers this keybinding
* @param code the default key code
* @param category the id of the category which should include this keybinding
* @param defaultModifiers the default modifiers
*/
public AmecsKeyBinding(String id, InputUtil.Type type, int code, String category, KeyModifiers defaultModifiers) {
super(id, type, code, category);
if (defaultModifiers == null || defaultModifiers == KeyModifiers.NO_MODIFIERS) {
defaultModifiers = new KeyModifiers(); // the modifiable version of: KeyModifiers.NO_MODIFIERS
}
this.defaultModifiers = defaultModifiers;
((IKeyBinding) this).amecs$getKeyModifiers().copyModifiers(this.defaultModifiers);
}
@Override
public void setPressed(boolean pressed) {
super.setPressed(pressed);
if (pressed) {
onPressed();
} else {
onReleased();
}
}
/**
* A convenience method which gets fired when the keybinding is used
*/
public void onPressed() {
}
/**
* A convenience method which gets fired when the keybinding is stopped being used
*/
public void onReleased() {
}
/**
* Resets this keybinding (triggered when the user clicks on the "Reset" button).
*/
public void resetKeyBinding() {
((IKeyBinding) this).amecs$getKeyModifiers().copyModifiers(defaultModifiers);
}
@Override
public boolean isDefault() {
return defaultModifiers.equals(((IKeyBinding) this).amecs$getKeyModifiers()) && super.isDefault();
}
public KeyModifiers getDefaultModifiers() {
return defaultModifiers;
}
}