C#:
using System;
using System.Collections.Generic;
using Oxide.Core;
using Oxide.Core.Plugins;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("AutoPlace", "RandomCoder", "1.0.0")]
[Description("Automatically places doors when a door frame or doorway is placed.")]
class AutoPlace : RustPlugin
{
private Dictionary<NetworkableId, BaseEntity> doorFrames = new Dictionary<NetworkableId, BaseEntity>();
private bool isActive = false;
private Timer activationTimer;
private ulong garageDoorSkinID = 2936423837;
private ulong DoorSkinID = 869475498;
private void OnEntityBuilt(Planner planner, GameObject gameObj)
{
if (planner == null || gameObj == null || !isActive)
return;
BaseEntity entity = gameObj.ToBaseEntity();
if (entity == null)
return;
if (entity.ShortPrefabName == "wall.frame" || entity.ShortPrefabName == "wall.doorway" || entity.ShortPrefabName == "wall.window" || entity.ShortPrefabName == "floor.frame")
{
doorFrames[entity.net.ID] = entity;
NextTick(() => TryPlaceDoor(entity));
}
}
private void TryPlaceDoor(BaseEntity doorFrame)
{
if (doorFrame == null || !doorFrame.IsValid() || !doorFrames.ContainsKey(doorFrame.net.ID))
return;
string doorType = DetermineDoorType(doorFrame);
if (doorType != null)
{
BaseEntity door = GameManager.server.CreateEntity(doorType, doorFrame.transform.position, doorFrame.transform.rotation, true);
if (door != null)
{
if (doorType.Contains("garagedoor"))
{
door.skinID = garageDoorSkinID;
}
if (doorType.Contains("hinged"))
{
door.skinID = DoorSkinID;
}
if (doorType.Contains("glass"))
{
Vector3 doorPosition = door.transform.position;
doorPosition.y += 1.0f;
door.transform.position = doorPosition;
}
door.Spawn();
doorFrames.Remove(doorFrame.net.ID);
}
}
}
private string DetermineDoorType(BaseEntity doorFrame)
{
if (doorFrame.ShortPrefabName == "wall.frame")
{
return "assets/prefabs/building/wall.frame.garagedoor/wall.frame.garagedoor.prefab";
}
else if (doorFrame.ShortPrefabName == "wall.doorway")
{
return "assets/prefabs/building/door.hinged/door.hinged.toptier.prefab";
}
else if (doorFrame.ShortPrefabName == "wall.window")
{
return "assets/prefabs/building/wall.window.reinforcedglass/wall.window.glass.reinforced.prefab";
}
// else if (doorFrame.ShortPrefabName == "floor.frame")
// {
// Puts("Floor Frame: " + doorFrame.ShortPrefabName);
// return "assets/content/building/parts/static/floor.grill.prefab";
// }
return null;
}
[ChatCommand("ap")]
private void ActivateAutoPlaceCommand(BasePlayer player, string command, string[] args)
{
if (isActive)
{
player.ChatMessage("AutoPlace is already active.");
return;
}
isActive = true;
player.ChatMessage("AutoPlace is now active for 30 seconds.");
activationTimer = timer.Once(30f, () =>
{
isActive = false;
player.ChatMessage("AutoPlace is now inactive.");
});
}
}
}