{SOLVED} Problem with detecting SLeepingBag on placement

ice cold

Active Member
Member
Oct 24, 2016
610
102
28
Canada
i'm coding an PlaceRestriction plugin but for soem reason The SingleBed works but the Sleeping bag not

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Fougerite;
using Fougerite.Events;
using UnityEngine;
using RustPP.Social;

namespace PlaceRestriction
{
    public class PlaceRestriction : Fougerite.Module
    {
        public bool UsingRustPP = false;
        public string sys = "PlaceRestriction";
        private string orange = "[color #FF8000]";
        public override string Name { get { return "PlaceRestriction"; } }
        public override string Author { get { return "ice cold"; } }
        public override string Description { get { return "Blocks any placement on your ceiling/foundation from others unless they are in your friendslist with /addfriend"; } }
        public override Version Version { get { return new Version("1.0"); } }

        public override void Initialize()
        {
            Fougerite.Hooks.OnCommand += OnCommand;
            Fougerite.Hooks.OnModulesLoaded += OnModulesLoaded;
            Fougerite.Hooks.OnEntityDeployedWithPlacer += OnEntityDeployed;
        }
        public override void DeInitialize()
        {
            Fougerite.Hooks.OnCommand -= OnCommand;
            Fougerite.Hooks.OnModulesLoaded -= OnModulesLoaded;
            Fougerite.Hooks.OnEntityDeployedWithPlacer -= OnEntityDeployed;
        }
        public void OnModulesLoaded()
        {
            if (Fougerite.Server.GetServer().HasRustPP)
            {
                Logger.Log("Rust++ was founded now this plugin can be used");
                UsingRustPP = true;
            }
            else
            {
                Logger.Log("Rust++ was not found now this plugin doesnt work");
                UsingRustPP = false;
                Fougerite.Hooks.OnEntityDeployedWithPlacer -= OnEntityDeployed;
            }
        }
        public void OnCommand(Fougerite.Player pl, string cmd, string[] args)
        {
            if (cmd == "placerestriction")
            {
                pl.MessageFrom(sys, orange + "PlaceRestriction by ice cold");
                pl.MessageFrom(sys, orange + "/pr - to get information about this plugin");
            }
            else if (cmd == "pr")
            {
                pl.MessageFrom(sys, orange + "This plugin blocks all placement on ceilings/foundation");
                pl.MessageFrom(sys, orange + "if the player is not owner or in the friends list");
            }
        }
        public void OnEntityDeployed(Fougerite.Player pl, Fougerite.Entity e, Fougerite.Player actualplacer)
        {
            if (UsingRustPP == true)
            {
                if (e.Name == "SleepingBag")
                {
                    foreach (Fougerite.Entity xx in Util.GetUtil().FindEntitiesAround(e.Location, 5f))
                    {
                        if (xx.Name.ToLower().Contains("ceiling") || (xx.Name.ToLower().Contains("foundation")))
                        {
                            if (xx.OwnerID == actualplacer.SteamID)
                            {
                                pl.MessageFrom(sys, orange + "hee this is your base");

                            }
                            else
                            {
                                var friendc = Fougerite.Server.GetServer().GetRustPPAPI().GetFriendsCommand.GetFriendsLists();
                                if (friendc.ContainsKey(xx.UOwnerID))
                                {
                                    var fs = (RustPP.Social.FriendList)friendc[xx.UOwnerID];
                                    bool isfriend = fs.Cast<FriendList.Friend>().Any(friend => friend.GetUserID() == actualplacer.UID);
                                    if (isfriend)
                                    {
                                        pl.MessageFrom(sys, orange + "This is an Friended base");
                                    }
                                    else
                                    {
                                        e.Destroy();
                                        {
                                            actualplacer.MessageFrom(sys, orange + "You cannot place near Structures [color green](/pr)");
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                else if (e.Name == "SingleBed")
                {
                    foreach (Fougerite.Entity xx in Util.GetUtil().FindEntitiesAround(e.Location, 5f))
                    {
                        if (xx.Name.ToLower().Contains("ceiling") || (xx.Name.ToLower().Contains("foundation")))
                        {
                            if (xx.OwnerID == actualplacer.SteamID)
                            {
                                pl.MessageFrom(sys, orange + "hee this is your base");
                            }
                            else
                            {
                                var friendc = Fougerite.Server.GetServer().GetRustPPAPI().GetFriendsCommand.GetFriendsLists();
                                if (friendc.ContainsKey(xx.UOwnerID))
                                {
                                    var fs = (RustPP.Social.FriendList)friendc[xx.UOwnerID];
                                    bool isfriend = fs.Cast<FriendList.Friend>().Any(friend => friend.GetUserID() == actualplacer.UID);
                                    if (isfriend)
                                    {
                                        pl.MessageFrom(sys, orange + "This is an Friended base");
                                    }
                                    else
                                    {
                                        e.Destroy();
                                        actualplacer.MessageFrom(sys, orange + "You cannot place near Structures [color green](/pr)");
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
 

salva

Friendly self-taught developer
Administrator
Jan 31, 2016
576
164
43
i'm coding an PlaceRestriction plugin but for soem reason The SingleBed works but the Sleeping bag not

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Fougerite;
using Fougerite.Events;
using UnityEngine;
using RustPP.Social;

namespace PlaceRestriction
{
    public class PlaceRestriction : Fougerite.Module
    {
        public bool UsingRustPP = false;
        public string sys = "PlaceRestriction";
        private string orange = "[color #FF8000]";
        public override string Name { get { return "PlaceRestriction"; } }
        public override string Author { get { return "ice cold"; } }
        public override string Description { get { return "Blocks any placement on your ceiling/foundation from others unless they are in your friendslist with /addfriend"; } }
        public override Version Version { get { return new Version("1.0"); } }

        public override void Initialize()
        {
            Fougerite.Hooks.OnCommand += OnCommand;
            Fougerite.Hooks.OnModulesLoaded += OnModulesLoaded;
            Fougerite.Hooks.OnEntityDeployedWithPlacer += OnEntityDeployed;
        }
        public override void DeInitialize()
        {
            Fougerite.Hooks.OnCommand -= OnCommand;
            Fougerite.Hooks.OnModulesLoaded -= OnModulesLoaded;
            Fougerite.Hooks.OnEntityDeployedWithPlacer -= OnEntityDeployed;
        }
        public void OnModulesLoaded()
        {
            if (Fougerite.Server.GetServer().HasRustPP)
            {
                Logger.Log("Rust++ was founded now this plugin can be used");
                UsingRustPP = true;
            }
            else
            {
                Logger.Log("Rust++ was not found now this plugin doesnt work");
                UsingRustPP = false;
                Fougerite.Hooks.OnEntityDeployedWithPlacer -= OnEntityDeployed;
            }
        }
        public void OnCommand(Fougerite.Player pl, string cmd, string[] args)
        {
            if (cmd == "placerestriction")
            {
                pl.MessageFrom(sys, orange + "PlaceRestriction by ice cold");
                pl.MessageFrom(sys, orange + "/pr - to get information about this plugin");
            }
            else if (cmd == "pr")
            {
                pl.MessageFrom(sys, orange + "This plugin blocks all placement on ceilings/foundation");
                pl.MessageFrom(sys, orange + "if the player is not owner or in the friends list");
            }
        }
        public void OnEntityDeployed(Fougerite.Player pl, Fougerite.Entity e, Fougerite.Player actualplacer)
        {
            if (UsingRustPP == true)
            {
                if (e.Name == "SleepingBag")
                {
                    foreach (Fougerite.Entity xx in Util.GetUtil().FindEntitiesAround(e.Location, 5f))
                    {
                        if (xx.Name.ToLower().Contains("ceiling") || (xx.Name.ToLower().Contains("foundation")))
                        {
                            if (xx.OwnerID == actualplacer.SteamID)
                            {
                                pl.MessageFrom(sys, orange + "hee this is your base");

                            }
                            else
                            {
                                var friendc = Fougerite.Server.GetServer().GetRustPPAPI().GetFriendsCommand.GetFriendsLists();
                                if (friendc.ContainsKey(xx.UOwnerID))
                                {
                                    var fs = (RustPP.Social.FriendList)friendc[xx.UOwnerID];
                                    bool isfriend = fs.Cast<FriendList.Friend>().Any(friend => friend.GetUserID() == actualplacer.UID);
                                    if (isfriend)
                                    {
                                        pl.MessageFrom(sys, orange + "This is an Friended base");
                                    }
                                    else
                                    {
                                        e.Destroy();
                                        {
                                            actualplacer.MessageFrom(sys, orange + "You cannot place near Structures [color green](/pr)");
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                else if (e.Name == "SingleBed")
                {
                    foreach (Fougerite.Entity xx in Util.GetUtil().FindEntitiesAround(e.Location, 5f))
                    {
                        if (xx.Name.ToLower().Contains("ceiling") || (xx.Name.ToLower().Contains("foundation")))
                        {
                            if (xx.OwnerID == actualplacer.SteamID)
                            {
                                pl.MessageFrom(sys, orange + "hee this is your base");
                            }
                            else
                            {
                                var friendc = Fougerite.Server.GetServer().GetRustPPAPI().GetFriendsCommand.GetFriendsLists();
                                if (friendc.ContainsKey(xx.UOwnerID))
                                {
                                    var fs = (RustPP.Social.FriendList)friendc[xx.UOwnerID];
                                    bool isfriend = fs.Cast<FriendList.Friend>().Any(friend => friend.GetUserID() == actualplacer.UID);
                                    if (isfriend)
                                    {
                                        pl.MessageFrom(sys, orange + "This is an Friended base");
                                    }
                                    else
                                    {
                                        e.Destroy();
                                        actualplacer.MessageFrom(sys, orange + "You cannot place near Structures [color green](/pr)");
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
You do not need such a long code, you should reduce it
 

DreTaX

Probably knows the answer...
Administrator
Jun 29, 2014
3,956
1,129
113
At your house.
github.com
What is a better way sir?
Check if it’s on an entity?
Unless you don’t want them placing it within x metres of the structure
C#:
        public Entity FindClosestEntity(Vector3 givenPosition, float dist = 1f)
        {
            Collider[] array = Facepunch.MeshBatch.MeshBatchPhysics.OverlapSphere(givenPosition, dist);
            if (array.Length == 0)
            {
                return null;
            }
            Collider closest = array[0];
            for (int i = 0; i < array.Length; i++)
            {
                if (Vector3.Distance(array[i].transform.position, givenPosition) < Vector3.Distance(closest.transform.position, givenPosition))
                {
                    closest = array[i];
                }
            }
            if (closest.gameObject.GetComponent<StructureMaster>())
            {
                return new Entity(closest.gameObject.GetComponent<StructureMaster>());
            }
            if (closest.gameObject.GetComponent<StructureComponent>())
            {
                return new Entity(closest.gameObject.GetComponent<StructureComponent>());
            }
            if (closest.gameObject.GetComponent<DeployableObject>())
            {
                return new Entity(closest.gameObject.GetComponent<DeployableObject>());
            }
            if (closest.gameObject.GetComponent<LootableObject>())
            {
                return new Entity(closest.gameObject.GetComponent<LootableObject>());
            }
            if (closest.gameObject.GetComponent<SupplyCrate>())
            {
                return new Entity(closest.gameObject.GetComponent<SupplyCrate>());
            }
            if (closest.gameObject.GetComponent<ResourceTarget>())
            {
                return new Entity(closest.gameObject.GetComponent<ResourceTarget>());
            }
            return null;
        }

        public List<Entity> FindEntitysAroundFast(Vector3 givenPosition, float dist = 1f)
        {
            Collider[] array = Facepunch.MeshBatch.MeshBatchPhysics.OverlapSphere(givenPosition, dist);
            List<Entity> list = new List<Entity>();
            foreach (var x in array)
            {
                if (x.gameObject.GetComponent<StructureMaster>())
                {
                    list.Add(new Entity(x.gameObject.GetComponent<StructureMaster>()));
                }
                else if (x.gameObject.GetComponent<StructureComponent>())
                {
                    list.Add(new Entity(x.gameObject.GetComponent<StructureComponent>()));
                }
                else if (x.gameObject.GetComponent<DeployableObject>())
                {
                    list.Add(new Entity(x.gameObject.GetComponent<DeployableObject>()));
                }
                else if (x.gameObject.GetComponent<LootableObject>())
                {
                    list.Add(new Entity(x.gameObject.GetComponent<LootableObject>()));
                }
                else if (x.gameObject.GetComponent<SupplyCrate>())
                {
                    list.Add(new Entity(x.gameObject.GetComponent<SupplyCrate>()));
                }
                else if (x.gameObject.GetComponent<ResourceTarget>())
                {
                    list.Add(new Entity(x.gameObject.GetComponent<ResourceTarget>()));
                }
            }
            return list;
        }
Both of these methods will be added in Fougerite, since facepunch libs find the game objects correctly, and also using this is a lot faster than looping through the bugged findobjectsoftype. I will add this to HungerGames too.