Skip to content

Commit

Permalink
Rework controls and add option to allow repeating macros to resume
Browse files Browse the repository at this point in the history
  • Loading branch information
NotRyken committed Jan 29, 2025
1 parent 2bb87d5 commit a6d34e6
Show file tree
Hide file tree
Showing 14 changed files with 307 additions and 119 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 2.3.6

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

## 2.3.5

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ public static void type(String message) {
public static void send(boolean type, String message, boolean addToHistory, boolean showHudMsg) {
Minecraft mc = Minecraft.getInstance();
if (mc.player == null) return;
if (!mc.player.connection.isAcceptingMessages()) return;
Pair<String,Integer> result = PlaceholderUtil.replace(message);
message = result.getFirst();
int faults = result.getSecond();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ public Profile activeProfile() {
* active.
*/
public void activateProfile(int index) {
profiles.getFirst().getMacros().forEach(Macro::stopRepeating);
profiles.getFirst().getMacros().forEach((macro) -> {
if (!macro.resumeRepeatingStatus) macro.stopRepeating();
});
if (index != 0) {
profiles.addFirst(profiles.remove(index));
if (index == spDefault) spDefault = 0;
Expand Down
95 changes: 66 additions & 29 deletions common/src/main/java/dev/terminalmc/commandkeys/config/Macro.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,21 @@
* a list of {@link Message} instances.
*/
public class Macro {
public final int version = 4;
public final int version = 5;

public static final Random RANDOM = new Random();

boolean addToHistory;
public transient boolean historyEnabled;
public transient boolean addToHistoryStatus;
boolean showHudMessage;
public transient boolean hudMessageEnabled;

public boolean ignoreRatelimit;
public transient boolean showHudMessageStatus;
boolean resumeRepeating;
public transient boolean resumeRepeatingStatus;
boolean useRatelimit;
public transient boolean useRatelimitStatus;

ConflictStrategy conflictStrategy;

public enum ConflictStrategy {
SUBMIT,
ASSERT,
Expand Down Expand Up @@ -78,31 +81,45 @@ public enum SendMode {
* Creates a default empty instance.
*/
public Macro() {
this.addToHistory = false;
this.showHudMessage = false;
this.ignoreRatelimit = false;
this.conflictStrategy = Config.get().defaultConflictStrategy;
this.sendMode = Config.get().defaultSendMode;
this.spaceTicks = 0;
this.cycleIndex = 0;
this.keybind = new Keybind();
this.altKeybind = new Keybind();
this.messages = new ArrayList<>();
this(
false,
false,
false,
false,
Config.get().defaultConflictStrategy,
Config.get().defaultSendMode,
0,
0,
new Keybind(),
new Keybind(),
new ArrayList<>()
);
}

/**
* Not validated, only for use by self-validating deserializer.
*/
private Macro(boolean addToHistory, boolean showHudMessage, boolean ignoreRatelimit,
ConflictStrategy conflictStrategy, SendMode sendMode, int spaceTicks,
Keybind keybind, Keybind altKeybind, List<Message> messages) {
private Macro(
boolean addToHistory,
boolean showHudMessage,
boolean resumeRepeating,
boolean useRatelimit,
ConflictStrategy conflictStrategy,
SendMode sendMode,
int spaceTicks,
int cycleIndex,
Keybind keybind,
Keybind altKeybind,
List<Message> messages
) {
this.addToHistory = addToHistory;
this.showHudMessage = showHudMessage;
this.ignoreRatelimit = ignoreRatelimit;
this.resumeRepeating = resumeRepeating;
this.useRatelimit = useRatelimit;
this.conflictStrategy = conflictStrategy;
this.sendMode = sendMode;
this.spaceTicks = spaceTicks;
this.cycleIndex = 0;
this.cycleIndex = cycleIndex;
this.keybind = keybind;
this.altKeybind = altKeybind;
this.messages = messages;
Expand All @@ -116,6 +133,14 @@ public boolean getShowHudMessage() {
return showHudMessage;
}

public boolean getResumeRepeating() {
return resumeRepeating;
}

public boolean getUseRatelimit() {
return useRatelimit;
}

public ConflictStrategy getStrategy() {
return conflictStrategy;
}
Expand Down Expand Up @@ -193,8 +218,8 @@ public void trigger(@Nullable Keybind trigger) {
int cumulativeDelay = standardDelay ? -spaceTicks : 0;
for (Message msg : messages) {
cumulativeDelay += standardDelay ? spaceTicks : msg.delayTicks;
schedule(cumulativeDelay, -1, msg.string,
historyEnabled, hudMessageEnabled);
schedule(cumulativeDelay, -1, msg.string,
addToHistoryStatus, showHudMessageStatus);
}
}
case TYPE -> {
Expand All @@ -212,24 +237,24 @@ public void trigger(@Nullable Keybind trigger) {
// Allow spacer blank messages, and multiple messages per press.
for (String msg : messages.get(cycleIndex).string.split(",,")) {
if (!msg.isBlank()) {
CommandKeys.send(msg, historyEnabled, hudMessageEnabled);
CommandKeys.send(msg, addToHistoryStatus, showHudMessageStatus);
}
}
}
case RANDOM -> {
if (!messages.isEmpty()) {
Message msg = messages.get(RANDOM.nextInt(messages.size()));
if (!msg.string.isBlank()) {
CommandKeys.send(msg.string, historyEnabled, hudMessageEnabled);
CommandKeys.send(msg.string, addToHistoryStatus, showHudMessageStatus);
}
}
}
case REPEAT -> {
int cumulativeDelay = 0;
for (Message msg : messages) {
cumulativeDelay += msg.delayTicks;
schedule(cumulativeDelay, spaceTicks, msg.string,
historyEnabled, hudMessageEnabled);
schedule(cumulativeDelay, spaceTicks, msg.string,
addToHistoryStatus, showHudMessageStatus);
}
}
}
Expand Down Expand Up @@ -305,7 +330,8 @@ public Macro deserialize(JsonElement json, Type typeOfT, JsonDeserializationCont

boolean addToHistory = version >= 3 ? obj.get("addToHistory").getAsBoolean() : false;
boolean showHudMessage = version >= 3 ? obj.get("showHudMessage").getAsBoolean() : false;
boolean ignoreRatelimit = version >= 4 ? obj.get("ignoreRatelimit").getAsBoolean() : false;
boolean resumeRepeating = version >= 5 ? obj.get("resumeRepeating").getAsBoolean() : false;
boolean useRatelimit = version >= 4 ? obj.get("useRatelimit").getAsBoolean() : false;

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

return new Macro(addToHistory, showHudMessage, ignoreRatelimit, conflictStrategy,
sendMode, spaceTicks, keybind, altKeybind, messages);
return new Macro(
addToHistory,
showHudMessage,
resumeRepeating,
useRatelimit,
conflictStrategy,
sendMode,
spaceTicks,
0,
keybind,
altKeybind,
messages
);
}

public static ConflictStrategy getConflictStrategy(String str) {
Expand Down
100 changes: 90 additions & 10 deletions common/src/main/java/dev/terminalmc/commandkeys/config/Profile.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
* </p>
*/
public class Profile {
public final int version = 3;
public final int version = 4;

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

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

// Behavior controls
public static final Control addToHistoryDefault = Control.OFF;
private Control addToHistory;
public static final Control showHudMessageDefault = Control.OFF;
private Control showHudMessage;
public static final Control resumeRepeatingDefault = Control.OFF;
private Control resumeRepeating;
public static final Control useRatelimitDefault = Control.ON;
private Control useRatelimit;
public enum Control {
ON,
OFF,
Expand All @@ -67,22 +73,39 @@ public enum Control {
* Creates a default empty instance.
*/
public Profile() {
this("", new ArrayList<>(), Control.OFF, Control.OFF, new ArrayList<>());
this("");
}

public Profile(String name) {
this(name, new ArrayList<>(), Control.OFF, Control.OFF, new ArrayList<>());
this(
name,
new ArrayList<>(),
addToHistoryDefault,
showHudMessageDefault,
resumeRepeatingDefault,
useRatelimitDefault,
new ArrayList<>()
);
}

/**
* Not validated, only for use by self-validating deserializer.
*/
private Profile(String name, List<String> links, Control addToHistory,
Control showHudMessage, List<Macro> macros) {
private Profile(
String name,
List<String> links,
Control addToHistory,
Control showHudMessage,
Control resumeRepeating,
Control useRatelimit,
List<Macro> macros
) {
this.name = name;
this.links = links;
this.addToHistory = addToHistory;
this.showHudMessage = showHudMessage;
this.resumeRepeating = resumeRepeating;
this.useRatelimit = useRatelimit;
this.macros = macros;
// Add missing links to map
this.links.removeIf((link) -> LINK_PROFILE_MAP.putIfAbsent(link, this) != null);
Expand All @@ -96,6 +119,8 @@ private Profile(String name, List<String> links, Control addToHistory,
this.links = new ArrayList<>();
this.addToHistory = profile.addToHistory;
this.showHudMessage = profile.showHudMessage;
this.resumeRepeating = profile.resumeRepeating;
this.useRatelimit = profile.useRatelimit;
this.macros = profile.macros;
}

Expand Down Expand Up @@ -157,6 +182,24 @@ public void setShowHudMessage(Control showHudMessage) {
this.showHudMessage = showHudMessage;
macros.forEach((macro) -> setShowHudMessage(macro, macro.showHudMessage));
}

public Control getResumeRepeating() {
return resumeRepeating;
}

public void setResumeRepeating(Control resumeRepeating) {
this.resumeRepeating = resumeRepeating;
macros.forEach((macro) -> setResumeRepeating(macro, macro.resumeRepeating));
}

public Control getUseRatelimit() {
return useRatelimit;
}

public void setUseRatelimit(Control useRatelimit) {
this.useRatelimit = useRatelimit;
macros.forEach((macro) -> setUseRatelimit(macro, macro.useRatelimit));
}

// Macro management

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

public void setAddToHistory(Macro macro, boolean value) {
macro.addToHistory = value;
macro.historyEnabled = switch(this.addToHistory) {
macro.addToHistoryStatus = switch(this.addToHistory) {
case ON -> true;
case OFF -> false;
case DEFER -> macro.addToHistory;
Expand All @@ -261,13 +304,31 @@ public void setAddToHistory(Macro macro, boolean value) {

public void setShowHudMessage(Macro macro, boolean value) {
macro.showHudMessage = value;
macro.hudMessageEnabled = switch(this.showHudMessage) {
macro.showHudMessageStatus = switch(this.showHudMessage) {
case ON -> true;
case OFF -> false;
case DEFER -> macro.showHudMessage;
};
}

public void setResumeRepeating(Macro macro, boolean value) {
macro.resumeRepeating = value;
macro.resumeRepeatingStatus = switch(this.resumeRepeating) {
case ON -> true;
case OFF -> false;
case DEFER -> macro.resumeRepeating;
};
}

public void setUseRatelimit(Macro macro, boolean value) {
macro.useRatelimit = value;
macro.useRatelimitStatus = switch(this.useRatelimit) {
case ON -> true;
case OFF -> false;
case DEFER -> macro.useRatelimit;
};
}

// Cleanup and validation

void cleanup() {
Expand All @@ -281,6 +342,11 @@ void cleanup() {
!macro.sendMode.equals(Macro.SendMode.TYPE)) {
macro.messages.removeIf((msg) -> msg.string.isBlank());
}
// Update transients in macros
setAddToHistory(addToHistory);
setShowHudMessage(showHudMessage);
setResumeRepeating(resumeRepeating);
setUseRatelimit(useRatelimit);
return macro.messages.isEmpty();
});
}
Expand All @@ -301,15 +367,29 @@ public Profile deserialize(JsonElement json, Type typeOfT, JsonDeserializationCo
}
Control addToHistory = version >= 2
? Control.valueOf(obj.get("addToHistory").getAsString())
: Control.OFF;
: addToHistoryDefault;
Control showHudMessage = version >= 2
? Control.valueOf(obj.get("showHudMessage").getAsString())
: Control.OFF;
: showHudMessageDefault;
Control resumeRepeating = version >= 4
? Control.valueOf(obj.get("resumeRepeating").getAsString())
: resumeRepeatingDefault;
Control useRatelimit = version >= 4
? Control.valueOf(obj.get("useRatelimit").getAsString())
: useRatelimitDefault;

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

Profile profile = new Profile(name, addresses, addToHistory, showHudMessage, macros);
Profile profile = new Profile(
name,
addresses,
addToHistory,
showHudMessage,
resumeRepeating,
useRatelimit,
macros
);
for (JsonElement je : obj.getAsJsonArray(version >= 2 ? "macros" : "commandKeys")) {
macros.add(ctx.deserialize(je, Macro.class));
}
Expand Down
Loading

0 comments on commit a6d34e6

Please sign in to comment.