Skip to content

Commit f9e8373

Browse files
committed
huge module feedback update
1 parent c20dccf commit f9e8373

File tree

9 files changed

+440
-493
lines changed

9 files changed

+440
-493
lines changed

src/main/java/meteordevelopment/meteorclient/gui/MessageFormatter.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package meteordevelopment.meteorclient.gui;
77

8+
import meteordevelopment.meteorclient.systems.modules.Module;
89
import meteordevelopment.meteorclient.utils.misc.text.MessageKind;
910
import net.minecraft.entity.Entity;
1011
import net.minecraft.entity.player.PlayerEntity;
@@ -13,16 +14,19 @@
1314
import net.minecraft.util.math.Vec3d;
1415
import net.minecraft.util.math.Vec3i;
1516

17+
import java.util.Optional;
18+
1619
public interface MessageFormatter {
1720
Text formatPlayerName(PlayerEntity player);
1821
Text formatEntityName(Entity entity);
1922

2023
Text formatCoords(Vec3i blockPos);
2124
Text formatCoords(Vec3d pos);
2225

23-
Text formatHighlight(MutableText text);
26+
Text formatHighlight(Text text);
2427
Text formatDecimal(double decimal);
2528

2629
Text formatPrefix(Text prefix);
27-
Text formatMessage(MutableText message, MessageKind messageKind);
30+
Text formatToggleFeedback(Text clientPrefix, Text featurePrefix, Module module, boolean enabled);
31+
Text formatMessage(Text clientPrefix, Optional<Text> featurePrefix, Text message, MessageKind messageKind);
2832
}

src/main/java/meteordevelopment/meteorclient/gui/themes/meteor/MeteorMessageFormatter.java

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55

66
package meteordevelopment.meteorclient.gui.themes.meteor;
77

8+
import meteordevelopment.meteorclient.MeteorClient;
89
import meteordevelopment.meteorclient.gui.MessageFormatter;
910
import meteordevelopment.meteorclient.pathing.NopPathManager;
1011
import meteordevelopment.meteorclient.pathing.PathManagers;
12+
import meteordevelopment.meteorclient.systems.modules.Module;
1113
import meteordevelopment.meteorclient.utils.misc.text.MessageKind;
1214
import meteordevelopment.meteorclient.utils.misc.text.RunnableClickEvent;
1315
import net.minecraft.entity.Entity;
@@ -22,6 +24,7 @@
2224
import net.minecraft.util.math.Vec3i;
2325

2426
import java.text.DecimalFormat;
27+
import java.util.Optional;
2528

2629
public class MeteorMessageFormatter implements MessageFormatter {
2730
private final DecimalFormat decimalFormat = new DecimalFormat("0.0##");
@@ -63,8 +66,8 @@ private Text formatCoords(int x, int y, int z) {
6366
}
6467

6568
@Override
66-
public Text formatHighlight(MutableText text) {
67-
return text.formatted(Formatting.WHITE);
69+
public Text formatHighlight(Text text) {
70+
return Text.empty().formatted(Formatting.WHITE).append(text);
6871
}
6972

7073
@Override
@@ -81,12 +84,27 @@ public Text formatPrefix(Text prefix) {
8184
}
8285

8386
@Override
84-
public Text formatMessage(MutableText message, MessageKind messageKind) {
87+
public Text formatToggleFeedback(Text clientPrefix, Text featurePrefix, Module module, boolean enabled) {
88+
Text feedback = MeteorClient.translatable(
89+
"module.base.toggled",
90+
module.getTitleText(),
91+
enabled ? Text.literal("on").formatted(Formatting.GREEN) : Text.literal("off").formatted(Formatting.RED)
92+
);
93+
94+
return this.formatMessage(clientPrefix, Optional.of(featurePrefix), feedback, MessageKind.Passthrough);
95+
}
96+
97+
@Override
98+
public Text formatMessage(Text clientPrefix, Optional<Text> featurePrefix, Text message, MessageKind messageKind) {
99+
MutableText formattedMessage = Text.empty().append(clientPrefix);
100+
featurePrefix.ifPresent(formattedMessage::append);
101+
formattedMessage.append(message);
102+
85103
return switch (messageKind) {
86-
case Passthrough -> message;
87-
case Info -> message.formatted(Formatting.GRAY);
88-
case Warning -> message.formatted(Formatting.YELLOW);
89-
case Error -> message.formatted(Formatting.RED);
104+
case Passthrough -> formattedMessage;
105+
case Info -> formattedMessage.formatted(Formatting.GRAY);
106+
case Warning -> formattedMessage.formatted(Formatting.YELLOW);
107+
case Error -> formattedMessage.formatted(Formatting.RED);
90108
};
91109
}
92110
}

src/main/java/meteordevelopment/meteorclient/systems/modules/Module.java

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,23 @@
99
import meteordevelopment.meteorclient.addons.AddonManager;
1010
import meteordevelopment.meteorclient.addons.MeteorAddon;
1111
import meteordevelopment.meteorclient.gui.GuiTheme;
12+
import meteordevelopment.meteorclient.gui.GuiThemes;
1213
import meteordevelopment.meteorclient.gui.widgets.WWidget;
1314
import meteordevelopment.meteorclient.settings.Settings;
1415
import meteordevelopment.meteorclient.systems.config.Config;
1516
import meteordevelopment.meteorclient.utils.Utils;
1617
import meteordevelopment.meteorclient.utils.misc.ISerializable;
1718
import meteordevelopment.meteorclient.utils.misc.Keybind;
1819
import meteordevelopment.meteorclient.utils.misc.MeteorTranslations;
20+
import meteordevelopment.meteorclient.utils.misc.text.MessageBuilder;
21+
import meteordevelopment.meteorclient.utils.misc.text.MessageKind;
1922
import meteordevelopment.meteorclient.utils.player.ChatUtils;
2023
import meteordevelopment.meteorclient.utils.render.color.Color;
2124
import net.minecraft.client.MinecraftClient;
2225
import net.minecraft.nbt.NbtCompound;
2326
import net.minecraft.nbt.NbtElement;
2427
import net.minecraft.text.MutableText;
2528
import net.minecraft.text.Text;
26-
import net.minecraft.util.Formatting;
2729
import org.jetbrains.annotations.NotNull;
2830

2931
import java.util.Objects;
@@ -117,44 +119,34 @@ public void disable() {
117119

118120
public void sendToggledMsg() {
119121
if (Config.get().chatFeedback.get() && chatFeedback) {
120-
ChatUtils.forceNextPrefixClass(getClass());
121-
ChatUtils.sendMsg(this.hashCode(), Formatting.GRAY, "module.base.toggled", this.getTitleText(), isActive() ? Text.literal("on").formatted(Formatting.GREEN) : Text.literal("off").formatted(Formatting.RED));
122+
GuiTheme theme = GuiThemes.get();
123+
ChatUtils.sendMsg(this.hashCode(), theme.messageFormatter().formatToggleFeedback(
124+
theme.messageFormatter().formatPrefix(ChatUtils.getPrefix(this, theme)),
125+
theme.messageFormatter().formatPrefix(this.getTitleText()),
126+
this,
127+
this.isActive()
128+
));
122129
}
123130
}
124131

125-
public void info(Text message) {
126-
ChatUtils.forceNextPrefixClass(getClass());
127-
ChatUtils.sendMsg(this.getTranslationKey(), message);
132+
public MessageBuilder info(Text message) {
133+
return MessageBuilder.create().setSource(this).setTranslationContext(this.getTranslationKey())
134+
.body(message).setKind(MessageKind.Info);
128135
}
129136

130-
public void info(String messageKey, Object... args) {
131-
ChatUtils.forceNextPrefixClass(getClass());
132-
ChatUtils.infoPrefix(this.getTranslationKey(), this.getTranslationKey() + ".info." + messageKey, args);
137+
public MessageBuilder info(String message, Object... args) {
138+
return MessageBuilder.create().setSource(this).setTranslationContext(this.getTranslationKey())
139+
.body(message, args).setKind(MessageKind.Info);
133140
}
134141

135-
public void infoRaw(String message, Object... args) {
136-
ChatUtils.forceNextPrefixClass(getClass());
137-
ChatUtils.infoPrefixRaw(this.getTranslationKey(), message, args);
142+
public MessageBuilder warning(String message, Object... args) {
143+
return MessageBuilder.create().setSource(this).setTranslationContext(this.getTranslationKey())
144+
.body(message, args).setKind(MessageKind.Warning);
138145
}
139146

140-
public void warning(String messageKey, Object... args) {
141-
ChatUtils.forceNextPrefixClass(getClass());
142-
ChatUtils.warningPrefix(this.getTranslationKey(), this.getTranslationKey() + ".warning." + messageKey, args);
143-
}
144-
145-
public void warningRaw(String message, Object... args) {
146-
ChatUtils.forceNextPrefixClass(getClass());
147-
ChatUtils.warningPrefixRaw(this.getTranslationKey(), message, args);
148-
}
149-
150-
public void error(String messageKey, Object... args) {
151-
ChatUtils.forceNextPrefixClass(getClass());
152-
ChatUtils.errorPrefix(this.getTranslationKey(), this.getTranslationKey() + ".error." + messageKey, args);
153-
}
154-
155-
public void errorRaw(String message, Object... args) {
156-
ChatUtils.forceNextPrefixClass(getClass());
157-
ChatUtils.errorPrefixRaw(this.getTranslationKey(), message, args);
147+
public MessageBuilder error(String message, Object... args) {
148+
return MessageBuilder.create().setSource(this).setTranslationContext(this.getTranslationKey())
149+
.body(message, args).setKind(MessageKind.Error);
158150
}
159151

160152
public boolean isActive() {

src/main/java/meteordevelopment/meteorclient/utils/misc/text/MessageBuilder.java

Lines changed: 109 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,133 @@
55

66
package meteordevelopment.meteorclient.utils.misc.text;
77

8-
import net.minecraft.text.MutableText;
98
import net.minecraft.text.Text;
109
import net.minecraft.util.Formatting;
1110

11+
import java.util.function.Supplier;
12+
1213
public interface MessageBuilder {
1314
static MessageBuilder create() {
1415
return new MessageBuilderImpl();
1516
}
1617

18+
static Text highlight(Object argument) {
19+
return MessageBuilderImpl.highlight(argument);
20+
}
21+
22+
/**
23+
* Sets the id associated with this message. There can only be a single message with a given id in chat.
24+
*
25+
* @return this builder
26+
* @throws IllegalStateException if this builder is already closed.
27+
*/
1728
MessageBuilder setId(int id);
29+
30+
/**
31+
* Sets the kind of message you want to send, for use in styling. Use {@link MessageKind#Passthrough} to avoid
32+
* the formatter to send the message body as-is, with no additional styling.
33+
*
34+
* @return this builder
35+
* @throws IllegalStateException if this builder is already closed.
36+
*/
1837
MessageBuilder setKind(MessageKind kind);
38+
39+
/**
40+
* Sets the context to be used for translation keys within this builder. If this is set, it will automatically
41+
* attempt to resolve translations using the key {@code "${context}.${kind}.${body}"}.
42+
*
43+
* @return this builder
44+
* @throws IllegalStateException if this builder is already closed.
45+
*/
1946
MessageBuilder setTranslationContext(String translationContext);
47+
48+
/**
49+
* Sets the source of this builder. If set, the Meteor Client prefix will be replaced based on
50+
* {@link meteordevelopment.meteorclient.utils.player.ChatUtils#registerCustomPrefix(String, Supplier)}.
51+
*
52+
* @return this builder
53+
* @throws IllegalStateException if this builder is already closed.
54+
*/
2055
MessageBuilder setSource(Object source);
2156

22-
MessageBuilder prefix(MutableText prefix);
57+
/**
58+
* Sets the prefix to show in front of the message. This can optionally be styled, but should not contain any
59+
* decorations such as brackets as those will be added by the formatter.
60+
*
61+
* @return this builder
62+
* @throws IllegalStateException if this builder is already closed.
63+
*/
64+
MessageBuilder prefix(Text prefix);
65+
66+
/**
67+
* Sets the prefix to show in front of the message. This should not contain any decorations such as brackets as
68+
* those will be added by the formatter.
69+
*
70+
* @return this builder
71+
* @throws IllegalStateException if this builder is already closed.
72+
*/
2373
MessageBuilder prefix(String prefix);
74+
75+
/**
76+
* Sets the prefix to show in front of the message. This should not contain any decorations such as brackets as
77+
* those will be added by the formatter. The prefix color is a suggestion, the formatter is allowed to override it.
78+
*
79+
* @return this builder
80+
* @throws IllegalStateException if this builder is already closed.
81+
*/
2482
MessageBuilder prefix(String prefix, Formatting prefixColor);
2583

26-
MessageBuilder body(MutableText body);
84+
/**
85+
* Sets the message body.
86+
*
87+
* @return this builder
88+
* @throws IllegalStateException if this builder is already closed.
89+
*/
90+
MessageBuilder body(Text body);
91+
92+
/**
93+
* Sets the message body.
94+
*
95+
* @param body the message body. If this is a valid Meteor Client translation key, the message body will instead be
96+
* the translated text. If {@link MessageBuilder#setTranslationContext(String)} is set and
97+
* {@link MessageBuilder#setKind(MessageKind)} is not set to {@link MessageKind#Passthrough}, the
98+
* message body will also attempt to resolve the key made using the format
99+
* {@code "${context}.${kind}.${body}"}. For example,
100+
* {@code builder.setKind(MessageKind.Info).setTranslationContext("example").body("test")} would result
101+
* in the key {@code "example.info.test"}. Otherwise, the message body will simply be passed in plain
102+
* text.
103+
* @param args the arguments that will be used to replace the format specifiers in the body. This builder supports
104+
* many special argument types: <ul>
105+
* <li>{@link Text} — Keeps the styling.</li>
106+
* <li>{@link net.minecraft.entity.player.PlayerEntity} — Uses the player's name.</li>
107+
* <li>{@link net.minecraft.entity.Entity} — Uses the entity's display name.</li>
108+
* <li>{@link net.minecraft.util.math.BlockPos} — Formatted coordinate display.</li>
109+
* <li>{@link net.minecraft.util.math.Vec3d} — Formatted coordinate display.</li>
110+
* <li>{@link Float} & {@link Double} — Truncates some decimals.</li>
111+
* <li>{@link net.minecraft.entity.effect.StatusEffect} — Uses the status effect's display name.</li>
112+
* <li>{@link net.minecraft.item.Item} — Uses the item's display name.</li>
113+
* <li>{@link net.minecraft.block.Block} — Uses the block's display name.</li>
114+
* <li>{@link net.minecraft.entity.EntityType} — Uses the entity type's display name.</li>
115+
* </ul> Otherwise the argument will be passed through {@link String#valueOf(Object)}.
116+
* @return this builder
117+
* @throws IllegalStateException if this builder is already closed.
118+
*/
27119
MessageBuilder body(String body, Object... args);
28120

121+
/**
122+
* Builds the message and closes this builder.
123+
*
124+
* @return the built message
125+
* @throws IllegalArgumentException if the message has no body or no kind.
126+
* @throws IllegalStateException if this builder is already closed.
127+
*/
29128
Text build();
129+
130+
/**
131+
* Sends the message in chat and closes this builder.
132+
*
133+
* @throws IllegalArgumentException if the message has no body or no kind.
134+
* @throws IllegalStateException if this builder is already closed.
135+
*/
30136
void send();
31137
}

0 commit comments

Comments
 (0)