From 1f81a4e54d29a2d7b8ffc390bc97336bcc28f8aa Mon Sep 17 00:00:00 2001 From: Jason Watkins Date: Tue, 7 Apr 2015 10:12:00 -0700 Subject: [PATCH] Added Java client support for multiplayer sendCTRL --- Java/src/XPlaneConnect.java | 11 ++--- TestScripts/Java Tests/XPlaneConnectTest.java | 46 ++++++++++++++++--- 2 files changed, 44 insertions(+), 13 deletions(-) diff --git a/Java/src/XPlaneConnect.java b/Java/src/XPlaneConnect.java index 6959f3e..cdc4a37 100644 --- a/Java/src/XPlaneConnect.java +++ b/Java/src/XPlaneConnect.java @@ -410,7 +410,7 @@ public class XPlaneConnect implements AutoCloseable * @param aircraft The aircraft to set. 0 for the player's aircraft. * @throws IOException If the command cannot be sent. */ - private void sendCTRL(float[] ctrl, int aircraft) throws IOException + public void sendCTRL(float[] ctrl, int aircraft) throws IOException { //Preconditions if(ctrl == null) @@ -421,13 +421,9 @@ public class XPlaneConnect implements AutoCloseable { throw new IllegalArgumentException("ctrl must have 6 or fewer elements."); } - if(aircraft < 0) + if(aircraft < 0 || aircraft > 9) { - throw new IllegalArgumentException("aircraft must be non-negative."); - } - if(aircraft != 0) //TODO: Implement support for non-player aircraft on plugin side. - { - throw new Error("Non-player aircraft not supported yet."); + throw new IllegalArgumentException("aircraft must be non-negative and less than 9."); } //Pad command values and convert to bytes @@ -461,6 +457,7 @@ public class XPlaneConnect implements AutoCloseable cur += 4; } } + bb.put(cur, (byte)aircraft); //Build and send message ByteArrayOutputStream os = new ByteArrayOutputStream(); diff --git a/TestScripts/Java Tests/XPlaneConnectTest.java b/TestScripts/Java Tests/XPlaneConnectTest.java index defc80c..3cfc43e 100644 --- a/TestScripts/Java Tests/XPlaneConnectTest.java +++ b/TestScripts/Java Tests/XPlaneConnectTest.java @@ -303,12 +303,12 @@ public class XPlaneConnectTest public void testSendCTRL() throws IOException { String[] drefs = { - "sim/cockpit2/controls/yoke_pitch_ratio", - "sim/cockpit2/controls/yoke_roll_ratio", - "sim/cockpit2/controls/yoke_heading_ratio", - "sim/flightmodel/engine/ENGN_thro", - "sim/cockpit/switches/gear_handle_status", - "sim/flightmodel/controls/flaprqst" + "sim/cockpit2/controls/yoke_pitch_ratio", + "sim/cockpit2/controls/yoke_roll_ratio", + "sim/cockpit2/controls/yoke_heading_ratio", + "sim/flightmodel/engine/ENGN_thro", + "sim/cockpit/switches/gear_handle_status", + "sim/flightmodel/controls/flaprqst" }; float[] ctrl = new float[] {0, 0, 1, 0.8F, 0, 1}; try(XPlaneConnect xpc = new XPlaneConnect()) @@ -326,6 +326,40 @@ public class XPlaneConnectTest } } + @Test + public void testSendCTRL_NPC() throws IOException + { + String[] drefs1 = { + "sim/multiplayer/position/plane1_yolk_pitch", + "sim/multiplayer/position/plane1_yolk_roll", + "sim/multiplayer/position/plane1_yolk_yaw", + "sim/multiplayer/position/plane1_throttle" + }; + String[] drefs2 = { + "sim/multiplayer/position/plane1_gear_deploy", + "sim/multiplayer/position/plane1_flap_ratio" + }; + float[] ctrl = new float[] {0, 0, 1, 0.8F, 0, 0.5F}; + try(XPlaneConnect xpc = new XPlaneConnect()) + { + xpc.sendCTRL(ctrl, 1); + float[][] result1 = xpc.requestDREFs(drefs1); + float[][] result2 = xpc.requestDREFs(drefs2); + if(result1.length != 4 || result2.length != 2) + { + fail(); + } + for(int i = 0; i < 4; ++i) + { + assertEquals(ctrl[i], result1[i][0], 1e-2); + } + for(int i = 0; i < 2; ++i) + { + assertEquals(ctrl[i + 4], result2[i][0], 1e-2); + } + } + } + @Test(expected = IllegalArgumentException.class) public void testSendCTRL_NullCtrl() throws IOException {