Skip to content

Instantly share code, notes, and snippets.

@KevinCathcart
Created October 21, 2014 03:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KevinCathcart/92d7d7d80bac480df7d6 to your computer and use it in GitHub Desktop.
Save KevinCathcart/92d7d7d80bac480df7d6 to your computer and use it in GitHub Desktop.
CLSStockTransfer Plugin
// Copyright (c) 2014 Kevin Cathcart
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Linq;
using System.Reflection;
using UnityEngine;
[assembly: AssemblyTitle("CLSStockTransfer")]
[assembly: AssemblyCopyright("Copyright © 2014 Kevin Cathcart")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: KSPAssembly("CLSStockTransfer", 1, 0)]
namespace CLSStockTransfer
{
[KSPAddon(KSPAddon.Startup.Instantly, true)]
public class CLSStockTransferPlugin : MonoBehaviour
{
private void Awake()
{
GameEvents.onCrewTransferred.Add(CrewTransfered);
}
private void CrewTransfered(GameEvents.HostedFromToAction<ProtoCrewMember, Part> data)
{
if (!CLSClient.CLSInstalled) return;
if (data.from.Modules.Cast<PartModule>().Any(x => x is KerbalEVA) ||
data.to.Modules.Cast<PartModule>().Any(x => x is KerbalEVA))
{
// "Transfers" to/from EVA are always permitted.
// Trying to step them results in really bad things happening, and would be out of
// scope for this plugin anyway.
return;
}
var clsFrom = CLSClient.GetCLS().Vessel.Parts.Find(x => x.Part == data.from);
var clsTo = CLSClient.GetCLS().Vessel.Parts.Find(x => x.Part == data.to);
if (clsFrom==null|| clsTo==null ||clsFrom.Space != clsTo.Space)
{
data.to.RemoveCrewmember(data.host);
data.from.AddCrewmember(data.host);
var message=new ScreenMessage(string.Empty, 15f, ScreenMessageStyle.UPPER_CENTER);
ScreenMessages.PostScreenMessage(string.Format("<color=orange>{0} is unable to reach {1}.</color>", data.host.name, data.to.partInfo.title), message, true);
// Now try to remove the sucessful transfer message
// that stock displayed.
var messages=FindObjectOfType<ScreenMessages>();
if (messages != null)
{
var messagesToRemove = messages.activeMessages.Where(x => x.startTime == message.startTime && x.style == ScreenMessageStyle.LOWER_CENTER).ToList();
foreach (var m in messagesToRemove)
{
ScreenMessages.RemoveMessage(m);
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment