Skip to content

Commit 4132a08

Browse files
Rewrite MeteorToast via builder pattern [Addons breaking] (#5496)
1 parent 3125e55 commit 4132a08

3 files changed

Lines changed: 73 additions & 56 deletions

File tree

src/main/java/meteordevelopment/meteorclient/systems/modules/world/StashFinder.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,14 @@ private void onChunkData(ChunkDataEvent event) {
125125
if (sendNotifications.get() && (!chunk.equals(prevChunk) || !chunk.countsEqual(prevChunk))) {
126126
switch (notificationMode.get()) {
127127
case Chat -> info("Found stash at (highlight)%s(default), (highlight)%s(default).", chunk.x, chunk.z);
128-
case Toast -> mc.getToastManager().add(new MeteorToast(Items.CHEST, title, "Found Stash!"));
128+
case Toast -> {
129+
MeteorToast toast = new MeteorToast.Builder(title).icon(Items.CHEST).text("Found Stash!").build();
130+
mc.getToastManager().add(toast);
131+
}
129132
case Both -> {
130133
info("Found stash at (highlight)%s(default), (highlight)%s(default).", chunk.x, chunk.z);
131-
mc.getToastManager().add(new MeteorToast(Items.CHEST, title, "Found Stash!"));
134+
MeteorToast toast = new MeteorToast.Builder(title).icon(Items.CHEST).text("Found Stash!").build();
135+
mc.getToastManager().add(toast);
132136
}
133137
}
134138
}

src/main/java/meteordevelopment/meteorclient/utils/player/TitleScreenCredits.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ private static void init() {
5555
switch (res.statusCode()) {
5656
case Http.UNAUTHORIZED -> {
5757
String message = "Invalid authentication token for repository '%s'".formatted(repo.getOwnerName());
58-
mc.getToastManager().add(new MeteorToast(Items.BARRIER, "GitHub: Unauthorized", message));
58+
MeteorToast toast = new MeteorToast.Builder("GitHub: Unauthorized").icon(Items.BARRIER).text(message).build();
59+
mc.getToastManager().add(toast);
5960
MeteorClient.LOG.warn(message);
6061
if (System.getenv("meteor.github.authorization") == null) {
6162
MeteorClient.LOG.info("Consider setting an authorization " +

src/main/java/meteordevelopment/meteorclient/utils/render/MeteorToast.java

Lines changed: 65 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,68 @@
2626
import static meteordevelopment.meteorclient.MeteorClient.mc;
2727

2828
public class MeteorToast implements Toast {
29-
public static final int TITLE_COLOR = Color.fromRGBA(145, 61, 226, 255);
30-
public static final int TEXT_COLOR = Color.fromRGBA(220, 220, 220, 255);
29+
private static final int TITLE_COLOR = Color.fromRGBA(145, 61, 226, 255);
30+
private static final int TEXT_COLOR = Color.fromRGBA(220, 220, 220, 255);
3131
private static final Identifier TEXTURE = Identifier.of("toast/advancement");
32-
33-
private ItemStack icon;
34-
private Text title, text;
35-
private boolean justUpdated = true, playedSound;
36-
private long start, duration;
37-
private Toast.Visibility visibility = Visibility.HIDE;
38-
39-
public MeteorToast(@Nullable Item item, @NotNull String title, @Nullable String text, long duration) {
40-
this.icon = item != null ? item.getDefaultStack() : null;
41-
this.title = Text.literal(title).setStyle(Style.EMPTY.withColor(TextColor.fromRgb(TITLE_COLOR)));
42-
this.text = text != null ? Text.literal(text).setStyle(Style.EMPTY.withColor(TextColor.fromRgb(TEXT_COLOR))) : null;
43-
this.duration = duration;
32+
private static final long DEFAULT_DURATION = 6000;
33+
private static final SoundInstance DEFAULT_SOUND = PositionedSoundInstance.master(SoundEvents.BLOCK_NOTE_BLOCK_CHIME.value(), 1.2f, 1);
34+
35+
// Toast fields
36+
private final @NotNull Text title;
37+
private final @Nullable Text text;
38+
private final @Nullable ItemStack icon;
39+
private final @Nullable SoundInstance customSound;
40+
private final long duration;
41+
42+
// State variables
43+
private boolean playedSound;
44+
private long start = -1;
45+
private Visibility visibility = Visibility.HIDE;
46+
47+
private MeteorToast(Builder builder) {
48+
this.title = builder.title;
49+
this.text = builder.text;
50+
this.icon = builder.icon;
51+
this.customSound = builder.customSound;
52+
this.duration = builder.duration;
4453
}
4554

46-
public MeteorToast(@Nullable Item item, @NotNull String title, @Nullable String text) {
47-
this.icon = item != null ? item.getDefaultStack() : null;
48-
this.title = Text.literal(title).setStyle(Style.EMPTY.withColor(TextColor.fromRgb(TITLE_COLOR)));
49-
this.text = text != null ? Text.literal(text).setStyle(Style.EMPTY.withColor(TextColor.fromRgb(TEXT_COLOR))) : null;
50-
this.duration = 6000;
55+
public static class Builder {
56+
private final @NotNull Text title;
57+
private @Nullable Text text;
58+
private @Nullable ItemStack icon;
59+
private @Nullable SoundInstance customSound = DEFAULT_SOUND;
60+
private long duration = DEFAULT_DURATION;
61+
62+
public Builder(@NotNull String title) {
63+
this.title = Text.literal(title).setStyle(Style.EMPTY.withColor(TextColor.fromRgb(TITLE_COLOR)));
64+
}
65+
66+
public Builder text(@Nullable String text) {
67+
this.text = text != null && !text.trim().isEmpty() ?
68+
Text.literal(text).setStyle(Style.EMPTY.withColor(TextColor.fromRgb(TEXT_COLOR))) :
69+
null;
70+
return this;
71+
}
72+
73+
public Builder icon(@Nullable Item item) {
74+
this.icon = item != null ? item.getDefaultStack() : null;
75+
return this;
76+
}
77+
78+
public Builder sound(@Nullable SoundInstance sound) {
79+
this.customSound = sound;
80+
return this;
81+
}
82+
83+
public Builder duration(long duration) {
84+
this.duration = Math.max(0, duration);
85+
return this;
86+
}
87+
88+
public MeteorToast build() {
89+
return new MeteorToast(this);
90+
}
5191
}
5292

5393
@Override
@@ -57,15 +97,12 @@ public Visibility getVisibility() {
5797

5898
@Override
5999
public void update(ToastManager manager, long time) {
60-
if (justUpdated) {
61-
start = time;
62-
justUpdated = false;
63-
}
100+
if (start == -1) start = time;
64101

65-
visibility = time - start >= duration ? Toast.Visibility.HIDE : Toast.Visibility.SHOW;
102+
visibility = time - start >= duration ? Visibility.HIDE : Visibility.SHOW;
66103

67104
if (!playedSound) {
68-
mc.getSoundManager().play(getSound());
105+
mc.getSoundManager().play(customSound);
69106
playedSound = true;
70107
}
71108
}
@@ -74,41 +111,16 @@ public void update(ToastManager manager, long time) {
74111
public void draw(DrawContext context, TextRenderer textRenderer, long startTime) {
75112
context.drawGuiTexture(RenderPipelines.GUI_TEXTURED, TEXTURE, 0, 0, getWidth(), getHeight());
76113

77-
int x = icon != null ? 28 : 12;
114+
int textX = icon != null ? 28 : 12;
78115
int titleY = 12;
79116

80117
if (text != null) {
81-
context.drawText(mc.textRenderer, title, x, 18, TITLE_COLOR, false);
118+
context.drawText(textRenderer, text, textX, 18, TEXT_COLOR, false);
82119
titleY = 7;
83120
}
84121

85-
context.drawText(mc.textRenderer, title, x, titleY, TITLE_COLOR, false);
122+
context.drawText(textRenderer, title, textX, titleY, TITLE_COLOR, false);
86123

87124
if (icon != null) context.drawItem(icon, 8, 8);
88125
}
89-
90-
public void setIcon(@Nullable Item item) {
91-
this.icon = item != null ? item.getDefaultStack() : null;
92-
justUpdated = true;
93-
}
94-
95-
public void setTitle(@NotNull String title) {
96-
this.title = Text.literal(title).setStyle(Style.EMPTY.withColor(TextColor.fromRgb(TITLE_COLOR)));
97-
justUpdated = true;
98-
}
99-
100-
public void setText(@Nullable String text) {
101-
this.text = text != null ? Text.literal(text).setStyle(Style.EMPTY.withColor(TextColor.fromRgb(TEXT_COLOR))) : null;
102-
justUpdated = true;
103-
}
104-
105-
public void setDuration(long duration) {
106-
this.duration = duration;
107-
justUpdated = true;
108-
}
109-
110-
// Addons: @Override this method in a subclass if you want a different sound
111-
public SoundInstance getSound() {
112-
return PositionedSoundInstance.master(SoundEvents.BLOCK_NOTE_BLOCK_CHIME.value(), 1.2f, 1);
113-
}
114126
}

0 commit comments

Comments
 (0)