Иконка ресурса

FREE DefaultSkins : 1.0.2 1.0.2

Нет прав для скачивания
C#:
using Facepunch;
using Newtonsoft.Json;
using Oxide.Core;
using System;
using System.Collections.Generic;
using UnityEngine;

namespace Oxide.Plugins
{
    [Info("Default Skins", "SiCkNeSs", "1.0.2")]
    [Description("Changes skins for items, when crafted or placed if there is no skin set")]
    public class DefaultSkins : RustPlugin
    {
        private ConfigData config;

        #region hooks
        private void OnServerInitialized()
        {
            if (!config.changeCrafted)
                Unsubscribe("OnItemCraftFinished");

            if (!config.changePlaced)
                Unsubscribe("OnEntityBuilt");

            if (!config.changeLooted)
                Unsubscribe("OnPlayerLootEnd");

            if (!config.changeRespawn)
                Unsubscribe("OnPlayerRespawned");
        }

        private void OnEntityBuilt(Planner plan, GameObject gameObject)
        {
            if (gameObject == null || !gameObject.ToBaseEntity().OwnerID.IsSteamId() || gameObject.ToBaseEntity().skinID != 0) return;

            Debug($"EntityBuilt Prefab Name is: {gameObject.ToBaseEntity().ShortPrefabName}");

            if (!config.defaultSkins.TryGetValue(gameObject.ToBaseEntity().ShortPrefabName, out ulong defaultSkin)) return;

            Debug($"EntityBuilt Detected SkinID is: {defaultSkin}");

            SkinItem(gameObject.ToBaseEntity(), defaultSkin);
        }

        private void OnItemCraftFinished(ItemCraftTask task, Item item, ItemCrafter itemCrafter)
        {
            if (task.skinID != 0) return;

            if (itemCrafter.owner == null || !itemCrafter.owner.UserIDString.IsSteamId()) return;
            
            Debug($"CraftFinish Prefab Name is: {item.info.shortname}");

            if (!config.defaultSkins.TryGetValue(item.info.shortname, out ulong defaultSkin)) return;

            Debug($"CraftFinish Detected SkinID is: {defaultSkin}");

            SkinItem(item, defaultSkin);
        }

        private void OnPlayerLootEnd(PlayerLoot instance)
        {
            //instance.baseEntity.userID;

            if (instance == null || !instance.baseEntity.UserIDString.IsSteamId()) return;
            var player = BasePlayer.FindAwakeOrSleepingByID(instance.baseEntity.userID);
            var playerItems = Pool.Get<List<Item>>();
            player.inventory.GetAllItems(playerItems);

            foreach (var item in playerItems)
            {
                Debug($"LootEnd Prefab Name is: {item.info.shortname}");
                if (item.skin > 0 || !config.defaultSkins.TryGetValue(item.info.shortname, out ulong defaultSkin)) continue;
                Debug($"LootEnd Detected SkinID is: {defaultSkin}");

                SkinItem(item, defaultSkin);
            }

            Pool.FreeUnmanaged(ref playerItems);

        }

        private void OnPlayerRespawned(BasePlayer player)
        {
            if (player == null || !player.UserIDString.IsSteamId()) return;

            var playerItems = Pool.Get<List<Item>>();
            player.inventory.GetAllItems(playerItems);

            foreach (var item in playerItems)
            {
                Debug($"Respawn Prefab Name is: {item.info.shortname}");
                if (item.skin > 0 || !config.defaultSkins.TryGetValue(item.info.shortname, out ulong defaultSkin)) continue;
                Debug($"Respawn Detected SkinID is: {defaultSkin}");

                SkinItem(item, defaultSkin);
            }

            Pool.FreeUnmanaged(ref playerItems);

        }
        #endregion hooks


        private void Debug(string message)
        {
            if (config.debug)
                PrintWarning(message);
        }

        private void SkinItem(BaseEntity entity, ulong skinID)
        {
            if (skinID > 0)
            {
                entity.skinID = skinID;
                entity.SendNetworkUpdate();
            }
        }

        private void SkinItem(Item item, ulong skinID)
        {
            if (skinID > 0)
            {
                item.skin = skinID;
                item.MarkDirty();

                var held = item.GetHeldEntity();

                if (held != null)
                {
                    held.skinID = skinID;
                    held.SendNetworkUpdate();
                }
            }
        }

        public class ConfigData
        {
            [JsonProperty(PropertyName = "Default Skins")]
            public Dictionary<string, ulong> defaultSkins;

            [JsonProperty(PropertyName = "Change Crafted Item Skins")]
            public bool changeCrafted;

            [JsonProperty(PropertyName = "Change Placed Item Skins")]
            public bool changePlaced;

            [JsonProperty(PropertyName = "Change Skins on Respawn")]
            public bool changeRespawn;

            [JsonProperty(PropertyName = "Change Looted Item Skin")]
            public bool changeLooted;

            [JsonProperty(PropertyName = "Debug")]
            public bool debug;

            [JsonProperty(PropertyName = "Version")]
            public VersionNumber Version;
        }

        private ConfigData GetDefaultConfig()
        {
            PrintWarning("Creating a new configuration file");
            return new ConfigData
            {
                defaultSkins = new Dictionary<string, ulong>
                {
                    { "box.wooden.large", 0}
                },
                changeCrafted = true,
                changePlaced = true,
                changeRespawn = true,
                changeLooted = true,
                debug = false,
                Version = this.Version
            };
        }

        protected override void LoadDefaultConfig()
        {
            Puts("Configuration file missing, creating default config file.");
            config = GetDefaultConfig();
        }

        protected override void SaveConfig()
        {
            Config.WriteObject(config);
        }

        protected override void LoadConfig()
        {
            base.LoadConfig();

            try
            {
                config = Config.ReadObject<ConfigData>();

                if (config == null)
                {
                    LoadDefaultConfig();
                    SaveConfig();
                }

                if(config.Version < new VersionNumber(1, 0, 2))
                {
                    config.debug = false;
                    config.changeRespawn = true;
                    config.changeLooted = true;
                }

                if(config.Version != this.Version)
                {
                    config.Version = this.Version;
                    SaveConfig();
                }
            }

            catch (Exception ex)
            {
                PrintError($"The configuration file is corrupted, creating default config file. \n");
                LoadDefaultConfig();

                SaveConfig();
            }
        }
    }
}

Автор
15FPS
Скачиваний
0
Просмотры
1
Первый выпуск
Обновление
Оценка
0.00 звёзд 0 оценок
Сверху