Skip to content

Commit a6d34e6

Browse files
committed
Rework controls and add option to allow repeating macros to resume
1 parent 2bb87d5 commit a6d34e6

14 files changed

Lines changed: 307 additions & 119 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 2.3.6
44

55
- Added high contrast button textures
6+
- Added option to allow repeating macros to resume on re-activation of profile
67

78
## 2.3.5
89

common/src/main/java/dev/terminalmc/commandkeys/CommandKeys.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ public static void type(String message) {
134134
public static void send(boolean type, String message, boolean addToHistory, boolean showHudMsg) {
135135
Minecraft mc = Minecraft.getInstance();
136136
if (mc.player == null) return;
137+
if (!mc.player.connection.isAcceptingMessages()) return;
137138
Pair<String,Integer> result = PlaceholderUtil.replace(message);
138139
message = result.getFirst();
139140
int faults = result.getSecond();

common/src/main/java/dev/terminalmc/commandkeys/config/Config.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,9 @@ public Profile activeProfile() {
155155
* active.
156156
*/
157157
public void activateProfile(int index) {
158-
profiles.getFirst().getMacros().forEach(Macro::stopRepeating);
158+
profiles.getFirst().getMacros().forEach((macro) -> {
159+
if (!macro.resumeRepeatingStatus) macro.stopRepeating();
160+
});
159161
if (index != 0) {
160162
profiles.addFirst(profiles.remove(index));
161163
if (index == spDefault) spDefault = 0;

common/src/main/java/dev/terminalmc/commandkeys/config/Macro.java

Lines changed: 66 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,21 @@
3232
* a list of {@link Message} instances.
3333
*/
3434
public class Macro {
35-
public final int version = 4;
35+
public final int version = 5;
3636

3737
public static final Random RANDOM = new Random();
3838

3939
boolean addToHistory;
40-
public transient boolean historyEnabled;
40+
public transient boolean addToHistoryStatus;
4141
boolean showHudMessage;
42-
public transient boolean hudMessageEnabled;
43-
44-
public boolean ignoreRatelimit;
42+
public transient boolean showHudMessageStatus;
43+
boolean resumeRepeating;
44+
public transient boolean resumeRepeatingStatus;
45+
boolean useRatelimit;
46+
public transient boolean useRatelimitStatus;
4547

4648
ConflictStrategy conflictStrategy;
49+
4750
public enum ConflictStrategy {
4851
SUBMIT,
4952
ASSERT,
@@ -78,31 +81,45 @@ public enum SendMode {
7881
* Creates a default empty instance.
7982
*/
8083
public Macro() {
81-
this.addToHistory = false;
82-
this.showHudMessage = false;
83-
this.ignoreRatelimit = false;
84-
this.conflictStrategy = Config.get().defaultConflictStrategy;
85-
this.sendMode = Config.get().defaultSendMode;
86-
this.spaceTicks = 0;
87-
this.cycleIndex = 0;
88-
this.keybind = new Keybind();
89-
this.altKeybind = new Keybind();
90-
this.messages = new ArrayList<>();
84+
this(
85+
false,
86+
false,
87+
false,
88+
false,
89+
Config.get().defaultConflictStrategy,
90+
Config.get().defaultSendMode,
91+
0,
92+
0,
93+
new Keybind(),
94+
new Keybind(),
95+
new ArrayList<>()
96+
);
9197
}
9298

9399
/**
94100
* Not validated, only for use by self-validating deserializer.
95101
*/
96-
private Macro(boolean addToHistory, boolean showHudMessage, boolean ignoreRatelimit,
97-
ConflictStrategy conflictStrategy, SendMode sendMode, int spaceTicks,
98-
Keybind keybind, Keybind altKeybind, List<Message> messages) {
102+
private Macro(
103+
boolean addToHistory,
104+
boolean showHudMessage,
105+
boolean resumeRepeating,
106+
boolean useRatelimit,
107+
ConflictStrategy conflictStrategy,
108+
SendMode sendMode,
109+
int spaceTicks,
110+
int cycleIndex,
111+
Keybind keybind,
112+
Keybind altKeybind,
113+
List<Message> messages
114+
) {
99115
this.addToHistory = addToHistory;
100116
this.showHudMessage = showHudMessage;
101-
this.ignoreRatelimit = ignoreRatelimit;
117+
this.resumeRepeating = resumeRepeating;
118+
this.useRatelimit = useRatelimit;
102119
this.conflictStrategy = conflictStrategy;
103120
this.sendMode = sendMode;
104121
this.spaceTicks = spaceTicks;
105-
this.cycleIndex = 0;
122+
this.cycleIndex = cycleIndex;
106123
this.keybind = keybind;
107124
this.altKeybind = altKeybind;
108125
this.messages = messages;
@@ -116,6 +133,14 @@ public boolean getShowHudMessage() {
116133
return showHudMessage;
117134
}
118135

136+
public boolean getResumeRepeating() {
137+
return resumeRepeating;
138+
}
139+
140+
public boolean getUseRatelimit() {
141+
return useRatelimit;
142+
}
143+
119144
public ConflictStrategy getStrategy() {
120145
return conflictStrategy;
121146
}
@@ -193,8 +218,8 @@ public void trigger(@Nullable Keybind trigger) {
193218
int cumulativeDelay = standardDelay ? -spaceTicks : 0;
194219
for (Message msg : messages) {
195220
cumulativeDelay += standardDelay ? spaceTicks : msg.delayTicks;
196-
schedule(cumulativeDelay, -1, msg.string,
197-
historyEnabled, hudMessageEnabled);
221+
schedule(cumulativeDelay, -1, msg.string,
222+
addToHistoryStatus, showHudMessageStatus);
198223
}
199224
}
200225
case TYPE -> {
@@ -212,24 +237,24 @@ public void trigger(@Nullable Keybind trigger) {
212237
// Allow spacer blank messages, and multiple messages per press.
213238
for (String msg : messages.get(cycleIndex).string.split(",,")) {
214239
if (!msg.isBlank()) {
215-
CommandKeys.send(msg, historyEnabled, hudMessageEnabled);
240+
CommandKeys.send(msg, addToHistoryStatus, showHudMessageStatus);
216241
}
217242
}
218243
}
219244
case RANDOM -> {
220245
if (!messages.isEmpty()) {
221246
Message msg = messages.get(RANDOM.nextInt(messages.size()));
222247
if (!msg.string.isBlank()) {
223-
CommandKeys.send(msg.string, historyEnabled, hudMessageEnabled);
248+
CommandKeys.send(msg.string, addToHistoryStatus, showHudMessageStatus);
224249
}
225250
}
226251
}
227252
case REPEAT -> {
228253
int cumulativeDelay = 0;
229254
for (Message msg : messages) {
230255
cumulativeDelay += msg.delayTicks;
231-
schedule(cumulativeDelay, spaceTicks, msg.string,
232-
historyEnabled, hudMessageEnabled);
256+
schedule(cumulativeDelay, spaceTicks, msg.string,
257+
addToHistoryStatus, showHudMessageStatus);
233258
}
234259
}
235260
}
@@ -305,7 +330,8 @@ public Macro deserialize(JsonElement json, Type typeOfT, JsonDeserializationCont
305330

306331
boolean addToHistory = version >= 3 ? obj.get("addToHistory").getAsBoolean() : false;
307332
boolean showHudMessage = version >= 3 ? obj.get("showHudMessage").getAsBoolean() : false;
308-
boolean ignoreRatelimit = version >= 4 ? obj.get("ignoreRatelimit").getAsBoolean() : false;
333+
boolean resumeRepeating = version >= 5 ? obj.get("resumeRepeating").getAsBoolean() : false;
334+
boolean useRatelimit = version >= 4 ? obj.get("useRatelimit").getAsBoolean() : false;
309335

310336
ConflictStrategy conflictStrategy = version >= 3
311337
? ConflictStrategy.valueOf(obj.get("conflictStrategy").getAsString())
@@ -340,8 +366,19 @@ public Macro deserialize(JsonElement json, Type typeOfT, JsonDeserializationCont
340366
// Validate
341367
if (spaceTicks < 0) throw new JsonParseException("Macro Error: spaceTicks < 0");
342368

343-
return new Macro(addToHistory, showHudMessage, ignoreRatelimit, conflictStrategy,
344-
sendMode, spaceTicks, keybind, altKeybind, messages);
369+
return new Macro(
370+
addToHistory,
371+
showHudMessage,
372+
resumeRepeating,
373+
useRatelimit,
374+
conflictStrategy,
375+
sendMode,
376+
spaceTicks,
377+
0,
378+
keybind,
379+
altKeybind,
380+
messages
381+
);
345382
}
346383

347384
public static ConflictStrategy getConflictStrategy(String str) {

common/src/main/java/dev/terminalmc/commandkeys/config/Profile.java

Lines changed: 90 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
* </p>
3939
*/
4040
public class Profile {
41-
public final int version = 3;
41+
public final int version = 4;
4242

4343
public static final Map<String, Profile> LINK_PROFILE_MAP = new HashMap<>();
4444

@@ -52,8 +52,14 @@ public class Profile {
5252
private final List<String> links;
5353

5454
// Behavior controls
55+
public static final Control addToHistoryDefault = Control.OFF;
5556
private Control addToHistory;
57+
public static final Control showHudMessageDefault = Control.OFF;
5658
private Control showHudMessage;
59+
public static final Control resumeRepeatingDefault = Control.OFF;
60+
private Control resumeRepeating;
61+
public static final Control useRatelimitDefault = Control.ON;
62+
private Control useRatelimit;
5763
public enum Control {
5864
ON,
5965
OFF,
@@ -67,22 +73,39 @@ public enum Control {
6773
* Creates a default empty instance.
6874
*/
6975
public Profile() {
70-
this("", new ArrayList<>(), Control.OFF, Control.OFF, new ArrayList<>());
76+
this("");
7177
}
7278

7379
public Profile(String name) {
74-
this(name, new ArrayList<>(), Control.OFF, Control.OFF, new ArrayList<>());
80+
this(
81+
name,
82+
new ArrayList<>(),
83+
addToHistoryDefault,
84+
showHudMessageDefault,
85+
resumeRepeatingDefault,
86+
useRatelimitDefault,
87+
new ArrayList<>()
88+
);
7589
}
7690

7791
/**
7892
* Not validated, only for use by self-validating deserializer.
7993
*/
80-
private Profile(String name, List<String> links, Control addToHistory,
81-
Control showHudMessage, List<Macro> macros) {
94+
private Profile(
95+
String name,
96+
List<String> links,
97+
Control addToHistory,
98+
Control showHudMessage,
99+
Control resumeRepeating,
100+
Control useRatelimit,
101+
List<Macro> macros
102+
) {
82103
this.name = name;
83104
this.links = links;
84105
this.addToHistory = addToHistory;
85106
this.showHudMessage = showHudMessage;
107+
this.resumeRepeating = resumeRepeating;
108+
this.useRatelimit = useRatelimit;
86109
this.macros = macros;
87110
// Add missing links to map
88111
this.links.removeIf((link) -> LINK_PROFILE_MAP.putIfAbsent(link, this) != null);
@@ -96,6 +119,8 @@ private Profile(String name, List<String> links, Control addToHistory,
96119
this.links = new ArrayList<>();
97120
this.addToHistory = profile.addToHistory;
98121
this.showHudMessage = profile.showHudMessage;
122+
this.resumeRepeating = profile.resumeRepeating;
123+
this.useRatelimit = profile.useRatelimit;
99124
this.macros = profile.macros;
100125
}
101126

@@ -157,6 +182,24 @@ public void setShowHudMessage(Control showHudMessage) {
157182
this.showHudMessage = showHudMessage;
158183
macros.forEach((macro) -> setShowHudMessage(macro, macro.showHudMessage));
159184
}
185+
186+
public Control getResumeRepeating() {
187+
return resumeRepeating;
188+
}
189+
190+
public void setResumeRepeating(Control resumeRepeating) {
191+
this.resumeRepeating = resumeRepeating;
192+
macros.forEach((macro) -> setResumeRepeating(macro, macro.resumeRepeating));
193+
}
194+
195+
public Control getUseRatelimit() {
196+
return useRatelimit;
197+
}
198+
199+
public void setUseRatelimit(Control useRatelimit) {
200+
this.useRatelimit = useRatelimit;
201+
macros.forEach((macro) -> setUseRatelimit(macro, macro.useRatelimit));
202+
}
160203

161204
// Macro management
162205

@@ -252,7 +295,7 @@ public void setLimitKey(Macro macro, Keybind keybind, InputConstants.Key key) {
252295

253296
public void setAddToHistory(Macro macro, boolean value) {
254297
macro.addToHistory = value;
255-
macro.historyEnabled = switch(this.addToHistory) {
298+
macro.addToHistoryStatus = switch(this.addToHistory) {
256299
case ON -> true;
257300
case OFF -> false;
258301
case DEFER -> macro.addToHistory;
@@ -261,13 +304,31 @@ public void setAddToHistory(Macro macro, boolean value) {
261304

262305
public void setShowHudMessage(Macro macro, boolean value) {
263306
macro.showHudMessage = value;
264-
macro.hudMessageEnabled = switch(this.showHudMessage) {
307+
macro.showHudMessageStatus = switch(this.showHudMessage) {
265308
case ON -> true;
266309
case OFF -> false;
267310
case DEFER -> macro.showHudMessage;
268311
};
269312
}
270313

314+
public void setResumeRepeating(Macro macro, boolean value) {
315+
macro.resumeRepeating = value;
316+
macro.resumeRepeatingStatus = switch(this.resumeRepeating) {
317+
case ON -> true;
318+
case OFF -> false;
319+
case DEFER -> macro.resumeRepeating;
320+
};
321+
}
322+
323+
public void setUseRatelimit(Macro macro, boolean value) {
324+
macro.useRatelimit = value;
325+
macro.useRatelimitStatus = switch(this.useRatelimit) {
326+
case ON -> true;
327+
case OFF -> false;
328+
case DEFER -> macro.useRatelimit;
329+
};
330+
}
331+
271332
// Cleanup and validation
272333

273334
void cleanup() {
@@ -281,6 +342,11 @@ void cleanup() {
281342
!macro.sendMode.equals(Macro.SendMode.TYPE)) {
282343
macro.messages.removeIf((msg) -> msg.string.isBlank());
283344
}
345+
// Update transients in macros
346+
setAddToHistory(addToHistory);
347+
setShowHudMessage(showHudMessage);
348+
setResumeRepeating(resumeRepeating);
349+
setUseRatelimit(useRatelimit);
284350
return macro.messages.isEmpty();
285351
});
286352
}
@@ -301,15 +367,29 @@ public Profile deserialize(JsonElement json, Type typeOfT, JsonDeserializationCo
301367
}
302368
Control addToHistory = version >= 2
303369
? Control.valueOf(obj.get("addToHistory").getAsString())
304-
: Control.OFF;
370+
: addToHistoryDefault;
305371
Control showHudMessage = version >= 2
306372
? Control.valueOf(obj.get("showHudMessage").getAsString())
307-
: Control.OFF;
373+
: showHudMessageDefault;
374+
Control resumeRepeating = version >= 4
375+
? Control.valueOf(obj.get("resumeRepeating").getAsString())
376+
: resumeRepeatingDefault;
377+
Control useRatelimit = version >= 4
378+
? Control.valueOf(obj.get("useRatelimit").getAsString())
379+
: useRatelimitDefault;
308380

309381
// Deserialize CommandKey objects with link to deserialized Profile
310382
List<Macro> macros = new ArrayList<>();
311383

312-
Profile profile = new Profile(name, addresses, addToHistory, showHudMessage, macros);
384+
Profile profile = new Profile(
385+
name,
386+
addresses,
387+
addToHistory,
388+
showHudMessage,
389+
resumeRepeating,
390+
useRatelimit,
391+
macros
392+
);
313393
for (JsonElement je : obj.getAsJsonArray(version >= 2 ? "macros" : "commandKeys")) {
314394
macros.add(ctx.deserialize(je, Macro.class));
315395
}

0 commit comments

Comments
 (0)