using System; using UnityEngine; using Oxide.Core; using System.Collections.Generic; namespace Oxide.Plugins { [Info("GroupCrafting", "Death", "1.0.4")] class GroupCrafting : RustPlugin { #region Declarations const string perm = "groupcrafting.admin"; Dictionary PlayerCrafting = new Dictionary(); float cacheSpeed; #endregion #region Hooks void OnServerInitialized() { foreach (var group in permission.GetGroups()) { if (!configData.CraftingSpeed.ContainsKey(group)) { configData.CraftingSpeed.Add(group, 1); } } foreach (var player in BasePlayer.activePlayerList) { OnPlayerConnected(player); } Config.WriteObject(configData, true); permission.RegisterPermission(perm, this); } void OnGroupCreated(string name) { if (!configData.CraftingSpeed.ContainsKey(name)) { configData.CraftingSpeed.Add(name, 1); } } void OnPlayerConnected(BasePlayer player) { if (player == null) { return; } if (PlayerCrafting.ContainsKey(player.userID)) { PlayerCrafting.Remove(player.userID); } var speed = 1f; foreach (var group in configData.CraftingSpeed) { if (!permission.UserHasGroup(player.UserIDString, group.Key) || group.Value < speed) { continue; } speed = group.Value; } PlayerCrafting.Add(player.userID, speed); } void OnPlayerDisconnected(BasePlayer player) { if (PlayerCrafting.ContainsKey(player.userID)) { PlayerCrafting.Remove(player.userID); } } void OnItemCraft(ItemCraftTask task, BasePlayer player, Item item) { if (!PlayerCrafting.TryGetValue(player.userID, out cacheSpeed)) { return; } task.blueprint = UnityEngine.Object.Instantiate(task.blueprint); if (cacheSpeed == 0) { task.blueprint.time = 0; } else { task.blueprint.time /= cacheSpeed; } } #endregion #region Functions void UpdateOnlinePlayers() { List Players = new List(BasePlayer.activePlayerList); timer.Repeat(0.1f, Players.Count, () => { OnPlayerConnected(Players[0]); Players.RemoveAt(0); }); } #region Commands [ConsoleCommand("gcraft")] void ConfigCommand(ConsoleSystem.Arg arg) { if (arg.Player() != null && !permission.UserHasPermission(arg.Player().UserIDString, perm)) { return; } if (arg.Args == null || arg.Args.Length != 2) { arg.ReplyWith("Invalid syntax: gcreaft "); return; } var group = arg.Args[0]; if (string.IsNullOrEmpty(group) || !configData.CraftingSpeed.ContainsKey(group)) { arg.ReplyWith("Error: Group is invalid or does not exist."); return; } var multiplier = float.Parse(arg.Args[1]); if (multiplier == null) { arg.ReplyWith("Error: Integer is invalid."); return; } configData.CraftingSpeed[group] = multiplier; Config.WriteObject(configData, true); UpdateOnlinePlayers(); arg.ReplyWith($"Sucessfully update {group}'s crafting speed to {(multiplier == 0 ? "instant" : $"x{multiplier}")}!"); } #endregion #region Config void Init() { LoadConfigVariables(); } ConfigData configData; class ConfigData { public Dictionary CraftingSpeed = new Dictionary(); } private void LoadConfigVariables() { configData = Config.ReadObject(); SaveConfig(configData); } protected override void LoadDefaultConfig() { var config = new ConfigData(); SaveConfig(config); } void SaveConfig(ConfigData config) { Config.WriteObject(config, true); } #endregion #endregion } }