Skip to content

Commit

Permalink
perf: some optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
Apehum committed Jan 7, 2023
1 parent 6ea39d1 commit 5f3240e
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 24 deletions.
8 changes: 5 additions & 3 deletions src/main/java/su/plo/voice/PlasmoVoice.java
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,9 @@ public boolean unmute(UUID playerUUID, boolean silent) {

@Override
public Set<Player> getConnectedPlayers() {
return SocketServerUDP.clients.keySet();
return SocketServerUDP.clients.values().stream()
.map(SocketClientUDP::getPlayer)
.collect(Collectors.toSet());
}

@Override
Expand All @@ -375,7 +377,7 @@ public boolean sendVoicePacketToPlayer(Packet packet, Player recipient) {
}

try {
SocketServerUDP.sendTo(bytes, SocketServerUDP.clients.get(recipient));
SocketServerUDP.sendTo(bytes, SocketServerUDP.clients.get(recipient.getUniqueId()));
} catch (IOException e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -415,7 +417,7 @@ public Map<UUID, ServerMutedEntity> getMutedMap() {
public boolean hasVoiceChat(UUID player) {
return SocketServerUDP.clients.entrySet()
.stream()
.anyMatch(entry -> entry.getKey().getUniqueId().equals(player));
.anyMatch(entry -> entry.getKey().equals(player));
}

@Nullable
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/su/plo/voice/commands/VoiceList.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import su.plo.voice.PlasmoVoice;
import su.plo.voice.socket.SocketClientUDP;
import su.plo.voice.socket.SocketServerUDP;

import java.util.ArrayList;
Expand All @@ -15,7 +16,9 @@
public class VoiceList implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String s, String[] args) {
List<String> clients = SocketServerUDP.clients.keySet().stream()
List<String> clients = SocketServerUDP.clients.values()
.stream()
.map(SocketClientUDP::getPlayer)
.map(Player::getName)
.collect(Collectors.toList());

Expand Down
6 changes: 4 additions & 2 deletions src/main/java/su/plo/voice/commands/VoiceReload.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.UUID;

public class VoiceReload implements CommandExecutor {
@Override
Expand All @@ -26,9 +27,10 @@ public boolean onCommand(CommandSender sender, Command command, String s, String

Bukkit.getScheduler().runTaskAsynchronously(PlasmoVoice.getInstance(), () -> {
try {
Enumeration<Player> it = SocketServerUDP.clients.keys();
Enumeration<UUID> it = SocketServerUDP.clients.keys();
while (it.hasMoreElements()) {
Player player = it.nextElement();
Player player = Bukkit.getPlayer(it.nextElement());
if (player == null) continue;

ConfigPacket configPacket = new ConfigPacket(
config.getSampleRate(),
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/su/plo/voice/listeners/PlayerListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void onPlayerJoin(PlayerJoinEvent e) {

if (PlasmoVoice.getInstance().getVoiceConfig().isClientModRequired()) {
kickTimeouts.put(player.getUniqueId(), Bukkit.getScheduler().runTaskLater(PlasmoVoice.getInstance(), () -> {
if (!SocketServerUDP.clients.containsKey(player)) {
if (!SocketServerUDP.clients.containsKey(player.getUniqueId())) {
if (!PlasmoVoice.getInstance().getConfig().getBoolean("disable_logs")) {
PlasmoVoice.getVoiceLogger().info(String.format("Player: %s does not have the mod installed!", player.getName()));
}
Expand All @@ -111,7 +111,7 @@ public void onPlayerQuit(PlayerQuitEvent e) {
}

public static void disconnectClient(Player player) {
SocketClientUDP clientUDP = SocketServerUDP.clients.get(player);
SocketClientUDP clientUDP = SocketServerUDP.clients.get(player.getUniqueId());

try {
if (clientUDP != null) {
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/su/plo/voice/listeners/PluginChannelListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,11 @@ public static void sendToClients(Packet packet, @Nullable Player sender) {
Bukkit.getScheduler().runTaskAsynchronously(PlasmoVoice.getInstance(), () -> {
try {
byte[] pkt = PacketTCP.write(packet);
Enumeration<Player> it = SocketServerUDP.clients.keys();
Enumeration<UUID> it = SocketServerUDP.clients.keys();
while (it.hasMoreElements()) {
Player player = it.nextElement();
Player player = Bukkit.getPlayer(it.nextElement());
if (player == null) continue;

if (sender != null) {
if (player.canSee(sender)) {
player.sendPluginMessage(PlasmoVoice.getInstance(), "plasmo:voice", pkt);
Expand All @@ -101,9 +103,11 @@ public static void sendToClients(Packet packet, UUID except, @Nullable Player se
Bukkit.getScheduler().runTaskAsynchronously(PlasmoVoice.getInstance(), () -> {
try {
byte[] pkt = PacketTCP.write(packet);
Enumeration<Player> it = SocketServerUDP.clients.keys();
Enumeration<UUID> it = SocketServerUDP.clients.keys();
while (it.hasMoreElements()) {
Player player = it.nextElement();
Player player = Bukkit.getPlayer(it.nextElement());
if (player == null) continue;

if (!player.getUniqueId().equals(except)) {
if (sender != null) {
if (player.canSee(sender)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public boolean persist() {
@Override
public String onPlaceholderRequest(Player player, @NotNull String params) {
if (params.equalsIgnoreCase("installed") && player != null) {
return SocketServerUDP.clients.containsKey(player) ? "true" : "false";
return SocketServerUDP.clients.containsKey(player.getUniqueId()) ? "true" : "false";
} else if (params.equalsIgnoreCase("players")) {
return String.valueOf(SocketServerUDP.clients.size());
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/su/plo/voice/socket/SocketClientUDP.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ public int getPort() {
}

public void close() {
if (SocketServerUDP.clients.containsKey(player)) {
if (SocketServerUDP.clients.containsKey(player.getUniqueId())) {
// call event
Bukkit.getScheduler().runTask(PlasmoVoice.getInstance(), () ->
Bukkit.getPluginManager().callEvent(new PlayerVoiceDisconnectedEvent(player)));

if (!PlasmoVoice.getInstance().getConfig().getBoolean("disable_logs")) {
PlasmoVoice.getVoiceLogger().info("Remove client UDP: " + this.player.getName());
}
SocketServerUDP.clients.remove(player);
SocketServerUDP.clients.remove(player.getUniqueId());
SocketServerUDP.clientByAddress.remove(socketAddress);
}
}
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/su/plo/voice/socket/SocketServerUDP.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package su.plo.voice.socket;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import su.plo.voice.PlasmoVoice;
Expand All @@ -14,7 +15,7 @@
import java.util.concurrent.ConcurrentHashMap;

public class SocketServerUDP extends Thread {
public static final ConcurrentHashMap<Player, SocketClientUDP> clients = new ConcurrentHashMap<>();
public static final ConcurrentHashMap<UUID, SocketClientUDP> clients = new ConcurrentHashMap<>();
public static final ConcurrentHashMap<InetSocketAddress, SocketClientUDP> clientByAddress = new ConcurrentHashMap<>();

public static final Map<UUID, Long> talking = new HashMap<>();
Expand Down Expand Up @@ -44,11 +45,13 @@ public static void sendToNearbyPlayers(Packet packet, Player player, double maxD
final Location playerLocation = player.getLocation();
final Location receiverLocation = playerLocation.clone();

SocketServerUDP.clients.forEach((receiver, sock) -> {
if (!player.getUniqueId().equals(receiver.getUniqueId())) {
SocketServerUDP.clients.forEach((receiverId, sock) -> {
if (!player.getUniqueId().equals(receiverId)) {
if (maxDistanceSquared > 0) {
receiver.getLocation(receiverLocation);
Player receiver = Bukkit.getPlayer(receiverId);
if (receiver == null) return;

receiver.getLocation(receiverLocation);
if (!playerLocation.getWorld().getName().equals(receiverLocation.getWorld().getName())) {
return;
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/su/plo/voice/socket/SocketServerUDPQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,18 @@ public void run() {
String type = player.get().getListeningPluginChannels().contains("fml:handshake") ? "forge" : "fabric";
SocketClientUDP sock = new SocketClientUDP(player.get(), type, message.getInetSocketAddress());

if (!SocketServerUDP.clients.containsKey(player.get())) {
SocketServerUDP.clients.put(player.get(), sock);
if (!SocketServerUDP.clients.containsKey(player.get().getUniqueId())) {
SocketServerUDP.clients.put(player.get().getUniqueId(), sock);
SocketServerUDP.clientByAddress.put(
message.getInetSocketAddress(),
sock
);

// Clients list packet
List<UUID> clients = new ArrayList<>();
SocketServerUDP.clients.forEach((p, c) -> {
if (player.get().canSee(p)) {
clients.add(p.getUniqueId());
SocketServerUDP.clients.forEach((playerId, c) -> {
if (player.get().canSee(c.getPlayer())) {
clients.add(playerId);
}
});

Expand Down

0 comments on commit 5f3240e

Please sign in to comment.