Skip to content

Commit

Permalink
Merge pull request #439 from Mindgamesnl/feature/voicechat-display-ov…
Browse files Browse the repository at this point in the history
…errides

Plugin-side API for custom voicechat display UUID's or names
  • Loading branch information
Mindgamesnl authored Jun 13, 2024
2 parents c156107 + dbc661b commit 276872e
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 12 deletions.
14 changes: 14 additions & 0 deletions api/src/main/java/com/craftmend/openaudiomc/api/VoiceApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.craftmend.openaudiomc.api.channels.VoiceChannel;
import com.craftmend.openaudiomc.api.clients.Client;
import com.craftmend.openaudiomc.api.voice.CustomPlayerFilter;
import com.craftmend.openaudiomc.api.voice.DisplayOverride;
import com.craftmend.openaudiomc.api.voice.VoicePeerOptions;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -67,6 +68,19 @@ static VoiceApi getInstance() {
*/
void addStaticPeer(Client client, Client peerToAdd, boolean visible, boolean mutual);

/**
* Add a peer (partner) to someone's voice chat.
* This would let the client hear the peerToAdd as a global voice (without spatial audio/distance) until it's removed.
*
* @param client The web client that should receive this update
* @param peerToAdd The peer that should be added
* @param visible Whether the peer should be visible in the client
* @param mutual Whether the peer should also hear the client (repeat the call for mutual)
* @param displayOverride A display override, which can be used to change the display name and skin of a player in the voice chat system.
* @since 6.10.2
*/
void addStaticPeer(Client client, Client peerToAdd, boolean visible, boolean mutual, DisplayOverride displayOverride);

/**
* Remove a global peer from someone's voice chat.
* This would remove a static peer if they have been added through addStaticPeer, but not
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.craftmend.openaudiomc.api.voice;

import lombok.Data;
import org.jetbrains.annotations.Nullable;

import java.util.UUID;

/**
* This class is used to override the display name of a player in the voice chat system.
* This is useful for when you want to display a different name than the player's actual name,
* to add compatibility for bedrock players or display game ranks.
* @since 6.10.2
*/
@Data
public class DisplayOverride {

/**
* The name that should be displayed in the voice chat system.
* MUST be 32 characters or less.
*/
@Nullable
private String name;

/**
* The new UUID that should be used to obtain skin data.
*/
@Nullable
private UUID displayUuid;

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.jetbrains.annotations.Nullable;

@Data
@NoArgsConstructor
Expand Down Expand Up @@ -36,13 +37,21 @@ public class VoicePeerOptions implements Cloneable {
*/
private boolean spatialAudio = true;

/**
* An optional display override, which can be used to change the display name and skin of a player in the voice chat system.
* This can be left null if no override is needed.
* @since 6.10.2
*/
@Nullable
private DisplayOverride displayOverride;

/**
* Clone the object
* @return a clone of the object
*/
@Override
public VoicePeerOptions clone() {
return new VoicePeerOptions(visible, spatialAudio);
return new VoicePeerOptions(visible, spatialAudio, null);
}

}
2 changes: 1 addition & 1 deletion client/public/metadata.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"buildMajor":1,"buildMinor":125,"buildRevision":244,"buildTag":"dev","buildDate":"Tue Jun 11 2024","build":"1.125.244 dev"}
{"buildMajor":1,"buildMinor":125,"buildRevision":250,"buildTag":"dev","buildDate":"Thu Jun 13 2024","build":"1.125.250 dev"}
23 changes: 22 additions & 1 deletion client/src/client/services/voice/peers/VoicePeer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ export class VoicePeer {
constructor(peerName, peerUuid, peerStreamKey, location, options) {
this.options = options;

let displayName = peerName;
let displayUuid = peerUuid;

if (options.displayOverride) {
// override display name and uuid
displayName = options.displayOverride.name || peerName;
displayUuid = options.displayOverride.displayUuid || peerUuid;
}

// register in global state
setGlobalState({
voiceState: {
Expand All @@ -21,13 +30,17 @@ export class VoicePeer {
muted: false,
loading: true,
options: this.options,
displayName,
displayUuid,
},
},
},
});

this.peerName = peerName;
this.peerUuid = peerUuid;
this.displayName = displayName;
this.displayUuid = displayUuid;
this.peerStreamKey = peerStreamKey;
this.location = location;
this.killed = false;
Expand Down Expand Up @@ -62,9 +75,17 @@ export class VoicePeer {
}
}

let { displayUuid, displayName } = this;

if (changedOptions.displayOverride) {
// override display name and uuid
displayName = changedOptions.displayOverride.name || this.peerName;
displayUuid = changedOptions.displayOverride.displayUuid || this.peerUuid;
}

this.options = changedOptions;
// update global state
setGlobalState({ voiceState: { peers: { [this.peerStreamKey]: { options: this.options } } } });
setGlobalState({ voiceState: { peers: { [this.peerStreamKey]: { options: this.options, displayName, displayUuid } } } });
}

updateLocation(x, y, z) {
Expand Down
3 changes: 3 additions & 0 deletions client/src/client/services/voice/peers/VoicePeerOptions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ export class VoicePeerOptions {
constructor(
visible = true,
spatialAudio = getGlobalState().settings.voicechatSurroundSound,
displayOverride = null,
) {
this.visible = visible;
this.spatialAudio = spatialAudio;
this.displayOverride = displayOverride;
}
}

Expand All @@ -16,5 +18,6 @@ export function peerOptionsFromObj(obj) {
return new VoicePeerOptions(
(obj.visible !== undefined) ? obj.visible : true,
(obj.spatialAudio !== undefined) ? obj.spatialAudio : getGlobalState().settings.voicechatSurroundSound,
(obj.displayOverride !== undefined) ? obj.displayOverride : null,
);
}
3 changes: 2 additions & 1 deletion client/src/components/voice/VoicePeerBox.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ function VoicePeerBox(props) {
return (
<VoicePeerRow
loading={peer.loading}
name={peer.name}
name={peer.displayName}
displayUuid={peer.displayUuid}
key={peer.uuid}
streamKey={peer.streamKey}
uuid={peer.uuid}
Expand Down
16 changes: 9 additions & 7 deletions client/src/components/voice/VoicePeerRow.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export class VoicePeerRow extends React.Component {
streamKey: PropTypes.string.isRequired,
name: PropTypes.string,
uuid: PropTypes.string,
displayUuid: PropTypes.string,
muted: PropTypes.bool,
speaking: PropTypes.bool,
loading: PropTypes.bool,
Expand All @@ -26,6 +27,7 @@ export class VoicePeerRow extends React.Component {
static defaultProps = {
name: 'Unknown',
uuid: '00000000-0000-0000-0000-000000000000',
displayUuid: '00000000-0000-0000-0000-000000000000',
muted: false,
speaking: false,
loading: false,
Expand Down Expand Up @@ -68,7 +70,7 @@ export class VoicePeerRow extends React.Component {
render() {
// get props
const {
name, muted, speaking, uuid, loading,
name, muted, speaking, displayUuid, loading,
} = this.props;

let avatarClass = 'avatar w-16 rounded-2xl';
Expand All @@ -90,7 +92,7 @@ export class VoicePeerRow extends React.Component {
const { volume } = this.state;

return (
<li className={parentClass}>
<li className={`flex items-center ${parentClass}`}>
{loading ? (
<div className="absolute inset-0 flex items-center z-20 justify-center bg-gray-800 bg-opacity-70">
<svg
Expand Down Expand Up @@ -120,16 +122,16 @@ export class VoicePeerRow extends React.Component {
</small>
</div>
) : null}
<div>
<div className="flex-shrink-0">
<img
src={`https://visage.surgeplay.com/face/512/${uuid}`}
src={`https://visage.surgeplay.com/face/512/${displayUuid}`}
className={avatarClass}
alt={`Avatar for ${name}`}
/>
</div>
<div className="flex-1">
<div className="flex items-centerhidden-on-mobile py-1">
<h1 className="ml-2">
<div className="flex-1 min-w-0">
<div className="flex items-center py-1">
<h1 className="ml-2 break-all text-ellipsis overflow-hidden whitespace-nowrap">
{muted ? (<PeerMutedSvg />) : null}
{this.props.spatialAudio ? (<ProximitySvg />) : <GlobalSvg />}
{name}
Expand Down
2 changes: 1 addition & 1 deletion client/src/metadata.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"buildMajor":1,"buildMinor":125,"buildRevision":244,"buildTag":"dev","buildDate":"Tue Jun 11 2024","build":"1.125.244 dev"}
{"buildMajor":1,"buildMinor":125,"buildRevision":250,"buildTag":"dev","buildDate":"Thu Jun 13 2024","build":"1.125.250 dev"}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.craftmend.openaudiomc.api.events.client.ClientPeerRemovedEvent;
import com.craftmend.openaudiomc.api.interfaces.AudioApi;
import com.craftmend.openaudiomc.api.voice.CustomPlayerFilter;
import com.craftmend.openaudiomc.api.voice.DisplayOverride;
import com.craftmend.openaudiomc.api.voice.VoicePeerOptions;
import com.craftmend.openaudiomc.generic.client.objects.ClientConnection;
import com.craftmend.openaudiomc.generic.networking.interfaces.NetworkingService;
Expand Down Expand Up @@ -83,13 +84,24 @@ public boolean isGlobalPeer(Client haystack, Client needle) {

@Override
public void addStaticPeer(Client client, Client peerToAdd, boolean visible, boolean mutual) {
addStaticPeer(client, peerToAdd, visible, mutual, null);
}

@Override
public void addStaticPeer(Client client, Client peerToAdd, boolean visible, boolean mutual, DisplayOverride displayOverride) {
if (OpenAudioMc.getInstance().getPlatform() != Platform.SPIGOT) {
throw new IllegalStateException("This method is only available on the spigot platform");
}

if (displayOverride != null && displayOverride.getName() != null && displayOverride.getName().length() > 32) {
throw new IllegalArgumentException("Display name cannot be longer than 32 characters");
}

VoicePeerOptions options = new VoicePeerOptions();
options.setSpatialAudio(false);
options.setVisible(visible);
// may put in null, that's fine.
options.setDisplayOverride(displayOverride);

ClientConnection clientConnection = (ClientConnection) client;
ClientConnection peerConnection = (ClientConnection) peerToAdd;
Expand Down

0 comments on commit 276872e

Please sign in to comment.