private void ApplyMachine(VendingMachine machine, WorldShopsSettings.Shop shop) { machine.sellOrders.sellOrders = shop.SellOrders.Select(x => x.ProtoBuf).ToList(); machine.shopName = shop.WorldName; machine.skinID = shop.SkinId; machine.health = machine.MaxHealth(); machine.inventory.Clear(); foreach (WorldShopsSettings.SellOrder order in shop.SellOrders) { if (machine.inventory.itemList.Select(x => x.info).Contains(order.SellItem.Definition)) { continue; } if (order.SellItem.Blueprint) { this.GetBlueprint(order.SellItem.Definition, order.SellItem.Quantity).MoveToContainer(machine.inventory); } else { ItemManager.Create(order.SellItem.Definition, order.SellItem.Quantity).MoveToContainer(machine.inventory); //restock } } }
private void OnServerInitialized() { foreach (VendingMachine machine in UnityEngine.Object.FindObjectsOfType <VendingMachine>()) { string machineId = machine.ServerPosition.ToString("f2"); if (!WorldShopsData.Loaded.Shops.ContainsKey(machineId)) { return; } string shopName = WorldShopsData.Loaded.Shops[machineId]; if (shopName == null) { return; } WorldShopsSettings.Shop shop = WorldShopsSettings.Loaded.Shops.FirstOrDefault(x => x.CommandName == shopName); if (shop == null) { return; } this.ApplyMachine(machine, shop); _activeShops.Add(machine, shop); } }
private void SaveMachine(VendingMachine machine, WorldShopsSettings.Shop shop) { shop.SellOrders = machine.sellOrders.sellOrders.Select(x => new WorldShopsSettings.SellOrder { BuyItem = new WorldShopsSettings.Item { Definition = ItemManager.FindItemDefinition(x.currencyID), Quantity = x.currencyAmountPerItem, Blueprint = x.currencyIsBP }, SellItem = new WorldShopsSettings.Item { Definition = ItemManager.FindItemDefinition(x.itemToSellID), Quantity = x.itemToSellAmount, Blueprint = x.itemToSellIsBP } }).ToArray(); shop.SkinId = machine.skinID; shop.WorldName = machine.shopName; }
private void Init() { _timer = this.timer; JsonConvert.DefaultSettings = () => new JsonSerializerSettings { Formatting = Formatting.Indented }; WorldShopsSettings.Loaded = this.Config.ReadObject <WorldShopsSettings.General>(); WorldShopsData.Loaded = Interface.Oxide.DataFileSystem.ReadObject <WorldShopsData.General>(nameof(WorldShops)); if (WorldShopsData.Loaded.Shops == null) { WorldShopsData.Loaded.Shops = new Dictionary <string, string>(); } for (int i = 0; i < WorldShopsSettings.Loaded.Shops.Count; i++) { WorldShopsSettings.Shop shop = WorldShopsSettings.Loaded.Shops[i]; if (shop.CommandName == null) { this.PrintError($"Unable to load WorldShops. The {nameof(WorldShopsSettings.Shop.CommandName)} of {nameof(WorldShopsSettings.Shop)} #{i + 1} is null. Please set a value."); this.Manager.RemovePlugin(this); return; } if (shop.SellOrders == null) { shop.SellOrders = new WorldShopsSettings.SellOrder[0]; this.PrintWarning($"The {nameof(WorldShopsSettings.Shop.SellOrders)} of {nameof(WorldShopsSettings.Shop)} \"{shop.CommandName}\" is null. The value has been set to an empty array."); } if (shop.WorldName == null) { shop.WorldName = "A Shop"; this.PrintWarning($"The {nameof(WorldShopsSettings.Shop.WorldName)} of {nameof(WorldShopsSettings.Shop)} \"{shop.CommandName}\" is null. The value has been set \"A Shop\"."); } for (int j = 0; j < shop.SellOrders.Length; j++) { if (shop.SellOrders[j].BuyItem.Definition == null) { this.RaiseError($"Unable to load WorldShops. The {nameof(WorldShopsSettings.SellOrder.BuyItem)} of {nameof(WorldShopsSettings.SellOrder)} #{j + 1} in {nameof(WorldShopsSettings.Shop)} \"{shop.CommandName}\" has an invalid item shortname."); this.Manager.RemovePlugin(this); return; } if (shop.SellOrders[j].SellItem.Definition == null) { this.RaiseError($"Unable to load WorldShops. The {nameof(WorldShopsSettings.SellOrder.SellItem)} of {nameof(WorldShopsSettings.SellOrder)} #{j + 1} in {nameof(WorldShopsSettings.Shop)} \"{shop.CommandName}\" has an invalid item shortname."); this.Manager.RemovePlugin(this); return; } } } string[] names = WorldShopsSettings.Loaded.Shops.Select(x => x.CommandName).ToArray(); string conflictingName = names.FirstOrDefault(x => names.Length - names.Except(new string[] { x }).Count() > 1); if (conflictingName != null) { this.RaiseError($"Unable to load WorldShops. Two or more shops have a conflicting command name: {conflictingName}"); this.Manager.RemovePlugin(this); return; } this.queuedShops = new Dictionary <BasePlayer, WorldShopsSettings.Shop>(); this.queuedWipes = new List <BasePlayer>(); this.queuedSaves = new Dictionary <BasePlayer, WorldShopsSettings.Shop>(); this.queuedDisables = new List <BasePlayer>(); this.queuedSpawns = new List <BasePlayer>(); _activeShops = new Dictionary <VendingMachine, WorldShopsSettings.Shop>(); this.permission.RegisterPermission("worldshops.build", this); this.permission.RegisterPermission("worldshops.spawn", this); this.permission.RegisterPermission("worldshops.apply", this); this.permission.RegisterPermission("worldshops.disable", this); this.permission.RegisterPermission("worldshops.wipe", this); this.permission.RegisterPermission("worldshops.save", this); this.permission.RegisterPermission("worldshops.delete", this); foreach (string name in names) { this.permission.RegisterPermission($"worldshops.remote.{name}", this); } this.Config.WriteObject(WorldShopsSettings.Loaded); if (WorldShopsSettings.Loaded.Notification.Enabled) { BasePlayer.activePlayerList.ForEach(x => x.gameObject.AddComponent <ShopBlock>()); } }
private void ShopCommand(BasePlayer player, string command, string[] args) { if (args.Length == 0) { player.ChatMessage(this.Lang("Help", player)); return; } bool commandPlaced; string fullName; bool fullPlaced; WorldShopsSettings.Shop foundShop; switch (args[0]) { case "spawn": if (!player.IPlayer.HasPermission("worldshops.spawn")) { player.ChatMessage(this.Lang("InsufficientPermission", player)); return; } this.queuedSpawns.Add(player); player.ChatMessage(this.Lang("SpawnReady", player)); _timer.Once(5f, () => { if (!this.queuedSpawns.Contains(player)) { return; } this.queuedSpawns.Remove(player); player.ChatMessage(this.Lang("SpawnTimeout", player)); }); break; case "apply": if (args.Length != 2) { player.ChatMessage(this.Lang("Help", player)); return; } if (!player.IPlayer.HasPermission("worldshops.apply")) { player.ChatMessage(this.Lang("InsufficientPermission", player)); return; } if (!WorldShopsSettings.Loaded.Shops.Select(x => x.CommandName).Contains(args[1])) { player.ChatMessage(this.Lang("InvalidShop", player, args[1])); return; } WorldShopsSettings.Shop applyShop = WorldShopsSettings.Loaded.Shops.First(x => x.CommandName == args[1]); this.queuedShops.Add(player, applyShop); player.ChatMessage(this.Lang("ApplyReady", player, args[1])); _timer.Once(5f, () => { if (!this.queuedShops.ContainsKey(player)) { return; } this.queuedShops.Remove(player); player.ChatMessage(this.Lang("ApplyTimeout", player, args[1])); }); break; case "save": if (args.Length != 2) { player.ChatMessage(this.Lang("Help", player)); return; } if (!player.IPlayer.HasPermission("worldshops.save")) { player.ChatMessage(this.Lang("InsufficientPermission", player)); return; } if (WorldShopsSettings.Loaded.Shops.Select(x => x.CommandName).Contains(args[1])) { player.ChatMessage(this.Lang("ShopExists", player, args[1])); return; } WorldShopsSettings.Shop newShop = new WorldShopsSettings.Shop { CommandName = args[1] }; WorldShopsSettings.Loaded.Shops.Add(newShop); this.queuedSaves.Add(player, newShop); player.ChatMessage(this.Lang("SaveReady", player, args[1])); _timer.Once(5f, () => { if (!this.queuedSaves.ContainsKey(player)) { return; } this.queuedSaves.Remove(player); player.ChatMessage(this.Lang("SaveTimeout", player, args[1])); }); break; case "delete": if (args.Length < 2) { player.ChatMessage(this.Lang("Help", player)); return; } if (!player.IPlayer.HasPermission("worldshops.delete")) { player.ChatMessage(this.Lang("InsufficientPermission", player)); return; } if (!WorldShopsSettings.Loaded.Shops.Any(x => x.CommandName == args[1] || x.WorldName == string.Join(" ", args))) { player.ChatMessage(this.Lang("InvalidShop", player, args[1])); return; } fullName = string.Join(" ", args); foundShop = WorldShopsSettings.Loaded.Shops.First(x => x.CommandName == args[1] || x.WorldName == fullName); commandPlaced = _activeShops.Any(x => x.Value.CommandName == args[1]); fullPlaced = _activeShops.Any(x => x.Value.WorldName == fullName); WorldShopsSettings.Loaded.Shops.RemoveAll(x => x.CommandName == args[1] || x.WorldName == string.Join(" ", args)); this.Config.WriteObject(WorldShopsSettings.Loaded); player.ChatMessage(this.Lang("DeletedShop", player, args[1])); if (!commandPlaced || !fullPlaced) { player.ChatMessage(this.Lang("DeletedShopPlaced", player, !commandPlaced ? args[1] : fullName)); foreach (VendingMachine vendingMachine in _activeShops.Where(x => x.Value == foundShop).Select(x => x.Key).ToArray()) { vendingMachine.Kill(); } } break; case "wipe": if (!player.IPlayer.HasPermission("worldshops.wipe")) { player.ChatMessage(this.Lang("InsufficientPermission", player)); return; } this.queuedWipes.Add(player); player.ChatMessage(this.Lang("WipeReady", player)); _timer.Once(5f, () => { if (!this.queuedWipes.Contains(player)) { return; } this.queuedWipes.Remove(player); player.ChatMessage(this.Lang("WipeTimeout", player)); }); break; case "disable": if (!player.IPlayer.HasPermission("worldshops.disable")) { player.ChatMessage(this.Lang("InsufficientPermission", player)); return; } this.queuedDisables.Add(player); player.ChatMessage(this.Lang("DisableReady", player)); _timer.Once(5f, () => { if (!this.queuedDisables.Contains(player)) { return; } this.queuedDisables.Remove(player); player.ChatMessage(this.Lang("DisableTimeout", player)); }); break; case "list": player.ChatMessage(string.Join("\n", WorldShopsSettings.Loaded.Shops.Select(x => this.Lang("ShopListElement", player, x.WorldName, x.CommandName, _activeShops.Count(y => y.Value == x))).ToArray())); break; case "remote": if (args.Length < 2) { player.ChatMessage(this.Lang("Help", player)); return; } commandPlaced = _activeShops.Any(x => x.Value.CommandName == args[1]); fullName = string.Join(" ", args); fullPlaced = _activeShops.Any(x => x.Value.WorldName == fullName); if (!commandPlaced || !fullPlaced) { player.ChatMessage(this.Lang("ShopNotPlaced", player, !commandPlaced ? args[1] : fullName)); return; } KeyValuePair <VendingMachine, WorldShopsSettings.Shop> remoteMachine = _activeShops.First(x => x.Value.CommandName == args[1] || x.Value.WorldName == fullName); if (!player.IPlayer.HasPermission($"worldshops.remote.{remoteMachine.Value.CommandName}")) { player.ChatMessage(this.Lang("InsufficientPermission", player)); return; } _timer.Once(0.1f, () => PlayerLootContainer(player, remoteMachine.Key, remoteMachine.Key.customerPanel)); break; default: player.ChatMessage(this.Lang("Help", player)); break; } }