Skip to content

Commit 39104d1

Browse files
committed
Add config option for notification sounds
1 parent cb6f1d0 commit 39104d1

3 files changed

Lines changed: 61 additions & 13 deletions

File tree

UserJoinLeaveNotifications/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@
3131
// You can specify all the values or you can default the Build and Revision Numbers
3232
// by using the '*' as shown below:
3333
// [assembly: AssemblyVersion("1.0.*")]
34-
[assembly: AssemblyVersion("1.2.0.0")]
35-
[assembly: AssemblyFileVersion("1.2.0.0")]
34+
[assembly: AssemblyVersion("1.3.0.0")]
35+
[assembly: AssemblyFileVersion("1.3.0.0")]

UserJoinLeaveNotifications/UserJoinLeaveNotifications.cs

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,34 @@ public class UserJoinLeaveNotifications : NeosMod
1313
{
1414
public override string Name => "UserJoinLeaveNotifications";
1515
public override string Author => "badhaloninja";
16-
public override string Version => "1.2.0";
16+
public override string Version => "1.3.0";
1717
public override string Link => "https://github.com/badhaloninja/UserJoinLeaveNotifications";
1818
public override void OnEngineInit()
1919
{
20+
config = GetConfiguration();
21+
config.OnThisConfigurationChanged += UpdateAssets;
22+
23+
2024
Engine.Current.RunPostInit(Setup);
2125
}
26+
27+
28+
[AutoRegisterConfigKey]
29+
private static readonly ModConfigurationKey<Uri> NotificationSoundUri = new ModConfigurationKey<Uri>("NotificationSound", "Notification sound for user joining or leaving - Disabled when null", () => null);
2230

31+
[AutoRegisterConfigKey]
32+
private static readonly ModConfigurationKey<bool> OverrideLeaveSound = new ModConfigurationKey<bool>("OverrideLeaveSound", "Override the notification sound for leaving", () => false);
33+
[AutoRegisterConfigKey]
34+
private static readonly ModConfigurationKey<Uri> NotificationLeaveSoundUri = new ModConfigurationKey<Uri>("NotificationLeaveSound", "Notification sound for leaving - Only used if override is enabled - Disabled when null", () => null);
2335

36+
private static ModConfiguration config;
2437
private static MethodInfo addNotification;
2538

2639
private static UserBag currentUserBag;
27-
28-
40+
41+
private static StaticAudioClip joinLeaveAudioClip;
42+
private static StaticAudioClip leaveAudioClip;
43+
2944
public static void Setup()
3045
{
3146
// Hook into the world focused event
@@ -54,12 +69,11 @@ private static void OnUserJoined(SyncBagBase<RefID, User> bag, RefID key, User u
5469
{
5570
if (user.IsLocalUser) return;
5671
if (NotificationPanel.Current == null) return;
57-
72+
5873
NotificationPanel.Current.RunInUpdates(3, async () =>
5974
{ // Running immediately results in the getuser to return a BadRequest
6075
Uri thumbnail = await GetUserThumbnail(user.UserID);
61-
62-
AddNotification(string.Format("{0} joined", user.UserName),MathX.Lerp(color.Blue, color.White, 0.5f), "User Joined", thumbnail);
76+
AddNotification(string.Format("{0} joined", user.UserName),MathX.Lerp(color.Blue, color.White, 0.5f), "User Joined", thumbnail, false);
6377
});
6478
}
6579

@@ -69,7 +83,7 @@ private static async void OnUserLeft(SyncBagBase<RefID, User> bag, RefID key, Us
6983
if (NotificationPanel.Current == null) return;
7084

7185
Uri thumbnail = await GetUserThumbnail(user.UserID);
72-
AddNotification(string.Format("{0} left", user.UserName), MathX.Lerp(color.Red, color.White, 0.5f), "User Left", thumbnail);
86+
AddNotification(string.Format("{0} left", user.UserName), MathX.Lerp(color.Red, color.White, 0.5f), "User Left", thumbnail, true);
7387
}
7488

7589

@@ -87,15 +101,46 @@ private static UserBag GetUserbag(World world)
87101
{ // Return the user bag field
88102
return Traverse.Create(world).Field("_users").GetValue<UserBag>();
89103
}
90-
91-
private static void AddNotification(string message, color backgroundColor, string mainMessage, Uri overrideProfile)
104+
105+
private static void AddNotification(string message, color backgroundColor, string mainMessage, Uri overrideProfile, bool UserLeaving)
92106
{
93107
// Not using Show Notification because it does not expose main message
94-
if (NotificationPanel.Current == null) return;
108+
if (addNotification == null || NotificationPanel.Current == null) return;
109+
95110
NotificationPanel.Current.RunSynchronously(() =>
96111
{ // ;-;
97-
addNotification.Invoke(NotificationPanel.Current, new object[] { null, message, null, backgroundColor, mainMessage, overrideProfile, null });
112+
if (joinLeaveAudioClip == null)
113+
{
114+
joinLeaveAudioClip = NotificationPanel.Current.Slot.AttachComponent<StaticAudioClip>();
115+
joinLeaveAudioClip.URL.Value = config.GetValue(NotificationSoundUri);
116+
}
117+
if (leaveAudioClip == null)
118+
{
119+
leaveAudioClip = NotificationPanel.Current.Slot.AttachComponent<StaticAudioClip>();
120+
leaveAudioClip.URL.Value = config.GetValue(NotificationLeaveSoundUri);
121+
}
122+
123+
StaticAudioClip clip = (joinLeaveAudioClip.URL.Value != null) ? joinLeaveAudioClip : null;
124+
125+
if (UserLeaving && config.GetValue(OverrideLeaveSound))
126+
{
127+
clip = (leaveAudioClip.URL.Value != null) ? leaveAudioClip : null;
128+
}
129+
130+
addNotification.Invoke(NotificationPanel.Current, new object[] { null, message, null, backgroundColor, mainMessage, overrideProfile, clip });
98131
});
99132
}
133+
134+
private void UpdateAssets(ConfigurationChangedEvent @event)
135+
{
136+
if (@event.Key == NotificationSoundUri && joinLeaveAudioClip != null)
137+
{
138+
joinLeaveAudioClip.URL.Value = config.GetValue(NotificationSoundUri);
139+
}
140+
if (@event.Key == NotificationLeaveSoundUri && leaveAudioClip != null)
141+
{
142+
leaveAudioClip.URL.Value = config.GetValue(NotificationLeaveSoundUri);
143+
}
144+
}
100145
}
101146
}

UserJoinLeaveNotifications/UserJoinLeaveNotifications.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
<Reference Include="BaseX">
3131
<HintPath>$(NeosPath)Neos_Data\Managed\BaseX.dll</HintPath>
3232
</Reference>
33+
<Reference Include="CodeX">
34+
<HintPath>$(NeosPath)Neos_Data\Managed\CodeX.dll</HintPath>
35+
</Reference>
3336
<Reference Include="CloudX.Shared">
3437
<HintPath>$(NeosPath)Neos_Data\Managed\CloudX.Shared.dll</HintPath>
3538
</Reference>

0 commit comments

Comments
 (0)