1. Hi guys, I would like to ask for some help for a plugin I’m working on: I was looking for a plugin that could turn on and off lanterns placed by players at a certain game time. So I looked into oxide archive and I found the R-lantern plugin by Reneb that turns on and off lanterns when a player enters a cupboard radius. I don’t really like it to work like that, I would like to have all lanterns (or all the lanterns placed by players who activate a specific plugin command maybe) to be turned on at night time and off at daytime.

    So I tried to modify the plugin by Reneb eliminating stuff for the calculation of radius and player zone and I finally made it.

    The plugin checks the time onTick and if it's the first time for night or for morning it activates / deactivates all server lanterns.

    This is made using FindObjectsOfTypeAll
    Code:
    UnityEngine.Object.FindObjectsOfTypeAll(global.BaseEntity._type)
    And then looping through all entities looking for lanterns, here’s the code
    Code:
    local function switchAllLanterns(light)
      allWorldItems = UnityEngine.Object.FindObjectsOfTypeAll(global.BaseEntity._type)  for i=0, allWorldItems.Length-1 do
          if tostring(allWorldItems[i]):find("lantern") then
              allWorldItems[i]:SetFlag(global["BaseEntity+Flags"].On, light)
          end
      end
    end
    Now I’m not very ok with the results for two reasons

    1- I would like to rewrite it in C# (at the moment it’s in LUA since I started modifying Reneb code)
    2- I would like to achieve a better way to find lanterns (if possible) since looping through all entities takes from 1s to, sometimes, 4s

    So, is there a better way to look for specific items such as lanterns and how can I write it down in C#?

    Thank you very much!
     
    Last edited by a moderator: Jun 16, 2015
  2. Example C# code that you could use:
    Code:
    [ChatCommand("lanterns")]
            void chatCommand_Lanterns(BasePlayer player, string command, string[] args)
            {
                List<BaseOven> ovens = Component.FindObjectsOfType<BaseOven>().ToList();
                foreach (BaseOven oven in ovens)
                {
                    if (oven.LookupShortPrefabName() == "lantern_deployed")
                    {
                        //Do lantern stuff?
                        oven.SetFlag(BaseEntity.Flags.On, true); //Turn lantern on
                    }
                }
            }
     
    Last edited by a moderator: Jun 16, 2015
  3. Thank you very much Lederp, I'll give it a try asap!
    [DOUBLEPOST=1434616142,1434476311][/DOUBLEPOST]It seems to be working very well! I'm not 100% sure if my c# rewrite is completely ok beacuse I've got some problems with my server and I couldn't fully test it with day/night cycle yesterday.
    Anyway, calling your code with chat command turns lanterns on and off perfectly (passing true or false to SetFlag), thank you very much!
     
  4. After last oxide update, with day/night cycle turned back to normal, and some days of testing I can say my C# rewrite does its job veeerrryyyy well!

    Thanks Lederp!
     
    Last edited by a moderator: Jun 22, 2015
  5. it seems all the mods that turn lanterns on are now broken with this update. Does anyone know what changed? I have take a look at the code and I have looped through the deployable "ovens" on my server and I see lantern.deployed.prefab yet turning them on like Lederp shows above does not seem to work anymore.
     
  6. Wulf

    Wulf Community Admin

    The prefab was renamed. You'd need to change it to the correct one, though not sure if all are needed below.
    Code:
    assets/prefabs/deployable/jack o lantern/jackolantern.angry.item.prefab
    assets/prefabs/deployable/jack o lantern/jackolantern.angry.prefab
    assets/prefabs/deployable/jack o lantern/jackolantern.angry.worldmodel.prefab
    assets/prefabs/deployable/jack o lantern/jackolantern.happy.item.prefab
    assets/prefabs/deployable/jack o lantern/jackolantern.happy.prefab
    assets/prefabs/deployable/jack o lantern/jackolantern.happy.worldmodel.prefab
     
  7. Thanks Wulf, two questions:
    1. how do I get a list of current prefabs? is there a mod like the Itemlist.cs mod?
    2. what is the lantern prefab name? that is the one I want to get working.

    once I get it fixed in NightLantern.cs I will rev and publish it so others can get it.
    [DOUBLEPOST=1446855485][/DOUBLEPOST]I made changes and the lanterns still do not turn on:

    Code:
    void OnItemDeployed(Deployer deployer, BaseEntity deployedEntity)
            {
                bool status = false;
                double currentTime = TOD_Sky.Instance.Cycle.Hour;
               
                //myPuts("deployedEntity.LookupShortPrefabName() : " + deployedEntity.LookupShortPrefabName());
               
                if (deployedEntity.LookupShortPrefabName() == "lantern.deployed.prefab"
                    || deployedEntity.LookupShortPrefabName() == "jackolantern.happy.prefab"
                    || deployedEntity.LookupShortPrefabName() == "jackolantern.angry.prefab"
                    || deployedEntity.LookupShortPrefabName() == "campfire.deployed.prefab"    )
                {
                   
                    if ( currentTime < sunriseHour && isAutoTurnLanternsEnabled || currentTime >= sunsetHour && isAutoTurnLanternsEnabled )
                        {
                            status = true;
                        }
                   
                    deployedEntity.SetFlag(BaseEntity.Flags.On, status);
                }
            }
    
     
  8. I know this is not the best way to do it but in the mean time just use
    Code:
    if (Convert.ToString(oven.LookupShortPrefabName()).Contains("lantern"))
        {
           oven.SetFlag(BaseEntity.Flags.On, status);
        }