Skip to content

Commit

Permalink
feat: port to pv 2.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Apehum committed Apr 8, 2024
1 parent a0177c8 commit 12aae23
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 47 deletions.
18 changes: 11 additions & 7 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
plugins {
id("java")
kotlin("jvm") version("1.6.10")
id("su.plo.voice.plugin") version("1.0.0")
kotlin("jvm") version(libs.versions.kotlin.get())
alias(libs.plugins.pv.entrypoints)
alias(libs.plugins.pv.java.templates)
}

group = "su.plo"
version = "1.0.0"

dependencies {
compileOnly("su.plo.voice.api:server:2.0.0+ALPHA")
compileOnly(libs.pv)
annotationProcessor(libs.lombok)
}

annotationProcessor("org.projectlombok:lombok:+")
repositories {
mavenLocal()
mavenCentral()
maven("https://repo.plasmoverse.com/snapshots")
maven("https://repo.plasmoverse.com/releases")
}

tasks {
Expand Down
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Plasmo Voice 2.1.x support. This version supports only PV 2.1+, if you're using PV 2.0.x, use 1.0.x versions of the addon.
2 changes: 2 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
group=su.plo
version=1.1.0
16 changes: 16 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[versions]
kotlin = "1.8.22"
lombok = "1.18.24"

pv = "2.1.0-SNAPSHOT"

pv-gradle-plugin = "1.0.2-SNAPSHOT"

[libraries]
lombok = { module = "org.projectlombok:lombok", version.ref = "lombok" }

pv = { module = "su.plo.voice.api:server", version.ref = "pv" }

[plugins]
pv-entrypoints = { id = "su.plo.voice.plugin.entrypoints", version.ref = "pv-gradle-plugin" }
pv-java-templates = { id = "su.plo.voice.plugin.java-templates", version.ref = "pv-gradle-plugin" }
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
4 changes: 2 additions & 2 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ pluginManagement {
mavenLocal()
mavenCentral()
gradlePluginPortal()
maven("https://repo.plo.su")
maven("https://repo.plasmoverse.com/snapshots")
maven("https://repo.plasmoverse.com/releases")
maven("https://jitpack.io/")
maven("https://maven.minecraftforge.net")
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package su.plo.voice.spectator;

// The constants are replaced before compilation
public final class BuildConstants {

public static final String VERSION = "${version}";
}
82 changes: 45 additions & 37 deletions src/main/java/su/plo/voice/spectator/SpectatorAddon.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
package su.plo.voice.spectator;

import com.google.common.collect.Maps;
import com.google.inject.Inject;
import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
import su.plo.config.provider.ConfigurationProvider;
import su.plo.config.provider.toml.TomlConfiguration;
import su.plo.lib.api.server.entity.MinecraftServerPlayerEntity;
import su.plo.lib.api.server.world.ServerPos3d;
import su.plo.slib.api.server.entity.McServerEntity;
import su.plo.slib.api.server.entity.player.McServerPlayer;
import su.plo.slib.api.server.position.ServerPos3d;
import su.plo.voice.api.addon.AddonInitializer;
import su.plo.voice.api.addon.AddonLoaderScope;
import su.plo.voice.api.addon.InjectPlasmoVoice;
import su.plo.voice.api.addon.annotation.Addon;
import su.plo.voice.api.event.EventCancellableBase;
import su.plo.voice.api.event.EventSubscribe;
import su.plo.voice.api.server.PlasmoVoiceServer;
import su.plo.voice.api.server.audio.capture.SelfActivationInfo;
import su.plo.voice.api.server.audio.source.ServerEntitySource;
import su.plo.voice.api.server.audio.source.ServerPlayerSource;
import su.plo.voice.api.server.audio.source.ServerPositionalSource;
import su.plo.voice.api.server.audio.source.ServerProximitySource;
import su.plo.voice.api.server.audio.source.ServerStaticSource;
import su.plo.voice.api.server.event.audio.source.ServerSourceAudioPacketEvent;
import su.plo.voice.api.server.event.audio.source.ServerSourcePacketEvent;
Expand All @@ -38,12 +39,12 @@
import java.util.Optional;
import java.util.UUID;

@Addon(id = "pv-addon-spectator", scope = AddonLoaderScope.SERVER, version = "1.0.0", authors = {"Apehum"})
@Addon(id = "pv-addon-spectator", scope = AddonLoaderScope.SERVER, version = BuildConstants.VERSION, authors = {"Apehum"})
public final class SpectatorAddon implements AddonInitializer {

private static final ConfigurationProvider toml = ConfigurationProvider.getProvider(TomlConfiguration.class);

@Inject
@InjectPlasmoVoice
private PlasmoVoiceServer voiceServer;
private SelfActivationInfo selfActivationInfo;
private SpectatorConfig config;
Expand Down Expand Up @@ -125,7 +126,7 @@ public void onSourcePacket(@NotNull ServerSourcePacketEvent event) {

private void loadConfig() {
try {
File addonFolder = new File(voiceServer.getConfigsFolder(), "pv-addon-spectator");
File addonFolder = new File(voiceServer.getMinecraftServer().getConfigsFolder(), "pv-addon-spectator");
File configFile = new File(addonFolder, "config.toml");

this.config = toml.load(SpectatorConfig.class, configFile, false);
Expand All @@ -137,15 +138,17 @@ private void loadConfig() {
staticSourceById.forEach((playerId, source) -> source.setIconVisible(config.showIcon()));
}

private Optional<ServerPositionalSource<?>> getTargetSource(@NotNull ServerPlayerSource source,
@NotNull VoiceServerPlayer player,
@NotNull EventCancellableBase event) {
private Optional<ServerProximitySource<?>> getTargetSource(
@NotNull ServerPlayerSource source,
@NotNull VoiceServerPlayer player,
@NotNull EventCancellableBase event
) {
if (!player.getInstance().isSpectator()) {
removeSources(player);
return Optional.empty();
}

if (player.getInstance().getSpectatorTarget().isPresent()) {
if (player.getInstance().getSpectatorTarget() != null) {
event.setCancelled(true);
return Optional.of(getEntitySource(source, player));
} else {
Expand All @@ -154,18 +157,23 @@ private Optional<ServerPositionalSource<?>> getTargetSource(@NotNull ServerPlaye
}

private void removeSources(@NotNull VoiceServerPlayer player) {
lastPlayerPositionTimestampById.remove(player.getInstance().getUUID());
ServerStaticSource staticSource = staticSourceById.remove(player.getInstance().getUUID());
UUID playerUuid = player.getInstance().getUuid();

lastPlayerPositionTimestampById.remove(playerUuid);

ServerStaticSource staticSource = staticSourceById.remove(playerUuid);
if (staticSource != null) staticSource.getLine().removeSource(staticSource);

ServerEntitySource entitySource = entitySourceById.remove(player.getInstance().getUUID());
ServerEntitySource entitySource = entitySourceById.remove(playerUuid);
if (entitySource != null) entitySource.getLine().removeSource(entitySource);
}

private ServerStaticSource getStaticSource(@NotNull ServerPlayerSource playerSource,
@NotNull VoiceServerPlayer player) {
private ServerStaticSource getStaticSource(
@NotNull ServerPlayerSource playerSource,
@NotNull VoiceServerPlayer player
) {
PlayerSourceInfo sourceInfo = playerSource.getSourceInfo();
SourceLineKey sourceLineKey = new SourceLineKey(player.getInstance().getUUID(), playerSource.getLine().getId());
SourceLineKey sourceLineKey = new SourceLineKey(player.getInstance().getUuid(), playerSource.getLine().getId());

ServerStaticSource staticSource = staticSourceById.computeIfAbsent(
sourceLineKey,
Expand All @@ -178,7 +186,7 @@ private ServerStaticSource getStaticSource(@NotNull ServerPlayerSource playerSou
source.setIconVisible(config.showIcon());

source.addFilter((listener) ->
!listener.equals(player) && !((MinecraftServerPlayerEntity) listener.getInstance()).isSpectator()
!listener.equals(player) && !((McServerPlayer) listener.getInstance()).isSpectator()
);
return source;
}
Expand All @@ -190,25 +198,25 @@ private ServerStaticSource getStaticSource(@NotNull ServerPlayerSource playerSou
return staticSource;
}

private ServerEntitySource getEntitySource(@NotNull ServerPlayerSource playerSource,
@NotNull VoiceServerPlayer player) {
private ServerEntitySource getEntitySource(
@NotNull ServerPlayerSource playerSource,
@NotNull VoiceServerPlayer player
) {
PlayerSourceInfo sourceInfo = playerSource.getSourceInfo();
SourceLineKey sourceLineKey = new SourceLineKey(player.getInstance().getUUID(), playerSource.getLine().getId());

ServerEntitySource entitySource = entitySourceById.computeIfAbsent(
sourceLineKey,
(sourceId) -> {
ServerEntitySource source = playerSource.getLine().createEntitySource(
player.getInstance().getSpectatorTarget().get(),
sourceInfo.isStereo(),
new OpusDecoderInfo()
);
source.setIconVisible(config.showIcon());
SourceLineKey sourceLineKey = new SourceLineKey(player.getInstance().getUuid(), playerSource.getLine().getId());

McServerEntity spectatorTarget = player.getInstance().getSpectatorTarget();

ServerEntitySource entitySource = entitySourceById.get(sourceLineKey);
if (entitySource == null ||
!entitySource.getEntity().getInstance().equals(spectatorTarget.getInstance())
) {
entitySource = playerSource.getLine().createEntitySource(spectatorTarget, sourceInfo.isStereo());
entitySource.setIconVisible(config.showIcon());
entitySource.addFilter((listener) -> !listener.equals(player));
entitySourceById.put(sourceLineKey, entitySource);
}

source.addFilter((listener) -> !listener.equals(player));
return source;
}
);
entitySource.setStereo(sourceInfo.isStereo());
entitySource.setName(player.getInstance().getName());

Expand All @@ -217,7 +225,7 @@ private ServerEntitySource getEntitySource(@NotNull ServerPlayerSource playerSou

private void updateSourcePosition(@NotNull VoiceServerPlayer player, @NotNull ServerStaticSource source) {
long lastUpdate = lastPlayerPositionTimestampById.getOrDefault(
player.getInstance().getUUID(),
player.getInstance().getUuid(),
0L
);

Expand All @@ -226,7 +234,7 @@ private void updateSourcePosition(@NotNull VoiceServerPlayer player, @NotNull Se
position.setY(position.getY() + player.getInstance().getHitBoxHeight() + 0.5D);
source.setPosition(position);

lastPlayerPositionTimestampById.put(player.getInstance().getUUID(), System.currentTimeMillis());
lastPlayerPositionTimestampById.put(player.getInstance().getUuid(), System.currentTimeMillis());
}

@EqualsAndHashCode
Expand Down

0 comments on commit 12aae23

Please sign in to comment.