Skip to content

Commit

Permalink
fix(proxy): replace VoicePlayer if it's not equals in channel handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
Apehum committed Mar 3, 2023
1 parent e75a5e0 commit 0799b77
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package su.plo.voice.proxy.connection;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.apache.logging.log4j.LogManager;
import org.jetbrains.annotations.NotNull;
import su.plo.voice.api.proxy.PlasmoVoiceProxy;
Expand All @@ -17,11 +20,13 @@
// should be two channel handlers
// server -> proxy -> player (ServerToPlayerChannelHandler)
// player -> proxy -> server (PlayerToServerChannelHandler)
@RequiredArgsConstructor
@AllArgsConstructor
public final class PlayerToServerChannelHandler implements ServerPacketTcpHandler {

private final PlasmoVoiceProxy voiceProxy;
private final VoiceProxyPlayer player;
@Setter
@Getter
private VoiceProxyPlayer player;

@Override
public void handle(@NotNull PlayerInfoPacket packet) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import com.google.common.collect.Sets;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import lombok.RequiredArgsConstructor;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import org.jetbrains.annotations.NotNull;
import su.plo.voice.api.proxy.player.VoiceProxyPlayer;
import su.plo.voice.api.proxy.server.RemoteServer;
Expand All @@ -26,11 +28,13 @@
import java.util.UUID;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@AllArgsConstructor
public final class ServerToPlayerChannelHandler implements ClientPacketTcpHandler {

private final BaseVoiceProxy voiceProxy;
private final VoiceProxyPlayer player;
@Setter
@Getter
private VoiceProxyPlayer player;

@Override
public void handle(@NotNull ConnectionPacket packet) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ public void clearConnections() {
getConnections().forEach(this::removeConnection);
}

private void disconnect(UdpProxyConnection connection) {
private void disconnect(@NotNull UdpProxyConnection connection) {
if (!connection.isConnected()) return;
connection.disconnect();

secretByPlayerId.remove(connection.getPlayer().getInstance().getUUID());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,29 @@ public void onPlasmoVoicePacket(@NotNull PluginMessageEvent event) {
Player player = (Player) event.getSource();
VoiceProxyPlayer voicePlayer = voiceProxy.getPlayerManager().wrap(player);

handler = playerToServerChannels.computeIfAbsent(
PlayerToServerChannelHandler playerToServerHandler = playerToServerChannels.computeIfAbsent(
player.getUniqueId(),
(playerId) -> new PlayerToServerChannelHandler(voiceProxy, voicePlayer)
);

if (!playerToServerHandler.getPlayer().equals(voicePlayer)) {
playerToServerHandler.setPlayer(voicePlayer);
}
handler = playerToServerHandler;
} else if (event.getTarget() instanceof Player) {
Player player = (Player) event.getTarget();
VoiceProxyPlayer voicePlayer = voiceProxy.getPlayerManager().wrap(player);

handler = serverToPlayerChannels.computeIfAbsent(
ServerToPlayerChannelHandler serverToPlayerHandler = serverToPlayerChannels.computeIfAbsent(
player.getUniqueId(),
(playerId) -> new ServerToPlayerChannelHandler(voiceProxy, voicePlayer)
);

if (!serverToPlayerHandler.getPlayer().equals(voicePlayer)) {
serverToPlayerHandler.setPlayer(voicePlayer);
}

handler = serverToPlayerHandler;
} else return;

try {
Expand Down

0 comments on commit 0799b77

Please sign in to comment.