Skip to content

Commit

Permalink
Toast sound effects
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaming32 committed May 23, 2023
1 parent 23b8c37 commit e3ed61f
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/main/java/io/github/gaming32/worldhost/WorldHost.java
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ public static boolean isFriend(UUID user) {
return CONFIG.isEnableFriends() && CONFIG.getFriends().contains(user);
}

public static void showProfileToast(UUID user, String title, String description, int ticks, Runnable clickAction) {
public static void showFriendOrOnlineToast(UUID user, String title, String description, int ticks, Runnable clickAction) {
Util.backgroundExecutor().execute(() -> {
final GameProfile profile = Minecraft.getInstance()
.getMinecraftSessionService()
Expand All @@ -495,6 +495,7 @@ public static void showProfileToast(UUID user, String title, String description,
})
.clickAction(clickAction)
.ticks(ticks)
.important()
.show();
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ record FriendRequest(UUID fromUser) implements WorldHostS2CMessage {
public void handle(ProtocolClient client) {
if (!WorldHost.CONFIG.isEnableFriends()) return;
final boolean isFriend = WorldHost.isFriend(fromUser);
WorldHost.showProfileToast(
WorldHost.showFriendOrOnlineToast(
fromUser,
isFriend ? "world-host.friend_added_you.already" : "world-host.friend_added_you",
isFriend ? "world-host.friend_added_you.already.desc" : "world-host.need_add_back",
Expand All @@ -107,7 +107,7 @@ public void handle(ProtocolClient client) {
Minecraft.getInstance().execute(() -> {
WorldHost.ONLINE_FRIENDS.put(user, connectionId);
WorldHost.ONLINE_FRIEND_UPDATES.forEach(FriendsListUpdate::friendsListUpdate);
WorldHost.showProfileToast(
WorldHost.showFriendOrOnlineToast(
user, "world-host.went_online", "world-host.went_online.desc", 200,
() -> WorldHost.join(connectionId, null)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class ToastBuilder {
private IconRenderer iconRenderer = null;
@Nullable
private Runnable clickAction = null;
private boolean important = false;
private int ticks = 100;

ToastBuilder(@NotNull Component title) {
Expand All @@ -35,12 +36,23 @@ public ToastBuilder clickAction(Runnable clickAction) {
return this;
}

public ToastBuilder important() {
return important(true);
}

public ToastBuilder important(boolean important) {
this.important = important;
return this;
}

public ToastBuilder ticks(int ticks) {
this.ticks = ticks;
return this;
}

public void show() {
Minecraft.getInstance().execute(() -> WHToast.TOASTS.add(new ToastInstance(title, description, iconRenderer, clickAction, ticks)));
Minecraft.getInstance().execute(() -> WHToast.add(
new ToastInstance(title, description, iconRenderer, clickAction, important, ticks)
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class ToastInstance {
public final IconRenderer iconRenderer;
@Nullable
public final Runnable clickAction;
public final boolean important;

public final int width;
public int height;
Expand All @@ -67,21 +68,19 @@ public ToastInstance(
@Nullable Component description,
@Nullable IconRenderer iconRenderer,
@Nullable Runnable clickAction,
boolean important,
int ticks
) {
this.title = title;
this.description = description;
this.iconRenderer = iconRenderer;
this.clickAction = clickAction;
this.important = important;

width = TEXT_WIDTH
+ 2 * BORDER_SIZE
+ (iconRenderer != null ? ICON_SIZE + BORDER_SIZE : 0);

if (WHToast.ready) {
calculateText();
}

ticksRemaining = ticksTotal = ticks;
}

Expand Down
45 changes: 42 additions & 3 deletions src/main/java/io/github/gaming32/worldhost/toast/WHToast.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import com.mojang.blaze3d.vertex.PoseStack;
import io.github.gaming32.worldhost.versions.Components;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.sounds.SimpleSoundInstance;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundEvent;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayDeque;
Expand All @@ -15,8 +18,12 @@ public class WHToast {
private static final int X_OFFSET = 4;
private static final int Y_OFFSET = 4;

static boolean ready = false;
static final Deque<ToastInstance> TOASTS = new ArrayDeque<>();
private static final SoundEvent IMPORTANT = newSoundEvent("important");
private static final SoundEvent REGULAR = newSoundEvent("regular");
private static final SoundEvent REMOVED = newSoundEvent("removed");

private static boolean ready = false;
private static final Deque<ToastInstance> TOASTS = new ArrayDeque<>();

public static ToastBuilder builder(@NotNull Component title) {
return new ToastBuilder(title);
Expand All @@ -28,9 +35,24 @@ public static ToastBuilder builder(@NotNull String titleKey) {

public static void ready() {
TOASTS.forEach(ToastInstance::calculateText);
if (!TOASTS.isEmpty()) {
if (TOASTS.stream().anyMatch(t -> t.important)) {
playSound(IMPORTANT);
} else {
playSound(REGULAR);
}
}
ready = true;
}

static void add(ToastInstance toast) {
if (ready) {
toast.calculateText();
playSound(toast.important ? IMPORTANT : REGULAR);
}
TOASTS.add(toast);
}

public static void tick() {
if (!ready) return;

Expand All @@ -40,7 +62,10 @@ public static void tick() {
final ToastInstance toast = it.next();
toast.yShift += shift;
shift = 0;
if (--toast.ticksRemaining <= 0) {
toast.ticksRemaining--;
if (toast.ticksRemaining == 19) {
playSound(REMOVED);
} else if (toast.ticksRemaining <= 0) {
it.remove();
shift = toast.height + GAP + toast.yShift;
}
Expand Down Expand Up @@ -97,4 +122,18 @@ public static boolean click(double mouseX, double mouseY, int button) {

return false;
}

private static SoundEvent newSoundEvent(String location) {
//#if MC >= 11904
return SoundEvent.createVariableRangeEvent(
//#else
//$$ return new SoundEvent(
//#endif
new ResourceLocation("world-host:toast/" + location)
);
}

private static void playSound(SoundEvent event) {
Minecraft.getInstance().getSoundManager().play(SimpleSoundInstance.forUI(event, 1f, 1f));
}
}
26 changes: 26 additions & 0 deletions src/main/resources/assets/world-host/sounds.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"toast/important": {
"sounds": [
{
"name": "minecraft:note/icechime",
"volume": 0.5
}
]
},
"toast/regular": {
"sounds": [
{
"name": "minecraft:ui.toast.in",
"type": "event"
}
]
},
"toast/removed": {
"sounds": [
{
"name": "minecraft:ui.toast.out",
"type": "event"
}
]
}
}

0 comments on commit e3ed61f

Please sign in to comment.