Skip to content

Commit

Permalink
Merge pull request #403 from Mindgamesnl/feature/peer-filter-api
Browse files Browse the repository at this point in the history
Migrate peer filter api, support legacy, add author DiamondDagger590 …
  • Loading branch information
Mindgamesnl authored Feb 11, 2024
2 parents 2c346f8 + 5de1dfc commit faa7278
Show file tree
Hide file tree
Showing 13 changed files with 224 additions and 101 deletions.
41 changes: 41 additions & 0 deletions api/dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,39 @@
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-maven-plugin</artifactId>
<version>1.18.20.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>delombok</goal>
</goals>
</execution>
</executions>
<configuration>
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
<outputDirectory>${delombok.output}</outputDirectory>
<addOutputDirectory>false</addOutputDirectory>
</configuration>
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
<configuration>
<source>8</source>
<sourcepath>${delombok.output}</sourcepath>
</configuration>
</plugin>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
Expand All @@ -47,9 +80,17 @@
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.13.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
<properties>
<delombok.output>${project.build.directory}/delombok</delombok.output>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<sourcepath>src/main/java</sourcepath>
</properties>
</project>
55 changes: 46 additions & 9 deletions api/src/main/java/com/craftmend/openaudiomc/api/VoiceApi.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.craftmend.openaudiomc.api;

import com.craftmend.openaudiomc.api.clients.Client;
import com.craftmend.openaudiomc.api.voice.CustomPlayerFilter;
import com.craftmend.openaudiomc.api.voice.VoicePeerOptions;

import java.util.List;
import java.util.UUID;

/**
Expand All @@ -14,6 +16,7 @@ public interface VoiceApi {

/**
* Get the voice api instance, or null if the plugin is not loaded yet
*
* @return instance
*/
static VoiceApi getInstance() {
Expand All @@ -25,47 +28,81 @@ static VoiceApi getInstance() {

/**
* Register a client as a voice-chat peer
*
* @param haystack The client that will be the host
* @param needle The client that will be the peer
* @param needle The client that will be the peer
* @return true if the client was registered, false if the client was already registered
*/
boolean hasPeer(Client haystack, Client needle);

/**
* Register a client as a voice-chat peer
*
* @param haystack The client that will be the host
* @param needle The client that will be the peer
* @param needle The client that will be the peer
* @return true if the client was registered, false if the client was already registered
*/
boolean hasPeer(Client haystack, UUID needle);

/**
* Push new options for a peer, changing how its rendered in the client
* @param client The web client that should receive this update
*
* @param client The web client that should receive this update
* @param peerToUpdate The peer that should be updated
* @param options The new options
* @param options The new options
*/
void updatePeerOptions(Client client, Client peerToUpdate, VoicePeerOptions options);

/**
* 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 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 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)
*/
void addStaticPeer(Client client, Client peerToAdd, boolean visible, boolean mutual);

/**
* Remove a global peer from someone's voice chat.
* This would remove a static peer if they have been added through addStaticPeer, but not
* if they have been added through the regular voice-chat system.
* @param client The web client that should receive this update
*
* @param client The web client that should receive this update
* @param peerToRemove The peer that should be removed
* @param mutual Whether the peer should also stop hearing the client (repeat the call for mutual)
* @param mutual Whether the peer should also stop hearing the client (repeat the call for mutual)
*/
void removeStaticPeer(Client client, Client peerToRemove, boolean mutual);

/**
* Adds a {@link CustomPlayerFilter} to the list of functions to filter out players in {@code com.craftmend.openaudiomc.spigot.modules.voicechat.filters.PeerFilter#wrap(Stream, Player)} (which lives in the plugin, not api).
* These functions are called in {@code com.craftmend.openaudiomc.spigot.modules.voicechat.filters.PeerFilter#wrap(Stream, Player)} to allow for plugins to add custom sorting for
* players. An example being staff members shouldn't be heard by other players so adding a custom function implementation via
* {@link #addFilterFunction(CustomPlayerFilter)} allows for such functionality to exist.
* <br />
* Please read the documentation in the {@link CustomPlayerFilter} before planning your implementation,
* because you are probably better off using the event system for most use-cases.
*
* @param customPlayerFilter The {@link CustomPlayerFilter} to be added to the list of functions
* @author DiamondDagger590
*/
void addFilterFunction(CustomPlayerFilter customPlayerFilter);

/**
* Returns a copy of the internal {@link List} of {@link CustomPlayerFilter}s. This means
* modifications done to the {@link List} returned by this method will not result
* in changes in the actual list.
* <br />
* These functions will be called in whatever order they are stored in.
* <br />
* These functions are called in {@code com.craftmend.openaudiomc.spigot.modules.voicechat.filters.PeerFilter#wrap(Stream, Player)} to allow for plugins to add custom sorting for
* players. An example being staff members shouldn't be heard by other players so adding a custom function implementation via
* {@link #addFilterFunction(CustomPlayerFilter)} allows for such functionality to exist.
*
* @return A copied {@link List} of {@link CustomPlayerFilter}s
* @author DiamondDagger590
*/
List<CustomPlayerFilter> getCustomPlayerFilters();

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* This is a type templated lambda for single event handlers.
* See {@link com.craftmend.openaudiomc.api.EventApi} for more information
*/
@FunctionalInterface
public interface SingleHandler<T> {

@Handler
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.craftmend.openaudiomc.api.voice;

import org.bukkit.entity.Player;

/**
* <p>This represents a function that can be implemented by any plugin in order to modify how players will be checked
* against each other.</p>
*
* <p>The easiest example is a game plugin, which will not let players hear each other if they are on different teams, or moderation
* where you would prevent two players from hearing each other if one of them has been muted/punished.</p>
*
* <p>This function is called AFTER the other sanity checks at the time of writing, meaning that these functions will
* be called assuming the players are in valid range of each other and so forth.</p>
*
* <p>Please note that a filter is only called in one direction, meaning that if its called for playerA -> playerB, it will not be called for playerB -> playerA.
* This is done as an optimization to prevent poor scaling with a large amount of players.</p>
*
* <p>Because of that, filters should only be used to filter players that should be considered for voicechat.
* If you with to setup one-directional voicechat, then you should use {@link com.craftmend.openaudiomc.api.events.client.ClientPeerAddEvent}, which will be called twice
* (once for each player) and allow you to cancel the event if you don't want the players to be able to hear each other.</p>
*
* <p>Filters are managed through the {@link com.craftmend.openaudiomc.api.VoiceApi}</p>
*
* @author DiamondDagger590
* @author Mats
* @since 6.10.0
*/
@FunctionalInterface
public interface CustomPlayerFilter {

/**
* This method is effectively a filter call from a Stream.
* The return value decides if the two players should be allowed to connect in voicechat, granted that the events (which are only fired
* for players who pass) aren't cancelled.
* <br />
* This method is called once for every combination of players (ignoring order)
* <br />
* If this combination should result in a valid connection, then the method should return {@code true}.
* If the combination should not be valid, then the method should return {@code false}, which will also prevent further
* events or filters from being called for this combination.
*
* @param listener The {@link Player} searching for other players to listen to
* @param possibleSpeaker The {@link Player} who is being checked to see if they can be heard
* @return {@code true} if the listener should be able to hear the possibleSpeaker
*/
boolean isPlayerValidListener(Player listener, Player possibleSpeaker);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.craftmend.openaudiomc.spigot.modules.voicechat.filters;

import com.craftmend.openaudiomc.api.voice.CustomPlayerFilter;

@FunctionalInterface
@Deprecated
/**
* This is a deprecated class, only kept to prevent breaking changes in the API due to class path.
* Please use {@link CustomPlayerFilter} instead
*/
public interface CustomFilterFunction extends CustomPlayerFilter {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* This package only contains some deprecated classes by the name/classpath of some old commonly used classes.
* They are kept here to prevent breaking changes in the API, but are not used anymore and will be removed in the future.
*/
package com.craftmend.openaudiomc.spigot.modules.voicechat.filters;
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import com.craftmend.openaudiomc.OpenAudioMc;
import com.craftmend.openaudiomc.api.VoiceApi;
import com.craftmend.openaudiomc.api.clients.Client;
import com.craftmend.openaudiomc.api.voice.CustomPlayerFilter;
import com.craftmend.openaudiomc.api.voice.VoicePeerOptions;
import com.craftmend.openaudiomc.generic.client.objects.ClientConnection;
import com.craftmend.openaudiomc.generic.networking.interfaces.NetworkingService;
import com.craftmend.openaudiomc.generic.networking.packets.client.voice.PacketClientVoiceOptionsUpdate;
import com.craftmend.openaudiomc.generic.networking.payloads.client.voice.ClientVoiceOptionsPayload;
import com.craftmend.openaudiomc.generic.platform.Platform;
import com.craftmend.openaudiomc.spigot.modules.voicechat.filters.FilterService;

import java.util.List;
import java.util.Objects;
import java.util.UUID;

Expand Down Expand Up @@ -121,4 +124,14 @@ public void removeStaticPeer(Client client, Client peerToRemove, boolean mutual)
removeStaticPeer(peerToRemove, client, false);
}
}

@Override
public void addFilterFunction(CustomPlayerFilter customPlayerFilter) {
OpenAudioMc.getService(FilterService.class).addCustomFilter(customPlayerFilter);
}

@Override
public List<CustomPlayerFilter> getCustomPlayerFilters() {
return OpenAudioMc.getService(FilterService.class).getCustomPlayerFilters();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import com.craftmend.openaudiomc.spigot.modules.players.SpigotPlayerService;
import com.craftmend.openaudiomc.spigot.modules.voicechat.filters.FilterService;
import com.craftmend.openaudiomc.spigot.modules.voicechat.filters.PeerFilter;
import com.craftmend.openaudiomc.spigot.modules.voicechat.filters.impl.GamemodeFilter;
import com.craftmend.openaudiomc.spigot.modules.voicechat.filters.impl.TeamFilter;
import com.craftmend.openaudiomc.spigot.modules.voicechat.filters.impl.GamemodeFilterCustom;
import com.craftmend.openaudiomc.spigot.modules.voicechat.filters.impl.TeamFilterCustom;
import com.craftmend.openaudiomc.spigot.modules.voicechat.tasks.PlayerPeerTicker;
import com.craftmend.openaudiomc.spigot.modules.voicechat.tasks.PlayerVicinityMessageTask;
import com.craftmend.openaudiomc.spigot.modules.voicechat.tasks.TickVoicePacketQueue;
Expand Down Expand Up @@ -182,12 +182,12 @@ public void onEnable() {
// enable default rules
if (StorageKey.SETTINGS_VOICE_FILTERS_GAMEMODE.getBoolean()) {
OpenAudioLogger.toConsole("Enabling voicechat gamemode filter");
getService(FilterService.class).addFilterFunction(new GamemodeFilter());
getService(FilterService.class).addCustomFilter(new GamemodeFilterCustom());
}

if (StorageKey.SETTINGS_VOICE_FILTERS_TEAM.getBoolean()) {
OpenAudioLogger.toConsole("Enabling voicechat team filter");
getService(FilterService.class).addFilterFunction(new TeamFilter());
getService(FilterService.class).addCustomFilter(new TeamFilterCustom());
}
}

Expand Down

This file was deleted.

Loading

0 comments on commit faa7278

Please sign in to comment.