From d4fc39274e7641ac9887825f8f0c0d1204a3617b Mon Sep 17 00:00:00 2001 From: Aidan Brady Date: Sun, 22 Dec 2013 16:49:04 -0500 Subject: [PATCH] Threaded VoiceConnection, made client connection fire with server-configured port --- .../client/ClientConnectionHandler.java | 6 +- common/mekanism/client/ClientProxy.java | 8 + common/mekanism/client/gui/GuiEnergyCube.java | 4 +- common/mekanism/client/voice/VoiceClient.java | 7 +- common/mekanism/common/CommonProxy.java | 5 + common/mekanism/common/EnergyDisplay.java | 182 ++++++++++++++++ .../common/network/PacketConfigSync.java | 4 +- .../mekanism/common/util/MekanismUtils.java | 13 +- .../common/voice/VoiceConnection.java | 5 +- .../core/electricity/ElectricityDisplay.java | 199 ------------------ 10 files changed, 216 insertions(+), 217 deletions(-) create mode 100644 common/mekanism/common/EnergyDisplay.java delete mode 100644 common/universalelectricity/core/electricity/ElectricityDisplay.java diff --git a/common/mekanism/client/ClientConnectionHandler.java b/common/mekanism/client/ClientConnectionHandler.java index 3602f9859..30eaa81bd 100644 --- a/common/mekanism/client/ClientConnectionHandler.java +++ b/common/mekanism/client/ClientConnectionHandler.java @@ -33,8 +33,7 @@ public class ClientConnectionHandler implements IConnectionHandler if(Mekanism.voiceServerEnabled) { try { - MekanismClient.voiceClient = new VoiceClient(server, Mekanism.VOICE_PORT); - MekanismClient.voiceClient.start(); + MekanismClient.voiceClient = new VoiceClient(server); } catch(Exception e) {} } } @@ -46,8 +45,7 @@ public class ClientConnectionHandler implements IConnectionHandler if(Mekanism.voiceServerEnabled) { try { - MekanismClient.voiceClient = new VoiceClient(InetAddress.getLocalHost().getHostAddress(), Mekanism.VOICE_PORT); - MekanismClient.voiceClient.start(); + MekanismClient.voiceClient = new VoiceClient(InetAddress.getLocalHost().getHostAddress()); } catch(Exception e) {} } } diff --git a/common/mekanism/client/ClientProxy.java b/common/mekanism/client/ClientProxy.java index 497ab8fd0..482dc8485 100644 --- a/common/mekanism/client/ClientProxy.java +++ b/common/mekanism/client/ClientProxy.java @@ -478,4 +478,12 @@ public class ClientProxy extends CommonProxy { return Minecraft.getMinecraft().mcDataDir; } + + @Override + public void onConfigSync() + { + super.onConfigSync(); + + MekanismClient.voiceClient.start(); + } } diff --git a/common/mekanism/client/gui/GuiEnergyCube.java b/common/mekanism/client/gui/GuiEnergyCube.java index 661d16bc5..78dc6d846 100644 --- a/common/mekanism/client/gui/GuiEnergyCube.java +++ b/common/mekanism/client/gui/GuiEnergyCube.java @@ -1,5 +1,7 @@ package mekanism.client.gui; +import mekanism.common.EnergyDisplay; +import mekanism.common.EnergyDisplay.ElectricUnit; import mekanism.common.inventory.container.ContainerEnergyCube; import mekanism.common.tileentity.TileEntityEnergyCube; import mekanism.common.util.MekanismUtils; @@ -9,8 +11,6 @@ import net.minecraft.util.StatCollector; import org.lwjgl.opengl.GL11; -import universalelectricity.core.electricity.ElectricityDisplay; -import universalelectricity.core.electricity.ElectricityDisplay.ElectricUnit; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; diff --git a/common/mekanism/client/voice/VoiceClient.java b/common/mekanism/client/voice/VoiceClient.java index 3bee035f4..fea78c88a 100644 --- a/common/mekanism/client/voice/VoiceClient.java +++ b/common/mekanism/client/voice/VoiceClient.java @@ -9,6 +9,7 @@ import java.net.Socket; import javax.sound.sampled.AudioFormat; +import mekanism.common.Mekanism; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; @@ -18,7 +19,6 @@ public class VoiceClient extends Thread public Socket socket; public String ip; - public int port; public AudioFormat format = new AudioFormat(11025.0F, 8, 1, true, true); @@ -30,10 +30,9 @@ public class VoiceClient extends Thread public boolean running; - public VoiceClient(String s, int i) + public VoiceClient(String s) { ip = s; - port = i; } @Override @@ -42,7 +41,7 @@ public class VoiceClient extends Thread System.out.println("[Mekanism] VoiceServer: Starting client connection..."); try { - socket = new Socket(ip, port); + socket = new Socket(ip, Mekanism.VOICE_PORT); running = true; input = new DataInputStream(new BufferedInputStream(socket.getInputStream())); diff --git a/common/mekanism/common/CommonProxy.java b/common/mekanism/common/CommonProxy.java index ee170380c..3926aa110 100644 --- a/common/mekanism/common/CommonProxy.java +++ b/common/mekanism/common/CommonProxy.java @@ -365,4 +365,9 @@ public class CommonProxy { return null; } + + public void onConfigSync() + { + System.out.println("[Mekanism] Received config from server."); + } } diff --git a/common/mekanism/common/EnergyDisplay.java b/common/mekanism/common/EnergyDisplay.java new file mode 100644 index 000000000..ada88d386 --- /dev/null +++ b/common/mekanism/common/EnergyDisplay.java @@ -0,0 +1,182 @@ +package mekanism.common; + +/** + * Code taken from UE and modified to fit Mekanism. + */ +public class EnergyDisplay +{ + public static enum ElectricUnit + { + AMPERE("Amp", "I"), AMP_HOUR("Amp Hour", "Ah"), VOLTAGE("Volt", "V"), WATT("Watt", "W"), + WATT_HOUR("Watt Hour", "Wh"), RESISTANCE("Ohm", "R"), CONDUCTANCE("Siemen", "S"), + JOULES("Joule", "J"); + + public String name; + public String symbol; + + private ElectricUnit(String s, String s1) + { + name = s; + symbol = s1; + } + + public String getPlural() + { + return name + "s"; + } + } + + /** Metric system of measurement. */ + public static enum MeasurementUnit + { + MICRO("Micro", "u", 0.000001D), MILLI("Milli", "m", 0.001D), BASE("", "", 1), + KILO("Kilo", "k", 1000D), MEGA("Mega", "M", 1000000D), GIGA("Giga", "G", 1000000000D), + TERA("Tera", "T", 1000000000000D), PETA("Peta", "P", 1000000000000000D), + EXA("Exa", "E", 1000000000000000000D), ZETTA("Zetta", "Z", 1000000000000000000000D), + YOTTA("Yotta", "Y", 1000000000000000000000000D); + + /** long name for the unit */ + public String name; + + /** short unit version of the unit */ + public String symbol; + + /** Point by which a number is consider to be of this unit */ + public double value; + + private MeasurementUnit(String s, String s1, double v) + { + name = s; + symbol = s1; + value = v; + } + + public String getName(boolean getShort) + { + if(getShort) + { + return symbol; + } + else { + return name; + } + } + + public double process(double d) + { + return d / value; + } + + public boolean above(double d) + { + return d > value; + } + + public boolean below(double d) + { + return d < value; + } + } + + /** + * Displays the unit as text. Does handle negative numbers, and will place a negative sign in + * front of the output string showing this. Use string.replace to remove the negative sign if + * unwanted + */ + public static String getDisplay(double value, ElectricUnit unit, int decimalPlaces, boolean isShort) + { + String unitName = unit.name; + String prefix = ""; + + if(value < 0) + { + value = Math.abs(value); + prefix = "-"; + } + + if(isShort) + { + unitName = unit.symbol; + } + else if(value > 1) + { + unitName = unit.getPlural(); + } + + if(value == 0) + { + return value + " " + unitName; + } + else { + for(int i = 0; i < MeasurementUnit.values().length; i++) + { + MeasurementUnit lowerMeasure = MeasurementUnit.values()[i]; + + if(lowerMeasure.below(value) && lowerMeasure.ordinal() == 0) + { + return prefix + roundDecimals(lowerMeasure.process(value), decimalPlaces) + " " + lowerMeasure.getName(isShort) + unitName; + } + + if(lowerMeasure.ordinal() + 1 >= MeasurementUnit.values().length) + { + return prefix + roundDecimals(lowerMeasure.process(value), decimalPlaces) + " " + lowerMeasure.getName(isShort) + unitName; + } + + MeasurementUnit upperMeasure = MeasurementUnit.values()[i + 1]; + + if((lowerMeasure.above(value) && upperMeasure.below(value)) || lowerMeasure.value == value) + { + return prefix + roundDecimals(lowerMeasure.process(value), decimalPlaces) + " " + lowerMeasure.getName(isShort) + unitName; + } + } + } + + return prefix + roundDecimals(value, decimalPlaces) + " " + unitName; + } + + public static String getDisplay(double value, ElectricUnit unit) + { + return getDisplay(value, unit, 2, false); + } + + public static String getDisplayShort(double value, ElectricUnit unit) + { + return getDisplay(value, unit, 2, true); + } + + public static String getDisplayShort(double value, ElectricUnit unit, int decimalPlaces) + { + return getDisplay(value, unit, decimalPlaces, true); + } + + public static String getDisplaySimple(double value, ElectricUnit unit, int decimalPlaces) + { + if(value > 1) + { + if(decimalPlaces < 1) + { + return (int)value + " " + unit.getPlural(); + } + + return roundDecimals(value, decimalPlaces) + " " + unit.getPlural(); + } + + if(decimalPlaces < 1) + { + return (int)value + " " + unit.name; + } + + return roundDecimals(value, decimalPlaces) + " " + unit.name; + } + + public static double roundDecimals(double d, int decimalPlaces) + { + int j = (int)(d*Math.pow(10, decimalPlaces)); + return j/Math.pow(10, decimalPlaces); + } + + public static double roundDecimals(double d) + { + return roundDecimals(d, 2); + } +} diff --git a/common/mekanism/common/network/PacketConfigSync.java b/common/mekanism/common/network/PacketConfigSync.java index 0a849cc17..26d5f8409 100644 --- a/common/mekanism/common/network/PacketConfigSync.java +++ b/common/mekanism/common/network/PacketConfigSync.java @@ -66,8 +66,8 @@ public class PacketConfigSync implements IMekanismPacket Mekanism.rotaryCondensentratorUsage = dataStream.readDouble(); Mekanism.chemicalFormulatorUsage = dataStream.readDouble(); Mekanism.chemicalInfuserUsage = dataStream.readDouble(); - - System.out.println("[Mekanism] Received config from server."); + + Mekanism.proxy.onConfigSync(); } @Override diff --git a/common/mekanism/common/util/MekanismUtils.java b/common/mekanism/common/util/MekanismUtils.java index a72b784d4..2993105b1 100644 --- a/common/mekanism/common/util/MekanismUtils.java +++ b/common/mekanism/common/util/MekanismUtils.java @@ -17,8 +17,10 @@ import java.util.Map; import mekanism.api.EnumColor; import mekanism.api.Coord4D; import mekanism.common.DynamicTankCache; +import mekanism.common.EnergyDisplay; import mekanism.common.IActiveState; import mekanism.common.IFactory; +import mekanism.common.EnergyDisplay.ElectricUnit; import mekanism.common.IFactory.RecipeType; import mekanism.common.IInvConfiguration; import mekanism.common.IModule; @@ -63,8 +65,6 @@ import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fluids.IFluidBlock; import net.minecraftforge.oredict.OreDictionary; import net.minecraftforge.oredict.ShapedOreRecipe; -import universalelectricity.core.electricity.ElectricityDisplay; -import universalelectricity.core.electricity.ElectricityDisplay.ElectricUnit; import cpw.mods.fml.common.network.PacketDispatcher; import cpw.mods.fml.server.FMLServerHandler; @@ -1115,12 +1115,12 @@ public final class MekanismUtils public static String getEnergyDisplay(double energy) { - return ElectricityDisplay.getDisplayShort((float)(energy*Mekanism.TO_UE), ElectricUnit.JOULES); + return EnergyDisplay.getDisplayShort(energy, ElectricUnit.JOULES); } public static String getPowerDisplay(double energy) { - return ElectricityDisplay.getDisplayShort((float)(energy*Mekanism.TO_UE), ElectricUnit.WATT); + return EnergyDisplay.getDisplayShort(energy, ElectricUnit.WATT); } public static boolean useBuildcraft() @@ -1133,6 +1133,11 @@ public final class MekanismUtils return "[" + obj.xCoord + ", " + obj.yCoord + ", " + obj.zCoord + "]"; } + /** + * Splits a string of text into a list of new segments, using the splitter "!n." + * @param s - string to split + * @return split string + */ public static List splitLines(String s) { ArrayList ret = new ArrayList(); diff --git a/common/mekanism/common/voice/VoiceConnection.java b/common/mekanism/common/voice/VoiceConnection.java index c09866596..acae3321a 100644 --- a/common/mekanism/common/voice/VoiceConnection.java +++ b/common/mekanism/common/voice/VoiceConnection.java @@ -16,7 +16,7 @@ import net.minecraft.item.ItemStack; import net.minecraft.server.MinecraftServer; import cpw.mods.fml.common.FMLCommonHandler; -public class VoiceConnection +public class VoiceConnection extends Thread { public Socket socket; @@ -34,7 +34,8 @@ public class VoiceConnection socket = s; } - public void start() + @Override + public void run() { try { input = new DataInputStream(new BufferedInputStream(socket.getInputStream())); diff --git a/common/universalelectricity/core/electricity/ElectricityDisplay.java b/common/universalelectricity/core/electricity/ElectricityDisplay.java deleted file mode 100644 index f3e5221d5..000000000 --- a/common/universalelectricity/core/electricity/ElectricityDisplay.java +++ /dev/null @@ -1,199 +0,0 @@ -package universalelectricity.core.electricity; - -/** - * An easy way to display information on electricity for the client. - * - * @author Calclavia - */ -public class ElectricityDisplay -{ - /** - * Universal Electricity's units are in KILOJOULES, KILOWATTS and KILOVOLTS. Try to make your - * energy ratio as close to real life as possible. - */ - public static enum ElectricUnit - { - AMPERE("Amp", "I"), AMP_HOUR("Amp Hour", "Ah"), VOLTAGE("Volt", "V"), WATT("Watt", "W"), - WATT_HOUR("Watt Hour", "Wh"), RESISTANCE("Ohm", "R"), CONDUCTANCE("Siemen", "S"), - JOULES("Joule", "J"); - - public String name; - public String symbol; - - private ElectricUnit(String name, String symbol) - { - this.name = name; - this.symbol = symbol; - } - - public String getPlural() - { - return this.name + "s"; - } - } - - /** Metric system of measurement. */ - public static enum MeasurementUnit - { - MICRO("Micro", "u", 0.000001f), MILLI("Milli", "m", 0.001f), BASE("", "", 1), - KILO("Kilo", "k", 1000f), MEGA("Mega", "M", 1000000f), GIGA("Giga", "G", 1000000000f), - TERA("Tera", "T", 1000000000000f), PETA("Peta", "P", 1000000000000000f), - EXA("Exa", "E", 1000000000000000000f), ZETTA("Zetta", "Z", 1000000000000000000000f), - YOTTA("Yotta", "Y", 1000000000000000000000000f); - - /** long name for the unit */ - public String name; - /** short unit version of the unit */ - public String symbol; - /** Point by which a number is consider to be of this unit */ - public float value; - - private MeasurementUnit(String name, String symbol, float value) - { - this.name = name; - this.symbol = symbol; - this.value = value; - } - - public String getName(boolean getShort) - { - if (getShort) - { - return symbol; - } - else - { - return name; - } - } - - /** Divides the value by the unit value start */ - public double process(double value) - { - return value / this.value; - } - - /** Checks if a value is above the unit value start */ - public boolean isAbove(float value) - { - return value > this.value; - } - - /** Checks if a value is lower than the unit value start */ - public boolean isBellow(float value) - { - return value < this.value; - } - } - - /** By default, mods should store energy in Kilo-Joules, hence a multiplier of 1/1000. */ - public static String getDisplay(float value, ElectricUnit unit, int decimalPlaces, boolean isShort) - { - return getDisplay(value, unit, decimalPlaces, isShort, 1000); - } - - /** - * Displays the unit as text. Does handle negative numbers, and will place a negative sign in - * front of the output string showing this. Use string.replace to remove the negative sign if - * unwanted - */ - public static String getDisplay(float value, ElectricUnit unit, int decimalPlaces, boolean isShort, float multiplier) - { - String unitName = unit.name; - String prefix = ""; - if (value < 0) - { - value = Math.abs(value); - prefix = "-"; - } - value *= multiplier; - - if (isShort) - { - unitName = unit.symbol; - } - else if (value > 1) - { - unitName = unit.getPlural(); - } - - if (value == 0) - { - return value + " " + unitName; - } - else - { - for (int i = 0; i < MeasurementUnit.values().length; i++) - { - MeasurementUnit lowerMeasure = MeasurementUnit.values()[i]; - if (lowerMeasure.isBellow(value) && lowerMeasure.ordinal() == 0) - { - return prefix + roundDecimals(lowerMeasure.process(value), decimalPlaces) + " " + lowerMeasure.getName(isShort) + unitName; - } - if (lowerMeasure.ordinal() + 1 >= MeasurementUnit.values().length) - { - return prefix + roundDecimals(lowerMeasure.process(value), decimalPlaces) + " " + lowerMeasure.getName(isShort) + unitName; - } - MeasurementUnit upperMeasure = MeasurementUnit.values()[i + 1]; - if ((lowerMeasure.isAbove(value) && upperMeasure.isBellow(value)) || lowerMeasure.value == value) - { - return prefix + roundDecimals(lowerMeasure.process(value), decimalPlaces) + " " + lowerMeasure.getName(isShort) + unitName; - } - } - } - - return prefix + roundDecimals(value, decimalPlaces) + " " + unitName; - } - - public static String getDisplay(float value, ElectricUnit unit) - { - return getDisplay(value, unit, 2, false); - } - - public static String getDisplayShort(float value, ElectricUnit unit) - { - return getDisplay(value, unit, 2, true); - } - - public static String getDisplayShort(float value, ElectricUnit unit, int decimalPlaces) - { - return getDisplay(value, unit, decimalPlaces, true); - } - - public static String getDisplaySimple(float value, ElectricUnit unit, int decimalPlaces) - { - if (value > 1) - { - if (decimalPlaces < 1) - { - return (int) value + " " + unit.getPlural(); - } - - return roundDecimals(value, decimalPlaces) + " " + unit.getPlural(); - } - - if (decimalPlaces < 1) - { - return (int) value + " " + unit.name; - } - - return roundDecimals(value, decimalPlaces) + " " + unit.name; - } - - /** - * Rounds a number to a specific number place places - * - * @param The number - * @return The rounded number - */ - public static double roundDecimals(double d, int decimalPlaces) - { - int j = (int) (d * Math.pow(10, decimalPlaces)); - return j / Math.pow(10, decimalPlaces); - } - - public static double roundDecimals(double d) - { - return roundDecimals(d, 2); - } -}