mirror of
https://github.com/Thorfusion/Mekanism-Community-Edition.git
synced 2026-11-26 23:03:01 +00:00
Readd voice server (#5374)
* Add back the voice server defaulted to disabled * Port 098472a make voice output properly sleepy * Cleanup some of the voice server code * Keep track of UUID in VoiceConnection instead of username * Collapse a few missed error logging into stack trace printing * Add tooltip to walkie talkie when voice server is disabled * Fix single player crash if voice server is enabled after launch before world start
This commit is contained in:
1 parent
a1e0688af8
commit
a776879eed
16 files changed
+661
-6
No files matched your search
@@ -0,0 +1,107 @@
|
||||
package mekanism.common.voice;
|
||||
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketException;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import mekanism.common.Mekanism;
|
||||
import mekanism.common.config.MekanismConfig.general;
|
||||
|
||||
public class VoiceServerManager {
|
||||
|
||||
private Set<VoiceConnection> connections = new HashSet<>();
|
||||
private ServerSocket serverSocket;
|
||||
private Thread listenThread;
|
||||
private boolean foundLocal = false;
|
||||
private boolean running;
|
||||
|
||||
public void start() {
|
||||
Mekanism.logger.info("VoiceServer: Starting up server...");
|
||||
|
||||
try {
|
||||
running = true;
|
||||
serverSocket = new ServerSocket(general.VOICE_PORT);
|
||||
(listenThread = new ListenThread()).start();
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
try {
|
||||
Mekanism.logger.info("VoiceServer: Shutting down server...");
|
||||
|
||||
try {
|
||||
listenThread.interrupt();
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
|
||||
foundLocal = false;
|
||||
|
||||
try {
|
||||
serverSocket.close();
|
||||
serverSocket = null;
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Mekanism.logger.error("VoiceServer: Error while shutting down server.", e);
|
||||
}
|
||||
|
||||
running = false;
|
||||
}
|
||||
|
||||
public void removeConnection(VoiceConnection connection) {
|
||||
connections.remove(connection);
|
||||
}
|
||||
|
||||
public boolean isFoundLocal() {
|
||||
return foundLocal;
|
||||
}
|
||||
|
||||
public void setFoundLocal(boolean found) {
|
||||
foundLocal = found;
|
||||
}
|
||||
|
||||
public void sendToPlayers(short byteCount, byte[] audioData, VoiceConnection connection) {
|
||||
if (connection.getPlayer() == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
int channel = connection.getCurrentChannel();
|
||||
|
||||
if (channel == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (VoiceConnection iterConn : connections) {
|
||||
if (iterConn.getPlayer() != null && iterConn != connection && iterConn.canListen(channel)) {
|
||||
iterConn.sendToPlayer(byteCount, audioData, connection);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class ListenThread extends Thread {
|
||||
|
||||
private ListenThread() {
|
||||
setDaemon(true);
|
||||
setName("VoiceServer Listen Thread");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (running) {
|
||||
try {
|
||||
Socket s = serverSocket.accept();
|
||||
VoiceConnection connection = new VoiceConnection(s);
|
||||
connection.start();
|
||||
connections.add(connection);
|
||||
|
||||
Mekanism.logger.info("VoiceServer: Accepted new connection.");
|
||||
} catch (SocketException | NullPointerException ignored) {
|
||||
} catch (Exception e) {
|
||||
Mekanism.logger.error("VoiceServer: Error while accepting connection.", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user