Cleaned up code

This commit is contained in:
Aidan C. Brady 2013-10-14 14:08:15 -04:00
parent 0feba0d9b3
commit 5dbfd15a0e
10 changed files with 282 additions and 308 deletions

View file

@ -1,5 +1,9 @@
package mekanism.client; package mekanism.client;
import java.net.InetAddress;
import java.net.Socket;
import mekanism.client.voice.VoiceClient;
import mekanism.common.Mekanism; import mekanism.common.Mekanism;
import net.minecraft.network.INetworkManager; import net.minecraft.network.INetworkManager;
import net.minecraft.network.NetLoginHandler; import net.minecraft.network.NetLoginHandler;
@ -14,10 +18,12 @@ import cpw.mods.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT) @SideOnly(Side.CLIENT)
public class ClientConnectionHandler implements IConnectionHandler public class ClientConnectionHandler implements IConnectionHandler
{ {
public VoiceClient voiceClient = new VoiceClient();
@Override @Override
public void playerLoggedIn(Player player, NetHandler netHandler, INetworkManager manager) public void playerLoggedIn(Player player, NetHandler netHandler, INetworkManager manager)
{ {
} }
@Override @Override
@ -29,24 +35,24 @@ public class ClientConnectionHandler implements IConnectionHandler
@Override @Override
public void connectionOpened(NetHandler netClientHandler, String server, int port, INetworkManager manager) public void connectionOpened(NetHandler netClientHandler, String server, int port, INetworkManager manager)
{ {
voiceClient.start(server, 36123);
} }
@Override @Override
public void connectionOpened(NetHandler netClientHandler, MinecraftServer server, INetworkManager manager) public void connectionOpened(NetHandler netClientHandler, MinecraftServer server, INetworkManager manager)
{ {
try {
voiceClient.start(InetAddress.getLocalHost().getHostAddress(), 36123);
} catch(Exception e) {}
} }
@Override @Override
public void connectionClosed(INetworkManager manager) public void connectionClosed(INetworkManager manager)
{ {
voiceClient.stop();
Mekanism.proxy.unloadSoundHandler(); Mekanism.proxy.unloadSoundHandler();
} }
@Override @Override
public void clientLoggedIn(NetHandler clientHandler, INetworkManager manager, Packet1Login login) public void clientLoggedIn(NetHandler clientHandler, INetworkManager manager, Packet1Login login) {}
{
}
} }

View file

@ -369,7 +369,6 @@ public class ClientProxy extends CommonProxy
TickRegistry.registerTickHandler(new RenderTickHandler(), Side.CLIENT); TickRegistry.registerTickHandler(new RenderTickHandler(), Side.CLIENT);
NetworkRegistry.instance().registerConnectionHandler(new ClientConnectionHandler()); NetworkRegistry.instance().registerConnectionHandler(new ClientConnectionHandler());
NetworkRegistry.instance().registerConnectionHandler(new VoiceClientManager());
KeyBindingRegistry.registerKeyBinding(new MekanismKeyHandler()); KeyBindingRegistry.registerKeyBinding(new MekanismKeyHandler());
} }

View file

@ -1,227 +0,0 @@
package mekanism.client;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
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;
import javax.sound.sampled.TargetDataLine;
import net.minecraft.network.INetworkManager;
import net.minecraft.network.NetLoginHandler;
import net.minecraft.network.packet.NetHandler;
import net.minecraft.network.packet.Packet1Login;
import net.minecraft.server.MinecraftServer;
import cpw.mods.fml.common.network.IConnectionHandler;
import cpw.mods.fml.common.network.Player;
public class VoiceClientManager implements IConnectionHandler
{
public Socket socket;
public AudioFormat format = new AudioFormat(11025.0F, 8, 1, true, true);
public DataLine.Info microphone = new DataLine.Info(TargetDataLine.class, this.format, 2200);
public DataLine.Info speaker = new DataLine.Info(SourceDataLine.class, this.format, 2200);
public TargetDataLine targetLine;
public SourceDataLine sourceLine;
public Thread microphoneThread;
public Thread speakerThread;
public DataInputStream input;
public DataOutputStream output;
public boolean running;
@Override
public void playerLoggedIn(Player player, NetHandler netHandler, INetworkManager manager)
{
try {
socket = new Socket(manager.getSocketAddress().toString(), 36123);
} catch(Exception e) {}
}
@Override
public String connectionReceived(NetLoginHandler netHandler, INetworkManager manager)
{
return null;
}
@Override
public void connectionOpened(NetHandler netClientHandler, String server, int port, INetworkManager manager)
{
//connecting to foreign server
try {
socket = new Socket(server, 36123);
running = true;
start();
} catch(Exception e) {}
}
@Override
public void connectionOpened(NetHandler netClientHandler, MinecraftServer server, INetworkManager manager)
{
//connecting to LAN server on same instance
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()
{
System.out.println("Started client connection.");
try {
input = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
output = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
//Speaker (Out)
speakerThread = 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)
{
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.");
e.printStackTrace();
}
}
});
speakerThread.start();
//Microphone (In)
microphoneThread = 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)
{
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();
}
}
});
microphoneThread.start();
} catch(Exception e) {
System.err.println("Error in core client initiation.");
e.printStackTrace();
}
}
public void stop()
{
System.out.println("Stopped client connection.");
try {
microphoneThread.interrupt();
speakerThread.interrupt();
sourceLine.flush();
sourceLine.close();
targetLine.flush();
targetLine.close();
output.flush();
output.close();
output = null;
input.close();
input = null;
socket.close();
socket = null;
running = false;
} catch(Exception e) {
System.err.println("Error while ending client connection.");
e.printStackTrace();
}
}
}

View file

@ -197,20 +197,21 @@ public class MekanismRenderer
texturemanager.bindTexture(texturemanager.getResourceLocation(item.getItemSpriteNumber())); texturemanager.bindTexture(texturemanager.getResourceLocation(item.getItemSpriteNumber()));
Tessellator tessellator = Tessellator.instance; Tessellator tessellator = Tessellator.instance;
float f = icon.getMinU();
float f1 = icon.getMaxU(); float minU = icon.getMinU();
float f2 = icon.getMinV(); float maxU = icon.getMaxU();
float f3 = icon.getMaxV(); float minV = icon.getMinV();
float f4 = 0.0F; float maxV = icon.getMaxV();
float f5 = 0.3F;
GL11.glEnable(GL12.GL_RESCALE_NORMAL); GL11.glEnable(GL12.GL_RESCALE_NORMAL);
GL11.glTranslatef(-f4, -f5, 0.0F); GL11.glTranslatef(0.0F, -0.3F, 0.0F);
float f6 = 1.5F;
GL11.glScalef(f6, f6, f6); GL11.glScalef(1.5F, 1.5F, 1.5F);
GL11.glRotatef(50.0F, 0.0F, 1.0F, 0.0F); GL11.glRotatef(50.0F, 0.0F, 1.0F, 0.0F);
GL11.glRotatef(335.0F, 0.0F, 0.0F, 1.0F); GL11.glRotatef(335.0F, 0.0F, 0.0F, 1.0F);
GL11.glTranslatef(-0.9375F, -0.0625F, 0.0F); GL11.glTranslatef(-0.9375F, -0.0625F, 0.0F);
RenderManager.instance.itemRenderer.renderItemIn2D(tessellator, f1, f2, f, f3, icon.getIconWidth(), icon.getIconHeight(), 0.0625F);
RenderManager.instance.itemRenderer.renderItemIn2D(tessellator, maxU, minV, minU, maxV, icon.getIconWidth(), icon.getIconHeight(), 0.0625F);
GL11.glDisable(GL12.GL_RESCALE_NORMAL); GL11.glDisable(GL12.GL_RESCALE_NORMAL);
} }

View file

@ -0,0 +1,85 @@
package mekanism.client.voice;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
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;
import javax.sound.sampled.TargetDataLine;
import mekanism.client.MekanismKeyHandler;
import net.minecraft.network.INetworkManager;
import net.minecraft.network.NetLoginHandler;
import net.minecraft.network.packet.NetHandler;
import net.minecraft.network.packet.Packet1Login;
import net.minecraft.server.MinecraftServer;
import cpw.mods.fml.common.network.IConnectionHandler;
import cpw.mods.fml.common.network.Player;
public class VoiceClient
{
public Socket socket;
public AudioFormat format = new AudioFormat(11025.0F, 8, 1, true, true);
public VoiceInput inputThread;
public VoiceOutput outputThread;
public DataInputStream input;
public DataOutputStream output;
public boolean running;
public void start(String ip, int port)
{
System.out.println("Started client connection.");
try {
socket = new Socket(ip, port);
running = true;
input = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
output = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
(outputThread = new VoiceOutput(this)).start();
(inputThread = new VoiceInput(this)).start();
} catch(Exception e) {
System.err.println("Error in core client initiation.");
e.printStackTrace();
}
}
public void stop()
{
System.out.println("Stopped client connection.");
try {
inputThread.interrupt();
outputThread.interrupt();
inputThread.close();
outputThread.close();
output.flush();
output.close();
output = null;
input.close();
input = null;
socket.close();
socket = null;
running = false;
} catch(Exception e) {
System.err.println("Error while ending client connection.");
e.printStackTrace();
}
}
}

View file

@ -0,0 +1,89 @@
package mekanism.client.voice;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.TargetDataLine;
import mekanism.client.MekanismKeyHandler;
public class VoiceInput extends Thread
{
public VoiceClient voiceClient;
public DataLine.Info microphone = new DataLine.Info(TargetDataLine.class, voiceClient.format, 2200);
public TargetDataLine targetLine;
public VoiceInput(VoiceClient client)
{
voiceClient = client;
setDaemon(true);
setName("VoiceServer Client Input Thread");
}
@Override
public void run()
{
try {
targetLine = ((TargetDataLine)AudioSystem.getLine(microphone));
targetLine.open(voiceClient.format, 2200);
targetLine.start();
AudioInputStream audioInput = new AudioInputStream(targetLine);
boolean doFlush = false;
while(voiceClient.running)
{
if(MekanismKeyHandler.voiceDown)
{
targetLine.flush();
while(voiceClient.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)
{
voiceClient.output.writeShort(audioData.length);
voiceClient.output.write(audioData);
}
}
try {
Thread.sleep(200L);
} catch(Exception e) {
e.printStackTrace();
}
doFlush = true;
}
else if(doFlush)
{
voiceClient.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();
}
}
public void close()
{
targetLine.flush();
targetLine.close();
}
}

View file

@ -0,0 +1,50 @@
package mekanism.client.voice;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
public class VoiceOutput extends Thread
{
public VoiceClient voiceClient;
public DataLine.Info speaker = new DataLine.Info(SourceDataLine.class, voiceClient.format, 2200);
public SourceDataLine sourceLine;
public VoiceOutput(VoiceClient client)
{
voiceClient = client;
setDaemon(true);
setName("VoiceServer Client Output Thread");
}
@Override
public void run()
{
try {
sourceLine = ((SourceDataLine)AudioSystem.getLine(speaker));
sourceLine.open(voiceClient.format, 2200);
sourceLine.start();
while(voiceClient.running)
{
short byteCount = voiceClient.input.readShort();
byte[] audioData = new byte[byteCount];
voiceClient.input.readFully(audioData);
sourceLine.write(audioData, 0, audioData.length);
}
} catch(Exception e) {
System.err.println("Error while running speaker loop.");
e.printStackTrace();
}
}
public void close()
{
sourceLine.flush();
sourceLine.close();
}
}

View file

@ -83,6 +83,7 @@ import mekanism.common.tileentity.TileEntityTeleporter;
import mekanism.common.tileentity.TileEntityUniversalCable; import mekanism.common.tileentity.TileEntityUniversalCable;
import mekanism.common.util.MekanismUtils; import mekanism.common.util.MekanismUtils;
import mekanism.common.util.MekanismUtils.ResourceType; import mekanism.common.util.MekanismUtils.ResourceType;
import mekanism.common.voice.VoiceServerManager;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
@ -1114,7 +1115,6 @@ public class Mekanism
//Set up VoiceServerManager //Set up VoiceServerManager
voiceManager = new VoiceServerManager(); voiceManager = new VoiceServerManager();
NetworkRegistry.instance().registerConnectionHandler(voiceManager);
//Register with TransmitterNetworkRegistry //Register with TransmitterNetworkRegistry
TransmitterNetworkRegistry.initiate(); TransmitterNetworkRegistry.initiate();

View file

@ -1,4 +1,4 @@
package mekanism.common; package mekanism.common.voice;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream; import java.io.BufferedOutputStream;
@ -6,6 +6,7 @@ import java.io.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.net.Socket; import java.net.Socket;
import mekanism.common.Mekanism;
import mekanism.common.item.ItemWalkieTalkie; import mekanism.common.item.ItemWalkieTalkie;
import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;

View file

@ -1,4 +1,4 @@
package mekanism.common; package mekanism.common.voice;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
@ -14,7 +14,7 @@ import net.minecraft.server.MinecraftServer;
import cpw.mods.fml.common.network.IConnectionHandler; import cpw.mods.fml.common.network.IConnectionHandler;
import cpw.mods.fml.common.network.Player; import cpw.mods.fml.common.network.Player;
public class VoiceServerManager implements IConnectionHandler public class VoiceServerManager
{ {
public Set<VoiceConnection> connections = new HashSet<VoiceConnection>(); public Set<VoiceConnection> connections = new HashSet<VoiceConnection>();
@ -30,33 +30,7 @@ public class VoiceServerManager implements IConnectionHandler
try { try {
serverSocket = new ServerSocket(36123); serverSocket = new ServerSocket(36123);
(listenThread = new ListenThread()).start();
listenThread = new Thread(new Runnable()
{
@Override
public void run()
{
while(running)
{
try {
Socket s = serverSocket.accept();
VoiceConnection connection = new VoiceConnection(s);
connections.add(connection);
System.out.println("Accepted new connection.");
} catch(SocketException e) {
if(!e.getLocalizedMessage().toLowerCase().equals("socket closed"))
{
e.printStackTrace();
}
} catch(Exception e) {
System.err.println("Error while accepting connection.");
e.printStackTrace();
}
}
}
});
listenThread.start();
} catch(Exception e) {} } catch(Exception e) {}
running = true; running = true;
@ -109,39 +83,35 @@ public class VoiceServerManager implements IConnectionHandler
} }
} }
@Override public class ListenThread extends Thread
public void playerLoggedIn(Player player, NetHandler netHandler, INetworkManager manager)
{
}
@Override
public String connectionReceived(NetLoginHandler netHandler, INetworkManager manager)
{
return null;
}
@Override
public void connectionOpened(NetHandler netClientHandler, String server, int port, INetworkManager manager)
{
}
@Override
public void connectionOpened(NetHandler netClientHandler, MinecraftServer server, INetworkManager manager)
{
}
@Override
public void connectionClosed(INetworkManager manager)
{
}
@Override
public void clientLoggedIn(NetHandler clientHandler, INetworkManager manager, Packet1Login login)
{ {
public ListenThread()
{
setDaemon(true);
setName("VoiceServer Listen Thread");
}
@Override
public void run()
{
while(running)
{
try {
Socket s = serverSocket.accept();
VoiceConnection connection = new VoiceConnection(s);
connections.add(connection);
System.out.println("Accepted new connection.");
} catch(SocketException e) {
if(!e.getLocalizedMessage().toLowerCase().equals("socket closed"))
{
e.printStackTrace();
}
} catch(Exception e) {
System.err.println("Error while accepting connection.");
e.printStackTrace();
}
}
}
} }
} }