Skip to content

Commit 0daaf52

Browse files
committed
Add a global-config length limit check on send
1 parent ae6ff25 commit 0daaf52

File tree

12 files changed

+139
-24
lines changed

12 files changed

+139
-24
lines changed

changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 2.3.11
4+
5+
- Added a configurable message length limit to mitigate being kicked when sending overlength messages
6+
37
## 2.3.10
48

59
- Execute non-delayed macros immediately to preserve relative ordering (Starjon)

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,13 @@ public static boolean canTrigger(InputConstants.Key key, boolean sendMessage) {
137137
if (sendMessage && ratelimitedKey != key) {
138138
Minecraft.getInstance().gui.getChat().addMessage(PREFIX.copy().append(
139139
localized(
140-
"message", "sendBlocked",
140+
"message", "blocked.ratelimit",
141141
key.getDisplayName().copy().withStyle(ChatFormatting.GRAY),
142142
Component.literal(String.valueOf(Config.get().getRatelimitCount()))
143143
.withStyle(ChatFormatting.GRAY),
144144
Component.literal(String.valueOf(Config.get().getRatelimitTicks()))
145145
.withStyle(ChatFormatting.GRAY)
146-
)
147-
.withStyle(ChatFormatting.RED)));
146+
).withStyle(ChatFormatting.RED)));
148147
ratelimitedKey = key;
149148
}
150149
if (Config.getAndSave().ratelimitStrict)
@@ -181,6 +180,16 @@ public static void send(
181180
if (faults == 0) {
182181
if (type) {
183182
mc.setScreen(new ChatScreen(message));
183+
} else if (message.length() > Config.get().getLengthLimitLength()) {
184+
MutableComponent msg = PREFIX.copy();
185+
msg.append(localized(
186+
"message", "blocked.lengthlimit",
187+
Component.literal(String.valueOf(message.length()))
188+
.withStyle(ChatFormatting.GRAY),
189+
Component.literal(String.valueOf(Config.get().getLengthLimitLength()))
190+
.withStyle(ChatFormatting.GRAY)
191+
).withStyle(ChatFormatting.RED));
192+
mc.gui.getChat().addMessage(msg);
184193
} else {
185194
// new ChatScreen("").handleChatInput(message, addToHistory)
186195
// could be slightly better for compat but costs performance.
@@ -202,8 +211,7 @@ public static void send(
202211
msg.append(localized(
203212
"message", "placeholderFault",
204213
Component.literal(message).withStyle(ChatFormatting.GRAY)
205-
)
206-
.withStyle(ChatFormatting.RED));
214+
).withStyle(ChatFormatting.RED));
207215
mc.gui.getChat().addMessage(msg);
208216
}
209217
}

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

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import dev.terminalmc.commandkeys.CommandKeys;
2121
import dev.terminalmc.commandkeys.platform.Services;
2222
import dev.terminalmc.commandkeys.util.JsonUtil;
23+
import net.minecraft.SharedConstants;
2324
import org.jetbrains.annotations.NotNull;
2425
import org.jetbrains.annotations.Nullable;
2526

@@ -90,6 +91,10 @@ public class Config {
9091
public boolean ratelimitSp;
9192
public static final boolean ratelimitSpDefault = false;
9293

94+
// Length limit options
95+
private int lengthLimitLength;
96+
public static final int lengthLimitLengthDefault = SharedConstants.MAX_CHAT_LENGTH;
97+
9398
/**
9499
* Creates a profile list with a single profile, set as both singleplayer and multiplayer
95100
* default.
@@ -105,7 +110,8 @@ public Config() {
105110
ratelimitCountDefault,
106111
ratelimitTicksDefault,
107112
ratelimitStrictDefault,
108-
ratelimitSpDefault
113+
ratelimitSpDefault,
114+
lengthLimitLengthDefault
109115
);
110116
}
111117

@@ -122,7 +128,8 @@ private Config(
122128
int ratelimitCount,
123129
int ratelimitTicks,
124130
boolean ratelimitStrict,
125-
boolean ratelimitSp
131+
boolean ratelimitSp,
132+
int lengthLimitLength
126133
) {
127134
this.profiles = profiles;
128135
this.spDefault = spDefault;
@@ -134,6 +141,7 @@ private Config(
134141
this.ratelimitTicks = ratelimitTicks;
135142
this.ratelimitStrict = ratelimitStrict;
136143
this.ratelimitSp = ratelimitSp;
144+
this.lengthLimitLength = lengthLimitLength;
137145
}
138146

139147
// Default profile pointer management
@@ -180,6 +188,18 @@ public void setRatelimitTicks(int ticks) {
180188
this.ratelimitTicks = ticks;
181189
}
182190

191+
// Length limit management
192+
193+
public int getLengthLimitLength() {
194+
return lengthLimitLength;
195+
}
196+
197+
public void setLengthLimitLength(int length) {
198+
if (length < 1)
199+
throw new IllegalArgumentException();
200+
this.lengthLimitLength = length;
201+
}
202+
183203
// Profile activation handling
184204

185205
/**
@@ -422,6 +442,10 @@ private Config validate() {
422442
if (ratelimitTicks < 1)
423443
ratelimitTicks = ratelimitTicksDefault;
424444

445+
// Validate length limit
446+
if (lengthLimitLength < 1)
447+
lengthLimitLength = lengthLimitLengthDefault;
448+
425449
return this;
426450
}
427451

@@ -485,6 +509,9 @@ public Config deserialize(JsonElement json, Type typeOfT, JsonDeserializationCon
485509
boolean ratelimitSp =
486510
JsonUtil.getOrDefault(obj, "ratelimitSp", ratelimitSpDefault, silent);
487511

512+
int lengthLimitLength =
513+
JsonUtil.getOrDefault(obj, "lengthLimitLength", lengthLimitLengthDefault, silent);
514+
488515
return new Config(
489516
profiles,
490517
spDefault,
@@ -495,7 +522,8 @@ public Config deserialize(JsonElement json, Type typeOfT, JsonDeserializationCon
495522
ratelimitCount,
496523
ratelimitTicks,
497524
ratelimitStrict,
498-
ratelimitSp
525+
ratelimitSp,
526+
lengthLimitLength
499527
).validate();
500528
}
501529
}

common/src/main/java/dev/terminalmc/commandkeys/gui/widget/list/MainOptionList.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,14 @@ protected void addEntries() {
119119
Tooltip.create(localized("option", "main.ratelimit.tooltip")), 500
120120
));
121121
addEntry(new Entry.Ratelimit(dynEntryX, dynEntryWidth, entryHeight));
122+
123+
// Length limit options
124+
addEntry(new OptionList.Entry.Text(
125+
dynEntryX, dynEntryWidth, entryHeight,
126+
localized("option", "main.lengthlimit", "\u2139"),
127+
Tooltip.create(localized("option", "main.lengthlimit.tooltip")), 500
128+
));
129+
addEntry(new Entry.LengthLimit(dynEntryX, dynEntryWidth, entryHeight));
122130
}
123131

124132
// Sub-screen opening
@@ -564,5 +572,37 @@ private static class Ratelimit extends Entry {
564572
elements.add(spButton);
565573
}
566574
}
575+
576+
private static class LengthLimit extends Entry {
577+
578+
LengthLimit(int x, int width, int height) {
579+
super();
580+
581+
// Max length field
582+
TextField lengthField = new TextField(x, 0, width, height);
583+
lengthField.posIntValidator().strict();
584+
lengthField.setMaxLength(6);
585+
lengthField.setResponder((val) -> {
586+
try {
587+
int space = Integer.parseInt(val.strip());
588+
if (space < 1)
589+
throw new NumberFormatException();
590+
Config.get().setLengthLimitLength(space);
591+
lengthField.setTextColor(16777215);
592+
} catch (NumberFormatException ignored) {
593+
lengthField.setTextColor(16711680);
594+
}
595+
});
596+
lengthField.setValue(String.valueOf(Config.get().getLengthLimitLength()));
597+
lengthField.setTooltip(Tooltip.create(
598+
localized("option", "main.lengthlimit.length.tooltip")
599+
.append("\n")
600+
.append(localized(
601+
"option",
602+
"main.lengthlimit.length.tooltip.warning"
603+
).withStyle(ChatFormatting.RED))));
604+
elements.add(lengthField);
605+
}
606+
}
567607
}
568608
}

common/src/main/resources/assets/commandkeys/lang/de_at.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{
22
"key.commandkeys.main": "CommandKeys",
33
"key.commandkeys.main.edit": "Aktives Profil bearbeiten",
4-
"modmenu.descriptionTranslation.commandkeys": "Sende vorab geschriebene Chatnachrichten und Befehle per Hotkey.",
4+
"message.commandkeys.blocked.lengthlimit": "Prevented sending message with length %d. Length limit is set to %d.",
5+
"message.commandkeys.blocked.ratelimit": "Ratenlimit von %s Taste überschritten. Ratenlimit ist auf %s Aktivierungen pro %s Ticks eingestellt.",
56
"message.commandkeys.placeholderFault": "Die Nachricht \"%s\" konnte nicht gesendet werden, da ein oder mehrere Platzhalter nicht angewendet werden konnten.",
6-
"message.commandkeys.sendBlocked": "Ratenlimit von %s Taste überschritten. Ratenlimit ist auf %s Aktivierungen pro %s Ticks eingestellt.",
7+
"modmenu.descriptionTranslation.commandkeys": "Sende vorab geschriebene Chatnachrichten und Befehle per Hotkey.",
78
"option.commandkeys.macro": "Makro Optionen",
89
"option.commandkeys.macro.activation": "Aktivierungstyp",
910
"option.commandkeys.macro.activation.HOLD": "Halten",
@@ -54,6 +55,10 @@
5455
"option.commandkeys.main.default.tooltip": "Diese Optionen werden von neuen Makro-Instanzen verwendet.",
5556
"option.commandkeys.main.exitWithoutSaving": "Bist du sicher, dass du ohne Speichern beenden möchtest?",
5657
"option.commandkeys.main.exitWithoutSaving.confirm": "Dadurch wird die Konfigurationsdatei neu geladen und alle vorgenommenen Änderungen werden verworfen. Falls du aktive Makros hast, werden diese gestoppt.",
58+
"option.commandkeys.main.lengthlimit": "Length Limit Options %s",
59+
"option.commandkeys.main.lengthlimit.length.tooltip": "Maximum allowed message length.",
60+
"option.commandkeys.main.lengthlimit.length.tooltip.warning": "Changing this value will NOT change the limit set by the game.",
61+
"option.commandkeys.main.lengthlimit.tooltip": "Prevents sending messages over the configured length. Useful for saving you from being kicked from the game when attempting to send very long messages.",
5762
"option.commandkeys.main.profile.details.tooltip": "Details bearbeiten",
5863
"option.commandkeys.main.profile.edit.tooltip": "Profil bearbeiten",
5964
"option.commandkeys.main.profile.link.remove.tooltip": "Verbindung entfernen",
@@ -97,4 +102,4 @@
97102
"toast.commandkeys.reset.message": "Backup gespeichert unter %s",
98103
"toast.commandkeys.reset.title": "CommandKeys-Optionen zurückgesetzt - Ladefehler",
99104
"ui.commandkeys.field.error.pos_int": "Positive ganze Zahl erforderlich"
100-
}
105+
}

common/src/main/resources/assets/commandkeys/lang/de_ch.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{
22
"key.commandkeys.main": "CommandKeys",
33
"key.commandkeys.main.edit": "Aktives Profil bearbeiten",
4-
"modmenu.descriptionTranslation.commandkeys": "Sende vorab geschriebene Chatnachrichten und Befehle per Hotkey.",
4+
"message.commandkeys.blocked.lengthlimit": "Prevented sending message with length %d. Length limit is set to %d.",
5+
"message.commandkeys.blocked.ratelimit": "Ratenlimit von %s Taste überschritten. Ratenlimit ist auf %s Aktivierungen pro %s Ticks eingestellt.",
56
"message.commandkeys.placeholderFault": "Die Nachricht \"%s\" konnte nicht gesendet werden, da ein oder mehrere Platzhalter nicht angewendet werden konnten.",
6-
"message.commandkeys.sendBlocked": "Ratenlimit von %s Taste überschritten. Ratenlimit ist auf %s Aktivierungen pro %s Ticks eingestellt.",
7+
"modmenu.descriptionTranslation.commandkeys": "Sende vorab geschriebene Chatnachrichten und Befehle per Hotkey.",
78
"option.commandkeys.macro": "Makro Optionen",
89
"option.commandkeys.macro.activation": "Aktivierungstyp",
910
"option.commandkeys.macro.activation.HOLD": "Halten",
@@ -54,6 +55,10 @@
5455
"option.commandkeys.main.default.tooltip": "Diese Optionen werden von neuen Makro-Instanzen verwendet.",
5556
"option.commandkeys.main.exitWithoutSaving": "Bist du sicher, dass du ohne Speichern beenden möchtest?",
5657
"option.commandkeys.main.exitWithoutSaving.confirm": "Dadurch wird die Konfigurationsdatei neu geladen und alle vorgenommenen Änderungen werden verworfen. Falls du aktive Makros hast, werden diese gestoppt.",
58+
"option.commandkeys.main.lengthlimit": "Length Limit Options %s",
59+
"option.commandkeys.main.lengthlimit.length.tooltip": "Maximum allowed message length.",
60+
"option.commandkeys.main.lengthlimit.length.tooltip.warning": "Changing this value will NOT change the limit set by the game.",
61+
"option.commandkeys.main.lengthlimit.tooltip": "Prevents sending messages over the configured length. Useful for saving you from being kicked from the game when attempting to send very long messages.",
5762
"option.commandkeys.main.profile.details.tooltip": "Details bearbeiten",
5863
"option.commandkeys.main.profile.edit.tooltip": "Profil bearbeiten",
5964
"option.commandkeys.main.profile.link.remove.tooltip": "Verbindung entfernen",
@@ -97,4 +102,4 @@
97102
"toast.commandkeys.reset.message": "Backup gespeichert unter %s",
98103
"toast.commandkeys.reset.title": "CommandKeys-Optionen zurückgesetzt - Ladefehler",
99104
"ui.commandkeys.field.error.pos_int": "Positive ganze Zahl erforderlich"
100-
}
105+
}

common/src/main/resources/assets/commandkeys/lang/de_de.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{
22
"key.commandkeys.main": "CommandKeys",
33
"key.commandkeys.main.edit": "Aktives Profil bearbeiten",
4-
"modmenu.descriptionTranslation.commandkeys": "Sende vorab geschriebene Chatnachrichten und Befehle per Hotkey.",
4+
"message.commandkeys.blocked.lengthlimit": "Prevented sending message with length %d. Length limit is set to %d.",
5+
"message.commandkeys.blocked.ratelimit": "Ratenlimit von %s Taste überschritten. Ratenlimit ist auf %s Aktivierungen pro %s Ticks eingestellt.",
56
"message.commandkeys.placeholderFault": "Die Nachricht \"%s\" konnte nicht gesendet werden, da ein oder mehrere Platzhalter nicht angewendet werden konnten.",
6-
"message.commandkeys.sendBlocked": "Ratenlimit von %s Taste überschritten. Ratenlimit ist auf %s Aktivierungen pro %s Ticks eingestellt.",
7+
"modmenu.descriptionTranslation.commandkeys": "Sende vorab geschriebene Chatnachrichten und Befehle per Hotkey.",
78
"option.commandkeys.macro": "Makro Optionen",
89
"option.commandkeys.macro.activation": "Aktivierungstyp",
910
"option.commandkeys.macro.activation.HOLD": "Halten",
@@ -54,6 +55,10 @@
5455
"option.commandkeys.main.default.tooltip": "Diese Optionen werden von neuen Makro-Instanzen verwendet.",
5556
"option.commandkeys.main.exitWithoutSaving": "Bist du sicher, dass du ohne Speichern beenden möchtest?",
5657
"option.commandkeys.main.exitWithoutSaving.confirm": "Dadurch wird die Konfigurationsdatei neu geladen und alle vorgenommenen Änderungen werden verworfen. Falls du aktive Makros hast, werden diese gestoppt.",
58+
"option.commandkeys.main.lengthlimit": "Length Limit Options %s",
59+
"option.commandkeys.main.lengthlimit.length.tooltip": "Maximum allowed message length.",
60+
"option.commandkeys.main.lengthlimit.length.tooltip.warning": "Changing this value will NOT change the limit set by the game.",
61+
"option.commandkeys.main.lengthlimit.tooltip": "Prevents sending messages over the configured length. Useful for saving you from being kicked from the game when attempting to send very long messages.",
5762
"option.commandkeys.main.profile.details.tooltip": "Details bearbeiten",
5863
"option.commandkeys.main.profile.edit.tooltip": "Profil bearbeiten",
5964
"option.commandkeys.main.profile.link.remove.tooltip": "Verbindung entfernen",

common/src/main/resources/assets/commandkeys/lang/en_us.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
22
"key.commandkeys.main": "CommandKeys",
33
"key.commandkeys.main.edit": "Edit Active Profile",
4+
"message.commandkeys.blocked.lengthlimit": "Prevented sending message with length %d. Length limit is set to %d.",
5+
"message.commandkeys.blocked.ratelimit": "Ratelimit exceeded by key %s. Ratelimit is set to %s activations in %s ticks.",
46
"message.commandkeys.placeholderFault": "Message \"%s\" could not be sent because one or more placeholders failed to apply.",
5-
"message.commandkeys.sendBlocked": "Ratelimit exceeded by key %s. Ratelimit is set to %s activations in %s ticks.",
67
"option.commandkeys.macro": "Macro Options",
78
"option.commandkeys.macro.activation": "Activation Type",
89
"option.commandkeys.macro.activation.HOLD": "Hold",
@@ -53,6 +54,10 @@
5354
"option.commandkeys.main.default.tooltip": "These options will be used by new Macro instances.",
5455
"option.commandkeys.main.exitWithoutSaving": "Are you sure you want to exit without saving?",
5556
"option.commandkeys.main.exitWithoutSaving.confirm": "This will reload the configuration file to revert any changes you have made. If you have any active macros, they will be stopped.",
57+
"option.commandkeys.main.lengthlimit": "Length Limit Options %s",
58+
"option.commandkeys.main.lengthlimit.length.tooltip": "Maximum allowed message length.",
59+
"option.commandkeys.main.lengthlimit.length.tooltip.warning": "Changing this value will NOT change the limit set by the game.",
60+
"option.commandkeys.main.lengthlimit.tooltip": "Prevents sending messages over the configured length. Useful for saving you from being kicked from the game when attempting to send very long messages.",
5661
"option.commandkeys.main.profile.details.tooltip": "Edit details",
5762
"option.commandkeys.main.profile.edit.tooltip": "Edit profile",
5863
"option.commandkeys.main.profile.link.remove.tooltip": "Remove link",
@@ -96,4 +101,4 @@
96101
"toast.commandkeys.reset.message": "Backup saved to %s",
97102
"toast.commandkeys.reset.title": "CommandKeys Options Reset - Load Error",
98103
"ui.commandkeys.field.error.pos_int": "Positive integer required"
99-
}
104+
}

common/src/main/resources/assets/commandkeys/lang/ru_ru.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
22
"key.commandkeys.main": "CommandKeys",
33
"key.commandkeys.main.edit": "Изменить активный профиль",
4+
"message.commandkeys.blocked.lengthlimit": "Prevented sending message with length %d. Length limit is set to %d.",
5+
"message.commandkeys.blocked.ratelimit": "Превышен лимит для ключа %s. Лимит установлен на %s активаций за %s тиков.",
46
"message.commandkeys.placeholderFault": "Сообщение \"%s\" не может быть отправлено, так как не удалось применить один или несколько заполнителей.",
5-
"message.commandkeys.sendBlocked": "Превышен лимит для ключа %s. Лимит установлен на %s активаций за %s тиков.",
67
"option.commandkeys.macro": "Параметры макросов",
78
"option.commandkeys.macro.activation": "Тип активации",
89
"option.commandkeys.macro.activation.HOLD": "Удержание",
@@ -53,6 +54,10 @@
5354
"option.commandkeys.main.default.tooltip": "Эти параметры будут использоваться новыми экземплярами макроса.",
5455
"option.commandkeys.main.exitWithoutSaving": "Вы уверены, что хотите выйти без сохранения?",
5556
"option.commandkeys.main.exitWithoutSaving.confirm": "Это перезагрузит файлы конфигурации, чтобы отменить ВСЕ внесённые вами изменения. Если у вас есть активные макросы, то они будут остановлены.",
57+
"option.commandkeys.main.lengthlimit": "Length Limit Options %s",
58+
"option.commandkeys.main.lengthlimit.length.tooltip": "Maximum allowed message length.",
59+
"option.commandkeys.main.lengthlimit.length.tooltip.warning": "Changing this value will NOT change the limit set by the game.",
60+
"option.commandkeys.main.lengthlimit.tooltip": "Prevents sending messages over the configured length. Useful for saving you from being kicked from the game when attempting to send very long messages.",
5661
"option.commandkeys.main.profile.details.tooltip": "Редактировать детали",
5762
"option.commandkeys.main.profile.edit.tooltip": "Редактировать профиль",
5863
"option.commandkeys.main.profile.link.remove.tooltip": "Удалить ссылку",

0 commit comments

Comments
 (0)