Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --- EvilSensor.cs 2014-11-11 21:10:20.000000000 -0500
- +++ EvilSensor_new.cs 2014-11-13 23:11:04.698537600 -0500
- @@ -1,28 +1,32 @@
- using Sandbox.Common.Components;
- using Sandbox.Common.ObjectBuilders;
- using Sandbox.Definitions;
- +using Sandbox.ModAPI;
- using Sandbox.ModAPI.Ingame;
- +using System;
- -namespace EvilSensor
- +namespace EvilSensor_1
- {
- [MyEntityComponentDescriptor(typeof(MyObjectBuilder_SensorBlock))]
- public class EvilSensor : MyGameLogicComponent
- {
- // Initialize them variables
- - static System.String[] OreNames;
- + static String[] OreNames;
- IMySensorBlock Sensor;
- public override void Close()
- {
- - // State changed! I'm not sure how this part works since sensor_StateChanged returns void? Maybe someone could enlighten me?
- - // I'm probably confused by the use of the -= operator.
- + /* State changed! I'm not sure how this part works since sensor_StateChanged returns void?
- + * Maybe someone could enlighten me?
- + * I'm probably confused by the use of the -= operator.
- + */
- Sensor.StateChanged -= sensor_StateChanged;
- }
- public override MyObjectBuilder_EntityBase GetObjectBuilder(bool copy = false)
- {
- - // This is a required override, but we return null and let the caller handle the exception
- + // This is a required override, but we return null and let the caller handle null
- return null;
- }
- @@ -35,14 +39,17 @@
- }
- // Define that Sensor is a SensorBloack
- Sensor = Entity as IMySensorBlock;
- - // State changed! I'm not sure how this part works since sensor_StateChanged returns void? Maybe someone could enlighten me?
- - // I'm probably confused by the use of the -= operator.
- + /* State changed! I'm not sure how this part works since sensor_StateChanged returns void?
- + * Maybe someone could enlighten me?
- + * I'm probably confused by the use of the += operator.
- + */
- Sensor.StateChanged += sensor_StateChanged;
- }
- void sensor_StateChanged(bool obj)
- {
- - // I think this part is depending on whether you entered or exited the range. !obj would be when you exited the range, so nothing happens.
- + // I think this part is depending on whether you entered or exited the range.
- + // !obj would be when you exited the range, so nothing happens.
- if (!obj) return;
- // Define some mo' variables
- @@ -50,6 +57,8 @@
- string sensorName = Sensor.CustomName.ToLowerInvariant();
- + VRageMath.Vector3 direction = Sensor.WorldMatrix.Forward;
- +
- var oreAmount = 100;
- var distance = 1.5f;
- @@ -68,55 +77,160 @@
- // If the sensor name does not contain an ore name, we exit here
- if (ore == null)
- return;
- - // See if the sensor name contains the "data string" as defined by beginning with "[" and ending with "]"
- + // See if the sensor name contains the "data string" as defined
- + // by beginning with "[" and ending with "]"
- if (sensorName.Contains("[") && sensorName.Contains("]"))
- {
- /*
- * At this point, we parse the "data string" from the name,
- - * check to see what variables it contains and their order, then we parse our data from it.
- + * break it down into sections, see what tags those sections
- + * contain, then parse our data from it
- */
- try
- {
- - dataString = sensorName.Slice(sensorName.IndexOf("[") + 1, sensorName.LastIndexOf("]")).ToLowerInvariant().Replace(" ", "");
- - if (dataString.Contains("amt:") && dataString.Contains("dst:"))
- - {
- - if (dataString.IndexOf("amt:") < dataString.IndexOf("dst:")) {
- - oreAmount = System.Convert.ToInt32(dataString.Slice(dataString.IndexOf(":") + 1, dataString.IndexOf(";")));;
- - distance = System.Convert.ToSingle(dataString.Substring(dataString.LastIndexOf(":") + 1));
- - } else {
- - distance = System.Convert.ToSingle(dataString.Slice(dataString.IndexOf(":") + 1, dataString.IndexOf(";")));
- - oreAmount = System.Convert.ToInt32(dataString.Substring(dataString.LastIndexOf(":") + 1));
- + dataString = sensorName.Slice(sensorName.IndexOf("[") + 1, sensorName.LastIndexOf("]")).ToLowerInvariant().Replace(" ", "");
- + string dataSection;
- + bool control = true;
- + while (control)
- + {
- + if (dataString.Contains(";"))
- + {
- + dataSection = dataString.Slice(0, dataString.IndexOf(";"));
- + dataString = dataString.Substring(dataString.IndexOf(";") + 1);
- + string tag = dataSection.Slice(0, dataSection.IndexOf(":") + 1);
- + string data = dataSection.Substring(dataSection.IndexOf(":") + 1);
- + switch (tag)
- + {
- + case "amt:":
- + oreAmount = Convert.ToInt32(data);
- + break;
- + case "dst:":
- + distance = Convert.ToSingle(data);
- + break;
- + case "dir:":
- + switch (data)
- + {
- + case "up":
- + direction = Sensor.WorldMatrix.Up;
- + break;
- + case "down":
- + direction = Sensor.WorldMatrix.Down;
- + break;
- + case "left":
- + direction = Sensor.WorldMatrix.Left;
- + break;
- + case "right":
- + direction = Sensor.WorldMatrix.Right;
- + break;
- + case "front":
- + direction = Sensor.WorldMatrix.Forward;
- + break;
- + case "back":
- + direction = Sensor.WorldMatrix.Backward;
- + break;
- + default:
- + direction = Sensor.WorldMatrix.Forward;
- + break;
- + }
- + break;
- + default:
- + break;
- + }
- + }
- + else
- + {
- + dataSection = dataString;
- + string tag = dataSection.Slice(0, dataSection.IndexOf(":") + 1);
- + string data = dataSection.Substring(dataSection.IndexOf(":") + 1);
- + switch (tag)
- + {
- + case "amt:":
- + oreAmount = Convert.ToInt32(data);
- + break;
- + case "dst:":
- + distance = Convert.ToSingle(data);
- + break;
- + case "dir:":
- + switch (data)
- + {
- + case "up":
- + direction = Sensor.WorldMatrix.Up;
- + break;
- + case "down":
- + direction = Sensor.WorldMatrix.Down;
- + break;
- + case "left":
- + direction = Sensor.WorldMatrix.Left;
- + break;
- + case "right":
- + direction = Sensor.WorldMatrix.Right;
- + break;
- + case "front":
- + direction = Sensor.WorldMatrix.Forward;
- + break;
- + case "back":
- + direction = Sensor.WorldMatrix.Backward;
- + break;
- + default:
- + direction = Sensor.WorldMatrix.Forward;
- + break;
- + }
- + break;
- + default:
- + break;
- + }
- + control = false;
- + }
- }
- + /* Old string handling
- + if (dataString.Contains("amt:") && dataString.Contains("dst:"))
- + {
- + if (dataString.IndexOf("amt:") < dataString.IndexOf("dst:")) {
- + oreAmount = Convert.ToInt32(dataString.Slice(dataString.IndexOf(":") + 1, dataString.IndexOf(";")));;
- + distance = Convert.ToSingle(dataString.Substring(dataString.LastIndexOf(":") + 1));
- + } else {
- + distance = Convert.ToSingle(dataString.Slice(dataString.IndexOf(":") + 1, dataString.IndexOf(";")));
- + oreAmount = Convert.ToInt32(dataString.Substring(dataString.LastIndexOf(":") + 1));
- + }
- + }
- + else if (dataString.Contains("amt:"))
- + {
- + oreAmount = Convert.ToInt32(dataString.Substring(dataString.LastIndexOf(":") + 1));
- + }
- + else if (dataString.Contains("dst:"))
- + {
- + distance = Convert.ToSingle(dataString.Substring(dataString.LastIndexOf(":") + 1));
- + }
- + */
- }
- - else if (dataString.Contains("amt:"))
- - {
- - oreAmount = System.Convert.ToInt32(dataString.Substring(dataString.LastIndexOf(":") + 1));
- - }
- - else if (dataString.Contains("dst:"))
- - {
- - distance = System.Convert.ToSingle(dataString.Substring(dataString.LastIndexOf(":") + 1));
- - }
- -
- - }
- - catch (System.Exception)
- + catch (Exception ex)
- {
- // If something got fubar'ed, set the defaults and try to continue without crashing the damn game
- oreAmount = 100;
- distance = 1.5f;
- + direction = Sensor.WorldMatrix.Forward;
- + Logging.Instance.WriteLine(String.Format("sensor_StateChanged(), String Parser: {0}", ex.ToString()));
- + }
- + }
- + try
- + {
- + // Create the object to be spawned
- + MyObjectBuilder_FloatingObject floatingBuilder = new MyObjectBuilder_FloatingObject();
- + // Specify that the object is an ore inventory item of the amount & type specified by the user
- + floatingBuilder.Item = new MyObjectBuilder_InventoryItem() { Amount = oreAmount, Content = new MyObjectBuilder_Ore() { SubtypeName = ore } };
- + floatingBuilder.PersistentFlags = MyPersistentEntityFlags2.InScene; // Very important, but I'm not sure why. I guess if not flagged as InScene, it won't be recognized as actually being there would be my guess.
- + // Set the postition and orientation of the object in the world.
- + floatingBuilder.PositionAndOrientation = new MyPositionAndOrientation(Sensor.WorldMatrix.Translation + direction * distance, Sensor.WorldMatrix.Forward, Sensor.WorldMatrix.Up);
- + // Check to make sure this is a server instance of the mod (I think it runs on client as well), otherwise a duplicate, non-functional object will be spawned by the client as well. (Duplicate bug)
- + if (Sandbox.ModAPI.MyAPIGateway.Multiplayer.IsServer)
- + {
- + // Finally, we actually create & spawn the damn thing in the world here. Enjoy!
- + var floatingObject = Sandbox.ModAPI.MyAPIGateway.Entities.CreateFromObjectBuilderAndAdd(floatingBuilder);
- }
- }
- - // Create the object to be spawned
- - MyObjectBuilder_FloatingObject floatingBuilder = new MyObjectBuilder_FloatingObject();
- - // Specify that the object is an ore inventory item of the amount & type specified by the user
- - floatingBuilder.Item = new MyObjectBuilder_InventoryItem() { Amount = oreAmount, Content = new MyObjectBuilder_Ore() { SubtypeName = ore } };
- - floatingBuilder.PersistentFlags = MyPersistentEntityFlags2.InScene; // Very important, but I'm not sure why. I guess if not flagged as InScene, it won't be recognized as actually being there would be my guess.
- - // Set the postition and orientation of the object in the world.
- - floatingBuilder.PositionAndOrientation = new MyPositionAndOrientation(Sensor.WorldMatrix.Translation + Sensor.WorldMatrix.Forward * distance, Sensor.WorldMatrix.Forward, Sensor.WorldMatrix.Up);
- - // Check to make sure this is a server instance of the mod (I think it runs on client as well), otherwise a duplicate, non-functional object will be spawned by the client as well. (Duplicate bug)
- - if (Sandbox.ModAPI.MyAPIGateway.Multiplayer.IsServer)
- + catch (Exception ex)
- {
- - // Finally, we actually create & spawn the damn thing in the world here. Enjoy!
- - var floatingObject = Sandbox.ModAPI.MyAPIGateway.Entities.CreateFromObjectBuilderAndAdd(floatingBuilder);
- + Logging.Instance.WriteLine(String.Format("sensor_StateChanged(), Object Builder: {0}", ex.ToString()));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment