daily pastebin goal
69%
SHARE
TWEET

Eco Fire Barrel Mod Source Code v1.2

a guest May 4th, 2018 122 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. namespace Eco.Mods.TechTree {
  2.     using System;
  3.     using System.Collections.Generic;
  4.     using Eco.Core.Plugins.Interfaces;
  5.     using Eco.Gameplay.Components;
  6.     using Eco.Gameplay.Components.Auth;
  7.     using Eco.Gameplay.Interactions;
  8.     using Eco.Gameplay.Items;
  9.     using Eco.Gameplay.Objects;
  10.     using Eco.Gameplay.Pipes.Gases;
  11.     using Eco.Gameplay.Pipes.LiquidComponents;
  12.     using Eco.Gameplay.Skills;
  13.     using Eco.Gameplay.Systems.TextLinks;
  14.     using Eco.Gameplay.Systems.Tooltip;
  15.     using Eco.Shared.Math;
  16.     using Eco.Shared.Serialization;
  17.     using Eco.Shared.Utils;
  18.  
  19.     [Serialized]
  20.     [RequireComponent(typeof(AttachmentComponent))]
  21.     [RequireComponent(typeof(MinimapComponent))]
  22.     [RequireComponent(typeof(PipeComponent))]
  23.     [RequireComponent(typeof(FuelSupplyComponent))]
  24.     [RequireComponent(typeof(FuelConsumptionComponent))]
  25.     [RequireComponent(typeof(AirPollutionComponent))]
  26.     [RequireComponent(typeof(PropertyAuthComponent))]
  27.  
  28.     public partial class FireBarrelObject : WorldObject, IModKitPlugin {
  29.         public override string FriendlyName { get { return "Fire Barrel"; } }
  30.  
  31.         public string GetStatus() {
  32.             return String.Empty;
  33.         }
  34.  
  35.         static FireBarrelObject() {
  36.             AddOccupancyList(typeof(FireBarrelObject), new BlockOccupancy(new Vector3i(0, 0, 0), typeof(WorldObjectBlock), new Quaternion(0f, 0f, 0f, 1f)));
  37.         }
  38.  
  39.         private static Type[] fuelTypeList = new Type[]
  40.         {
  41.             typeof(ArrowItem),
  42.             typeof(BoardItem),
  43.             typeof(CharcoalItem),
  44.             typeof(CoalItem),
  45.             typeof(LogItem),
  46.             typeof(LumberItem),
  47.         };
  48.  
  49.         [Serialized] private bool fireEnabled = false;
  50.         [Serialized] private float garbageWeightFloat = 0f;
  51.  
  52.         protected override void Initialize() {
  53.             this.GetComponent<MinimapComponent>().Initialize("Fire Barrel");
  54.             this.GetComponent<FuelSupplyComponent>().Initialize(4, fuelTypeList);
  55.             this.GetComponent<FuelConsumptionComponent>().Initialize(20f);
  56.             this.GetComponent<AirPollutionComponent>().Initialize(0.1f);
  57.             this.GetComponent<PropertyAuthComponent>().Initialize(AuthModeType.Inherited);
  58.  
  59.             BlockOccupancy thisBlock = this.Occupancy.Find(x => x.Name == "ChimneyOut");
  60.             thisBlock.Offset = new Vector3i(0, 2, 0); //offset of chimney output
  61.             thisBlock.BlockType = typeof(WorldObjectBlock);
  62.  
  63.             List<LiquidTank> tankList = new List<LiquidTank> {
  64.                 new LiquidProducer("Chimney", typeof(SmogItem), 100, null, thisBlock, (float)(0.25f * SmogItem.SmogItemsPerCO2PPM) / TimeUtil.SecondsPerHour)
  65.             };
  66.  
  67.             this.GetComponent<PipeComponent>().Initialize(tankList);
  68.         }
  69.        
  70.         public override void Destroy() {
  71.             base.Destroy();
  72.         }
  73.  
  74.         public override void Tick() {
  75.             base.Tick();
  76.  
  77.             if (CheckFireEnabled()) { //check if there's fuel and garbage in the barrel, set animation state, set operating state
  78.                 garbageWeightFloat -= 2.0f;
  79.             }
  80.         }
  81.  
  82.         public override InteractResult OnActRight(InteractionContext context) {
  83.             if (context.Parameters != null && context.Parameters.ContainsKey("TakeButton")) {
  84.                 TakeBarrel(context); //add fuel inventory to player, give them the barrel
  85.                 return InteractResult.Success;
  86.             } else {
  87.                 ShowInfo(context); //show garbage weight
  88.                 return InteractResult.Success;
  89.             }
  90.         }
  91.  
  92.         public override InteractResult OnActLeft(InteractionContext context) {
  93.             if (context.SelectedItem != null) {
  94.                 string currentItemName = context.SelectedItem.FriendlyName;
  95.  
  96.                 bool stackMode = CheckStackMode(context.Player.User.Inventory.Toolbar.SelectedStack.Weight); //if garbage barrel + selected stack weight is less than barrel max weight
  97.  
  98.                 if ((garbageWeightFloat / 1000f) < 9.9) //this check is run here instead of AddTrash so the weight can go over the max weight by 1 item
  99.                     AddTrash(context, currentItemName, stackMode); //add to garbageWeight, notify player they threw x item(s) in trash, remove item(s)
  100.                 else
  101.                     context.Player.SendTemporaryErrorLoc("The barrel is full.");
  102.  
  103.                 return InteractResult.Success;
  104.             }
  105.  
  106.             return InteractResult.NoOp;
  107.         }
  108.  
  109.         private void AddTrash(InteractionContext context, string currentItemName, bool stackMode) {
  110.             if (stackMode) {
  111.                 garbageWeightFloat += CheckGarbageWeight(context, true);
  112.  
  113.                 context.Player.SendTemporaryMessageLoc("You threw " + context.Player.User.Inventory.Toolbar.SelectedStack.Quantity.ToString() + " " + currentItemName + " into the fire barrel.");
  114.                 context.Player.User.Inventory.TryRemoveItems(context.Player.User.Inventory.Toolbar.SelectedStack);
  115.             } else {
  116.                 garbageWeightFloat += CheckGarbageWeight(context, false);
  117.  
  118.                 context.Player.SendTemporaryMessageLoc("You threw " + currentItemName + " into the fire barrel.");
  119.                 context.Player.User.Inventory.TryRemoveItem(context.SelectedItem.Type);
  120.             }
  121.         }
  122.  
  123.         private float CheckGarbageWeight(InteractionContext context, bool stack) {
  124.             float addGarbageWeight = 0.0f;
  125.  
  126.             if (stack)
  127.                 addGarbageWeight = context.Player.User.Inventory.Toolbar.SelectedStack.Weight;
  128.             else
  129.                 addGarbageWeight = context.SelectedItem.Weight;
  130.  
  131.             if (addGarbageWeight == 0.0f)
  132.                 addGarbageWeight = 1000f;
  133.  
  134.             return addGarbageWeight;
  135.         }
  136.  
  137.         private bool CheckStackMode(float selectedStackWeight) {
  138.             bool stackMode = false;
  139.  
  140.             if (garbageWeightFloat / 1000f + selectedStackWeight / 1000f < 9.9)
  141.                 stackMode = true;
  142.             else
  143.                 stackMode = false;
  144.  
  145.             return stackMode;
  146.         }
  147.  
  148.         private void TakeBarrel(InteractionContext context) {
  149.             if (garbageWeightFloat < 0.01f)
  150.                 AddItemsFromBarrel(context); //Make sure the items can be added
  151.             else
  152.                 context.Player.SendTemporaryErrorLoc("You can not pick up the barrel with trash already in it.");
  153.         }
  154.  
  155.         private void AddItemsFromBarrel(InteractionContext context) {
  156.             FuelSupplyComponent fuelSupply = this.GetComponent<FuelSupplyComponent>();
  157.  
  158.             bool addLog = false;
  159.             bool addLumber = false;
  160.             bool addArrow = false;
  161.             bool addBoard = false;
  162.             bool addCharcoal = false;
  163.             bool addCoal = false;
  164.  
  165.             if (context.Player.User.Inventory.TryAddItems(typeof(LogItem), fuelSupply.Inventory.TotalNumberOfItems<LogItem>())) {
  166.                 fuelSupply.Inventory.RemoveItems<LogItem>(fuelSupply.Inventory.TotalNumberOfItems<LogItem>());
  167.                 addLog = true;
  168.             } else {
  169.                 context.Player.SendTemporaryErrorLoc("You do not have enough inventory space to pick up all of the logs.");
  170.             }
  171.  
  172.             if (context.Player.User.Inventory.TryAddItems(typeof(LumberItem), fuelSupply.Inventory.TotalNumberOfItems<LumberItem>())) {
  173.                 fuelSupply.Inventory.RemoveItems<LumberItem>(fuelSupply.Inventory.TotalNumberOfItems<LumberItem>());
  174.                 addLumber = true;
  175.             } else {
  176.                 context.Player.SendTemporaryErrorLoc("You do not have enough inventory space to pick up all of the lumber.");
  177.             }
  178.  
  179.             if (context.Player.User.Inventory.TryAddItems(typeof(ArrowItem), fuelSupply.Inventory.TotalNumberOfItems<ArrowItem>())) {
  180.                 fuelSupply.Inventory.RemoveItems<ArrowItem>(fuelSupply.Inventory.TotalNumberOfItems<ArrowItem>());
  181.                 addArrow = true;
  182.             } else {
  183.                 context.Player.SendTemporaryErrorLoc("You do not have enough inventory space to pick up all of the arrows.");
  184.             }
  185.  
  186.             if (context.Player.User.Inventory.TryAddItems(typeof(BoardItem), fuelSupply.Inventory.TotalNumberOfItems<BoardItem>())) {
  187.                 fuelSupply.Inventory.RemoveItems<BoardItem>(fuelSupply.Inventory.TotalNumberOfItems<BoardItem>());
  188.                 addBoard = true;
  189.             } else {
  190.                 context.Player.SendTemporaryErrorLoc("You do not have enough inventory space to pick up all of the boards.");
  191.             }
  192.  
  193.             if (context.Player.User.Inventory.TryAddItems(typeof(CharcoalItem), fuelSupply.Inventory.TotalNumberOfItems<CharcoalItem>())) {
  194.                 fuelSupply.Inventory.RemoveItems<CharcoalItem>(fuelSupply.Inventory.TotalNumberOfItems<CharcoalItem>());
  195.                 addCharcoal = true;
  196.             } else {
  197.                 context.Player.SendTemporaryErrorLoc("You do not have enough inventory space to pick up all of the charcoal.");
  198.             }
  199.  
  200.             if (context.Player.User.Inventory.TryAddItems(typeof(CoalItem), fuelSupply.Inventory.TotalNumberOfItems<CoalItem>())) {
  201.                 fuelSupply.Inventory.RemoveItems<CoalItem>(fuelSupply.Inventory.TotalNumberOfItems<CoalItem>());
  202.                 addCoal = true;
  203.             } else {
  204.                 context.Player.SendTemporaryErrorLoc("You do not have enough inventory space to pick up all of the coal.");
  205.             }
  206.  
  207.             if (addLog && addLumber && addArrow && addBoard && addCharcoal && addCoal) {
  208.                 if (context.Player.User.Inventory.TryAddItem(typeof(FireBarrelItem))) {
  209.                     this.Destroy();
  210.                 } else {
  211.                     context.Player.SendTemporaryErrorLoc("You do not have enough inventory space to pick up the fire barrel.");
  212.                 }
  213.             }
  214.         }
  215.        
  216.         private void ShowInfo(InteractionContext context) {
  217.             float garbagePercent = ((garbageWeightFloat / 1000f) / 10.0f) * 100;
  218.             garbagePercent = (float) Math.Round(garbagePercent, 2);
  219.  
  220.             context.Player.OpenInfoPanel("Fire Barrel Info", (GetColor(garbagePercent) + "<size=50>" + garbagePercent.ToString() + "% full</size>"));
  221.         }
  222.  
  223.         private bool CheckFireEnabled() {
  224.             FuelSupplyComponent fuelSupplyComponent = this.GetComponent<FuelSupplyComponent>();
  225.             float fuelSupply = fuelSupplyComponent.Energy;
  226.  
  227.             if (fuelSupply > 0 && (garbageWeightFloat) > 0) {
  228.                 fireEnabled = true;
  229.             } else if (garbageWeightFloat < 0) {
  230.                 garbageWeightFloat = 0f; //lazy fix for negative values
  231.                 fireEnabled = false;
  232.             } else
  233.                 fireEnabled = false;
  234.  
  235.             SetAnimatedState("FireBarrel", fireEnabled);
  236.             this.operating = fireEnabled;
  237.             return fireEnabled;
  238.         }
  239.  
  240.         private string GetColor(float garbagePercent) {
  241.             string colorString = "";
  242.  
  243.             if (garbagePercent < 25)
  244.                 colorString = "<color=#ffffff>";
  245.             else if (garbagePercent >= 25 && garbagePercent < 50)
  246.                 colorString = "<color=#33cc33>";
  247.             else if (garbagePercent >= 50 && garbagePercent < 75)
  248.                 colorString = "<color=#ffff00>";
  249.             else if (garbagePercent >= 75 && garbagePercent < 100)
  250.                 colorString = "<color=#ff6600>";
  251.             else if (garbagePercent >= 100)
  252.                 colorString = "<color=#ff0000>";
  253.  
  254.             return colorString;
  255.         }
  256.     }
  257.  
  258.     [Serialized]
  259.     public partial class FireBarrelItem : WorldObjectItem<FireBarrelObject> {
  260.         public override string FriendlyName { get { return "Fire Barrel"; } }
  261.         public override string Description { get { return "A barrel you can burn trash items in."; } }
  262.  
  263.         static FireBarrelItem() {
  264.  
  265.         }
  266.  
  267.     }
  268.  
  269.     [RequiresSkill(typeof(MetalworkingSkill), 1)]
  270.     public partial class FireBarrelRecipe : Recipe {
  271.         public FireBarrelRecipe() {
  272.             this.Products = new CraftingElement[]
  273.             {
  274.                 new CraftingElement<FireBarrelItem>(),
  275.             };
  276.  
  277.             this.Ingredients = new CraftingElement[]
  278.             {
  279.                 new CraftingElement<IronIngotItem>(typeof(MetalworkingEfficiencySkill), 25, MetalworkingEfficiencySkill.MultiplicativeStrategy),
  280.             };
  281.  
  282.             this.CraftMinutes = CreateCraftTimeValue(typeof(FireBarrelRecipe), Item.Get<FireBarrelItem>().UILink(), 5.0f, typeof(MetalworkingSpeedSkill));
  283.             this.Initialize("Fire Barrel", typeof(FireBarrelRecipe));
  284.             CraftingComponent.AddRecipe(typeof(AnvilObject), this);
  285.         }
  286.     }
  287. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top