mirror of
https://github.com/Thorfusion/Mekanism-Community-Edition.git
synced 2026-11-25 14:24:35 +00:00
Much more server work
This commit is contained in:
1 parent
17fdc03e9f
commit
cafe2a7dfb
5 files changed
+289
-57
No files matched your search
@@ -98,6 +98,7 @@ import net.minecraftforge.client.MinecraftForgeClient;
|
||||
import net.minecraftforge.common.Configuration;
|
||||
import cpw.mods.fml.client.FMLClientHandler;
|
||||
import cpw.mods.fml.client.registry.ClientRegistry;
|
||||
import cpw.mods.fml.client.registry.KeyBindingRegistry;
|
||||
import cpw.mods.fml.client.registry.RenderingRegistry;
|
||||
import cpw.mods.fml.common.network.NetworkRegistry;
|
||||
import cpw.mods.fml.common.registry.TickRegistry;
|
||||
@@ -369,6 +370,8 @@ public class ClientProxy extends CommonProxy
|
||||
|
||||
NetworkRegistry.instance().registerConnectionHandler(new ClientConnectionHandler());
|
||||
NetworkRegistry.instance().registerConnectionHandler(new VoiceClientManager());
|
||||
|
||||
KeyBindingRegistry.registerKeyBinding(new MekanismKeyHandler());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package mekanism.client;
|
||||
|
||||
import java.util.EnumSet;
|
||||
|
||||
import net.minecraft.client.settings.KeyBinding;
|
||||
|
||||
import org.lwjgl.input.Keyboard;
|
||||
|
||||
import cpw.mods.fml.client.registry.KeyBindingRegistry.KeyHandler;
|
||||
import cpw.mods.fml.common.TickType;
|
||||
|
||||
public class MekanismKeyHandler extends KeyHandler
|
||||
{
|
||||
public static KeyBinding modeSwitch = new KeyBinding("Mekanism Mode Switch", Keyboard.KEY_M);
|
||||
public static KeyBinding voice = new KeyBinding("Mekanism Voice", Keyboard.KEY_U);
|
||||
|
||||
public static boolean voiceDown = false;
|
||||
|
||||
public MekanismKeyHandler()
|
||||
{
|
||||
super(new KeyBinding[] {modeSwitch, voice});
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLabel()
|
||||
{
|
||||
return "MekanismKey";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyDown(EnumSet<TickType> types, KeyBinding kb, boolean tickEnd, boolean isRepeat)
|
||||
{
|
||||
if(kb == voice)
|
||||
{
|
||||
voiceDown = true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyUp(EnumSet<TickType> types, KeyBinding kb, boolean tickEnd)
|
||||
{
|
||||
if(kb == voice)
|
||||
{
|
||||
voiceDown = false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public EnumSet<TickType> ticks()
|
||||
{
|
||||
return EnumSet.of(TickType.CLIENT);
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import java.net.InetAddress;
|
||||
import java.net.Socket;
|
||||
|
||||
import javax.sound.sampled.AudioFormat;
|
||||
import javax.sound.sampled.AudioInputStream;
|
||||
import javax.sound.sampled.AudioSystem;
|
||||
import javax.sound.sampled.DataLine;
|
||||
import javax.sound.sampled.SourceDataLine;
|
||||
@@ -59,6 +60,7 @@ public class VoiceClientManager implements IConnectionHandler
|
||||
try {
|
||||
socket = new Socket(server, 36123);
|
||||
running = true;
|
||||
start();
|
||||
} catch(Exception e) {}
|
||||
}
|
||||
|
||||
@@ -69,11 +71,120 @@ public class VoiceClientManager implements IConnectionHandler
|
||||
try {
|
||||
socket = new Socket(InetAddress.getLocalHost().getHostAddress(), 36123);
|
||||
running = true;
|
||||
start();
|
||||
} catch(Exception e) {}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void connectionClosed(INetworkManager manager)
|
||||
{
|
||||
stop();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clientLoggedIn(NetHandler clientHandler, INetworkManager manager, Packet1Login login)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void start()
|
||||
{
|
||||
try {
|
||||
input = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
|
||||
output = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
|
||||
|
||||
//Speaker (Out)
|
||||
new Thread(new Runnable()
|
||||
{
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
try {
|
||||
VoiceClientManager.this.sourceLine = ((SourceDataLine)AudioSystem.getLine(VoiceClientManager.this.speaker));
|
||||
VoiceClientManager.this.sourceLine.open(VoiceClientManager.this.format, 2200);
|
||||
VoiceClientManager.this.sourceLine.start();
|
||||
|
||||
while(running)
|
||||
{
|
||||
System.out.println("Looped");
|
||||
short byteCount = VoiceClientManager.this.input.readShort();
|
||||
byte[] audioData = new byte[byteCount];
|
||||
VoiceClientManager.this.input.readFully(audioData);
|
||||
|
||||
VoiceClientManager.this.sourceLine.write(audioData, 0, audioData.length);
|
||||
}
|
||||
} catch(Exception e) {
|
||||
System.err.println("Error while running speaker loop.");
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
|
||||
//Microphone (In)
|
||||
new Thread(new Runnable()
|
||||
{
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
try {
|
||||
VoiceClientManager.this.targetLine = ((TargetDataLine)AudioSystem.getLine(microphone));
|
||||
VoiceClientManager.this.targetLine.open(VoiceClientManager.this.format, 2200);
|
||||
VoiceClientManager.this.targetLine.start();
|
||||
AudioInputStream audioInput = new AudioInputStream(VoiceClientManager.this.targetLine);
|
||||
|
||||
boolean doFlush = false;
|
||||
|
||||
while(running)
|
||||
{
|
||||
if(MekanismKeyHandler.voiceDown)
|
||||
{
|
||||
targetLine.flush();
|
||||
|
||||
while(running && MekanismKeyHandler.voiceDown)
|
||||
{
|
||||
int availableBytes = audioInput.available();
|
||||
byte[] audioData = new byte[availableBytes > 2200 ? 2200 : availableBytes];
|
||||
int bytesRead = audioInput.read(audioData, 0, audioData.length);
|
||||
|
||||
if(bytesRead > 0)
|
||||
{
|
||||
System.out.println("Writing");
|
||||
output.writeShort(audioData.length);
|
||||
output.write(audioData);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
Thread.sleep(200L);
|
||||
} catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
doFlush = true;
|
||||
}
|
||||
else if(doFlush)
|
||||
{
|
||||
VoiceClientManager.this.output.flush();
|
||||
doFlush = false;
|
||||
}
|
||||
|
||||
try {
|
||||
Thread.sleep(20L);
|
||||
} catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
audioInput.close();
|
||||
} catch(Exception e) {
|
||||
System.err.println("Error while running microphone loop.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
} catch(Exception e) {}
|
||||
}
|
||||
|
||||
public void stop()
|
||||
{
|
||||
try {
|
||||
sourceLine.flush();
|
||||
@@ -92,48 +203,9 @@ public class VoiceClientManager implements IConnectionHandler
|
||||
socket.close();
|
||||
socket = null;
|
||||
running = false;
|
||||
} catch(Exception e) {}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clientLoggedIn(NetHandler clientHandler, INetworkManager manager, Packet1Login login)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void init()
|
||||
{
|
||||
try {
|
||||
input = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
|
||||
output = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
|
||||
|
||||
//Speaker (Out)
|
||||
new Thread(new Runnable()
|
||||
{
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
try {
|
||||
VoiceClientManager.this.sourceLine = ((SourceDataLine)AudioSystem.getLine(VoiceClientManager.this.speaker));
|
||||
VoiceClientManager.this.sourceLine.open(VoiceClientManager.this.format, 2200);
|
||||
VoiceClientManager.this.sourceLine.start();
|
||||
} catch(Exception e) {}
|
||||
}
|
||||
}).start();
|
||||
|
||||
//Microphone (In)
|
||||
new Thread(new Runnable()
|
||||
{
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
try {
|
||||
VoiceClientManager.this.targetLine = ((TargetDataLine)AudioSystem.getLine(microphone));
|
||||
VoiceClientManager.this.targetLine.open(VoiceClientManager.this.format, 2200);
|
||||
VoiceClientManager.this.targetLine.start();
|
||||
} catch(Exception e) {}
|
||||
}
|
||||
}).start();
|
||||
} catch(Exception e) {}
|
||||
} catch(Exception e) {
|
||||
System.err.println("Error while ending client connection.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,9 @@ import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.net.Socket;
|
||||
|
||||
import mekanism.common.item.ItemWalkieTalkie;
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import cpw.mods.fml.common.FMLCommonHandler;
|
||||
|
||||
public class VoiceConnection
|
||||
@@ -62,24 +64,89 @@ public class VoiceConnection
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
try {
|
||||
while(open)
|
||||
{
|
||||
while(open)
|
||||
{
|
||||
try {
|
||||
short byteCount = VoiceConnection.this.input.readShort();
|
||||
byte[] audioData = new byte[byteCount];
|
||||
VoiceConnection.this.input.readFully(audioData);
|
||||
|
||||
if(byteCount > 0)
|
||||
{
|
||||
Mekanism.voiceManager.sendToPlayers(byteCount, audioData, VoiceConnection.this);
|
||||
}
|
||||
} catch(Exception e) {
|
||||
open = false;
|
||||
}
|
||||
} catch(Exception e) {
|
||||
open = false;
|
||||
}
|
||||
|
||||
if(!open)
|
||||
{
|
||||
try {
|
||||
input.close();
|
||||
output.close();
|
||||
socket.close();
|
||||
} catch(Exception e) {}
|
||||
kill();
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
public void kill()
|
||||
{
|
||||
try {
|
||||
input.close();
|
||||
output.close();
|
||||
socket.close();
|
||||
|
||||
Mekanism.voiceManager.connections.remove(this);
|
||||
} catch(Exception e) {}
|
||||
}
|
||||
|
||||
public void sendToPlayer(short byteCount, byte[] audioData, VoiceConnection connection)
|
||||
{
|
||||
if(!open)
|
||||
{
|
||||
kill();
|
||||
}
|
||||
|
||||
try {
|
||||
output.writeShort(byteCount);
|
||||
output.write(audioData);
|
||||
|
||||
output.flush();
|
||||
} catch(Exception e) {}
|
||||
}
|
||||
|
||||
public boolean canListen(int channel)
|
||||
{
|
||||
for(ItemStack itemStack : entityPlayer.inventory.mainInventory)
|
||||
{
|
||||
if(itemStack != null)
|
||||
{
|
||||
if(itemStack.getItem() instanceof ItemWalkieTalkie)
|
||||
{
|
||||
if(((ItemWalkieTalkie)itemStack.getItem()).getChannel(itemStack) == channel)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public int getCurrentChannel()
|
||||
{
|
||||
ItemStack itemStack = entityPlayer.getCurrentEquippedItem();
|
||||
|
||||
if(itemStack != null)
|
||||
{
|
||||
ItemWalkieTalkie walkieTalkie = (ItemWalkieTalkie)itemStack.getItem();
|
||||
|
||||
if(walkieTalkie != null)
|
||||
{
|
||||
return walkieTalkie.getChannel(itemStack);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -21,12 +21,14 @@ public class VoiceServerManager implements IConnectionHandler
|
||||
|
||||
public boolean running;
|
||||
|
||||
public Thread listenThread;
|
||||
|
||||
public void start()
|
||||
{
|
||||
try {
|
||||
serverSocket = new ServerSocket(36123);
|
||||
|
||||
new Thread(new Runnable()
|
||||
listenThread = new Thread(new Runnable()
|
||||
{
|
||||
@Override
|
||||
public void run()
|
||||
@@ -37,10 +39,15 @@ public class VoiceServerManager implements IConnectionHandler
|
||||
Socket s = serverSocket.accept();
|
||||
VoiceConnection connection = new VoiceConnection(s);
|
||||
connections.add(connection);
|
||||
} catch(Exception e) {}
|
||||
} catch(Exception e) {
|
||||
System.err.println("Error while accepting connection.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
});
|
||||
|
||||
listenThread.start();
|
||||
} catch(Exception e) {}
|
||||
|
||||
running = true;
|
||||
@@ -49,13 +56,43 @@ public class VoiceServerManager implements IConnectionHandler
|
||||
public void stop()
|
||||
{
|
||||
try {
|
||||
listenThread.interrupt();
|
||||
|
||||
serverSocket.close();
|
||||
serverSocket = null;
|
||||
} catch(Exception e) {}
|
||||
} catch(Exception e) {
|
||||
System.err.println("Error while stopping voice server.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
running = false;
|
||||
}
|
||||
|
||||
public void sendToPlayers(short byteCount, byte[] audioData, VoiceConnection connection)
|
||||
{
|
||||
if(connection.entityPlayer == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int channel = connection.getCurrentChannel();
|
||||
|
||||
if(channel == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
for(VoiceConnection iterConn : connections)
|
||||
{
|
||||
if(iterConn.entityPlayer == null || iterConn == connection || !iterConn.canListen(channel))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
iterConn.sendToPlayer(byteCount, audioData, connection);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void playerLoggedIn(Player player, NetHandler netHandler, INetworkManager manager)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user