{SOLVED} Problem with detecting SLeepingBag on placement

Discussion in 'General Discussion' started by ice cold, Feb 3, 2018.

Tags:
  1. ice cold
    Offline

    ice cold Plugin Developer Plugin Developer

    Joined:
    Oct 24, 2016
    Messages:
    579
    Likes Received:
    60
    i'm coding an PlaceRestriction plugin but for soem reason The SingleBed works but the Sleeping bag not

    Code (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)");
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
     
     
    #1
  2. Jakkee
    Offline

    Jakkee Plugin Developer Plugin Developer Contributor

    Joined:
    Jul 28, 2014
    Messages:
    1,450
    Likes Received:
    136
    SleepingBagA is the name ;)
     
    #2
    • Funny Funny x 1
  3. ice cold
    Offline

    ice cold Plugin Developer Plugin Developer

    Joined:
    Oct 24, 2016
    Messages:
    579
    Likes Received:
    60
    solved
     
    #3
  4. salva
    Offline

    salva Friendly self-taught developer Administrator

    Joined:
    Jan 31, 2016
    Messages:
    573
    Likes Received:
    88
    You do not need such a long code, you should reduce it
     
    #4
  5. DreTaX
    Offline

    DreTaX Probably knows the answer... Administrator

    Joined:
    Jun 29, 2014
    Messages:
    3,886
    Likes Received:
    677
    FindEntitiesAround may not be the fastest solution for this.
     
    #5
  6. ice cold
    Offline

    ice cold Plugin Developer Plugin Developer

    Joined:
    Oct 24, 2016
    Messages:
    579
    Likes Received:
    60
    What is a better way sir?
     
    #6
  7. Jakkee
    Offline

    Jakkee Plugin Developer Plugin Developer Contributor

    Joined:
    Jul 28, 2014
    Messages:
    1,450
    Likes Received:
    136
    Check if it’s on an entity?
    Unless you don’t want them placing it within x metres of the structure
     
    #7
  8. DreTaX
    Offline

    DreTaX Probably knows the answer... Administrator

    Joined:
    Jun 29, 2014
    Messages:
    3,886
    Likes Received:
    677
    Code (C#):
    Collider[] hitColliders = Physics.OverlapSphere(vector, 5f);
    foreach (var x in hitColliders)
    {
            Logger.Log("Test: " + x.gameObject.name);
    }
    I don't know if that will print out the object's name but give it a try.
     
    #8
  9. DreTaX
    Offline

    DreTaX Probably knows the answer... Administrator

    Joined:
    Jun 29, 2014
    Messages:
    3,886
    Likes Received:
    677
    Code (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.
     
    #9

Share This Page