From 5dbfd15a0ef90fde629576b6963a8b686363d159 Mon Sep 17 00:00:00 2001 From: "Aidan C. Brady" Date: Mon, 14 Oct 2013 14:08:15 -0400 Subject: [PATCH] Cleaned up code --- .../client/ClientConnectionHandler.java | 20 +- common/mekanism/client/ClientProxy.java | 1 - .../mekanism/client/VoiceClientManager.java | 227 ------------------ .../client/render/MekanismRenderer.java | 21 +- common/mekanism/client/voice/VoiceClient.java | 85 +++++++ common/mekanism/client/voice/VoiceInput.java | 89 +++++++ common/mekanism/client/voice/VoiceOutput.java | 50 ++++ common/mekanism/common/Mekanism.java | 2 +- .../common/{ => voice}/VoiceConnection.java | 3 +- .../{ => voice}/VoiceServerManager.java | 92 +++---- 10 files changed, 282 insertions(+), 308 deletions(-) delete mode 100644 common/mekanism/client/VoiceClientManager.java create mode 100644 common/mekanism/client/voice/VoiceClient.java create mode 100644 common/mekanism/client/voice/VoiceInput.java create mode 100644 common/mekanism/client/voice/VoiceOutput.java rename common/mekanism/common/{ => voice}/VoiceConnection.java (98%) rename common/mekanism/common/{ => voice}/VoiceServerManager.java (56%) diff --git a/common/mekanism/client/ClientConnectionHandler.java b/common/mekanism/client/ClientConnectionHandler.java index 04fd3a63a..0d7afc707 100644 --- a/common/mekanism/client/ClientConnectionHandler.java +++ b/common/mekanism/client/ClientConnectionHandler.java @@ -1,5 +1,9 @@ package mekanism.client; +import java.net.InetAddress; +import java.net.Socket; + +import mekanism.client.voice.VoiceClient; import mekanism.common.Mekanism; import net.minecraft.network.INetworkManager; import net.minecraft.network.NetLoginHandler; @@ -14,10 +18,12 @@ import cpw.mods.fml.relauncher.SideOnly; @SideOnly(Side.CLIENT) public class ClientConnectionHandler implements IConnectionHandler { + public VoiceClient voiceClient = new VoiceClient(); + @Override public void playerLoggedIn(Player player, NetHandler netHandler, INetworkManager manager) { - + } @Override @@ -29,24 +35,24 @@ public class ClientConnectionHandler implements IConnectionHandler @Override public void connectionOpened(NetHandler netClientHandler, String server, int port, INetworkManager manager) { - + voiceClient.start(server, 36123); } @Override public void connectionOpened(NetHandler netClientHandler, MinecraftServer server, INetworkManager manager) { - + try { + voiceClient.start(InetAddress.getLocalHost().getHostAddress(), 36123); + } catch(Exception e) {} } @Override public void connectionClosed(INetworkManager manager) { + voiceClient.stop(); Mekanism.proxy.unloadSoundHandler(); } @Override - public void clientLoggedIn(NetHandler clientHandler, INetworkManager manager, Packet1Login login) - { - - } + public void clientLoggedIn(NetHandler clientHandler, INetworkManager manager, Packet1Login login) {} } diff --git a/common/mekanism/client/ClientProxy.java b/common/mekanism/client/ClientProxy.java index 8f601ce5d..98ee3397f 100644 --- a/common/mekanism/client/ClientProxy.java +++ b/common/mekanism/client/ClientProxy.java @@ -369,7 +369,6 @@ public class ClientProxy extends CommonProxy TickRegistry.registerTickHandler(new RenderTickHandler(), Side.CLIENT); NetworkRegistry.instance().registerConnectionHandler(new ClientConnectionHandler()); - NetworkRegistry.instance().registerConnectionHandler(new VoiceClientManager()); KeyBindingRegistry.registerKeyBinding(new MekanismKeyHandler()); } diff --git a/common/mekanism/client/VoiceClientManager.java b/common/mekanism/client/VoiceClientManager.java deleted file mode 100644 index a717a1250..000000000 --- a/common/mekanism/client/VoiceClientManager.java +++ /dev/null @@ -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(); - } - } -} diff --git a/common/mekanism/client/render/MekanismRenderer.java b/common/mekanism/client/render/MekanismRenderer.java index 1e31b48ec..411ff9706 100644 --- a/common/mekanism/client/render/MekanismRenderer.java +++ b/common/mekanism/client/render/MekanismRenderer.java @@ -197,20 +197,21 @@ public class MekanismRenderer texturemanager.bindTexture(texturemanager.getResourceLocation(item.getItemSpriteNumber())); Tessellator tessellator = Tessellator.instance; - float f = icon.getMinU(); - float f1 = icon.getMaxU(); - float f2 = icon.getMinV(); - float f3 = icon.getMaxV(); - float f4 = 0.0F; - float f5 = 0.3F; + + float minU = icon.getMinU(); + float maxU = icon.getMaxU(); + float minV = icon.getMinV(); + float maxV = icon.getMaxV(); + GL11.glEnable(GL12.GL_RESCALE_NORMAL); - GL11.glTranslatef(-f4, -f5, 0.0F); - float f6 = 1.5F; - GL11.glScalef(f6, f6, f6); + GL11.glTranslatef(0.0F, -0.3F, 0.0F); + + GL11.glScalef(1.5F, 1.5F, 1.5F); GL11.glRotatef(50.0F, 0.0F, 1.0F, 0.0F); GL11.glRotatef(335.0F, 0.0F, 0.0F, 1.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); } diff --git a/common/mekanism/client/voice/VoiceClient.java b/common/mekanism/client/voice/VoiceClient.java new file mode 100644 index 000000000..75117088c --- /dev/null +++ b/common/mekanism/client/voice/VoiceClient.java @@ -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(); + } + } +} diff --git a/common/mekanism/client/voice/VoiceInput.java b/common/mekanism/client/voice/VoiceInput.java new file mode 100644 index 000000000..b8eec705a --- /dev/null +++ b/common/mekanism/client/voice/VoiceInput.java @@ -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(); + } +} diff --git a/common/mekanism/client/voice/VoiceOutput.java b/common/mekanism/client/voice/VoiceOutput.java new file mode 100644 index 000000000..2378d809b --- /dev/null +++ b/common/mekanism/client/voice/VoiceOutput.java @@ -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(); + } +} diff --git a/common/mekanism/common/Mekanism.java b/common/mekanism/common/Mekanism.java index 610641b8f..d6041d6b9 100644 --- a/common/mekanism/common/Mekanism.java +++ b/common/mekanism/common/Mekanism.java @@ -83,6 +83,7 @@ import mekanism.common.tileentity.TileEntityTeleporter; import mekanism.common.tileentity.TileEntityUniversalCable; import mekanism.common.util.MekanismUtils; import mekanism.common.util.MekanismUtils.ResourceType; +import mekanism.common.voice.VoiceServerManager; import net.minecraft.block.Block; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; @@ -1114,7 +1115,6 @@ public class Mekanism //Set up VoiceServerManager voiceManager = new VoiceServerManager(); - NetworkRegistry.instance().registerConnectionHandler(voiceManager); //Register with TransmitterNetworkRegistry TransmitterNetworkRegistry.initiate(); diff --git a/common/mekanism/common/VoiceConnection.java b/common/mekanism/common/voice/VoiceConnection.java similarity index 98% rename from common/mekanism/common/VoiceConnection.java rename to common/mekanism/common/voice/VoiceConnection.java index ca10d0075..1a61d4774 100644 --- a/common/mekanism/common/VoiceConnection.java +++ b/common/mekanism/common/voice/VoiceConnection.java @@ -1,4 +1,4 @@ -package mekanism.common; +package mekanism.common.voice; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; @@ -6,6 +6,7 @@ import java.io.DataInputStream; import java.io.DataOutputStream; import java.net.Socket; +import mekanism.common.Mekanism; import mekanism.common.item.ItemWalkieTalkie; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.item.ItemStack; diff --git a/common/mekanism/common/VoiceServerManager.java b/common/mekanism/common/voice/VoiceServerManager.java similarity index 56% rename from common/mekanism/common/VoiceServerManager.java rename to common/mekanism/common/voice/VoiceServerManager.java index 8ec1a2f59..9f955035d 100644 --- a/common/mekanism/common/VoiceServerManager.java +++ b/common/mekanism/common/voice/VoiceServerManager.java @@ -1,4 +1,4 @@ -package mekanism.common; +package mekanism.common.voice; import java.net.ServerSocket; 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.Player; -public class VoiceServerManager implements IConnectionHandler +public class VoiceServerManager { public Set connections = new HashSet(); @@ -30,33 +30,7 @@ public class VoiceServerManager implements IConnectionHandler try { serverSocket = new ServerSocket(36123); - - 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(); + (listenThread = new ListenThread()).start(); } catch(Exception e) {} running = true; @@ -109,39 +83,35 @@ public class VoiceServerManager implements IConnectionHandler } } - @Override - 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 class ListenThread extends Thread { + 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(); + } + } + } } }